admin管理员组

文章数量:1579086

实现目标:

  1. 连续执行至完毕
  2. 显示实现时间
  3. 单步执行:识别并统计单词
  4. 单步执行:删除并显示低频单词
  5. 单步执行:输出其余单词及其频率
  6. 单步执行:计算并输出ASL值

以下是KeywordsFiltering.h

#ifndef KEYWORDSFILTERING_H
#define KEYWORDSFILTERING_H
#include<iostream>
#include<fstream>
#include<string>

using namespace std;
class word{
	public:
	string WordName;	//单词名称 
	int count;		//单词出现频率 
};

class SeqList{
	public:
		word elem[1000];
		int length;
		SeqList();
};

class KeywordsFiltering
{
	public:
		KeywordsFiltering();
		~KeywordsFiltering();
		string getWordName();//每次从txt文件中取出一个单词 
		void sortSeq();
		SeqList getKeywords_0();
		void addNewKeywords_SeqList(string str);	//出现前所未有的单词 
		void addCount_SeqList();	//查找发现之前相同的单词以及包含其他步骤 
		void delAndOutPut();	//删除并显示低频单词
		void outPut();	//输出高频词汇
		void inPutFile();
		void GetASL();	//获取ASL值 
		fstream aFile;
	private:
		SeqList Keywords_0;
};

#endif

以下是KeywordsFiltering.cpp

#include "KeywordsFiltering.h"
#pragma once

SeqList::SeqList(){
	length=0;
}

KeywordsFiltering::KeywordsFiltering()
{
}

KeywordsFiltering::~KeywordsFiltering()
{
}

string KeywordsFiltering::getWordName(){
	string str;
	aFile>>str;
	return str;
}

void KeywordsFiltering::addNewKeywords_SeqList(string str){
	Keywords_0.elem[Keywords_0.length].WordName=str;
	Keywords_0.elem[Keywords_0.length].count++;
	Keywords_0.length++;
}

void KeywordsFiltering::addCount_SeqList(){
	string str=getWordName();

	for(int i=0;i<Keywords_0.length;i++){
		if(Keywords_0.elem[i].WordName==str){	//如果和表中重复,则频率加一 
			Keywords_0.elem[i].count++;
			break;	
		}
		if(Keywords_0.elem[i].WordName!=str&&i==Keywords_0.length-1)	//如果遍历表中全部元素均无相同,则添加 
			addNewKeywords_SeqList(str);
	}
		if(Keywords_0.length==0)	//从零开始 
		addNewKeywords_SeqList(str);
}

void KeywordsFiltering::sortSeq(){
	for(int i=0;i<Keywords_0.length-1;i++)
		for(int j=i+1;j<Keywords_0.length;j++)
			if(Keywords_0.elem[i].count<Keywords_0.elem[j].count){
				word temp=Keywords_0.elem[i];
				Keywords_0.elem[i]=Keywords_0.elem[j];
				Keywords_0.elem[j]=temp;
			} 
}

void KeywordsFiltering::delAndOutPut(){//先行函数是sortSeq函数,按照从高到低的频率顺序进行排序。 
	sortSeq();
	cout<<"以下是频率低的关键词:"<<endl;
	for(int i=0;i<Keywords_0.length;i++)
		if(Keywords_0.elem[i].count<5){
			for(int j=i;j<Keywords_0.length;j++)
				cout<<"单词名:"<<Keywords_0.elem[j].WordName<<'\t'<<"出现频率"<<Keywords_0.elem[j].count<<endl;
			Keywords_0.length-=(Keywords_0.length-i);
			break;
		}
}

void KeywordsFiltering::outPut(){
	cout<<"以下是高频词汇"<<endl;
	for(int i=0;i<Keywords_0.length;i++)
		cout<<"单词名:"<<Keywords_0.elem[i].WordName<<'\t'<<"出现频率"<<Keywords_0.elem[i].count<<endl;
}

void KeywordsFiltering::inPutFile(){
	aFile.open("OutFile",ios::out|ios::app);
	if(aFile.is_open()==1)
		cout<<"创建成功"<<endl;
	else
		cout<<"完犊子啦"<<endl;
	for(int i=0;i<Keywords_0.length;i++){
		aFile<<Keywords_0.elem[i].WordName;
		aFile<<"\n\n";
	}
}

SeqList KeywordsFiltering::getKeywords_0(){
	return Keywords_0;
}

以下是主函数段:

#include<iostream>
#include<fstream>
#include<string>
#include"KeywordsFiltering.h"
#include"time.h"

using namespace std;
using std::ios;


