急求大神分析程序

光影雨萱 2013-08-05 04:57:25
该程序是为了实现查字典功能,英汉,汉英翻译。新增单词之后不能查找到,提示段错误。弄了好几天了还是不知道哪里有错误!
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;
}







...全文
255 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-08-07
  • 打赏
  • 举报
回复
电脑时钟紊乱,或上次没编译连接通过。
halleyzhang3 2013-08-07
  • 打赏
  • 举报
回复
还是用c++,动态容器吧,或者找个现成的c语言的动态容器,在此基础上实现你要的功能,既快又稳定。
5t4rk 2013-08-07
  • 打赏
  • 举报
回复
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;
}
整理一下,看着舒服多了。
光影雨萱 2013-08-05
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
[quote=引用 2 楼 zhao4zhong1 的回复:]
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。

Windows:
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。
Linux:
进程意外退出会在当前目录下产生‘code’文件或形如‘core.数字’的文件比如‘core.1234’
使用命令
gdb 运行程序名 core或core.数字
进入gdb然后使用bt命令
可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。
如果进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令
[/quote]

这是什么意思
一根烂笔头 2013-08-05
  • 打赏
  • 举报
回复
importnew(void)函数内部do循环部分有个大bug

p2->next = p1;
p2 = p1;//把p2丢了
p1 = (lexicon)malloc(sizeof(vocab));//p2丢了以后,执行这句以后,p1也彻底丢了,虽然p2有,但是p2丢吧喽
一根烂笔头 2013-08-05
  • 打赏
  • 举报
回复
哥们,通篇浏览一下,没看到你一个free调用,malloc用的不少,这可不是好习惯那!对于动态分配内存用了记得还那!
赵4老师 2013-08-05
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。
Windows: 崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。 Linux: 进程意外退出会在当前目录下产生‘code’文件或形如‘core.数字’的文件比如‘core.1234’ 使用命令 gdb 运行程序名 core或core.数字 进入gdb然后使用bt命令 可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。 如果进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令
光影雨萱 2013-08-05
  • 打赏
  • 举报
回复
求大神解答啊
光影雨萱 2013-08-05
  • 打赏
  • 举报
回复
引用 4 楼 max_min_ 的回复:


整理下代码格式,
然后把重点的贴出来就好了吧!方便检查

或者楼上的那种调试检查也可以
那种方式没有学过,还不会
max_min_ 2013-08-05
  • 打赏
  • 举报
回复


整理下代码格式,
然后把重点的贴出来就好了吧!方便检查

或者楼上的那种调试检查也可以
光影雨萱 2013-08-05
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。
不懂
赵4老师 2013-08-05
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。
光影雨萱 2013-08-05
  • 打赏
  • 举报
回复
程序分app.c,app.h,module.c ,module.h,main.c

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