📄 例1-5 学生数据.c
字号:
/* Note:Your choice is C IDE */
#include <stdio.h>
#include <stdlib.h>
struct stud
{
char name[20];
long num;
int age;
char sex;
float score;
struct stud * next;
};
void new_record();
void listall();
struct stud *head, *cur, *newp;
void main()
{
char ch;
int flag=1;
head=NULL;
while(flag)
{
printf("type 'E' or 'e' to enter new record,");
printf("\n type 'L' or 'l' to list all records:");
ch=getchar();
getchar();
switch(ch)
{
case 'e':
case 'E':new_record();
break;
case 'l':
case 'L':listall();
break;
default:flag=0;
};
}
}
void new_record(void)
{
char numstr[20];
newp=(struct stud * )malloc(sizeof(struct stud)); /* 开辟新结点 */
if(head==NULL) /* 原来为空表 */
head=newp;
else
{
cur=head; /* 原来已有结点 */
while(cur->next!=NULL)
cur=cur->next;
cur->next=newp;
}
cur=newp; /* 使this指向新结点 */
printf("enter name:"); /* 以下输入新结点数据 */
gets(cur->name);
printf("enter number:");
gets(numstr);
cur->num=atol(numstr);
printf("enter age:");
gets(numstr);
cur->age=atoi(numstr);
printf("enter sex:");
cur->sex=getchar();
getchar();
printf("enter score:");
gets(numstr);
cur->score=atof(numstr);
cur->next=NULL; /* 新结点不再指向其它结点 */
}
void listall(void)
{
int i=0;
if(head==NULL)
{
printf("\n empty list.\n");
return;
}
cur=head; /* 使this指向第一个结点 */
do
{
printf("\n record number %d\n",++i);
printf("name: %s\n",cur->name);
printf("num: %ld\n",cur->num);
printf("age: %d\n",cur->age);
printf("sex: %c\n",cur->sex);
printf("score: %6.2f\n",cur->score);
cur=cur->next;
}while (cur!=NULL); /* 打印完最后一个结点不再打印 */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -