前言
学生信息包括:学号、姓名、三门课成绩、总成绩。
需要具备的基本功能包括:输入学生个基本项、自动计算总成绩、查询某个学生的基本信息、删除某个学生信息、增加一条学生信息。
头文件
#define _CRT_SECURE_NO_WARNINGS 1
//头文件的包含
#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<process.h>
//定义常量
#define MAXSIZE 50
//结构体的定义
/*学生信息结构体的定义*/
struct student
{
char sNo[10];//学号
char sName[10];//姓名
float fScore[3];//三科成绩
float fTotal;//总成绩
};
/*学生信息表结构体的定义*/
struct sqlist
{
struct student elem[50];
int length;
};
//函数的声明
/*初始化表格——构造一个空表*/
void Init(struct sqlist *sl);
//输出学生信息表
int OutPut(struct sqlist *sl);
//插入学生信息
int Append(struct sqlist *sl, struct student const*elem);
//删除一个学生的信息
int Delete(struct sqlist *sl, char *no);
//查询信息
int Find(struct sqlist *sl, char *no);
函数实现的源代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"fun.h"
//构造一个空表
void Init(struct sqlist *sl)
{
assert(sl);
sl->length = 0;
}
//输出表格
int OutPut(struct sqlist const *sl)
{
assert(sl);
//判断表中是否有信息
if (sl->length == 0)
{
printf("表为空!\n");
return 0;
}
//输出表中的内容——学生的学号,姓名,三门课成绩,总成绩
printf("表如下(包含%d个记录):\n", sl->length);
printf("学号\t姓名\t成绩1\t成绩2\t成绩3\t总成绩\t\n");
int i = 0;
for (i = 0; i < sl->length; i++)
{
printf("%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n", sl->elem[i].sNo, sl->elem[i].sName, sl->elem[i].fScore[0], sl->elem[i].fScore[1], sl->elem[i].fScore[2], sl->elem[i].fTotal);
}
return 1;
}
//增加一个学生信息
int Append(struct sqlist *sl, struct student const*elem)
{
assert(sl&&elem);
//判断表格是否被填满
if (sl->length == MAXSIZE)
{
printf("表格被填满,不能插入\n");
return 0;
}
sl->elem[sl->length] = *elem;//将信息插入对应的表格中
sl->elem[sl->length].fTotal = 0;//初始化总成绩为零
int i = 0;
//循环统计总成绩
for (i = 0; i < 3; i++)
{
sl->elem[sl->length].fTotal += sl->elem[sl->length].fScore[i];
}
sl->length++;
return 1;
}
//删除一个学生的信息
int Delete(struct sqlist *sl, char *no)
{
//判断表中是否有内容
if (!sl->length)
{
printf("表空,不能删除");
return 0;
}
//遍历学生信息表,找到到相应的学号
int i;
for (i = 0; i < sl->length; i++)
{
if (strcmp(sl->elem[i].sNo, no) == 0)
{
break;
}
}
if (i < sl->length)
{
for (int j = i; j < sl->length; j++)
{
sl->elem[j] = sl->elem[j + 1];
}
sl->length--;
return 1;
}
else
return 0;
}
//查询一个学生的信息
int Find(struct sqlist *sl, char *no)
{
int i;
for (i = 0; i < sl->length; i++)
{
if (strcmp(sl->elem[i].sNo, no) == 0)
{
printf("找到学生成绩的信息\n");
//找到后输出学生的信息
printf("%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n", sl->elem[i].sNo, sl->elem[i].sName, sl->elem[i].fScore[0], sl->elem[i].fScore[1], sl->elem[i].fScore[2], sl->elem[i].fTotal);
return 1;
}
}
printf("没有记录!\n");
return 0;
}
逻辑实现源代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"fun.h"
int main()
{
char ch;//操作代码
struct sqlist sl;//学生表结构体变量,存储学生的信息
struct student elem;//学生信息结构体
int ret = 0;
//提示菜单
printf("/*****学生成绩管理系统*****/\n");
printf("\n本系统基本操作如下:\n 0:退出\n 1:初始化\n 2:输出\n 3:插入\n 4:删除\n 5:按学号查询\n \n");
printf("请输入操作提示:(0~5)");
//开始操作
while (1)
{
ch = getchar();
switch (ch)
{
case '0':
return 0;
break;
case '1':
Init(&sl);
break;
case '2':
OutPut(&sl);
break;
case '3':
printf("输入插入元素:学号 姓名 成绩1 成绩2 成绩3\n");
scanf("%s%s%f%f%f", elem.sNo, elem.sName, &elem.fScore[0], &elem.fScore[1], &elem.fScore[2]);
ret = Append(&sl, &elem);
if (ret)
{
printf("插入成功\n");
}
else
{
printf("插入失败\n");
}
break;
case '4':
printf("请输入删除学号:");
char no[10];
scanf("%s", no);
ret = Delete(&sl, no);
if (ret)
{
printf("删除成功\n");
}
else
{
printf("删除失败\n");
}
break;
case '5':
printf("请输入要查询的学号\n");
scanf("%s", no);
Find(&sl, no);
break;
default:
continue;
}
printf("请输入操作提示:(0~5)");
}
return 0;
}