zoukankan      html  css  js  c++  java
  • 2.4.2程序作业

    本程序能正确统计某程序设计语言源文件的字符数,单词数和行数,并能实现部分拓展功能,如返回程序的代码行/空行/注释行/。程序攻分为4个主要函数,分别统计行数程序,统计字符数和单词数函数,返回代码行和空行函数,返回注释行函数。但程序不完善,我没有写出能递归处理目录下符合条件的文件函数。高级功能我也没有实现。能力有限,只能期待日后改进。

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    
     void hangshu()
    {    
      int num=0;
      FILE*fp;
      char str[500];
    
      if((fp=fopen("test.txt","rt"))==NULL) 
      {
       printf("cannot open file
    ");
       return;
      }
           
        while (fgets(str,500,fp))  
        {
          num++;
       }
      fclose(fp);
       printf("该文件共有%d行
    ",num);
    }
    
    
    void count()
    {
        char str[500];
    	int strLen;
         FILE*fp;
        
    	int i;
    	int j;
    	int blank=0;
    	int ch=0;
    	int word=0;
     
    
      if((fp=fopen("test.txt","rt"))==NULL) 
      {
       printf("cannot open file
    ");
       return;
      }
    
      
     while(fgets(str, 500, fp))
     {
            strLen = strlen(str);
    		for(i=0;i<strLen;i++)
    		{
    			j=str[i];
    			if(j==' '||j=='	')
    			{
    		!blank&&word++;
    		blank=1;
    			
    			}
    			else if(j!='
    '&&j!='
    ')
    			{
    				ch++;
    			blank=0;
    			
    			}
    		}
            !blank&&word++;
    		blank=1;
    		
     }
       printf("共有%d个单词,%d个字符",word,ch);
    }
    
     void advanced()
     {
    
    int strLen;	 
    
    	
     FILE*fp;
     char str[500];
    
    
      if((fp=fopen("test.txt","rt"))==NULL) 
      {
       printf("cannot open file
    ");
       return;
      }
    
           
        while (fgets(str,500,fp))  
    	{
    		
            strLen = strlen(str);
    	
             if(strLen>3)
    		 {
    			printf("%s是代码行.
    ",str);
    		 }
    		 
    
    		 if(strLen<=3)
    		  {
             
                printf("%s是空行.
    ",str);
    		  }	
    	}
    		
     }   
    
    
    
    void zhushi()
    {
             FILE*fp;
             int i =0;
            
             char str[500];
            if((fp=fopen("test.txt","rt"))==NULL) 
      {
       printf("cannot open file
    ");
       return;
      }
           
    		fp=fopen("test.txt","r");
        while(fgets(str,500,fp))
        {
                str[i]=fgetc(fp);
                    i++;
            }
            if (str[i]='\')
    			printf("%s是注释行
    ",str);
        fclose(fp);
              
    }
    
    
    	
    void Menu()
    {
            printf("--------------------
    ");
        printf("----程序主菜单------
    "); 
        printf("--------------------
    ");
        printf("----1显示行数---
    ");
     printf("----2显示单词数和字符数----
    ");
     printf("----3返回代码行和空行-------
    ");
     printf("----4返回注释行-------
    ");
    
            printf("----5退出-----------
    ");
        printf("---回车键返回菜单---
    "); 
        printf("--------------------
    ");
            printf("---请选择:(1-5)-:---
    ");
    
    }
    
    
    void main()
    {
            char menuNum;
           
            while(1)
            {
                    Menu();
                    scanf("%d",&menuNum);
            switch(menuNum)
            {
                   
    
                 case 1:
    				    hangshu();
                        break;
    			 case 2:
                        count();
    					break;
    			 case 3:
    				    advanced();
    				    break;
    			 case 4:
    				   zhushi();
    				   break;
            case 5:
                          
                            exit(0);
            
            default:
                            printf("
    输入错误!
    ");
                    printf("按任意键返回....
    ");
                   getchar();
    
    }
    }
    }
    

      

  • 相关阅读:
    megento 安装过程问题 畅想由我
    jquery更改html标签 span 文字获取 a,span,div,class对象内容 畅想由我
    Your web server is configured incorrectly. As a result, configuration files with sensitive information are accessible from the outside. Please contact 畅想由我
    Object 与 Type
    js中apply和join
    URLRewriter实现机制
    在项目中让Ajax面向对象 (一)
    浅谈javascript中Json方式与闭包式
    在项目中让Ajax面向对象 (二)
    使用JWT创建安全的ASP.NET Core Web API
  • 原文地址:https://www.cnblogs.com/ysw6/p/5310962.html
Copyright © 2011-2022 走看看