第十四周项目4(1)-处理C++源代码的程序

本文介绍了一个C++程序的分析方法,旨在判断程序中是否存在唯一的main()函数,通过读取源代码并使用特定算法进行检查,确保程序结构符合预期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 *Copyright (c) 2014, 烟台大学计算机学院
 *All rights reserved.
 *文件名称:week14-4-1.cpp
 *作者:高赞
 *完成日期: 2015 年 6 月 8 日
 *版本号:v1.0
 *
 *问题描述:读入一个C++程序,判断其中是否只有一个main()函数
 *
 *
 */
#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)
            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;
}
int appear(char*s1,char*s2)
{
    int n=0,flag;
    char *p,*q;
    for(; *s1!='\0'; s1++)
    {
        if (*s2==*s1)
        {
            flag=1;
            p=s1 ;
            q=s2;
            for(; *q!='\0';)
            {
                if (*q++!=*p++)
                {
                    flag=0;
                    break;
                }
            }
            if (flag==1) n++;
        }

    }
    return(n);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值