新手求助关于链表的问题
本人学完链表,自己写了个简单的学生成绩管理系统,写创建学生信息链表的时候,遇到了问题,就是输出的信息中,从第二个开始就没有学号,调试了半天也不知道怎么回事,麻烦各位帮小弟看看把;运行结果如图所示,非常感谢哈
// stu_manage.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
# include<stdio.h>
# include<string.h>
# include<malloc.h>
# include<stdlib.h>
# include<conio.h>
# define N 50
struct Stu
{
char num[10];
char name[6];
float score[5];
struct Stu *next;
};
typedef struct Stu stu;
void Welcome()
{
printf(" ");
for(int i = 0;i < N;i++)
printf("*");
printf("\n");
printf(" ");
for( i = 0;i < N;i++)
printf("*");
printf("\n");
printf(" ********************** 欢迎来到学生成绩管理系统 ***********************\n");
printf(" ******** 成绩输入顺序为高等数学,线性代数,数据结构,C语言 **********\n");
printf("*输入学号为e的时候结束输入,输出学生信息并将信息保存在当前目录Data_list中*\n");
printf(" ");
for( i = 0;i < N;i++)
printf("*");
printf("\n");
printf(" ");
for( i = 0;i < N;i++)
printf("*");
printf(" ");
printf("\n");
}
stu * Create()
{
stu * head,*pend,*new_;
char nu[10],na[6],nu_[10];
int n = 0;
new_=(stu*)malloc(sizeof(stu));
printf("请输入学生的学号: ");
strcpy(new_->num,gets(nu));
printf("请输入学生的姓名: ");
strcpy(new_->name,gets(na));
printf("请分别输入4项科目成绩: ");
for(int i = 0;i < 4;i++)
{
scanf("%f",&new_->score[i]);
}
while(1)
{
n++;
if(n == 1)
{
pend = new_;
head = pend;
}
else
{
pend->next = new_;
pend = new_;
}
new_=(stu*)malloc(sizeof(stu));
printf("学号:\t");
scanf("%s",new_->num);
if(new_->num[0] == 'e')
break;
strcpy(new_->num,gets(nu));
printf("姓名:\t");
strcpy(new_->name,gets(na));
printf("成绩:\t");
for(int i = 0;i < 4;i++)
scanf("%f",&new_->score[i]);
}
pend->next = NULL;
printf("%d students data created!\n",n);
return head;
}
void OutPut(stu * head)
{
stu* p;
int n=0;
if(head == NULL)
printf("error!!\n");
else
{
p= head;
printf("学号 ");
printf("姓名 ");
printf("高等数学 ");
printf("线性代数 ");
printf("数据结构 ");
printf("C语言\n");
do
{
printf("%s ",p->num);
printf("%s ",p->name);
printf("%.2f ",p->score[0]);
printf("%.2f ",p->score[1]);
printf("%.2f ",p->score[2]);
printf("%.2f\n",p->score[3]);
p = p->next;
}while(p != NULL );
}
}
stu * SaveData(stu *head)
{
FILE *fp;
stu *p;
float *q;
if((fp=fopen("Data_List.txt","at+"))==NULL)
{
printf("Can't open file!!\n");
exit(1);
}
p = head;
q = head->score;
fprintf(fp,"姓名\t");
fprintf(fp,"学号\t");
fprintf(fp,"高等数学\t");
fprintf(fp,"线性代数\t");
fprintf(fp,"数据结构\t");
fprintf(fp,"C语言程序设计\t");
fprintf(fp,"数据库\n");
do
{
fputs(p->name,fp);
fprintf(fp,"\t");
fputs(p->num,fp);
fprintf(fp,"\t");
for(int i = 0;i < 5;i ++)
{
fprintf(fp,"%f\t",q[i]);
}
fprintf(fp,"\n");
p = p->next;
}while(p != NULL);
fclose(fp);
printf("Data has been saved!\n");
return head;
}
int main(int argc, char* argv[])
{
stu *head;
Welcome();
head = Create();
OutPut(head);
SaveData(head);
return 0;
}
typedef struct Stu
{
char num[10];
char name[6];
float score[5];
struct Stu *next;
}stu;
scanf("%s",new_->num);
....
strcpy(new_->num,gets(nu));
// stu_manage.cpp : Defines the entry point for the console application.
# include<stdio.h>
# include<string.h>
# include<malloc.h>
# include<stdlib.h>
# include<conio.h>
# define N 50
struct Stu {
char num[10];
char name[6];
float score[5];
struct Stu* next;
};
typedef struct Stu stu;
void Welcome()
{
printf(" ");
for (int i = 0; i < N; i++)
printf("*");
printf("\n");
printf(" ");
for (int i = 0; i < N; i++)
printf("*");
printf("\n");
printf(" ********************** 欢迎来到学生成绩管理系统 ***********************\n");
printf(" ******** 成绩输入顺序为高等数学,线性代数,数据结构,C语言 **********\n");
printf("*输入学号为e的时候结束输入,输出学生信息并将信息保存在当前目录Data_list中*\n");
printf(" ");
for (int i = 0; i < N; i++)
printf("*");
printf("\n");
printf(" ");
for (int i = 0; i < N; i++)
printf("*");
printf(" ");
printf("\n");
}
stu* Create()
{
stu* head, *pend, *new_;
char nu[10], na[6], nu_[10];
int n = 0;
new_ = (stu*)malloc(sizeof(stu));
printf("请输入学生的学号: ");
strcpy(new_->num, gets(nu));
printf("请输入学生的姓名: ");
strcpy(new_->name, gets(na));
printf("请分别输入4项科目成绩: ");
for (int i = 0; i < 4; i++) {
scanf("%f", &new_->score[i]);
}
while (1) {
n++;
if (n == 1) {
pend = new_;
head = pend;
} else {
pend->next = new_;
pend = new_;
}
new_ = (stu*)malloc(sizeof(stu));
printf("学号:\t");
scanf("%s", new_->num);
if (new_->num[0] == 'e')
break;
strcpy(new_->num, gets(nu));
printf("姓名:\t");
strcpy(new_->name, gets(na));
printf("成绩:\t");
for (int i = 0; i < 4; i++)
scanf("%f", &new_->score[i]);
}
pend->next = NULL;
printf("%d students data created!\n", n);
return head;
}
void OutPut(stu* head)
{
stu* p;
int n = 0;
if (head == NULL)
printf("error!!\n");
else {
p = head;
printf("学号 ");
printf("姓名 ");
printf("高等数学 ");
printf("线性代数 ");
printf("数据结构 ");
printf("C语言\n");
do {
printf("%s ", p->num);
printf("%s ", p->name);
printf("%.2f ", p->score[0]);
printf("%.2f ", p->score[1]);
printf("%.2f ", p->score[2]);
printf("%.2f\n", p->score[3]);
p = p->next;
} while (p != NULL);
}
}
stu* SaveData(stu* head)
{
FILE* fp;
stu* p;
float* q;
if ((fp = fopen("Data_List.txt", "at+")) == NULL) {
printf("Can't open file!!\n");
exit(1);
}
p = head;
q = head->score;
fprintf(fp, "姓名\t");
fprintf(fp, "学号\t");
fprintf(fp, "高等数学\t");
fprintf(fp, "线性代数\t");
fprintf(fp, "数据结构\t");
fprintf(fp, "C语言程序设计\t");
fprintf(fp, "数据库\n");
do {
fputs(p->name, fp);
fprintf(fp, "\t");
fputs(p->num, fp);
fprintf(fp, "\t");
for (int i = 0; i < 5; i ++) {
fprintf(fp, "%f\t", q[i]);
}
fprintf(fp, "\n");
p = p->next;
} while (p != NULL);
fclose(fp);
printf("Data has been saved!\n");
return head;
}
int main(int argc, char* argv[])
{
stu* head;
Welcome();
head = Create();
OutPut(head);
SaveData(head);
return 0;
}
[解决办法]
scanf("%s",new_->num);
与 strcpy(new_->num,gets(nu));</SPAN>
对new_->num进行了两次赋值
愚见哈
[解决办法]
# include<stdio.h>
# include<string.h>
# include<malloc.h>
# include<stdlib.h>
# include<conio.h>
# define STARNUM 50
typedef struct Stu
{
char no[10];
char name[6];
float score[5];
struct Stu* next;
}stu;
void Welcome()
{
int i;
printf(" ");
for(i = 0;i < STARNUM;i++)
printf("*");
printf("\n");
printf(" ");
for( i = 0;i < STARNUM;i++)
printf("*");
printf("\n");
printf(" ********************** 欢迎来到学生成绩管理系统 ***********************\n");
printf(" ******** 成绩输入顺序为高等数学,线性代数,数据结构,C语言 **********\n");
printf("*输入学号为e的时候结束输入,输出学生信息并将信息保存在当前目录Data_list中*\n");
printf(" ");
for( i = 0;i < STARNUM;i++)
printf("*");
printf("\n");
printf(" ");
for( i = 0;i < STARNUM;i++)
printf("*");
printf("\n");
}
stu * Create()
{
//定义头指针、新节点
stu* pHead;
stu* pNewStu;
stu* pStu;
int stuNum= 0;//统计数据总数
if (NULL==(pHead=(stu*)malloc(sizeof(stu))))//判断是否成功的分配了内存,如果没有则报错
{
printf("error");
exit(1);
}
pStu=pHead;
while(1)
{
if (NULL==(pNewStu=(stu*)malloc(sizeof(stu))))//分配新的节点
{
printf("error");
exit(1);
}
printf("学号:\t");
scanf("%s",pNewStu->no);
if('e'== pNewStu->no[0] )
{
free(pNewStu);//记得释放掉没有使用的内存,以免造成内存泄露
break;
}
printf("姓名:\t");
scanf("%s",pNewStu->name);
printf("成绩:\t");
for(int i = 0;i < 4;i++)
scanf("%f",&pNewStu->score[i]);
pStu->next=pNewStu;
pNewStu->next=NULL;
pStu=pNewStu;
++stuNum;
}
printf("%d students data created!\n",stuNum);
return pHead;
}
void OutputInfo(stu * pHead)
{
stu* pStu;
int n=0;
if(NULL==pHead)
printf("error!!\n");
else
{
pStu= pHead->next;
printf("学号 ");
printf("姓名 ");
printf("高等数学 ");
printf("线性代数 ");
printf("数据结构 ");
printf("C语言\n");
while(pStu!= NULL )
{
printf("%s ",pStu->no);
printf("%s ",pStu->name);
for(int i=0;i<4;i++)//此处在for循环里面定义变量i,是因为i只需要在这个for循环里使用
printf("%.2f ",pStu->score[i]);
printf("\n");
pStu=pStu->next;
}
}
}
stu * SaveData(stu *pHead)
{
FILE *fpData;
stu *pStu;
if(NULL==(fpData=fopen("Data_List.txt","at+")))
{
printf("Can't open file!!\n");
exit(1);
}
pStu= pHead->next;
fprintf(fpData,"姓名\t");
fprintf(fpData,"学号\t");
fprintf(fpData,"高等数学\t");
fprintf(fpData,"线性代数\t");
fprintf(fpData,"数据结构\t");
fprintf(fpData,"C语言程序设计\n");
while(NULL!=pStu)
{
fputs(pStu->name,fpData);
fprintf(fpData,"\t");
fputs(pStu->no,fpData);
fprintf(fpData,"\t");
for(int i = 0;i < 4;i ++)
{
fprintf(fpData,"%f\t",pStu->score[i]);
}
fprintf(fpData,"\n");
pStu= pStu->next;
}
fclose(fpData);
printf("Data has been saved!\n");
return pHead;
}
int main(int argc, char* argv[])
{
stu *head;
Welcome();
head = Create();
OutputInfo(head);
SaveData(head);
return 0;
}