为啥P=Locate(head,findmess)P调用这个函数 P总为0

qaz0825 2010-12-03 09:54:59
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
char num[10];
char name[20];
char sex[4];

int cgrade;
int egrade;
int mgrade;
};
typedef struct node
{
struct student data;
struct node *next;
}Node;
Node *head,*thisn,*newn;

void printc()
{
printf("学号\t 姓名 性别 英语成绩 数学成绩 C语言成绩\n");
}
void printe(Node *p)
{
printf("%-12s%s\t%s\t%d\t%d\t%d\n",p->data.num,p->data.name,p->data.sex,p->data.egrade,p->data.mgrade,p->data.cgrade);
}
Node *Locate(Node *head,char findmess[20])
{
Node *r;
r=head;
while(r!=NULL)
{
if (strcmp(r->data.num,findmess)==0)
return r;
r=r->next;
}
return 0;
}
void NewNODE(void)
{
extern Node *newn,*head,*thisn;

newn=(Node*)malloc (sizeof(Node));
if(head==NULL)
head=newn;
else
{
thisn=head;
while(thisn->next!=NULL)
thisn=thisn->next;
thisn->next=newn;
}
thisn=newn;

printf("\nenter num:");
scanf("%s", &thisn->data.num);


printf("\nenter name:");
scanf("%s", &thisn->data.name);
printf("\nenter sex:");
scanf("%s", &thisn->data.sex);
printf("\nenter egrade:");
scanf("%d", &thisn->data.egrade);
printf("\nenter mgrade:");
scanf("%d", &thisn->data.mgrade);
printf("\nenter cgrade:");
scanf("%d", &thisn->data.cgrade);
thisn->next=NULL;
printf("====大学信息管理系统====\n");
printf("1:添加学生信息 2:查找学生信息\n");
printf(" 0:退出系统 \n");
printf("请你在上述功能中选择(0-2):");
return ;
}
void Qur()
{
char findmess[20];
Node *p;
p=(Node*)malloc (sizeof(Node));
printf("请你输入要查找的学号:");
scanf("%c",&findmess);
p=Locate(head,findmess);
if(p)
{
printf("\t\t\t\t查找结果\n");
printc();
printe(p);
}
else
printf("\n=====>提示:没有找到该学生!\n");
}
void export()
{
if(head==NULL)
{
printf("\nemty list.\n");
return;
}
thisn=head;
do
{
printf("num:%s\n",thisn->data.num);
printf("name:%s\n",thisn->data.name);
printf("sex:%s\n",thisn->data.sex);
printf("egrade:%d\n",thisn->data.egrade);
printf("mgrade:%d\n",thisn->data.mgrade);
printf("cgrade:%d\n",thisn->data.cgrade);

thisn=thisn->next;
}while(thisn!=NULL);
}
void menu()
{
char choice;
printf("====大学信息管理系统====\n");
printf("1:添加学生信息 2:查找学生信息\n");
printf(" 0:退出系统 \n");
printf("请你在上述功能中选择(0-2):");
do
{
choice=getchar();
switch(choice)
{
case'1':NewNODE();break;
case'2':Qur();break;
case'3':export();break;
case'0':exit(0);
}
}while(1);
return;
}

int main()
{
menu();
}
...全文
61 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
fjbuilding 2010-12-03
  • 打赏
  • 举报
回复
1.具体来说因为你这里用了%c,所以程序好像是跳过了

scanf("%s",&findmess);//

而继续运行,

2.从而findmess始终为乱码,

3.从而在函数:
Node *Locate(Node *head,char findmess[20])
{
Node *r;
r=head;
while(r!=NULL)
{
if (strcmp(r->data.num,findmess)==0)
return r;
r=r->next;
}
return 0;
}
中,strcmp(r->data.num,findmess)的匹配始终不能成功,所以只能返回0

4.从而p=Locate(head,findmess);就只能使得p等于0
fjbuilding 2010-12-03
  • 打赏
  • 举报
回复
我怀疑这样搞会不会被管理员认为我是在刷分...:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
char num[10];
char name[20];
char sex[4];

int cgrade;
int egrade;
int mgrade;
};
typedef struct node
{
struct student data;
struct node *next;
}Node;
Node *head,*thisn,*newn;

void printc()
{
printf("学号\t 姓名 性别 英语成绩 数学成绩 C语言成绩\n");
}
void printe(Node *p)
{
printf("%-12s%s\t%s\t%d\t%d\t%d\n",p->data.num,p->data.name,p->data.sex,p->data.egrade,p->data.mgrade,p->data.cgrade);
}
Node *Locate(Node *head,char findmess[20])
{
Node *r;
r=head;
while(r!=NULL)
{
if (strcmp(r->data.num,findmess)==0)
return r;
r=r->next;
}
return 0;
}
void NewNODE(void)
{
extern Node *newn,*head,*thisn;

newn=(Node*)malloc (sizeof(Node));
if(head==NULL)
head=newn;
else
{
thisn=head;
while(thisn->next!=NULL)
thisn=thisn->next;
thisn->next=newn;
}
thisn=newn;

printf("\nenter num:");
scanf("%s", &thisn->data.num);


printf("\nenter name:");
scanf("%s", &thisn->data.name);
printf("\nenter sex:");
scanf("%s", &thisn->data.sex);
printf("\nenter egrade:");
scanf("%d", &thisn->data.egrade);
printf("\nenter mgrade:");
scanf("%d", &thisn->data.mgrade);
printf("\nenter cgrade:");
scanf("%d", &thisn->data.cgrade);
thisn->next=NULL;
printf("====大学信息管理系统====\n");
printf("1:添加学生信息 2:查找学生信息\n");
printf(" 0:退出系统 \n");
printf("请你在上述功能中选择(0-2):");
return ;
}
void Qur()
{
char findmess[20];
Node *p;
p=(Node*)malloc (sizeof(Node));
printf("请你输入要查找的学号:");
scanf("%s",&findmess);//这里应该用%s因为是字符串
p=Locate(head,findmess);
if(p)
{
printf("\t\t\t\t查找结果\n");
printc();
printe(p);
}
else
printf("\n=====>提示:没有找到该学生!\n");
}
void export()
{
if(head==NULL)
{
printf("\nemty list.\n");
return;
}
thisn=head;
do
{
printf("num:%s\n",thisn->data.num);
printf("name:%s\n",thisn->data.name);
printf("sex:%s\n",thisn->data.sex);
printf("egrade:%d\n",thisn->data.egrade);
printf("mgrade:%d\n",thisn->data.mgrade);
printf("cgrade:%d\n",thisn->data.cgrade);

thisn=thisn->next;
}while(thisn!=NULL);
}
void menu()
{
char choice;
printf("====大学信息管理系统====\n");
printf("1:添加学生信息 2:查找学生信息\n");
printf(" 0:退出系统 \n");
printf("请你在上述功能中选择(0-2):");
do
{
choice=getchar();
switch(choice)
{
case'1':NewNODE();break;
case'2':Qur();break;
case'3':export();break;
case'0':exit(0);
}
}while(1);
return;
}

int main()
{
menu();
}

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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