202103226-1 编程作业
1.Gitee仓库地址
https://gitee.com/hezhichao69/project-c/tree/master/
2.PSP表格
3.解题思路描述
看到这个题目,先处理文本文档输入输出,然后去掉不需要的汉字这个时候我们可以开始统计字符行了,只有汉字的因为去掉了所以没有了,当空行处理,然后就是把字符串分成一个一个的单词并且统计,当然有一个非常重要的事情就是,我们需要一个统计每一个的单词的数量以及在字典里面的顺序。
4.代码规范制定链接
https://gitee.com/hezhichao69/project-c/blob/master/codestyle.md
5.基本代码
#include <iostream>
#include<sstream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
string RemoveChinese(char* str);
int CharacterValue(vector<string> str);
int Validline(vector<string> str);
int main()
{
vector<string> a;
int words = 0;
int sum = 0;
char buffer[2560];
string buyfferString;
ifstream in("input.txt");
if (!in.is_open())
{
cout << "Error opening file"; exit(1);
}
while (!in.eof())
{
in.getline(buffer, 100);
cout << RemoveChinese(buffer) << endl;
a.push_back(RemoveChinese(buffer));
if (RemoveChinese(buffer) != "")
{
sum++;
}
istringstream is(RemoveChinese(buffer));
string s;
while (is >> s)
{
words++;
}
}
ofstream mycout("output.txt");
cout << sum << endl;
mycout << "characters:" << words << endl;
mycout << "words:" << CharacterValue(a) << endl;
mycout << "lines:" << a.size() << endl;
mycout.close();
return 0;
}
string RemoveChinese(char* str)
{
string qwe;
for (int i = 0; i < strlen(str); i++)
{
if (str[i] - '\0' >= 0 && str[i] - '\0' <= 127)
qwe += str[i];
}
return qwe;
}
int CharacterValue(vector<string> str)
{
int sum = 0;
for (int i = 0; i < str.size(); i++)
{
sum += str[i].length();
}
return sum;
}
6.性能测试