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

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日

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?