⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 literature research assistant.cpp

📁 文学助手的课程设计 文学研究人员需要统计某篇英文小说中某些形容词的出现次数和位置。试写一个实现这一目标的文字统计系统
💻 CPP
字号:
#include <iostream> 
#include <string>
using namespace std;
#include <stdio.h>
struct word
{
	 int line;
	 int time;
	 word * next;
}; 
struct words
{
	 char word_re[20];
	 int check;
	 int time_t;
	 words * next_s;
	 word  * next;
}; 
words * input(words * head); //比較並儲存信息時候所用的函數
words * output(words * head); //單純輸出直觀數據時候所使用的函
words * input(words * head)
{
 int count=0; 
 int line=0; //記錄行數所使用到的變量
 int time_words=0;
 head->next_s=new words;
 int check=1;
 words * insert=new words;
 word * insert_word=new word;
 insert=head->next_s;
 cout<<"请输入需要查询的单词"<<endl;
 while(check)
 {
	  cin>>insert->word_re;
	  insert->time_t=0;
	  insert->check=0;
	  insert->next=new word;
	  cout<<"还希望搜索其他的单词么,1继续,0停止"<<endl;
	  cin>>check;
	  if(check>=1)
	  {
		   insert->next_s=new words;
		   insert=insert->next_s;
	  }
	 } 
	 insert->next_s=NULL;
	 insert=head->next_s;
	 while(insert!=NULL)
	 {
		  cout<<insert->word_re<<endl;
		  insert=insert->next_s;
	 }
	 insert=NULL;
	 int time=0;   
	 int time_gk=0,time_wt=0;
	 char compare[3]=" @";
	 char getkey[32];
	 char * word_tair;
	 FILE * getword;
	 if((getword=fopen("galgame.txt","r"))==NULL)   
	 {   
		  cout<<"error";     
		  return 0;   
	 }   
	 char word_c[80];
	 while(fgets(word_c,80,getword)!=NULL) //讀取一行中的80個字符
	 {  
		  line++;
		  time_words=0; //尋找的字符出現的次數
		  time_gk=0; //儲存用來比較的字符串所使用的變量
		  time_wt=0;  //在儲存行字符串的變量中定位
		  word_tair=NULL;
		  word_tair=word_c;  
		  time=strlen(word_tair);
		  cout<<time;
		  cout<<word_tair<<endl;
		 while(word_tair[time_wt]!='\n')  
		 { 
			 time_gk=0;
			 if((word_tair[time_wt]<='z'&&word_tair[time_wt]>='a')||(word_tair[time_wt]<='Z'&&word_tair[time_wt]>='A')||word_tair[time_wt]=='-')
		   {

				while((word_tair[time_wt]<='z'&&word_tair[time_wt]>='a')||(word_tair[time_wt]<='Z'&&word_tair[time_wt]>='A')||word_tair[time_wt]=='-')
				{
     
				 getkey[time_gk]=word_tair[time_wt];
				 time_wt++;
				 time_gk++;
		}  //讀取出一個單詞,並儲存在新的變量中
		getkey[time_gk++]='\0';
		insert=head->next_s;
		while(insert!=NULL)
		{
			 if(!strcmp(insert->word_re,getkey))
			 {
				  count=insert->check;
				  insert->check++;
				  insert_word=insert->next; //判斷是否是相同的單詞
				 while(count>0)
			  {
				   insert_word=insert_word->next;
				   count--;
			  }
			  insert_word->line=line;
			  insert_word->time=time_words;
			  insert_word->next=NULL;
			  insert_word->next=new word;      
			  break; 
			 } //儲存相應信息
			 insert=insert->next_s;
			}
			time_words++;
		   } 
		   if(word_tair[time_wt++]=='\n')
			break;
   
		  } 
	 }
	 cout<<endl;
	 return head;
}
//輸出的函數:
words * output(words * head)
{
	int time=0;
	words * first=head->next_s;
	words * insert=first;
	word * insert_word=new  word;
	while(insert!=NULL)
	{
		cout<<insert->word_re<<"单词共出现了"<<insert->check<<"次"<<endl;
		insert_word=insert->next;
		while(insert->check>time)
		{
			time++;
			cout<<"第"<<time<<"次出现在"<<insert_word->line<<"行,第"<<insert_word->time+1<<"个单词"<<endl;
			insert_word=insert_word->next;
		}
		insert=insert->next_s;
		time=0;
	}
	return head;
}  
int main()
{
      words * head_emp=new words; 
      strcpy(head_emp->word_re,"this is empty"); //母鏈的頭賦空
      head_emp->check=-1;
      head_emp->time_t=0;
      head_emp->next=NULL;
      input(head_emp);
      output(head_emp);
      return 0;   

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -