Simple instance of Python and C ++ programs

zhaozj2021-02-16  51

A netizen is working on the school to do a curriculum design, requiring the location of the given word to appear in a file and statistics. This is a relatively simple text handling problem, so I wrote one with Python and bet in 5 minutes to solve the problem in 5 minutes. I made it, the following is the program:

IF __name __ == '__ main__': file_name = Raw_Input ('Input the file you want to find in:') TRY: IN_FILE = Open (file_name, 'r') lines = in_file.readlines ()

Tag_tok = '' while tag_tok.upper ()! = 'q': tag_tok = Raw_INPUT (q for quit: ') if tag_tok.upper ()! =' q ': count = 0 line_no = 0 for line in line: line_no = line_no 1 inline_cnt = line.count (tag_tok) count = count inline_cnt if inline_cnt> 0: Print 'Find% S% D Time (s) in line:% d'% (tag_tok, inline_cnt, line_no) Print Line Print '--------------------------------' Print 'Total FOUNT% S% D Time (S) '% (tag_tok, count) Except: print "can't open file% s"% (file_name)

However, this netizen does not satisfy the procedure for a C , the reason is that their teachers will not python, just try to try to solve the comparison with Python with C :

#include #include #include #include #include

Using namespace std;

INT Brutefind (const char * x, int m, constor * y, int N, vector & colpos) {INT i, J, CNT = 0; / * searching * / for (j = 0; J <= N - m; j) {for (i = 0; i = m) {colpos [cnt ] = j; if (CNT == colpos.size ()) colpos.resize (cnturn);}}}}} incount_string (string source, string tag, vector & colpos) {INT FIND_CNT = 0; Find_cnt = Brutefind (tag.c_str (), tag.size (), source.c_str (), source.size (), colpos;

Return Find_cnt;}

INT main () {string file_name, line; vector lines; lines.resize (10); cout << "Input the file name:"; cin >> file_name; ifstream in_file; try {in_file.open (file_name). C_STR ()); if (! in_file);} catch (string file_name) {cout << "Fatal Error: File Not Found." << endl; exit (1);

INT line_count = 0; do {getLine (IN_FILE, LINES [LINE_COUNT]); line_count ; if (line_count == lines.size ()) {lines.resize (line_count * 2);}} while (in_file.eof == 0); string tag_tok; vector colpos; colpos.resize (10); do {cout << "Input the word you want to find (qfor quit):"; cin >> tag_tok; if ( tag_tok == "Q") {break;} int count = 0, line_no = 0, inline_count; for (line_no = 0; line_no 0) {cout << "" "" inline_count << "" "inline_count <<" Time (s) in line "<< line_no; cout <<", Column Pos IS "; For (int i = 0; I

From the program length and programming time, rough comparison:

Python 5 minutes 22 lines

C 30 minutes more than 60 lines

From this simple example, you can see the difference between the scripting language and the C language.

转载请注明原文地址:https://www.9cbs.com/read-19813.html

New Post(0)