Problem Description:
(3) read into a C + + program, enter m, n two digits, the n lines of code from line m will be used as a comment (that is, precede these lines with "//"), the new program is saved to another. cpp file, and the program is displayed with a line number on the screen.
Code implementation:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include < Fstream>using namespace Std;int Main () {FStream my1 ("Source.cpp"); if (!my1) {cerr<< "can ' t find source.cpp!\n"; Exit (1); } ofstream my2 ("Newsource.cpp"); if (!my2) {cerr<< "can ' t write the newsource.cpp!\n"; Exit (1); } int n,m; printf ("Please enter M and N, will make the n lines of code from line m of the program be used as comments \ n"); scanf ("%d%d", &m,&n); int i=1; string S; while (!my1.eof ()) {getline (my1,s); if (i>=m&&i<= (m+n)) {my2<< "\ \"; my2<< "\"; } my2<<s<< ' \12 '; i++; } my1.close (); My2.close (); FStream in ("Newsource.cpp"); if (!in) {cerr<< "can ' t find newsource.cpp!\n"; Exit (1); } printf ("\n\nwatch the newsource.cpp\n\n\n"); while (!in.eof ()) {getline (in,s); cout<<s<< ' \12 '; } return 0;}
Operation Result:
"Project 4-programs to process C + + source Code" (3)