《C语言及程序设计》实践参考——编程处理C源代码

来源:互联网 发布:it入门教程 编辑:程序博客网 时间:2024/05/16 03:57

返回:贺老师课程教学链接  实践要求


【项目5-编程处理C源代码】
在CodeBlocks等IDE中都提供了代码格式整理的功能。我们可以编写程序,处理的数据就是用C写的源代码文件。C源文件是一种文本文件,可以通过程序进行操作。
(1)读入一个C程序,判断其中是否只有一个main()函数,输出“暂时没有发现问题”,或者“没有main()函数”,或者“不能定义多个main()函数”;
提示1:简单处理,可以只比较判断“main()”,考虑实际上的各种可能,main后面的括号中还有其他多种写法。建议按最简单的情形处理。
提示2:建议设计一个函数,将读到的代码与字符串“main()”进行比较。函数用于判断s1是否“包含”在读入的一行s2中,调用时,形参s1处的实参用“main()”即可,这样写提升了“抽象”级别,更容易实现,对应更高的代码质量。
[参考解答]
测试时,建立一个source.c文件,分别使其中有main()、无main(),以及出现多个。
#include <stdio.h>#include<string.h>#include<stdlib.h>int appear(char*s1,char*s2);int main( ){    char line[256];    char main_fun[8]="main()";    int main_num=0;//初时,尚未发现    //将文件中的数据读入到字符数组中    FILE *fp = fopen("source.c","r");  //以输入的方式打开文件    if(fp==NULL)       //测试是否成功打开    {        printf("source code read error!\n");        exit(1);    }    while(!feof(fp))    {        fgets(line,256, fp);        main_num+=appear(line,main_fun);        if (main_num>1)  //多于1个,没有必要再去读取            break;    }    fclose(fp);    //识别结论    if(main_num==0)        printf("error: no main().\n");    else if (main_num==1)        printf("right: a main() be exist.\n");    else        printf("error: more than one main().\n");    return 0;}//返回s2在s1中出现了几次int appear(char*s1,char*s2){    int n=0,flag;    char *p,*q;    for(; *s1!='\0'; s1++)    {        if (*s2==*s1) /*判断字符串中是否有和要判断的字串首字符相同的字符*/        {            flag=1;            p=s1 ; /*s1 p 为第一个相同字符的地址*/            q=s2;            for(; *q!='\0';) /*如果有则判断接下去的几个字符是否相同*/            {                if (*q++!=*p++)                {                    flag=0;                    break;                }            }            if (flag==1) n++;        }    }    return(n);}

(2)编写一个程序并取名test.c,统计该文件中出现"if","while","for"的次数。
[参考解答]
#include <stdio.h>#include<string.h>#include<stdlib.h>int appear(char*s1,char*s2);int main( ){    char line[256];    int if_num=0, while_num, for_num=0;    //将文件中的数据读入到字符数组中    FILE *fp = fopen("test.c","r");  //以输入的方式打开文件    if(fp==NULL)       //测试是否成功打开    {        printf("source code read error!\n");        exit(1);    }    while(!feof(fp))    {        fgets(line,256, fp);  //读入一行        if_num+=appear(line,"if");        while_num+=appear(line,"while");        for_num+=appear(line,"for");    }    fclose(fp);    //识别结论    printf("if: %d\n", if_num);    printf("while: %d\n", while_num);    printf("for: %d\n", for_num);    return 0;}//返回s2在s1中出现了几次int appear(char*s1,char*s2){    int n=0,flag;    char *p,*q;    for(; *s1!='\0'; s1++)    {        if (*s2==*s1) /*判断字符串中是否有和要判断的字串首字符相同的字符*/        {            flag=1;            p=s1 ; /*s1 p 为第一个相同字符的地址*/            q=s2;            for(; *q!='\0';) /*如果有则判断接下去的几个字符是否相同*/            {                if (*q++!=*p++)                {                    flag=0;                    break;                }            }            if (flag==1) n++;        }    }    return(n);}
注:在真正的IDE中进行词分析时,需要找到if、for、while等关键词,还要识别是否符合语法规定的要求,并且这样的工作,就在一次“扫描”中完成,采用的技术,已经不是这种简单地进行字符串匹配了。

