项目 - 处理C++源代码的程序

来源:互联网 发布:印特软件 编辑:程序博客网 时间:2024/05/21 13:51

问题及代码:

/**Copyright (c) 2016,烟台大学计算机学院*All rights reserved.*文件名称:main.cpp*作    者:李磊涛*完成时间:2016年6月23日*版 本 号:v1.0**问题描述:项目 - 处理C++源代码的程序。*输入描述:无。*程序输出:无。*/#include <fstream>  #include<iostream>  #include<string>  #include<cstdlib>  using namespace std;  int appear(char*s1,char*s2);  int main( )  {      char line[256];      char main_fun[8]="main()";      int main_num=0;//初时,尚未发现        //将文件中的数据读入到字符数组中      ifstream sourceFile("source.cpp",ios::in);  //以输入的方式打开文件      if(!sourceFile)       //测试是否成功打开      {          cerr<<"source code read error!"<<endl;          exit(1);      }      while(!sourceFile.eof())      {          sourceFile.getline(line,255,'\n');          main_num+=appear(line,main_fun);          if (main_num>1)  //多于1个,没有必要再去读取              break;      }      sourceFile.close();        //识别结论      if(main_num==0)          cout<<"error: no main().";      else if (main_num==1)          cout<<"right: a main() be exist.";      else          cout<<"error: more than one main().";      cout<<endl;      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);  }  


运行结果:


知识点总结:
通过该程序,强化了我对简单程序结构的认识。
学习心得:
期间有很多小错误,要继续写程序争取早日掌握C++。
0 0
原创粉丝点击