69,368
社区成员
发帖
与我相关
我的任务
分享
app.c
#include<stdio.h>
#include<stdlib.h>
#include"module.h"
#include"app.h"
void function()
{
char input[100];
vocab * wp1;
vocab * wp2;
display();
printf("If you want to translate words,please input 1\n");
printf("If you want to add the new words,please input 2\n");
printf("If you want to exit,please input 0\n");
while(1)
{
printf("\nplease input your chioce:");
int choice;
scanf("%d",&choice);
switch(choice)
{
case 0 : exit(1);
case 1 : printf("please input a word :");
scanf("%s",input);
wp1 = import();
while(!compare(wp1->word,input))
{
if(wp1->next != NULL)
wp1 = wp1->next;
else break;
}
if(wp1->next != NULL)
{
outPut(wp1);
}
else
{
wp2 = importnew();
while(!compare(wp2->word,input))
{
if(wp2->next != NULL)
wp2 = wp2->next;
else break;
}
if(wp2->next != NULL)
outPut(wp2);
else
printf("\nCan't find you word");
}
break;
case 2 : addnew();
break;
}
printf("\n");
}
}
app.h
#ifndef __APP_H__
#define __APP_H__
extern void function();
#endif
module.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"module.h"
lexicon import(void)
{
int i = 0;
char c1,c2;
FILE *fp = NULL;
fp = fopen("dict.txt","r");
if(fp == NULL)
{
fprintf(stderr,"Failed to open dict\n");
exit(1);
}
lexicon head,p1,p2;
head = NULL;
p1 = (lexicon)malloc(sizeof(vocab));
p2 = (lexicon)malloc(sizeof(vocab));
for(i = 0;;i++)
{
c1 = fgetc(fp);
c2 = fgetc(fp);
if(c1 != '\n')
p1->word[i] = c2;
else break;
}
for(i = 1;;i++)
{
c1 = fgetc(fp);
if(c1!='\n')
{
p1->trans[0] = 'T';
p1->trans[i] = c1;
}
else break;
}
head = p1;
do{
p2->next = p1;
p2 = p1;
p1 = (lexicon)malloc(sizeof(vocab));
for(i = 0; ;i++)
{
c1 = fgetc(fp);
if(c1 != '\n')
p1->word[i] = c1;
else
break;
}
for(i = 0; ;i++)
{
c1 = fgetc(fp);
if(c1 != '\n')
p1->trans[i] = c1;
else
break;
}
}while((c1 = fgetc(fp)) != EOF);
p2->next = NULL;
fclose(fp);
return(head);
}
int compare(char *str1,char *str2)
{
if(strcmp(str2,str1) == 0)
{
return 1;
}
else
{
return 0;
}
}
int outPut(lexicon wp)
{
int i = 0;
for(i = 0;i <= 50;i++)
{
printf("%c",wp->trans[i]);
}
return 0;
}
void addnew()
{
char c;
char s1[100],s2[100];
FILE *fp = NULL;
fp = fopen("newfile.txt","a+");
if(fp == NULL)
{
fprintf(stderr,"failed to open newfile\n");
exit(1);
}
scanf("%s",s1);
fprintf(fp,"%s",s1);
fprintf(fp,"\n");
fflush(fp);
while( (c = getchar() ) =='\n' )
{
}
fgets(s2,99,stdin);
fprintf(fp,"T");
fprintf(fp,"%s",s2);
fclose(fp);
}
lexicon importnew(void)
{
int i = 0;
char c1,c2;
FILE *fp = NULL;
fp = fopen("newfile.txt","r");
if(fp == NULL)
{
fprintf(stderr,"Failed to open newfile\n");
exit(1);
}
lexicon head,p1,p2;
head = NULL;
p1 = (lexicon)malloc(sizeof(vocab));
p2 = (lexicon)malloc(sizeof(vocab));
for(i = 0;;i++)
{
c1 = fgetc(fp);
c2 = fgetc(fp);
if(c1 != '\n')
p1->word[i] = c2;
else break;
}
for(i = 1;;i++)
{
c1 = fgetc(fp);
if(c1!='\n')
{
p1->trans[0] = 'T';
p1->trans[i] = c1;
}
else break;
}
head = p1;
do{
p2->next = p1;
p2 = p1;
p1 = (lexicon)malloc(sizeof(vocab));
for(i = 0; ;i++)
{
c1 = fgetc(fp);
if(c1 != '\n')
p1->word[i] = c1;
else break;
}
for(i = 0; ;i++)
{
c1 = fgetc(fp);
if(c1 != '\n')
p1->trans[i] = c1;
else break;
}
}while((c1 = fgetc(fp)) != EOF);
p2->next = NULL;
fclose(fp);
return(head);
}
void display()
{
printf("\n\t************************************************");
printf("\n\t* *");
printf("\n\t* W E *");
printf("\n\t* *");
printf("\n\t* Electronic Dictionary *");
printf("\n\t* *");
printf("\n\t* *");
printf("\n\t* *");
printf("\n\t* 1----Translation *");
printf("\n\t* 2----Add new words *");
printf("\n\t* 0----Exit *");
printf("\n\t* *");
printf("\n\t************************************************");
printf("\n\n");
}
module.h
#ifndef __MODULE_H__
#define __MODULE_H__
typedef struct vocab
{
char word[100];
char trans[300];
struct vocab *next;
}vocab,*lexicon;
extern lexicon import(void);
extern char *getOut(lexicon wp,char wd[]);
extern int compare(char *, char * );
extern int outPut(lexicon wp);
extern void display();
extern void addnew();
extern lexicon importnew(void);
#endif
main.c
#include<stdio.h>
#include"module.h"
#include"app.h"
int main(int argc,char* argv[])
{
function();
return 0;
}
整理一下,看着舒服多了。
p2->next = p1;
p2 = p1;//把p2丢了
p1 = (lexicon)malloc(sizeof(vocab));//p2丢了以后,执行这句以后,p1也彻底丢了,虽然p2有,但是p2丢吧喽
整理下代码格式,
然后把重点的贴出来就好了吧!方便检查
或者楼上的那种调试检查也可以