(3)读入一个C程序,使程序中的所有左花括号“{”和右花括号“}”都单独占一行,新程序保存到另一个.c文件中,并在屏幕上显示处理过的程序,显示时加上行号。
[参考解答]
#include <stdio.h>#include<string.h>#include<stdlib.h>void formatPrograme(char *sourcefile, char *targetfile);void outprogram(char *filename);int main( ){    formatPrograme("source.c", "target.c");    printf("经过处理后的程序是:\n");    outprogram("target.c");    return 0;}void formatPrograme(char *sourcefile, char *targetfile){    char ch1, ch2;    //将文件中的数据读入到字符数组中    FILE *fpin = fopen(sourcefile,"r");  //以输入的方式打开文件    if(fpin==NULL)       //测试是否成功打开    {        printf("source code read error!\n");        exit(0);    }    FILE *fpout = fopen(targetfile,"w");  //以输入的方式打开文件    if(fpout==NULL)       //测试是否成功打开    {        printf("source code write error!\n");        exit(0);    }    ch1='\0';    while(!feof(fpin))    {        ch2= fgetc(fpin);        //读到了花括号,且前一个符号不是换行,应该加入一个换行        if((ch2=='{'||ch2=='}')&&(ch1!='\n'))            fputc('\n', fpout);        else            //当前读到的不是换行,但前一个是花括号,此时也该加            if((ch1=='{'||ch1=='}')&&(ch2!='\n'))                fputc('\n', fpout);        fputc(ch2, fpout);  //输出当前读入的符号        ch1=ch2;    }    fclose(fpout);    fclose(fpin);}void outprogram(char *filename){    char line[256];    int n = 1;       //将文件中的数据读入到字符数组中    FILE *fp = fopen(filename,"r");  //以输入的方式打开文件    if(fp==NULL)       //测试是否成功打开    {        printf("source code read error!\n");        exit(1);    }    while(!feof(fp))    {        fgets(line,256, fp);  //读入一行        printf("%d\t%s\n", n, line);        n++;    }    fclose(fp);    return;}

(4)读入一个C程序,输入m、n两个数字,从第m行起的n行代码将作为注释使用(即在这些行前面加上”//”),新程序保存到另一个.c文件中。
[参考解答]
#include <stdio.h>#include<string.h>#include<stdlib.h>void formatPrograme(char *sourcefile, char *targetfile);void outprogram(char *filename);int main( ){    formatPrograme("source.c", "target.c");    printf("任务完成!\n");    return 0;}void formatPrograme(char *sourcefile, char *targetfile){    int m, n;    char line[256];    //将文件中的数据读入到字符数组中    FILE *fpin = fopen(sourcefile,"r");  //以输入的方式打开文件    if(fpin==NULL)       //测试是否成功打开    {        printf("source code read error!\n");        exit(0);    }    FILE *fpout = fopen(targetfile,"w");  //以输入的方式打开文件    if(fpout==NULL)       //测试是否成功打开    {        printf("source code write error!\n");        exit(0);    }    printf("您要将第m行开始的n行代码作为注释,请输入m和n:");    scanf("%d %d", &m, &n);    int n1=0;    while(!feof(fpin))    {        fgets(line,255,fpin);        n1++;        if(n1>=m&&n1<m+n)            fputs("//",fpout); //指定行内,本行始加注释        fputs(line, fpout);    }    fclose(fpout);    fclose(fpin);}

(5)(拓展,不提供参考解答)读入一个C++程序,整理其格式,使其按规范排版,包括:①所有左花括号“{”和右花括号“}”都单独占一行;②每个语句单独占一行;③各行采用统一的缩格排放(每遇一个“{”,其下一行的程序,在第一个有意义的符号前的空格数增加4,每遇一个“}”,其下一行的程序,在第一个有意义的符号前的空格数减少4。
做完了?你怎么可以这么牛?你可以给CodeBlocks做“source code formatter”插件去了!什么?微软的产品中有“集成开发环境(IDE)”的,我去不去应聘?嘿嘿,看看薪水再说。不是不可以考虑。



1 0
原创粉丝点击