int main(){
	KeywordsFiltering Key;
 
	int judge;
	clock_t start,finish;
	start = finish;
	for(;;){
		cout<<"1、线性表"<<'\n'<<"2、退出系统"<<'\n'<<"请选择你需要的服务,输入数字(1/2):\n";
		cin>>judge;
		if(judge == 1){
			system("CLS");
			Key.aFile.open("Infile.txt",ios::in|ios::out|ios::app);
			cout<<"1、连续执行至完毕"<<'\n'<<"2、显示执行时间"<<'\n'<<"3、单步执行:识别并统计单词"<<'\n'<<"4、单步执行:删除并显示出现频率低单词"<<'\n'<<"5、单步执行:输出其余单词及其频率"<<'\n'<<"6、单步执行:计算并输出ASL值"<<'\n'<<"7、返回主菜单"<<endl;
			for(;;){
				cin>>judge;
				if(judge == 1){
					start=clock();
					for(;Key.aFile.eof()!=true;)
						Key.addCount_SeqList();
					Key.delAndOutPut();
					Key.outPut();
					Key.aFile.close();
					Key.inPutFile();
					Key.aFile.close();
					finish=clock();
				}
				else if(judge == 2)
					cout<<"运行时间为:"<<finish-start<<"毫秒"<<endl;
				else if(judge == 3){
					for(;Key.aFile.eof()!=true;)
						Key.addCount_SeqList();
					Key.aFile.close();
					cout<<"已完成"<<endl;
				}
				else if(judge == 4){
					Key.aFile.open("Infile.txt",ios::in|ios::out|ios::app);
					Key.delAndOutPut();
					Key.aFile.close();
				}
				else if(judge == 5){
					Key.aFile.open("Infile.txt",ios::in|ios::out|ios::app);
					Key.outPut();
					Key.aFile.close();
				}
				else if(judge == 6){
					int i=Key.getKeywords_0().length+1;
					cout<<"ASL值为:"<<double(i/2)<<endl;
				}
				else{
					system("CLS"); 
					break;
				}
			}
		}
		else
			break;
	}
	system("color F0");
	system("pause");
	return 0;
}

注:1、作者使用的编译器为DEV5.11,在其他编译器出现bug力所不能及

       2、文本文档可以自行寻找,这里会附加我使用的“Infile.txt”文档

以下为文档内容,不是代码。

My father was a self-taught mandolin player. He was one of the best string instrument players in our town. He could not read music, but if he heard a tune a few times, he could play it. When he was younger, he was a member of a small country music band. They would play at local dances and on a few occasions would play for the local radio station. He often told us how he had auditioned and earned a position in a band that featured Patsy Cline as their lead singer. He told the family that after he was hired he never went back. Dad was a very religious man. He stated that there was a lot of drinking and cursing the day of his audition and he did not want to be around that type of environment. 

  Occasionally, Dad would get out his mandolin and play for the family. We three children: Trisha, Monte and I, George Jr., would often sing along. Songs such as the Tennessee Waltz, Harbor Lights and around Christmas time, the well-known rendition of Silver Bells. "Silver Bells, Silver Bells, its Christmas time in the city" would ring throughout the house. One of Dad's favorite hymns was "The Old Rugged Cross". We learned the words to the hymn when we were very young, and would sing it with Dad when he would play and sing. Another song that was often shared in our house was a song that accompanied the Walt Disney series: Davey Crockett. Dad only had to hear the song twice before he learned it well enough to play it. "Davey, Davey Crockett, King of the Wild Frontier" was a favorite song for the family. He knew we enjoyed the song and the program and would often get out the mandolin after the program was over. I could never get over how he could play the songs so well after only hearing them a few times. I loved to sing, but I never learned how to play the mandolin. This is something I regret to this day. 

  Dad loved to play the mandolin for his family he knew we enjoyed singing, and hearing him play. He was like that. If he could give pleasure to others, he would, especially his family. He was always there, sacrificing his time and efforts to see that his family had enough in their life. I had to mature into a man and have children of my own before I realized how much he had sacrificed. 

  I joined the United States Air Force in January of 1962. Whenever I would come home on leave, I would ask Dad to play the mandolin. Nobody played the mandolin like my father. He could touch your soul with the tones that came out of that old mandolin. He seemed to shine when he was playing. You could see his pride in his ability to play so well for his family. 

  When Dad was younger, he worked for his father on the farm. His father was a farmer and sharecropped a farm for the man who owned the property. In 1950, our family moved from the farm. Dad had gained employment at the local limestone quarry. When the quarry closed in August of 1957, he had to seek other employment. He worked for Owens Yacht Company in Dundalk, Maryland and for Todd Steel in Point of Rocks, Maryland. While working at Todd Steel, he was involved in an accident. His job was to roll angle iron onto a conveyor so that the welders farther up the production line would have it to complete their job. On this particular day Dad got the third index finger of his left hand mashed between two pieces of steel. The doctor who operated on the finger could not save it, and Dad ended up having the tip of the finger amputated. He didn't lose enough of the finger where it would stop him picking up anything, but it did impact his ability to play the mandolin. 

  After the accident, Dad was reluctant to play the mandolin. He felt that he could not play as well as he had before the accident. When I came home on leave and asked him to play he would make excuses for why he couldn't play. Eventually, we would wear him down and he would say "Okay, but remember, I can't hold down on the strings the way I used to" or "Since the accident to this finger I can't play as good". For the family it didn't make any difference that Dad couldn't play as well. We were just glad that he would play. When he played the old mandolin it would carry us back to a cheerful, happier time in our lives. "Davey, Davey Crockett, King of the Wild Frontier", would again be heard in the little town of Bakerton, West Virginia. 

  In August of 1993 my father was diagnosed with inoperable lung cancer. He chose not to receive chemotherapy treatments so that he could live out the rest of his life in dignity. About a week before his death, we asked Dad if he would play the mandolin for us. He made excuses but said "okay". He knew it would probably be the last time he would play for us. He tuned up the old mandolin and played a few notes. When I looked around, there was not a dry eye in the family. We saw before us a quiet humble man with an inner strength that comes from knowing God, and living with him in one's life. Dad would never play the mandolin for us again. We felt at the time that he wouldn't have enough strength to play, and that makes the memory of that day even stronger. Dad was doing something he had done all his life, giving. As sick as he was, he was still pleasing others. Dad sure could play that Mandolin! 

本文标签: 低频顺序系统