课程作业
#ifndef wordcount_h #define wordcount_h class testfile { public: testfile countcha(char *, testfile);//计算字符数 testfile countword(char *, testfile);//计算单词数 testfile countline(char *, testfile);//计算行数 int getcharacters(); int getlines(); int getwords(); char *content;//存放文本文件数据 void init(); private: int characters; int words; int lines; }; ceshi ceshijirguo
void loadword(char w[])
{
string wr;
wr = w;
map<string, int>::iterator it1 = mapword1.find(wr);//在map红黑树中查找单词
if (it1 == mapword1.end())
mapword1.insert(pair<string, int>(wr, 1));//未找到单词,插入单词并设定频次为1
else
++it1->second;//找到单词,单词出现频次增加
}
testfile testfile::countword(char *t, testfile f1)
{
int n = 0;
ifstream myfile;
myfile.open(t);
if (!myfile.is_open())
{
cout << "文件打开失败" << endl;
}
char c;
myfile >> noskipws;
while (!myfile.eof())
{
myfile >> c;
if (myfile.eof())
break;//防止最后一个字符输出两次
if (c >= 65 && c <= 90)
c += 32;//大写字母转小写
f1.content[n++] = c;//把文本文件内的数据存入类的content字符数组中
}
myfile.close();
char temp[4];
int i = 0, j = 0, flag = 0, words = 0, m = 0, k = 0;
for (i = 0; i < n; i++)
{
if (!((f1.content[i] >= 48 && f1.content[i] <= 57) || (f1.content[i] >= 97 && f1.content[i] <= 122)))//跳过非字母和非数字字符
continue;
else
{
for (j = 0; j < 4 && i < n; j++)
{
if (!((f1.content[i] >= 48 && f1.content[i] <= 57) || (f1.content[i] >= 97 && f1.content[i] <= 122)))
break;
temp[j] = f1.content[i++];//temp中存入四个非空格字符
}
if (j == 4)
{
for (m = 0; m < 4; m++)
{
if (temp[m] < 97 || temp[m]>122)
{
flag = 1;
break;//判断这四个字符是否都是字母
}
}
if (flag == 0)//四个字符都是字母的情况,判断为一个单词
{
char *w = new char[100];//存放单词
for (m = 0; m < 4; m++)
{
w[k++] = temp[m];//temp中字符存入w
}
while (((f1.content[i] >= 48 && f1.content[i] <= 57) || (f1.content[i] >= 97 && f1.content[i] <= 122)) && i < n)//继续存入单词剩余字符
{
w[k++] = f1.content[i++];
}
w[k] = '\0';
loadword(w);//可以在此处插入一个外部函数返回一个单词存入map红黑树
delete[]w;
words++;
k = 0;
}
else
{
flag = 0;
j = 0;
}
}
}
}
f1.words = words;
return f1;
}
3.统计单词数并存储单词
testfile testfile::countcha(char *t, testfile f1) { int i = 0; ifstream myfile; myfile.open(t); if (!myfile.is_open()) { cout << "文件打开失败" << endl; } char c; myfile >> noskipws;//强制读入空格和换行符 while (!myfile.eof()) { myfile >> c; if (myfile.eof()) break;//防止最后一个字符输出两次 i++; } f1.characters = i; myfile.close(); return f1; } testfile testfile::countline(char *t, testfile f1) { ifstream myfile; myfile.open(t, ios::in); int i = 0; string temp;//作为getline参数使用 if (!myfile.is_open()) { cout << "文件打开失败" << endl; } while (getline(myfile, temp)) { if(temp.empty()) continue; i++; } f1.lines = i; myfile.close(); return f1; }
class testfile { public: testfile countcha(char *, testfile);//计算字符数 testfile countword(char *, testfile);//计算单词数 testfile countline(char *, testfile);//计算行数 int getcharacters(); int getlines(); int getwords(); char *content;//存放文本文件数据 void init(); private: int characters; int words; int lines; }; void testfile::init() { characters = 0; words = 0; lines = 0; content = (char*)malloc(sizeof(char*)*MAXN); } 这个作业属于哪个课程 | https://edu.cnblogs.com/campus/zswxy/computer-science-class4-2018 |
|---|---|
| 这个作业要求在哪里 | 20 |
| 这个作业的目标 | 编程 |
| 其他参考文献 | 菜鸟教程 |
头文件和类的定义
时间
| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) |
|---|---|---|
| Planning | 计划 | 0.5h |
| Estimate | 估计这个任务需要多少时间 | 15h |
| Development | 开发 | 3h |
| Analysis | 需求分析(包括学习新技术) | 2h |
| Design Spec | 生成设计文档 | 0.5h |
| Design Review | 设计复审 | 0.5h |
| Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 0.1h |
| Design | 具体设计 | 0.5h |
| Coding |
具体编码 |
3h |
| Code Review | 代码复审 |
3h |
| Test | 测试(自我测试,修改代码,提交修改) | 2h |
| Reporting | 报告 | 0.1h |
| Test Repor | 测试报告 | 0.1h |
| Size Measurement | 计算工作量 | 1.2h |
| Postmortem & Process Improvement Plan |
事后总结, 并提出过程改进计划 |
0.5h |
| 合计 |
18h |

浙公网安备 33010602011771号