2020-11-24

7-1 统计Java程序中关键词的出现次数 (100分)

编写程序统计一个输入的Java源码中关键字(区分大小写)出现的次数。说明如下:

  • Java中共有53个关键字(自行百度)
  • 从键盘输入一段源码,统计这段源码中出现的关键字的数量
  • 注释中出现的关键字不用统计
  • 字符串中出现的关键字不用统计
  • 统计出的关键字及数量按照关键字升序进行排序输出
  • 未输入源码则认为输入非法
  • 输入格式:

  • 输入Java源码字符串,可以一行或多行,以exit行作为结束标志

    输出格式:

  • 当未输入源码时,程序输出Wrong Format
  • 当没有统计数据时,输出为空
  • 当有统计数据时,关键字按照升序排列,每行输出一个关键字及数量,格式为数量\t关键字
  • 输入样例:

    在这里给出一组输入。例如:

  • //Test public method
    public HashMap(int initialCapacity) {
            this(initialCapacity, DEFAULT_LOAD_FACTOR);
        }
        public HashMap(int initialCapacity, float loadFactor) {
            if (initialCapacity < 0)
                throw new IllegalArgumentException("Illegal initial capacity: " +
                                                   initialCapacity);
            if (initialCapacity > MAXIMUM_CAPACITY)
                initialCapacity = MAXIMUM_CAPACITY;
            if (loadFactor <= 0 || Float.isNaN(loadFactor))
                throw new IllegalArgumentException("Illegal load factor: " +
                                                   loadFactor);
            this.loadFactor = loadFactor;
            this.threshold = tableSizeFor(initialCapacity);
        }
    exit

    输出样例:

    在这里给出相应的输出。例如:

  • 1    float
    3    if
    2    int
    2    new
    2    public
    3    this
    2    throw

    代码如下:

  • 
    import java.util.*;
    class guanjianci 
    {
    	void shuchu() 
    	{
    		int b = 0;
    		for(b=0;b<53;b++)
    		{
    			if(sum[b]!=0) 
    			{
    				System.out.printf("%d",sum[b]);
    				System.out.println("\t"+Guanjianci[b]);
    			}
    		}	
    	}
    	final String[] Guanjianci =
    		{ 
    			"abstract","assert","boolean" ,"break", "byte","case", "catch" ,"char", "class" ,"const" ,"continue", "default", "do" ,"double", "else" , "enum", "extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface" ,"long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw","throws", "transient", "try", "void","volatile","while","true", "false", "null"
    		};
    	void shunxu() 
    	{
    		Arrays.sort(Guanjianci);
    	}
    	int xuanzhe = 0;
    	int mubiao=0;
    	int [] sum=new int [53];
    	void xunzhao(String shuju) 
    	{
    			for(int a = 0;a<53;a++) 
    			{
    				if(shuju.equals(Guanjianci[a]))
    					sum[a]++;
    			}					
    	}
    	int leixing=0;
    }
    class shuju
    {
    	boolean Xxxxxxxxx2(String h) 
    	{
    		if(h.equals("/*"))
    		{
    			x = true;		
    		}
    		if(h.equals("*/"))
    		{
    			x = false;		
    		}
    		return x;
    	}
    	String []n;
    	List <String> Guanjian=new ArrayList<String>();
    	int sum;
    	int xuanzhe = 0;
    	int leixing=0;
    	guanjianci guanjian;
    	String shuju, shuju1;
    	shuju()
    	{
    		
    	}
    	shuju(String shuju) 
    	{
    		this.shuju = shuju;
    	}
    	public void setshuju(String shuju) 
    	{
    		this.shuju = shuju;
    	}
    	class bibibib
    	{
    		public double getccc()
    		{
    			return 0.0;
    		}
    	}
    	public String getshuju() 
    	{
    		return shuju;
    	}
    	boolean Xxxxxxxxx(String z) 
    	{
    		String [] z1=z.split("//");
    		if(z1.length!=1) 
    		{
    			Bbbbbbbbb(z1[0]);
    		return true;
    		}				
    		else 
    		{
    			return false;
    		}
    	}
    	int mubiao=0;
    	boolean x=false;
    	boolean y=false;
    	private int dang;
    	public class sssbibibib
    	{
    		public double getccc()
    		{
    			return 0.0;
    		}
    	}
    	boolean jjjjjjjj(String h)
    	{
    		 if(h.equals("\"")) 
    		 {
    				sum++;
    		 }
    			if(sum%2==1)
    			{
    				y = true;		
    			}
    			else if(sum%2==0)
    			{
    				y = false;	
    			}
    		     return y;
    	}
    	public void kedupanjiang()
    	{   
    		if(this.dang > 1)
    		{    
    			this.dang --;   
    		}  
    	} 
    	public void Bbbbbbbbb(String shuju) 
    	{		
    		shuju1=shuju.replaceAll("\\."," ");			
    		shuju1=shuju1.replaceAll("//"," // ");
    		shuju1=shuju1.replaceAll("\\/\\*", " /* ");	
    		shuju1=shuju1.replaceAll("\\*\\/", " */ ");	
    		shuju1=shuju1.replaceAll("\\\\\"", " ");		
    		shuju1=shuju1.replaceAll("\"", " \\\" ");		
    		shuju1=shuju1.replaceAll("//", "\n//");		
    		n= shuju1.split("[ \t\n\\;\\,\\{\\}\\[\\]\\(\\)]");
    	}
    }
    public class Main {
    	public static void main(String[] args) 
    	{
    		String [] sb=new String[11111111];
    		guanjianci guanjian=new guanjianci();
    		Scanner input=new Scanner(System.in);
    		int leixing=0;
    		shuju shuju=new shuju();
    		guanjian.shunxu();
    		int xuanzhe = 0;
    		int mubiao=0;
    		int o=0;
    		do 
    		{
    			sb[o]=input.nextLine();
    			o++;
    		}
    		while(!sb[o-1].equals("exit"));
    		for( o=0;sb[o]!=null;o++) 
    		{
    			if(o==0&&sb[o].equals("exit"))
    			{
    				System.out.print("Wron Format");
    				System.exit(0);
    			}
    			shuju.setshuju(sb[o]);
    			sb[o]=sb[o].split("//")[0];
    			shuju.Bbbbbbbbb(sb[o]);
    			if(leixing==1||leixing==2)
    			{						
    				xuanzhe = input.nextInt();
    				if(leixing==1)
    				{
    					System.out.print("Wrong Format");
    				}
    			}
    			for(String k1:shuju.n) 
    			{
    				if (shuju.Xxxxxxxxx2(k1))
    				{
    					continue;
    				}
    				else if(shuju.jjjjjjjj(k1))
    				{
    					continue;
    				}
    				shuju.Guanjian.add(k1);
    			}		
    		}
    		if(leixing==2) 
    		{
    			System.out.println();
    			xuanzhe = input.nextInt();
    		}
    		for(String k1:shuju.Guanjian)
    			guanjian.xunzhao(k1);
    		guanjian.shuchu();
    	}
    }	
    class kbs 
    {
    	public double getbs()
    	{
    		return 0.0;
    	}
    }
    class masdhjd 
    {
    	private int b;
    	public masdhjd(int r) 
    	{
    	       this.b = r;
    	}
    	public double getbs()
    	{
    		return 0;
    	}
    	public  boolean checkbbs(int r)
    	{
    			return true;
    	}
    }
    class good
    {
    	private double a;
    	public good()
    	{
    	}
    	public good(double l)
        {
            this.a=l;
        }
        public void setbs(double l)
        {
            this.a = l;
        }
        public double getbbs()
        {
            return a*a;
        }
        public  boolean checkbbbL(double w)
    	{
    			return true;
    	}
    }
    class duicuo extends  kbs
    {
    	private int x;
    	public duicuo()
    	{
    	}
    	public duicuo(int x)
        {
            this.x=x;
        }
        public double getbbs()
    	{
    		return 0;
    	}
        public  Boolean checkbbs(double x)
    	{
    		if(x<0)
    		{
    			return false;
    		}
    		else
    			return true;
    	}
    }
    
    

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值