该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#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();

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