求大神指教。帮忙运行一下看看错误在哪,怎么改正

Rookiekk 2015-03-18 12:57:15
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define ESC 0x1b
struct node
{
char name[20];
int num;
char sex;
int age;
float score[10];
struct node *next;
};
struct node *head,*tail;
void init_ht()
{
head=NULL;
tail=NULL;
}
struct node *p;
void put(p)
struct node *p;
{
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
}
void input_data()
{
int i;
char ch;
struct node *p;
while(1)
{
printf("input data(Y/N):");
ch=getchar();
if(ch=='y'||ch=='Y')
{
p=(struct node *)(malloc(sizeof(struct node)));
printf("input name:");
scanf("s%",&p->name);
printf("input sum:");
scanf("%d",&p->num);
printf("input sex:");
scanf("%c",&p->sex);
printf("input age:");
scanf("%d",&p->age);
printf("input 10 score:");
for(i=0;i<10;i++)
scanf("%f",&p->score[i]);
put(p);

}
else
break;
}
}

void FDname()
{
int i;
char name[20];
struct node *p;
printf("input name:");
scanf("%s",name);
p=head;
while(p!=NULL)
{
if(strcmp(name,p->name)==0)
{
printf("Name=%s\n",p->name);
printf("Num=%d\n",p->num);
printf("Sex=%c\n",p->sex);
printf("Age=%d\n",p->age);
for(i=0;i<10;i++)
printf("Score=%d",p->score[i]);
}
p=p->next;

}
}
void FDnum()
{
int i,snum;
struct node *p;
printf("input num:");
scanf("%d",&snum);
p=head;
while(p!=NULL)
{
if(snum==p->num)
{
printf("Name=%s\n",p->name);
printf("Num=%d\n",p->num);
printf("Sex=%c\n",p->sex);
printf("Age=%d\n",p->age);
for(i=0;i<10;i++)
printf("Score=%d",p->score[i]);
}
p=p->next;
}
}
void find_data()
{
char ch;
while(1)
{
printf("1:Find by name\n");
printf("2:Find by num\n");
printf("ESC:Exit\n");
ch=getchar();
switch(ch)
{
case'1':
FDname();
break;
case'2':
FDnum();
break;
case'ESC':
return;
}
}
}
main()
{
init_ht();
input_data();
find_data();
}
...全文
126 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
highnewrain 2015-03-20
  • 打赏
  • 举报
回复
引用 4 楼 qq_18888869 的回复:
谢谢 ,你的程序确实正确!能告诉一下我 我的那个程序错误在哪吗。。。十分感谢
嘿嘿,根据你贴的代码,首先有一些编译错误,比方说main函数声明错误、变量重复定义等等 但这些都是很简单的,编译期间的错误你根据编译器的提示来解决的话很方便的 至于如何解决运行期间的错误无非就是各种调式啦
Rookiekk 2015-03-18
  • 打赏
  • 举报
回复
谢谢 ,你的程序确实正确!能告诉一下我 我的那个程序错误在哪吗。。。十分感谢
赵4老师 2015-03-18
  • 打赏
  • 举报
回复
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
highnewrain 2015-03-18
  • 打赏
  • 举报
回复
简单测试结果
highnewrain 2015-03-18
  • 打赏
  • 举报
回复

//仅供参考
//简单的按照楼主的思路修改了下,希望有所帮助(楼主想用的是单向链表吧)
#include<stdio.h>
#include<string.h>
#include<malloc.h>

struct node
{
char name[20];
int num;
char sex[10];//考虑到中文字符男女性,所以改为数组
int age;
float score[10];
struct node *pNext;
};

struct node *g_pHead,*g_pTail;//全局指针类型变量,加了个g_p前缀,个人习惯,嘿嘿

void init_ht()
{
g_pHead=NULL;
g_pTail=NULL;
}

void destory_ht()//添加与init_ht()对应的销毁函数,释放资源,避免内存泄露
{
struct node* pTemp1=g_pHead;
while(pTemp1)
{
struct node* pTemp2=pTemp1->pNext;
free(pTemp1);
pTemp1=pTemp2;
}
g_pHead=NULL;
g_pTail=NULL;
}

void put(struct node *p)
{
if(p!=NULL)
{
if(g_pHead==NULL)
{
g_pHead=p;
}
if(g_pTail!=NULL)
{
g_pTail->pNext=p;//将上个尾节点的next指针指向当前节点
}
g_pTail=p;//更新尾节点
}
}

void print_node(struct node* p)//打印节点的函数
{
printf("\n%s\n","=========begin==========");//加个分割线
printf("name=%s\n",p->name);
printf("num=%d\n",p->num);
printf("sex=%s\n",p->sex);
printf("age=%d\n",p->age);
printf("scores=");
for(int i=0;i<10;i++)
{
printf("%f\t",p->score[i]);
}
printf("\n%s\n","=========end==========");//加个分割线
}

void input_data()
{
int i;
char ch;
struct node *p=NULL;
while(1)
{
fflush(stdin);
printf("input data(Y/N):");
scanf("%c",&ch);//这里将getch()改为scanf();不然会出问题
if(ch=='y'||ch=='Y')
{
p=(struct node *)(malloc(sizeof(struct node)));
memset(p,0,sizeof(struct node));//初始化p中属性,避免未知值
printf("input name:");
scanf("%s",&p->name);
printf("input num:");
scanf("%d",&p->num);
printf("input sex:");
scanf("%s",&p->sex);
printf("input age:");
scanf("%d",&p->age);
printf("input 10 score:");
for(i=0;i<10;i++)
{
scanf("%f",&p->score[i]);
}
put(p);
}
else
break;
}
}

void FDname()
{
char name[20];
struct node *p;
printf("input name:");
scanf("%s",name);
p=g_pHead;
int iCount=0;//统计计数
while(p!=NULL)
{
if(strcmp(name,p->name)==0)
{
++iCount;
print_node(p);
}
p=p->pNext;
}
if(iCount==0)
{
printf("没有该记录!\n");
}
}

void FDnum()
{
int i,num;
struct node *p;
printf("input num:");
scanf("%d",&num);
p=g_pHead;
int iCount=0;//统计计数
while(p!=NULL)
{
if(num==p->num)
{
++iCount;
print_node(p);
}
p=p->pNext;
}
if(iCount==0)
{
printf("没有该记录!\n");
}
}

void find_data()
{
char ch;
while(1)
{
fflush(stdin);//刷新输入缓冲,避免后续输入错误
printf("1:Find by name\n");
printf("2:Find by num\n");
printf("3:Exit\n");
scanf("%c",&ch);
switch(ch)
{
case '1':
FDname();
break;
case '2':
FDnum();
break;
case '3':
return;
}
}
}

void main()
{
init_ht();
input_data();
find_data();
destory_ht();
}

69,382

社区成员

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

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