14th Week "Project 4-Procedures for handling C + + source code" (1)

Source: Internet
Author: User

Problem Description:

The functionality of code formatting is provided in the IDE, such as Codeblocks. To complete this function of the program, the operation of the data is written in C + + source code files. A C + + source file is a text file that can be manipulated by a program.

The integrated development Environment (IDE) compiles the program, and the "data" of the operation is the source program. In the compilation, the source program should be the lexical check and grammar check, followed by the target code generation, code optimization and other work. The relevant technology will be learned in the principles of compiling lesson. These technologies can be used in many areas and, of course, allow us to have a deeper understanding of programming languages.
This project will be a C + + source program for the operation of the object, the source program to complete a series of processing. Each feature can be programmed with a single program implementation (which is recommended in this simple scenario), or it can be integrated (making the IDE work for itself).
(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";
Tip 1: Simple processing, you can only compare the judgment "main ()", consider the actual various possibilities, the parentheses after the parenthesis of any number of spaces and void 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.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <fstream> #include <    Cstdlib> #include <cctype>using namespace std;string s= "main ()"; string f (String str) {string return_str= "";    int Length=str.length ();    for (int i=0;i<length;i++) if (str[i]!= ") return_str+=str[i]; return RETURN_STR;}    int fun (String t) {int C;    int I=0,len=t.length (), J;        for (i=0;i<len;++i) {string k;        K+=T[I];K+=T[I+1];K+=T[I+2];K+=T[I+3];K+=T[I+4];K+=T[I+5];K+=T[I+6];                for (j=0,c=1;j<7;j++) {if (K[j]!=s[j]) {c=0;            Break    }} if (c) return 1; } return 0;}    int main () {string T;    int num=0,i;    FStream myfile ("Source.cpp", ios::in);        if (!myfile) {cerr<< "can ' t find sorce.cpp\n";    Exit (1);        } while (!myfile.eof ()) {getline (myfile,t);    T=f (t);//delete space Num+=fun (t);//Determine if there is a main () if (num>1) break; } IF (num==1) printf ("Source.cpp is legal\n");    else printf ("Source.cpp is illegal\n"); return 0;}

Operation Result:


It's finally done in the middle of the night!


14th Week "Project 4-Procedures for handling C + + source code" (1)

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.