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

📄 lassistant.cpp

📁 严蔚敏版《数据结构(C语言)》里的实习作业之“文学助手(LetterAssistant)”
💻 CPP
字号:
// LAssistant.cpp: implementation of the LAssistant class.
//
//////////////////////////////////////////////////////////////////////
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include "LAssistant.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

LAssistant::LAssistant()
{

}

LAssistant::~LAssistant()
{
    DeleteSpace();
}

Status LAssistant::Words_Input(){
    char c[50];
	int i=0,j,k;n=0;
	printf("请输入一组查找的单词,空格区分,回车结束:\n");
	gets(c);
	while(1){
		n++;
		word[n].T=(char*)malloc(10*sizeof(char));
		if(word[n].T==NULL)exit(OVERFLOW);
		k=0; 
		word[n].length=0;
		for(j=i;;j++){	
			if(c[j]==' '){word[n].T[k++]='\0';break;}
			if(c[j]=='\0'){word[n].T[k++]='\0';return OK;}
	    	word[n].T[k++]=c[j];		
			word[n].length++;
		}
		i=j+1;
	}	
}

int LAssistant::Words_Index(texts text,words &word,int pos){
	int i=pos,j=0;
	while(i<text.length&&j<word.length){
	    if(text.S[i]==word.T[j]){i++;j++;}
    	else{i=i-j+1;j=0;}
	}
	if(j>=word.length)
		return i-word.length+1;
    return 0;
}

Status LAssistant::Words_Statistic_Index(){
	FILE *str;
    int linenum[100];
    for(int i=1;i<=n;i++){
    printf("%d: 查找%s...\n",i,word[i].T);       
    int flag=0,k=0;
	text.line=0;word[i].num=0;
    str=fopen(filename,"r");
	while(fgets(text.S, sizeof(text.S)-1, str)!=NULL){
//printf("%s",text.S);
        text.length=strlen(text.S);
        text.line++;
		int loc=Words_Index(text,word[i],0);
		if(loc)flag=1;
		while(loc){
			word[i].num++;
			linenum[k++]=text.line;
			loc=Words_Index(text,word[i],loc++);
		}		
	}
    if(flag){
		printf("找到%s所在的行有:",word[i].T);
		for(int j=0;j<k-1;j++)printf("%d, ",linenum[j]);
		printf("%d。\n",linenum[k-1]);
	}
	else printf("找不到%s。\n",word[i].T);
    printf("该词在此文件中出现了%d次。\n",word[i].num);
}
	fclose(str);
	return OK;
}

void LAssistant::DeleteSpace(){
	for(int i=1;i<=n;i++){
		free(word[i].T);
		word[i].length=0;
	}
}

Status LAssistant::Choose_file(){
	FILE *fp;int i=0;
	getchar();
	printf("请输入文件名:");
    while(scanf("%c",&filename[i])&&filename[i]!='\n')i++;
	filename[i]='\0';
	printf("您要查找的文件是:");
	puts(filename);
	if((fp=fopen(filename,"r"))==NULL){
		printf("\n错误:无法打开文件!\n");
		return ERROR;
	}
	return OK;
}

void LAssistant::Words_Display(){
	printf("您输入的单词是:\n");
	for(int i=1;i<=n;i++)
		printf("%d.%s   ",i,word[i].T);
	printf("\n");
}

void LAssistant::Word_Modify(){
	int k=0;
	printf("请输入一个需要修改的词的序号:");
	scanf("%d",&k);
    printf("请重新输入这个单词,回车结束:");
	scanf("%s",word[k].T);
	printf("%d.%s\n",k,word[k].T);
}

⌨️ 快捷键说明

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