一 1 构造一个结构体,结构体中包含学生学号,姓名,性别,年龄信息,通过键盘输入3个结构体的信息,

2024-11-26 02:35:18
推荐回答(2个)
回答1:

/*楼主,你测试一下,应该是没问题了。楼主,您能不能多给点分呀。这个题不容易啊*/

#include
#include
#include
#include

typedef struct ln
{
char id[10];
char name[10];
char sex[2];
int age;

struct ln *next;
}student;

student *create(student *t)//输入3个结构体的信息,将其连接
{
system("cls");//清屏
student *p, *q;
printf("please enter 3 students' id, name, sex, age in proper order\n\n");
p = (student *)malloc(sizeof(student));
p->next=NULL;
t = p;
q = p;
scanf("%s %s %s %d", p->id, p->name, p->sex, &p->age);
p = (student *)malloc(sizeof(student));
p->next=NULL;
q->next = p;//将其连接
q = p;
scanf("%s %s %s %d", p->id, p->name, p->sex, &p->age);
p = (student *)malloc(sizeof(student));
p->next=NULL;
q->next = p;//将其连接
scanf("%s %s %s %d", p->id, p->name, p->sex, &p->age);

printf("\nthe type-in has finished\n\n\n");
system("pause");

return t;
}

void print(student *t)//输出链表中的信息
{
student *p=t;
printf(" id name sex age\n\n");
while(p)
{
printf("%-10s%-10s%-2s%-3d\n", p->id, p->name, p->sex, p->age);
p = p->next;
}
printf("output has finished\n\n\n");
system("pause");
}

student *deleted(student *t)//删除某个结构体
{
system("cls");//清屏
student *p, *q;
char id[10];
printf("please enter a student's id that you will delete\n\n");
scanf("%s", id);
p = t;
while(p)//寻找该结构体
{
if(strcmp(p->id, id)==0) break;
else
{
q = p;//一会儿删除的时候用
p = p->next;
}
}
if(p==NULL)//如果需要删除的学号不存在
{
printf("\nsorry, the student you will delete isn't exist\n\n\n");
system("pause");
return t;
}
else//如果需要删除的学号存在
{
if(p==t)//要删除第一个结构体
{
t = p->next;
}
else
{
q->next = p->next;
}
free(p);//释放该节点

printf("\nthe student has deleted successfully\n");

print(t);//输出删除后的链表的信息

return t;
}
}

student *add(student *t)
{
system("cls");//清屏
student *p, *q;
printf("please enter a student's information that you will add\n\n");
p = t;
while(p->next)//找到最后一个节点
{
p = p->next;
}
q = p;

p = (student *)malloc(sizeof(student));
p->next=NULL;
q->next=p;

scanf("%s %s %s %d", p->id, p->name, p->sex, &p->age);

printf("\nthe student has added successfully\n");
return t;
}

void error()//输入错误处理函数
{
system("cls");
printf("\n\nthe number you enterd is wrong, please enter again\n\n\n");
system("pause");
getchar();
}

int main()
{
int n=1, z;
student *head=NULL;

while(1)
{
system("cls");
printf("please choose ths number that you will do\n\n");
printf("******************************************\n");
printf("* 【1】 enter 3 sutdents' information *\n");
printf("* 【2】 delete a student *\n");
printf("* 【3】 add a student *\n");
printf("* 【0】 exit *\n");
printf("******************************************\n\n");

z = scanf("%d", &n);
if(z==0||n<0||n>3) error();
else if(n==1) head = create(head);
else if(n==2) head = deleted(head);
else if(n==3)
{
head = add(head);
print(head);
}
else if(n==0) break;
}
system("cls");
printf("\n\n\tthank you !!\n\n");
}

回答2:

struct stu {
int iNum;
char szName[20];
char szSex[1];
int iAge;
char adress[50];
};

void print(stu* lpStu) {
printf("学号: %d\r\n", lpStu->iNum);
printf("姓名: %s\r\n", lpStu->szName);
printf("性别: %s\r\n", lpStu->szSex);
printf("年龄: %d\r\n", lpStu->iAge);
printf("地址: %s\r\n", lpStu->adress);
}