第一次实验

#include <stdio.h>
#include <stdlib.h>

typedef struct student {
	int num;
	char name[10];
	char sex[10];
	int English;
	int computer;
	int math;
	int sum;
	double average;
}Student;
const int N = 100;
Student stu[N];
int n;
void InputStu(Student stu[], int n) {//输入
	for (int i = 1; i <= n; i++) {
		scanf("%d%s%s%d%d%d", &stu[i].num, &stu[i].name, stu[i].sex, &stu[i].English, &stu[i].computer, &stu[i].math);
		stu[i].sum = stu[i].English + stu[i].computer + stu[i].math;
		stu[i].average = stu[i].sum / 3;
	}
}

void OutputStu(Student stu[], int n) {//输出
	printf("序号\t学号\t姓名\t性别\t英语\t计算机\t高数\t总分\t平均分\n");
	Student* p;
	for (int i = 1; i <= n; i++) {
		p = stu + i;
		printf("%d\t%d\t%s\t%s\t%d\t%d\t%d\t%d\t%.2f\n", i, p->num, p->name, p->sex, p->English, p->computer, p->math, p->sum, p->average);
	}
}

void SearchStu(Student stu[], int n) {
	int num;
	printf("请输入待查找的学号:");
	scanf("%d", &num);
	bool flag = true;
	Student* p;
	for (int i = 1; i <= n; i++) {
		p = stu + i;
		if (p->num == num) {
			printf("查找成功\n");
			printf("%d\t%s\t%s\t%d\t%d\t%d\t%d\t%.2f\n",p->num, p->name, p->sex, p->English, p->computer, p->math, p->sum, p->average);
			flag = false;
			break;
		}
	}
	if (flag) printf("该学生的信息不存在,查找失败\n");
}

void UpdateStu(Student stu[], int n) {
	printf("请输入待修改的学号:");
	int num;
	scanf("%d", &num);
	bool flag = true;
	Student* p;
	for (int i = 1; i <= n; i++) {
		p = stu + i;
		if (p->num == num) {
			printf("%d\t%s\t%s\t%d\t%d\t%d\t%d\t%.2f\n", p->num, p->name, p->sex, p->English, p->computer, p->math, p->sum, p->average);
			printf("\t1.姓名\n\t2.性别\n\t3.英语\n\t4.计算机\n\t5.高数\n");
			int x;
			printf("请选择需要修改的信息(1-5):");
			scanf("%d", &x);
			printf("请输入修改后的信息:");
			if (x == 1) {
				scanf("%s", &stu[i].name);
			}
			else if (x == 2) {
				scanf("%s", &stu[i].sex);
			}
			else if (x == 3) {
				int s;
				scanf("%d", &s);
				stu[i].English = s;
			}
			else if (x == 4) {
				int s;
				scanf("%d", &s);
				stu[i].computer = s;
			}
			else {
				int s;
				scanf("%d", &s);
				stu[i].math = s;
			}
			if (x == 3 || x == 4 || x == 5) {
				stu[i].sum = stu[i].English + stu[i].computer + stu[i].math;
				stu[i].average = stu[i].sum / 3;
			}
			flag = false;
			printf("%d\t%s\t%s\t%d\t%d\t%d\t%d\t%.2f\n", p->num, p->name, p->sex, p->English, p->computer, p->math, p->sum, p->average);
			break;
		}
	}
	if (flag) printf("该学生的信息不存在,查找失败\n");
}

void AppendStu(Student stu[], int& n) {
	printf("请输入学生信息:");
	n++;
	scanf("%d%s%s%d%d%d", &stu[n].num, &stu[n].name, stu[n].sex, &stu[n].English, &stu[n].computer, &stu[n].math);
	stu[n].sum = stu[n].English + stu[n].computer + stu[n].math;
	stu[n].average = stu[n].sum / 3;
	printf("更新成功\n");
}

int main() {
	printf("请输入学生记录个数n=");
	scanf("%d", &n);
	printf("请输入学生信息:\n");
	InputStu(stu, n);
	while (true) {
		printf("%*s%s\n", 20, "", "学生信息管理系统");
		printf("%*s%s\n", 10, "", "显示学生信息");
		printf("%*s%s\n", 10, "", "查询学生信息");
		printf("%*s%s\n", 10, "", "修改学生信息");
		printf("%*s%s\n", 10, "", "添加学生信息");
		printf("%*s%s", 10, "", "请选择(1-4):");
		int x;
		scanf("%d", &x);
		if (x == 1) OutputStu(stu, n);
		else if (x == 2) SearchStu(stu, n);
		else if (x == 3) UpdateStu(stu, n);
		else if (x == 4)AppendStu(stu, n);
		else break;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值