处理C++源代码的程序

/*
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:main.cpp
*作    者:郭辉
*完成时间:2016年6月24日
*版 本 号: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);  
}  
运行结果:
<img src="https://img-blog.csdn.net/20160624132934628" alt="" />


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值