欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java 练习

程序员文章站 2022-06-05 22:04:57
...


java 练习
            
    
    博客分类: java  
 

 

import java.io.*;
public class  CharCounter{
	private int[] charcount = new int[26];
	Chart chart;
	public CharCounter(String filename){
		BufferedReader br = null;
		try{
			 br = new BufferedReader(new FileReader(filename));
			 String str=null;
			 while((str=br.readLine())!=null){
				str=str.toUpperCase();
				for(int i =0,length = str.length();i<length;i++){
					char ch = str.charAt(i);
					int index = ch-'A';		 //A-0,B-1....
					if(index>=0 && index <= 25)charcount[index]++;
				}
			 }
			 br.close();
			 chart = new Chart(new String[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"},charcount,"*");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void plainOut(){
		chart.plainOut();
	}
	public void horizontalOut(){
		chart.horizontalOut();
	}
	public void verticalOut(){
		chart.verticalOut();
	}
	public static void main(String[] args){
	     // new CharCounter("input.txt").horizontalOut();
		  new CharCounter("input.txt").verticalOut();
			//new CharCounter("input.txt").plainOut();

	}
}

class Chart{
	private String[] label;
	private int[] data;
	private String style;
	private RotateArray ra ;
	public Chart(String[] label,int[] data,String style){
		this.label = label;
		this.data = data;
		this.style = style;
		fillData();
	}
	public void fillData(){
		int max = max(data);
		String[][] str = new String[data.length][max+2];
		for(int i =0,len = data.length;i<len;i++){
			
			for(int j =0;j< max+2;j++){
				if(j>0&& j<data[i])str[i][j] = style;
				else str[i][j] = " ";
			}
			str[i][0] = label[i];
			str[i][max+1] = ""+data[i];
		}
		ra = new RotateArray(str);
	}
	public static String multi(String s,int rate){
		String str ="" ;
		for(int i =0;i<rate;i++){
			str+=s;
		}
		return str;
	}
	public static int max(int[] data){
		int max = data[0];
		for(int i =1,len = data.length;i<len;i++){
			if(max<data[i])max=data[i];
		}
		return max;
	}
	public void plainOut(){
		for(int i =0;i<label.length;i++){
			System.out.println(label[i]+":"+data[i]);
		}
	
	}
	public void horizontalOut(){
		out(ra.get());
	}
	public void verticalOut(){
		out(ra.rotate(-1).get());
	}
	public void out(String[][] a){
			for(int i =0;i<a.length;i++){
				for(int j =0;j<a[i].length;j++){
					System.out.print(a[i][j]+multi(" ",1));
				}
				System.out.println();
			}
	}

}

class RotateArray{
	private String[][] original ;
	private String[][] rotatedArray ;
	public RotateArray(String[][] a){
		this.original = a;
	}
	public RotateArray rotate(int rate){  
		for(int i =0,len =rate>=0? rate%4:rate%4+4;i<len;i++){
			rotate();
		}
		return this;
	}
	public RotateArray rotate(){
		int rows = rotatedArray==null?original[0].length:rotatedArray[0].length;
		int cols = rotatedArray==null?original.length:rotatedArray.length;
		String[][] temp = new String[rows][cols];
			for(int i =0,len = temp.length;i<len;i++){
				for(int j =0,len2 = temp[i].length;j<len2;j++){
					if(rotatedArray!=null)temp[i][j] = rotatedArray[len2-1-j][i];
					else temp[i][j] = original[len2-1-j][i];
				}
			}
		rotatedArray = temp;
		return this;
	}
	public String[][] get(){
		return rotatedArray!=null?rotatedArray:original;
	}
}
 
java 练习
            
    
    博客分类: java  
  • java 练习
            
    
    博客分类: java  
  • 大小: 43.6 KB
  • java 练习
            
    
    博客分类: java  
  • 大小: 35.1 KB