软件工程作业之单词统计之最

作业要求:不限语种、文本大小在30 ~ 400 K左右

分析:运用函数调用、链表指针、循环等完成


实现代码:

//头文件区
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<iomanip>
using namespace std;


//类
//用来纪录从文本中读出的所有单词
class Word
{
public:std::string word;
public:int num;
public:Word * nextP;
};
typedef Word * wordP;
//用来记录数量最多的单词
class word
{
public:string Tword;
public:word *  nextp;
};
typedef word * wordp;


//函数声明区
wordP head = NULL;
wordP nextP = NULL;
void Statics();
wordP NewMemory();
void WordCount(std::string w);
//void judgeWord(std::string w);
void print();
void releaceM(wordP p);
void releaceTM(wordp head);
int judgeSameWord(wordp p, string word);


//主函数
void main()
{
system("color F1");
Statics();
print();
releaceM(head);
system("pause");
}


//统计、从文件中去读单词到内存函数
void Statics()
{
string temp;
ifstream TextFile;
TextFile.open("test.txt");
while (TextFile>>temp)
{
WordCount(temp);
}
}


//申请内存
wordP NewMemory()
{
if (head == NULL)
{
wordP p = new Word;
head = p;
nextP = p;
p->nextP = NULL;
return p;
}
else
{
wordP p = new Word;
nextP->nextP = p;
nextP = p;
p->nextP = NULL;
return p;
}
}


//单词数量计算
void WordCount(std::string w)
{
bool Flag = false;
if (head != NULL)
{
wordP p = head;
while (p != NULL)
{
if (p->word == w)
{
p->num += 1;
Flag = true;
break;
}
else
p = p->nextP;
}
}
if (Flag == false)
{
wordP current = NewMemory();
current->word = w;
current->num = 1;
}
}


/*
void judgeWord(std::string w)
{
size_t size = w.length();
if (w[size - 1] == '\n')
{
std::string word = "";
int i = 0;
while (i != size - 2)
{
word += w[i];
i++;
}
word += '\n';
WordCount(word);
}
else if (w[0] == '\n')
{
std::string word = "";
int i = 1;
while (i != size - 1)
{
word += w[i];
i++;
}
word += '\n';
WordCount(word);
}
else
WordCount(w);
}
*/


//打印单词数量和数量以及之最
void print()
{
int num = 0;
wordP p = head;
wordp p1 = NULL,TEMP = NULL;
while (p)
{
cout << "词语:" <<setw(38)<<setiosflags(ios::left)<< p->word << "数量:" << p->num << endl;
if (p->num > num)
{
if (p1 == NULL)
{
wordp temp = new word;
temp->Tword = p->word;
temp->nextp = NULL;
TEMP = temp;
num = p->num;
p1 = temp;
}
else
{
releaceTM(p1);
p1 = TEMP = NULL;
wordp temp = new word;
temp->Tword = p->word;
temp->nextp = NULL;
TEMP = temp;
num = p->num;
p1 = TEMP;
}
}
if (p->num == num)
{
if (judgeSameWord(p1, p->word) == 0)
{
wordp temp = new word;
temp->Tword = p->word;
temp->nextp = NULL;
TEMP->nextp = temp;
TEMP = temp;
}
}
p = p->nextP;
}
cout <<endl<< "数量最多的单词是:" << endl;
wordp temp = p1;
while (temp != NULL)
{
cout << temp->Tword << endl;
temp = temp->nextp;
}



cout << "数量是:" << " " << num << endl;
releaceTM(p1);
}


//释放内存函数
void releaceM(wordP p)
{
wordP temp;
while (p != NULL)
{
temp = p;
p = p->nextP;
delete temp;
}
}


//释放内存函数
void releaceTM(wordp head)
{
wordp temp;
while (head != NULL)
{
temp = head->nextp;
delete head;
head = temp;
}
}




//在计算的过程中判断是否有相同的单词
int judgeSameWord(wordp p,string word)
{
while (p != NULL)
{
if (p->Tword == word)
return 1;
p = p->nextp;
}
return 0;
}



文本截图:



运行截图:



分析:

在做题的过程中出现了很多错误,包括语法和逻辑,所以需要自己细心、仔细才能完成程序的编写和调试,正确运行得到正确的结果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值