sqmw


私信TA

用户名:20206805

访问量:16188

签 名:

在否定中提升自己

等  级
排  名 331
经  验 5295
参赛次数 0
文章发表 91
年  龄 19
在职情况 学生
学  校 东大
专  业 软件工程

  自我简介:

代码是大一上学期写的,格式等都不规范,请多包涵。编写的代码,有些是为了练习库函数的使用,所以看着可能比较怪或者难以理解。

解题思路:

注意事项:

//fgets(p->s_num,sizeof(p->s_num),stdin);//全部接收32

//fgets(p->name,sizeof(p->name), stdin);//此处应该使用scanf(因为scanf遇到32(space)或13(enter)就结束,并保留32和13)



参考代码:

#include <stdio.h>
#include <malloc.h>
typedef struct student
{
char s_num[10];
char name[15];
int subject1;
int subject2;
int subject3;
struct student* next;
}stu;
stu* input(stu* head);
void print(stu* head);
int main()
{
stu* head = NULL,*p1 = NULL,*p2 = NULL;
int n = 0,i = 0;
scanf("%d", &n);
p1 = head = (stu*)malloc(sizeof(stu));
if (head == NULL)
{
puts("Fail to memery");
exit(-1);
}
while (i < n)
{
p2 = (stu*)malloc(sizeof(stu));
if (p2 == NULL)
{
puts("Fail to memery");
exit(-1);
}
else
{
p2->next = NULL;
p1->next = p2;
p1 = p2;
}
i++;
}
head = input(head);
print(head);
}
stu *input(stu*head)
{
stu* p = head;
while (p->next != NULL)
{
getchar();
scanf("%s",p->s_num);
scanf("%s",p->name);
//fgets(p->s_num,sizeof(p->s_num),stdin);//全部接收32
//fgets(p->name,sizeof(p->name), stdin);
scanf("%d", &p->subject1);
scanf("%d", &p->subject2);
scanf("%d", &p->subject3);
p = p->next;
}
return head;
}
void print(stu* head)
{
stu* p = head;
while (p->next)
{
printf("%s, %s, %d, %d, %d\n",p->s_num,p->name,p->subject1,p->subject2,p->subject3);
p = p->next;
}
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区