14th Week Project 4-1: programs that handle C + + source code-Find the main () function

Source: Internet
Author: User

Questions and codes:

/**copyright (c) 2015, College of Computer and Control engineering, Yantai University *all rights reserved.* file name: project.cpp* Author: wenqing * Completion Date: June 16, 2015 * version number: v1.0** Problem Description: (1) Read into a C + + program, determine whether there is only one main () function, the output "no problem", or "no main () function", or "cannot define multiple main () functions"; Hint 1: Simple processing, you can only compare the judgment "main ()", Consider the actual possibilities, and any number of spaces and void in parentheses after main should be counted. It is recommended that the simplest case be handled. Tip 2: It is recommended to design a function that compares the read code to the string "Main ()". The function is used to determine if S1 is "contained" in a row S2 that is being read, and when called, the argument at the formal parameter S1 is used with "main ()", which increases the "abstraction" level, which is easier to implement and corresponds to higher code quality. * Program Input: * Program output: */#include <fstream> #include <iostream> #include <string> #include <cstdlib>  Using namespace Std;int search_main (char*s);    In Q, find the main () character int search_main (char*s) {char main[8]= "main ()";               Used as the comparison character//accept character Char *p,*q;                Retrieves the required pointer int num=0 for main (); The number of main () functions for (; *s!= '); s++)//accepts a line of characters {if (*s== ' m ')//when the first character M is present; {in            T I=1;           P=main;            To main fetch address q=s;      while (*q!= ') {if (*q== ') ++q; Skip Space If (*q++!=*p++)//When the next character is incorrect, end the loop and return {break;                } ++i;       if (i==6) ++num; When there are six strings in the same time, it contains a main function}}} return num;}    int main () {char line[256];  int main_num=0;//at the beginning, not found//the data in the file read into the character array ifstream sourcefile ("Source.cpp", ios::in); Open file as input if (!sourcefile)//test successfully opened {cerr<< "source code not found!"        <<endl;    Exit (1);        } while (!sourcefile.eof ()) {Sourcefile.getline (line,255, ' \ n ');        Main_num+=search_main (line);    if (main_num>1)//More than 1, there is no need to read the break again;    } sourcefile.close (); Recognition conclusion if (main_num==0) cout<< "There is no main () function.    "; else if (main_num==1) cout<< "The problem is not found yet.    ";    else cout<< "cannot define multiple main () functions";    cout<<endl; return 0;}

Operation Result:





Summary of Knowledge points:
Write a lot of file stream program, file stream has no problem.

So the key is to find the main () function to work hard.

Use the pointer and the string main () to compare. If the six consecutive characters and the main () string are the same, then the Num_main will be +1, then each row will be counted once, then the final result is obtained.

14th Week Project 4-1: programs that handle C + + source code-Find the main () function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.