📄 namelist.c
字号:
#define NUM 0
#define OVERFLOW -2
#define INIT_LIST_SIZE 100
#define LISTINCREMENT 10
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char name[32]; // 必须为数组,否则无法存放内容
char num[10]; // 必须为数组,否则无法存放内容
int score1;
int score2;
int score3;
}elemtype;
typedef struct{
elemtype *elem;
int listsize;
int length;
}sqlist;
sqlist l; /*全局定义*/
void initsqlist(void) /*建立一个空顺序表*/
{
l.elem=(elemtype *)malloc(sizeof(elemtype)*INIT_LIST_SIZE);
if(!l.elem)
exit(OVERFLOW);
l.listsize=INIT_LIST_SIZE;
l.length=NUM;
}
void input(void) /*输入数据*/
{
elemtype *newbase;
printf("请输入 [ 姓名 ] [ 学号 ] [ 成绩1 ] [ 成绩2 ] [ 成绩3 ]\n");
do
{
scanf("%s %s %d %d %d",l.elem[l.length].name,l.elem[l.length].num,
&(l.elem[l.length].score1), &(l.elem[l.length].score2),
&(l.elem[l.length].score3));
l.length++;
if(l.length>=l.listsize) // 必须为大于等于
{// 下面的几条语句必须放在{}内才行,否则就乱套了
newbase=(elemtype *)realloc(l.elem,sizeof(elemtype)*(l.listsize+LISTINCREMENT));
if(!newbase)
exit(OVERFLOW);
l.listsize+=LISTINCREMENT;
l.elem=newbase;
}
}while(strcmp(l.elem[l.length-1].name, "exit") != 0); /*这句无法起作用,请问该如何解决?*/
// 字符串的比较不能直接进行,另外,这儿由于l.length已经递增,所以必须减1
}
void output(void) /*输出表*/
{
int i;
for(i=0;i<l.length;i++) // 下标从0开始
{
printf("%s\n,%s\n,%d\n,%d\n,%d\n",l.elem[i].name,l.elem[i].num,l.elem[i].score1,l.elem[i].score2,l.elem[i].score3);
}
}
main()
{
clrscr();
initsqlist();
input();
output();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -