利用链表记录输入的学生信息(学号、姓名、性别、年龄、得分、地址)。其中,学号长 度不超过 20, 姓名长度不超过 40, 性别长度为 1, 地址长度不超过 40
输入说明
包括若干行,每一行都是一个学生的信息,如: 00630018 zhouyan m 20 10.0 28#460 输入的最后以"end"结束
输出说明
将输入的内容倒序输出。每行一条记录,按照 学号 姓名 性别 年龄 得分 地址 的格式输出
我写的代码在VS上是对的,但是在平台提交零分,是哪里不合题意吗,还是输入方式太鸡肋了
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct Student)
typedef struct Student
{
char num[20];
char name[40];
char sex;
int age;
int score;
char address[40];
struct Student* next;
}student;
int main()
{
char c = '\0';
student* p1 = NULL, * p2 = NULL, * head = NULL, * L, * p;
L = (student*)malloc(LEN); L->next = NULL;
while (c != '\n')
{
p1 = (student*)malloc(LEN);
scanf_s("%s", p1->num, 20);
scanf_s("%s", p1->name, 40);
c = getchar();
scanf_s("%c", &p1->sex,1);
scanf_s("%d", &p1->age);
scanf_s("%d", &p1->score);
scanf_s("%s", p1->address, 40);
c = getchar();
if (head == NULL)
head = p1;
else
p2->next = p1;
p2 = p1;
}
p1->next = NULL;
L->next = head;
p1 = p2 = L->next; p1 = p1->next;
L->next = NULL;
while (p1)
{
p2->next = L->next;
L->next = p2;
p2 = p1;
p1 = p1->next;
}
p = L->next;
while (p)
{
printf("%s ", p->num);
printf("%s ", p->name);
printf("%c ", p->sex);
printf("%d ", p->age);
printf("%d ", p->score);
printf("%s ", p->address);
printf("\n");
p = p->next;
}
return 0;
}
}
修改处见注释,供参考:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define LEN sizeof(struct Student)
typedef struct Student
{
char num[20];
char name[40];
char sex;
int age;
float score; //int score; //修改
char address[40];
struct Student* next;
}student;
int main()
{
char num[20],name[40],sex,address[40];//修改
int age; //修改
float score; //修改
student* p1 = NULL, * p2 = NULL, * head = NULL, * L, * p;
L = (student*)malloc(LEN); L->next = NULL;
while (1) //修改
{
scanf_s("%s", num, 20);
if (strcmp(num,"end") == 0) break;
scanf_s("%s", name, 40);
getchar();
scanf_s(" %c", &sex,1);
scanf_s("%d", &age);
scanf_s("%f", &score);
getchar();
scanf_s("%s", address, 40);
p1 = (student*)malloc(LEN);
strcpy(p1->num,num);strcpy(p1->name,name); //修改
p1->sex = sex; p1->age =age; //修改
p1->score = score; strcpy(p1->address,address);//修改
p1->next = L->next; //修改
L->next = p1; //修改
//if (head == NULL)
// head = p1;
//else
// p2->next = p1;
//p2 = p1;
}
//p1->next = NULL;
//L->next = head;
//p1 = p2 = L->next; p1 = p1->next;
//L->next = NULL;
//while (p1)
//{
// p2->next = L->next;
// L->next = p2;
// p2 = p1;
// p1 = p1->next;
//}
p = L->next;
while (p)
{
printf("%s ", p->num);
printf("%s ", p->name);
printf("%c ", p->sex);
printf("%d ", p->age);
printf("%f ", p->score); //printf("%d ", p->score); //修改
printf("%s ", p->address);
printf("\n");
p = p->next;
}
return 0;
}