m0_62995344 2022-03-02 09:17 采纳率: 70%
浏览 145
已结题

c语言:简单英文词典排版系统,能解释一下原理和修改一下吗?

c语言:简单英文词典排版系统,代码可以运行但有错误
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define ROWS 258
#define COLS 32
#define N 1000
static FILE *fp;
static char a[ROWS][COLS];
char get_optioin(viod);
int b(int count);
void c(char pt[],int count);
int check(char arr[],int count);
void storage(char pt[],int count);
int n;
char word[N][20];
void menu()
{
int n,w;
do
{
puts("\t
MENU
\n");
puts("\t1.Add new word");
puts("\t2.Browse all the words");
puts("\t3.Search the word");
puts("\t4.Sort the words");
puts("\t5.Order by A-Z");
puts("\t6.Exit");
puts("*\n");
printf("Choice your number(1-6):[ ]\b\b");
scanf("%d",&n);
if(n<1||n>6)
{
w=1;
getchar();
}
else w=0;
}while(w==1);
switch(n)
{
case 1:add();break;
case 2:browse();break;
case 3:search();break;
case 4:sort();break;
case 5:order();break;
case 6:exit(0);
}
}
void main(){
menu();
}
int load()
{
int i,count;
int start;
char *pt[ROWS];
char ch,len;
char input;
if((fp=fopen("words.txt","a+"))==NULL)
{
printf("\ncannot open file!\n");
return NULL;
}
for(i=0;!feof(fp);i++)
fscanf(fp,"%s",&word[i]);
fclose(fp);
return i+1;
}
void save(int n)
{
FILE *fp;
int i;
if((fp=fopen("words.txt","a+"))==NULL)
{
printf("\ncannot open file!\n");
exit(0);
}
for(i=0;i<n;i++)
fprintf(fp,"%s",&word[i]);
fclose(fp);
}
void sort()
{
int i,j,k;
char c[20];
if((n=load())==0)
{
printf("\n cannot open file!\n");
exit(0);
}
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++);
if(strcmp(word[j],word[j+1])>0)
{
strcmp(c,word[j]);
strcmp(word[j],word[j+1]);
strcmp(word[j+1],c);
}
save(n);
printf("successful!\n");
printf("\nNow? 1.browse all 2.back");
scanf("%d",&k);
if(k==1)
browse();
else if(k==2)
menu();
}
void order()
{
int a[N],i,j,t;
struct words;
n=load();
for(i=0;i<N;i++)
for(i=0;i<N-1;i++)
for(j=i+i;j<N;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
for(j=0;j<N;j++)
printf("%3d",a[i]);
}
void modify(int a)
{
char c[20];
printf("Enter the new word:");
scanf("%s",c);
strcpy(word[a],c);
save(n);
}
void del(int a)
{
int x,i,y;
printf("are you sure to delate this word?\n\t1.sure 2.no and back menu []\b\b");
scanf("%d",&x);
if(x==1)
{
for(i=0;i<n-1;i++)
strcpy(word[i],word[i+1]);
save(n-1);
printf("successful! now? 1.one more 2.back menu");
scanf("%d",&y);
if(y==1)
search();
else if(y==2)
menu();
}
else if(x==2)
{
menu();
}
}
void add()
{
int i,x,u,v,w;
char c[20];
if((n=load())==0)
exit(0);
else
{
puts("Enter the new word!\n");
scanf("%s",c);
for(i=0;i<n;i++)
{
if(strcmp(word[i],c)==0)
break;
}
if(i<n)
{
w=1;
do
{
printf("the word has already exit!\n");
printf("\n\nwhat do you want to do?\n\t1.enter one more 2.back menu []\b\b");
scanf("%d",&x);
if(x<1||x>2)
u=1;
else
u=0;
}while(u==1);
}
else
{
v=1;
strcpy(word[i],c);
save(n+1);
printf("successful! now choice what you will do next?\n\t1.add another 2.back menu [ ]\b\b");
do
{
scanf("%d",&x);
if(x<1||x>2)
v=1;
else
v=0;
}while(v==1);
}
switch(x)
{
case 1:add();break;
case 2:menu();break;
}
}
}
void browse()
{
int i,w;
if((n=load())==0)
{
printf("\n cannot open file\n");
exit(0);
}
for(i=0;i<n-1;i++)
printf("%s/n",word[i]);
puts("successful! now 1.back menu 2.sort");
scanf("%d",&w);
if(w==1)
menu();
else if(w==2)
sort();
}
void search()
{
int i,x,y;
char vs[20];
if((n=load())==0)
{
printf("\n cannot open file\n");
exit(0);
}
printf("Enter the word what you want to search:\n");
scanf("%s",vs);
for(i=0;i<n;i++)
if(strcmp(word[i],vs)==0)
{
printf("successful!\n the word is %s\n",word[i]);
printf("what would you like to do with the word?\n\t 1.modify 2.delrte 3.nothing []\b\b");
scanf("%d",&y);
if(y==1)
modify(i);
else{del(i);}
}
if(i==n)
printf("hoho!sorry.not found~");
printf("now.1.one more 2.back 3exit");
scanf("%d",&x);
switch(x)
{
case 1:search();break;
case 2:menu();break;
case 3:exit(0);
}
}

可以运行但结果错误

有能解释一下原理和帮忙修改一下吗?谢谢。

  • 写回答

3条回答 默认 最新

  • 关注

    思路:
    char word[N][20];保存所有的单词,在这个二维数组中进行插入、删除、遍历操作实现单词的插入、删除和查询显示。
    错误:
    这个代码根本跑步起来啊。
    (1)
    char get_optioin(viod);
    int b(int count);
    void c(char pt[],int count);
    int check(char arr[],int count);
    void storage(char pt[],int count);
    这些函数你声明了没实现,反而在menu()函数中调用的函数都没有声明。
    (2)load()函数中,fscanf(fp,"%s",&word[i]);这里,不需要&符号。而且你的函数中,不需要每次都去调用load,在main函数中调用一次就可以了。
    (3)其它错误不一一说了,order函数没看明白要干啥,代码修改如下:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>
    #define ROWS 258
    #define COLS 32
    #define N 1000
    static FILE *fp;
    //static char a[ROWS][COLS];
    int load();
    void add();
    void browse();
    void search();
    void sort();
    void order();
    //char get_optioin();
    //int b(int count);
    //void c(char pt[],int count);
    //int check(char arr[],int count);
    //void storage(char pt[],int count);
    int n=0;
    char word[N][20];
    void menu()
    {
        int n,w;
        do
        {
            puts("\tMENU\n");
            puts("\t1.Add new word");
            puts("\t2.Browse all the words");
            puts("\t3.Search the word");
            puts("\t4.Sort the words");
            puts("\t5.Order by A-Z");
            puts("\t6.Exit");
            puts("*\n");
            printf("Choice your number(1-6):[ ]\b\b");
            scanf("%d",&n);
            if(n<1||n>6)
            {
                w=1;
                //getchar(); //这一句没用
            }
            else w=0;
        }while(w==1);
        switch(n)
        {
        case 1:add();break;
        case 2:browse();break;
        case 3:search();break;
        case 4:sort();break;
        case 5:order();break;
        case 6:exit(0);
        }
    }
    void main(){
        n=load();//这里先从文件中读取数据,并返回读取的单词个数
        menu();
    }
    int load()
    {
        int i;
        if((fp=fopen("words.txt","a+"))==NULL)
        {
            printf("\ncannot open file!\n");
            return NULL;
        }
        for(i=0;!feof(fp);i++)
            fscanf(fp,"%s",word[i]);
        fclose(fp);
        return i;
    }
    void save(int n)
    {
        FILE *fp;
        int i;
        if((fp=fopen("words.txt","a+"))==NULL)
        {
            printf("\ncannot open file!\n");
            exit(0);
        }
        for(i=0;i<n;i++)
        {
            if(i<n-1)
                fprintf(fp,"%s ",word[i]);
            else
                fprintf(fp,"%s",word[i]);
        }
        fclose(fp);
    }
    void sort()
    {
        int i,j,k;
        char c[20];
        /*if((n=load())==0)
        {
        printf("\n cannot open file!\n");
        exit(0);
        }*/
        for(i=0;i<n;i++)
        {
            for(j=0;j<n-i-1;j++)
            {
                if(strcmp(word[j],word[j+1])>0)
                {
                    strcmp(c,word[j]);
                    strcmp(word[j],word[j+1]);
                    strcmp(word[j+1],c);
                }
            }
    
        }
        save(n);
        printf("successful!\n");
        printf("\nNow? 1.browse all 2.back");
        scanf("%d",&k);
        if(k==1)
            browse();
        else if(k==2)
            menu();
    }
    void order()
    {
        int a[N],i,j,t;
        //struct words;
        //n=load();
        for(i=0;i<N;i++)
            for(i=0;i<N-1;i++)
                for(j=i+i;j<N;j++)
                    if(a[i]>a[j])
                    {
                        t=a[i];
                        a[i]=a[j];
                        a[j]=t;
                    }
                    for(j=0;j<N;j++)
                        printf("%3d",a[i]);
    }
    void modify(int a)
    {
        char c[20];
        printf("Enter the new word:");
        scanf("%s",c);
        strcpy(word[a],c);
        save(n);
    }
    void del(int a)
    {
        int x,i,y;
        printf("are you sure to delete this word?\n\t1.sure 2.no and back menu []\b\b");
        scanf("%d",&x);
        if(x==1)
        {
            for(i=a;i<n-1;i++) //这里从a开始
                strcpy(word[i],word[i+1]);
            n-=1;
            save(n);
            printf("successful! now? 1.one more 2.back menu");
            scanf("%d",&y);
            if(y==1)
                search();
            else if(y==2)
                menu();
        }
        else if(x==2)
        {
            menu();
        }
    }
    void add()
    {
        int i,x,u,v,w;
        char c[20];
        /*if((n=load())==0)
        exit(0);
        else*/
        {
            puts("Enter the new word!\n");
            scanf("%s",c);
            for(i=0;i<n;i++)
            {
                if(strcmp(word[i],c)==0)
                    break;
            }
            if(i<n)
            {
                w=1;
                do
                {
                    printf("the word has already exit!\n");
                    printf("\n\nwhat do you want to do?\n\t1.enter one more 2.back menu []\b\b");
                    scanf("%d",&x);
                    if(x<1||x>2)
                        u=1;
                    else
                        u=0;
                }while(u==1);
                if(x==1)
                    add();
                else
                    menu();
            }
            else
            {
                v=1;
                strcpy(word[i],c);
                n+=1;
                save(n);
                printf("successful! now choice what you will do next?\n\t1.add another 2.back menu [ ]\b\b");
                do
                {
                    scanf("%d",&x);
                    if(x<1||x>2)
                        v=1;
                    else
                        v=0;
                }while(v==1);
            }
            switch(x)
            {
            case 1:add();break;
            case 2:menu();break;
            }
        }
    }
    void browse()
    {
        int i,w;
        /*if((n=load())==0)
        {
        printf("\n cannot open file\n");
        exit(0);
        }*/
        for(i=0;i<n;i++)
            printf("%s/n",word[i]);
        puts("successful! now 1.back menu 2.sort");
        scanf("%d",&w);
        if(w==1)
            menu();
        else if(w==2)
            sort();
    }
    void search()
    {
        int i,x,y;
        char vs[20];
        /*if((n=load())==0)
        {
        printf("\n cannot open file\n");
        exit(0);
        }*/
        printf("Enter the word what you want to search:\n");
        scanf("%s",vs);
        for(i=0;i<n;i++)
        {
            if(strcmp(word[i],vs)==0)
            {
                printf("successful!\n the word is %s\n",word[i]);
                printf("what would you like to do with the word?\n\t 1.modify 2.delrte 3.nothing []\b\b");
                scanf("%d",&y);
                if(y==1)
                    modify(i);
                else{del(i);}
            }
        }
        if(i==n)
            printf("hoho!sorry.not found~");
        printf("now.1.one more 2.back 3exit");
        scanf("%d",&x);
        switch(x)
        {
        case 1:search();break;
        case 2:menu();break;
        case 3:exit(0);
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月10日
  • 已采纳回答 3月2日
  • 创建了问题 3月2日

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境