c语言问题求解答
2014-04-10 12:34
295 查看
#include<stdio.h> #include<stdlib.h> #define course 4 typedef struct student { long int num; char name[20]; char sex[3]; int age; float score[course]; float aver; float total; struct student *next; }STU; float sum(STU *stu) { int j; float result=0; for(j=0;j<course;j++) { result+=stu->score[j]; } return result; } STU *input(int n) { STU *head=NULL,*p=NULL,*end=NULL; int i; for(i=0;i<n;i++) { p=(STU*)malloc(sizeof(STU)); p->next=NULL; printf("请输入第%d的学生的学号,姓名,性别,年龄,成绩1,成绩2,成绩3,成绩4\n",i+1); scanf("%ld%s%s%d%f%f%f%f",&p->num,p->name,p->sex,&p->age,&p->score[0],&p->score[1],&p->score[2],&p->score[3]); p->total=sum(p); p->aver=p->total/course; if(i==0) { head=p; } else { end->next=p; } end=p; } return head; } STU *insert(STU *head) { STU *p=NULL,*p1=NULL,*tmp=NULL; tmp=(STU*)malloc(sizeof(STU)); tmp->next=NULL; printf("Input the num, name, sex, age and 4 scores of student you want to insert:\n"); scanf("%ld%s%s%d%f%f%f%f", &tmp->num, tmp->name, tmp->sex, &tmp->age, &tmp->score[0], &tmp->score[1], &tmp->score[2], &tmp->score[3]); tmp->total=sum(tmp); tmp->aver=tmp->total/course; if(head=NULL) { return tmp; } if(tmp->num < head->num) { tmp->next=head; return tmp; } p1=p=head; while(p!=NULL&&p->num<=tmp->num) { p1=p; p=p->next; } p1->next=tmp; tmp->next=p; return head; } void main() { int n; STU *p,*q; printf("请输入你想输入的学生的个数\n"); scanf("%d",&n); p=input(n); q=insert(p); printf("学号 姓名 性别 年龄 语文 数学 英语 政治 平均分 总分\n"); while(q) { printf("%-7ld%-7s%-4s%-5d%-9.2f%-9.2f%-9.2f%-9.2f%-9.2f%-9.2f",q->num,q->name,q->sex,q->age,q->score[0],q->score[1], q->score[2],q->score[3],q->aver,q->total); q=q->next; } }帮忙看下,这个最基本的学生管理系统只有输入,插入,显示功能,请问插入功能为什么不能实现,错误在哪里呢?
相关文章推荐
- c语言谭浩强课后作业乒乓球选对手问题
- 数据结构栈用C语言表示 有几点问题 求教
- 有关C语言指针访问问题
- C语言 特殊数问题 回文数 素数 亲密数 水仙花数
- 绝对能够测试你的C语言功力的几个问题!
- C语言 5个数最值问题
- vs2015中编写C语言代码出现错误:提示scanf is not safe 问题解决办法
- 最右校招-服务器日志分析:一个十万行log统计问题的c语言代码的实现1
- PAT 1008. 数组元素循环右移问题 (20) (C语言)
- C语言学习期间遇到的所有问题及解决-在15PB
- 二叉搜索树最低访问代价问题C语言
- C语言任务分配问题
- c语言学习零碎整理(2):结构体对齐问题
- c语言夅的问题
- 常见算法:C语言求素数的问题
- C语言中的 sizeof 问题
- C语言小问题
- 12个C语言的程序问题
- 关于C语言中 字符串常量的问题
- c语言有关内存的问题