单词检索程序

word.h头文件

#ifndef WORD_H_INCLUDED
#define WORD_H_INCLUDED
#include 
#include 
#include 
#include
#include
#define LIST_INIT_SIZE 500   /*线性表存储空间的初始分配量*/
#define LISTINCREMENT 10     /*线性表存储空间的分配增量*/
#define FILE_NAME_LEN 20        /*文件名长度*/
#define WORD_LEN     20         /*单词长度*/
#define MaxStrSize   256
#define llength 110          /*规定一行有110个字节*/
#define MaxStr 258
#define WORD 21
typedef struct
{
    char word[MaxStr];  /* ch是一个可容纳256个字符的字符数组 */
    int length;

} str; /* 定义顺序串类型 */
typedef struct
{
    char word[WORD];          /*存储单词,不超过20个字符*/
    int count;                 /*单词出现的次数*/
} danci;
typedef struct{
    danci *elem;           /*存储空间基址*/
    int length;                /*当前长度*/
    int listsize;              /*当前分配的存储容量*/
} sqlist;
char filename[20];

#endif // WORD_H_INCLUDED

主程序:

#include"word.h"
#include 
using namespace std;
int sqlist_init(sqlist *sq,danci *et)
{
    sq->elem=et;
    sq->length=0;
    return 0;
}
int sqlist_add(sqlist *sq,danci *et,char *word)
{
    int i,j;
    for(i=0;ilength;i++)
    {
        if(strcmp(et[i].word,word)==0)
        {
            et[i].count++;
            return 0;
        }
        else if (strcmp(et[i].word, word) > 0)  //保证了按照字典序插入
            break;
    }
        if(sq->length==LIST_INIT_SIZE){
            cout<<" 空间已满,单词"<length;j>i;j--)
        memcpy(et+j, et+j-1, sizeof(danci));//单词依次后移,直到i(字典序正确位置)
    sq->length++;
    strcpy(et[i].word, word);//将word插入
    et[i].count=1;
    return 0;
}
int denglu()
{
    int k;
    do
    {
    		system("cls");//清屏
            cout<<"         欢迎使用文件单词检索系统"<>k;
    }while(k<1||k>6);
    return k;
}
void getfile()
{
    cout<<"请输入你要新建的文件名 ";
    cin>>filename;
    cout<<"文件建立完成"<=b.length)
        return i-b.length;

    else return -1;
}
int getlocate()
{
    FILE *fp;
    str s,t;
    int loca[1000];
    int k;
    fp=fopen(filename,"r");//getline (in, line)
    //ifstream in(filename);
    cout<<"请输入需要检索的单词"<>t.word;
    t.length=strlen(t.word);
    int line=0;
    while(!feof(fp))//逐行读出至s.word
    {
        memset(s.word, 0, sizeof(s.word));
        fgets(s.word, sizeof(s.word) - 1, fp);
        //cout<0){
                cout<<"             行号为:"<

 

你可能感兴趣的:(数据结构课程设计)