用链表实现学生成绩管理系统,无法显示输出&&输出有错,请指教一下,实验两天了,仍未解决
1.程序功能: 数据输入输出,修改,添加,删除,并将结果保存在文件中2.运行环境: c4droid, 苦逼手机党,,见谅
3.说明: 程序就完成了输入输出,保存,链表的建立。由于第一次独立编写,有很多不严谨的地方 ,望在测试时按数据类型与大小准确输入。电脑调试时,去掉头文件conio.h以及函数clrscr,以及更改文件路径。
3.问题: 当第一次调试时数据正确存储到文件中 但无法输出,后经调试去掉在函数data_in中的语句pfirst = NULL(这是保存头结点的变量),去掉因为在输出函数data_out中会从新分配内存,选择先输入数据再输出之后显示成功,但数据重复显示(若有两条记录数据,会多显示两条),若不输入数据直接显示,则数据条数正常,但多出两个零。。。。。
以下是显示情况
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct student
{
int stu_id = 0;
char name[15];
char sex[2];
int age = 0;
int score = 0;
student *next = NULL;
}stu_info;
int stu_num = 0; //学生信息的数量
student *pfirst = NULL; //指向链表头节点
FILE *pfile = NULL;
char fname[] = "/sdcard/stu_info.txt";
void data_in();
void data_out();
//void data_modify();
//void data_del();
void create_table(student *);
void data_save(student *);
int main(void)
{
int fun_num = 0; //功能序号选择
int judge = 0; //判断是否退出程序
/*此下为主功能界面显示,可单独设置为一
个函数*/
puts("Please enter the function sequence"
" number: \n");
puts("1:The input data");
puts("2:The output data");
puts("3:Modify the data");
puts("4:Delete the data");
puts("5:Exit");
while(judge != 6)
{
scanf("%d", &fun_num);
switch(fun_num)
{
case 1:
data_in();
break;
case 2:
data_out();
break;
/* case 3:
data_modify();
break;
case 4:
data_del();
break;*/
case 5:
judge = 6;
clrscr();
break;
default:
puts("No found!");
judge = 6;
clrscr();
break;
}
}
return 0;
}
void data_in()
{
student *inf_in = NULL; //临时存数据
//student *data_s = NULL;
puts("Enter the number of data:");
scanf("%d", &stu_num);
for(int i = 1; i <= stu_num; i++)
{
inf_in = (student *)malloc(sizeof(stu_info));
puts("Input student ID:");
scanf("%d", &inf_in->stu_id);
puts("Input name:");
scanf("%s", &inf_in->name);
puts("Input gender:");
scanf("%s", &inf_in->sex);
puts("Input age:");
scanf("%d", &inf_in->age);
puts("Input scores:");
scanf("%d", &inf_in->score);
create_table(inf_in); //实际数据至链表
}
data_save(pfirst); //存至磁盘文件
free(inf_in);
inf_in = NULL;
}
void create_table(student *stu_tran)
{
static student *temp = NULL;
if(!temp)
{
temp = stu_tran;
pfirst = stu_tran;
}
else
{
temp->next = stu_tran;
}
stu_tran->next = NULL;
temp = stu_tran;
}
void data_save(student *stu_tran)
{
pfile = fopen(fname, "w");
while (stu_tran)
{
fprintf(pfile,"%d%s%s%d%d",stu_tran->stu_id, stu_tran->name, stu_tran->sex, stu_tran->age, stu_tran->score);
stu_tran = stu_tran->next;
}
fclose(pfile);
}
void data_out()
{
student *stu_inf = NULL;
puts("数据如下:");
pfile = fopen(fname, "r");
while(feof(pfile) == 0)
{
stu_inf = (student *)malloc(sizeof(stu_info));
fscanf(pfile,"%d%s%s%d%d",&stu_inf->stu_id, &stu_inf->name, &stu_inf->sex, &stu_inf->age,&stu_inf->score);
create_table(stu_inf);
}
while (pfirst)
{
printf("%d%s%s%d%d\n", pfirst->stu_id, pfirst->name, pfirst->sex,
pfirst ->age, stu_inf->score);
pfirst = pfirst -> next;
}
fclose(pfile);
pfirst = NULL;
free(stu_inf);
stu_inf = NULL;
}