个人第4次作业----原型设计
课程属性
git地址 | https://github.com/sulei1999/WordCount.git |
---|---|
结对伙伴作业地址 | https://www.cnblogs.com/qiheideyue121382/p/11580903.html |
结对伙伴信息 | 汤善康201831022208 |
part.1----个人开发流程(PSP)
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 50 | 60 |
Estimate | 估计这个任务需要多少时间 | 40 | 30 |
Development | 开发 | 200 | 400 |
Analysis | 需求分析(包括学习新技术) | 100 | 100 |
Design Spec | 生成设计文档 | 100 | 90 |
Design Review | 设计复审(和同事审核设计文档) | 100 | 80 |
Coding Standard | 代码规范(为目前的开发制定合适的规范) | 30 | 60 |
Design | 具体设计 | 300 | 400 |
Coding | 具体编码 | 1000 | 1200 |
Code Review | 代码复审 | 400 | 600 |
Test | 测试(自我测试,修改代码,提交修改) | 400 | 300 |
Reporting | 报告 | 200 | 120 |
Test Report | 测试报告 | 60 | 50 |
Size Measurement | 计算工作量 | 30 | 30 |
Postmortem & Process Improvement plan | 事后总结, 并提出过程改进计划 | 60 | 60 |
合计 | 3070 | 3600 |
part.2----前期分析
1.解题思路描述
(1) 拿到题目,大概的有了一个思路,首先注意到三个点:①命令行窗口运行程序②怎样通过一连串的地址输入,并将其分解为一个个独立的地址,并传到相应的地方③命令行输入文件名参数进行读取。④我认为项目的难点就在于文件内容的读取和写入操作。
(2)然后就是想到建立一个类,其内包含字符、单词、行等的记录变量,以及相关函数
2.设计实现过程
如上所言,需要用到一个类“Wordcount”,设置私有成员“characters”(统计记录字符数)“words”(统计记录单词数)“line”(统计记录行数),因此又要有三个公有成员函数用于返回三个私有成员值getcharacters()、getwords()、getlines(),然后再有三个对应的计算函数等等。
part.3----代码实现
1.代码规范制定
这方面并没有那么严格死板地有所制定,而是彼此都约定俗成地做到了规范书写代码,并给出适当注释说明
2.代码互审
代码互审我们是在写的过程中同时进行的,每写一个函数时就进行互相审查。####3.性能分析
Image
4.代码说明
结构体
class Wordcount
{
public:
Wordcount countcha(char *, Wordcount);
Wordcount countword(char *, Wordcount);
Wordcount countline(char *, Wordcount);
int getcharacters();
int getlines();
int getwords();
char *content;//存放文本文件数据
void init();//构造函数的功能,将数据初始化
private:
int characters;
int words;
int lines;
};
计算字符数单词数和行数
Wordcount Wordcount::countcha(char *t, Wordcount 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;
}
Wordcount Wordcount::countline(char *t, Wordcount 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;
}
返回私有元素值
int Wordcount::getcharacters()
{
return characters;
}
int Wordcount::getlines()
{
return lines;
}
int Wordcount::getwords()
{
return words;
}
主函数
int main(/*int argc, char *argv[]*/)
{
clock_t start = clock();
int i, num = 0, j, r = 0;
Wordcount f1;
char a[200];///////////////////////////
f1.init();
scanf_s("%[^\n]", a, 200);
/* for (int i = 0; a[i] != NULL; i++)
{
if (a[i] == '-')
if (a[i + 1] == 'i')
for (int j = i + 3; a[j] != ' '; j++)
{
wr[r] = a[j];
r++;
}
}*/
for (int i = 0; a[i] != NULL; i++)
{
if (a[i] == '-')
if (a[i + 1] == 'i')
for (int j = i + 1; a[j] != NULL; j++)
{
if (a[j] == ' ')
if (a[j + 1] != ' '&&a[j + 1] != '-') //输出文件入口判断
{
wr = a + j + 1;
break;
}
}
}
cout << wr << endl;
/* for (int i = 0; a[i] != NULL; i++)
{
if (a[i] == '-')
if (a[i + 1] == 'o')
for (int j = i + 1; a[j] != NULL; j++)
{
if (a[j] == ' ')
if (a[j + 1] != ' '&&a[j + 1] != '-') //输出文件入口判断
{
wo = a + j + 1;
break;
}
}
}*/
for (int i = 0; a[i] != NULL; i++)
{
if (a[i] == '-')
if (a[i + 1] == 'o')
for (int j = i + 3, k = 0; (a[j] != ' '); j++, k++)
{
wo[k] = a[j];
}
}
if (!wr)
{
cout << "未输入文件名或文件不存在" << endl;
return 0;
}
f1 = f1.countcha(wr, f1);
f1 = f1.countline(wr, f1);
f1 = f1.countword(wr, f1);
sWord *ww = new sWord[f1.getwords()];//给结构体分配一个大小为单词数目的动态空间
sWord *temp = new sWord[f1.getwords()];
map<string, int>::iterator it;
it = mapword1.begin();
for (it; it != mapword1.end(); it++)
{
ww[num].w = it->first;
ww[num].count = it->second;
num++;
}
sort(ww, temp, 0, num - 1);//把已经按字典序排号按出现频率进行从大到小的归并排序
//输出
ofstream fout;
fout.open(wo);
if (!fout)
cout << "文件打开失败" << endl;
// cout << "characters: " << f1.getcharacters() << endl;
fout << "characters: " << f1.getcharacters() << endl;
// cout << "words: " << f1.getwords() << endl;
fout << "words: " << f1.getwords() << endl;
// cout << "lines: " << f1.getlines() << endl;
fout << "lines: " << f1.getlines() << endl;
if (num < 10)
{
for (i = 0; i < num; i++)
{
// cout << "<" << ww[i].w << ">" << ": " << ww[i].count << endl;
fout << "<" << ww[i].w << ">" << ": " << ww[i].count << endl;
}
}
else
{
for (i = 0; i < 10; i++)
{
// cout << "<" << ww[i].w << ">" << ": " << ww[i].count << endl;
fout << "<" << ww[i].w << ">" << ": " << ww[i].count << endl;
}
}
fout.close();
delete[]temp;
delete[]ww;
free(f1.content);//动态空间释放
clock_t ends = clock();
cout << "运行时间 : " << (double)(ends - start) / CLOCKS_PER_SEC << "秒" << endl;
return 0;
}
按字典次序排序
void merge(sWord *a, sWord *c, int l, int mid, int r) {
int i = l, j = mid + 1, m = 1;
while (i <= mid && j <= r) {
if (a[i].count < a[j].count) c[m++] = a[j++];
else c[m++] = a[i++];
}
while (i <= mid) c[m++] = a[i++];
while (j <= r) c[m++] = a[j++];
for (int k = 1; k <= r - l + 1; k++)
a[l + k - 1] = c[k];
}
void sort(sWord *a, sWord *c, int l, int r) {
if (l < r) {
int mid = (l + r) / 2;
sort(a, c, l, mid);
sort(a, c, mid + 1, r);
merge(a, c, l, mid, r);
}
}
Image运行截图
以“-o 要读取的文档1路径 -i 要写入的文档2路径”输入,即可将对文档1的读取结果写入文档2中
part.4----项目收获与心得
结对心得与收获
本次作业总体来说,一个字就是“累”,时间上太过紧张,真的是心力交瘁,晚上睡觉还在想睡前没有解决的程序问题,但是在这几天忙碌的“边学边写代码”中,真的接触到了很多新的知识、技术,命令行传参等都是之前很少涉及的,因此而言,十分感谢本课程给了我们这样一个机会,去让我们在短期内有了显著的进步
结对心得与收获:
与个人编程感觉有很大的差别,前期效率有些过低,甚至怀疑过到底有没有这样结对的必要,但渐渐磨合之后发现1+1明显大于2,很多代码出现错误自己一直改不过来,在结对同伴的帮助下就很容易地得到了解决,而且软件开发的规范性上有了很大的提高,再次感谢我的伙伴汤善康。