请问各位大佬,为什么我这里无法按学号读取到数据呢?
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#include<math.h>
#define lenth sizeof(struct student)
typedef struct student{
long number;
char name;
float c;
float math;
float eng;
float sum;
float ave;
struct student *next;
}stu;//创造学生结构体
stu *createlist(){
stu *head = (stu*)malloc(lenth);//开辟头节点
head->next=NULL;
return head;
}
stu *luru(stu *head){
int n;//记录输入多少学生
int i;
printf("请输入学生人数:");
scanf("%d",&n);
stu *tnew = NULL;
//创建n个节点
for(i=0;i<n;i++){
tnew = (stu*)malloc(lenth);
printf("请输入该学生信息\n");
printf("请输入该学生学号\n");
scanf("%lld",&tnew->number);
printf("请输入该学生名字\n");
scanf("%s",tnew->name);
printf("请输入该学生C语言成绩\n");
scanf("%f",&tnew->c);
printf("请输入该学生数学成绩\n");
scanf("%f",&tnew->math);
printf("请输入该学生英语成绩\n");
scanf("%f",&tnew->eng);
tnew->next = NULL;
head->next = tnew;
head = head->next;
}
}
//按学号查找学生信息和成绩
stu *xuehaochazhao(stu *head,long num1){
stu *p1,*p2;
p1 = head;
if(head==NULL)//空链表返回
{
printf("/n链表为空!/n");
return(head);
}
else
{
while(num1!=p1->number&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->number==num1)//比较输入学号是否与链表中学生学号匹配
{
printf("查找的学生信息为:\n");
printf("学号:%lld\n",p1->number);
printf("名字:%s\n",p1->name);
printf("C语言成绩:%f\n",p1->c);
printf("数学成绩:%f\n",p1->math);
printf("英语成绩:%f\n",p1->eng);
p1->sum=p1->c+p1->math+p1->eng;
printf("总分:%5.3f\n",p1->sum);
printf("\n");
return head;
}
else
{
printf("无该学生数据\n");
return head;
}
}
}
int main()
{
int n;
stu *head = NULL;//定义一个接收地址的指针
head =createlist();//确定头节点
printf("****欢迎使用学生成绩管理系统****\n\n");
printf("------------------------------------\n\n");
printf("------------------------------------\n\n");
printf("\t\t0.退出系统\n\t\t1.录入学生信息和成绩\n");
printf("\t\t2.增加学生信息和成绩\n\t\t3.删除学生信息和成绩\n");
printf("\t\t4.查找学生信息和成绩(以学号查找)\n\t\t5.查找学生信息和成绩(以名字查找)\n\t\t6.按成绩总分排序\n");
printf("\t\t7.将数据保存到文件\n\t\t8.输出所以成绩信息\n");
printf("\t\t9.计算平均分\n\t\t10.统计各科挂科数据\n");
printf("\t\t11.修改\n\t\t12.统计各科挂科数据\n\t\t13.统计学生成绩等级\n");
system("pause") ;
system("cls");
int choose = -1;
while(choose!=0){
printf("****欢迎使用学生成绩管理系统****\n\n");
printf("------------------------------------\n\n");
printf("------------------------------------\n\n");
printf("\t\t0.退出系统\n\t\t1.录入学生信息和成绩\n");
printf("\t\t2.增加学生信息和成绩\n\t\t3.删除学生信息和成绩\n");
printf("\t\t4.查找学生信息和成绩(以学号查找)\n\t\t5.查找学生信息和成绩(以名字查找)\n\t\t6.按成绩总分排序\n");
printf("\t\t7.将数据保存到文件\n\t\t8.输出所以成绩信息\n");
printf("\t\t9.计算平均分\n\t\t10.统计各科挂科数据\n");
printf("\t\t11.修改\n\t\t12.统计各科挂科数据\n\t\t13.统计学生成绩等级\n");
system("pause") ;
printf("请参考菜单输入一个数字:");
scanf("%d",&n);
switch(n)
{
case 0:
printf("\n\n");
printf("****欢迎老师下次使用****");
printf("\n\n");
break;
case 1:
head = luru(head);
system("pause");
system("cls");
break;
case 4:
int num1;
printf("请输入您要查询学生的学号:");
scanf("%lld",&num1) ;
head = xuehaochazhao(head,num1);
system("pause");
system("cls");
break;
}
}
return 0;
}
你输入了什么?程序输出了什么?你期望输出什么?
另外,你的程序无法通过编译
main.c: In function ‘luru’:
main.c:33:27: warning: format ‘%lld’ expects argument of type ‘long long int *’, but argument 2 has type ‘long int *’ [-Wformat=]
33 | scanf("%lld",&tnew->number);
| ~~~^~~~~~~~~~~~~~
| ||
| |long int *
| long long int *
| %ld
main.c: In function ‘xuehaochazhao’:
main.c:66:38: warning: format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘long int’ [-Wformat=]
66 | printf("学号:%lld\n",p1->number);
| ~~~^ ~~~~~~~~~~
| | |
| | long int
| long long int
| %ld
main.c:49:26: warning: variable ‘p2’ set but not used [-Wunused-but-set-variable]
49 | stu *p1,*p2;
| ^~
main.c: In function ‘main’:
main.c:128:25: error: a label can only be part of a statement and a declaration is not a statement
128 | int num1;
| ^~~
main.c:130:35: warning: format ‘%lld’ expects argument of type ‘long long int *’, but argument 2 has type ‘int *’ [-Wformat=]
130 | scanf("%lld",&num1) ;
| ~~~^~~~~~
| ||
| |int *
| long long int *
| %d
main.c: In function ‘luru’:
main.c:46:1: warning: control reaches end of non-void function [-Wreturn-type]
46 | }
| ^
页:
[1]