该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
#include
void baocunXS(struct stu *head);
struct stu *creat();
struct stu
{
int chengji;
int xuhao;
struct stu *next;//结构体指针自引用
};
void ps(struct stu *head)
{
struct stu *p;
p=head;
if(p!=NULL)
{
do
{
printf("学号=%d 成绩=%d\n",p->xuhao,p->chengji);
p=p->next;
}while(p!=NULL);
}
}
void baocunXS(struct stu *head)
{
FILE *bcxs;
struct stu *p;
char x[30]={0};
char y[30]={0};
char z[30]={0};
p = head;
if ((bcxs = fopen("bcxs.txt", "a+")) = NULL)
{
printf("文件打开或创建失败!");
}
else
{
if (p != NULL)
{
do
{
fwrite(bcxs,sizeof(struct stu),1,bcxs);
p = p->next;
} while (p != NULL);
}
}
fclose(bcxs);
}
struct stu *creat()
{
struct stu *head,*p1,*p2;
p2=p1=(struct stu *)malloc(sizeof(struct stu));
printf("请输入学生信息\n");
printf("请输入学号\n");
scanf("%d",&p1->xuhao);
printf("请输入成绩\n");
scanf("%d",&p1->chengji);
head=NULL;
int n=0;
while(0!=p1->xuhao)
{
n++;
if(n==1)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct stu *)malloc(sizeof(struct stu));
printf("请输入学生信息\n");
printf("请输入学号\n");
scanf("%d",&p1->xuhao);
printf("请输入成绩");
scanf("%d",&p1->chengji);
}
p2->next=NULL;
return head;
}
main()
{
struct stu *s;
s=creat();
ps(s);
baocunXS(s);
getch();
}