#include<iostream> #include<fstream> #include<string> #include<Windows.h> using namespace std; void main() { char word[150][15]; int AllChar = 0, NoteSize = 0, Row = 1, WordSize = 0; char a[15], c, file[15]; int i = 0, j = 0; FILE *fp; cout << "#结束输入" << endl; cin >> file; while (1) { if (file[0] == ‘#‘) break; fp = fopen(file, "r"); if (fp == NULL) { cout << "the file:y.txt not found!" << endl; exit(-1); } c = fgetc(fp); AllChar++; if (c == ‘/‘) { c = fgetc(fp); AllChar++; if (c == ‘/‘) NoteSize++; } c = fgetc(fp); AllChar++; while (c != EOF) { if ((c >= ‘a‘&&c <= ‘z‘) || (c >= ‘A‘) && (c <= ‘Z‘)) { a[i] = c; c = fgetc(fp); AllChar++; i++; continue; } else if (c == ‘/‘) { c = fgetc(fp); AllChar++; if (c == ‘/‘) { c = fgetc(fp); AllChar++; NoteSize++; continue; } } else { if (c == ‘\n‘) { Row++; } if (i != 0) { a[i] = ‘\0‘; strcpy_s(word[j], a); j++; i = 0; } c = fgetc(fp); AllChar++; } } if (i != 0) { a[i] = ‘\0‘; strcpy_s(word[j], a); j++; } fclose(fp); WordSize = j; cout << "行数:" << Row << endl; cout << "字符数:" << AllChar << endl; cout << "单词数:" << WordSize << endl; cout << "注释行:" << NoteSize << endl; cin >> file; } }
时间: 2024-11-08 07:53:35