学生管理系统(数组实现)

头文件:

#ifndef _STUDENT_H_
#define _STUDENT_H_

#define N 100
struct student 
{
	char name[20];
	char sex;
	int id;
	int chinese;
	int math;
};

typedef struct student st;

void Welcome();
void Show();
int Input(st *stu);
void Display(st *stu, int len);
void Sort(st *stu, int len);

#endif


主函数:

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

int main()
{
	st stu[N] = {0};
	int num, len;

	Welcome();
	system("clear");

	while(1)
	{
		Show();

		scanf("%d", &num);

		switch(num)
		{
			case 1:
			len = Input(stu);
			break;

			case 2:
			Display(stu, len);
			break;

			case 3:
			Find(stu, len);
			break;

			case 4:
			Sort(stu, len);
			break;

			default:
			exit(1);
		}
	}

	return 0;
}


各个函数模块:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "student.h"

void Welcome()
{
	system("clear");

	printf("*********************************\n");
	printf("  WELCOME TO THE STUDENT SYSTEM! \n");
	printf("*********************************\n");
	sleep(2);
}

void Show()
{
	printf("*********************************\n");
	printf("  1. INPUT!        2.SHOW INFO!  \n");
	printf("  3. FIND!         4.SORT!       \n");
	printf("*********************************\n");

	printf("Please input your choice:\n");
}

int  Input(st *stu)
{
	int i, len;
	printf("Please input the information:\n");
	for(i = 0; i < N; i++)
	{
		scanf("%s", stu[i].name);
		if(strcmp(stu[i].name, "end") == 0)
		{
			len = i;
			break;
		}
		scanf("%s", &stu[i].sex);
		scanf("%d", &stu[i].id);
		scanf("%d", &stu[i].chinese);
		scanf("%d", &stu[i].math);
	}

	return len;
}

void Display(st *stu, int len)
{
	int i;
	for(i = 0; i < len; i++)
	{
		printf("name is %s\n", stu[i].name);
		printf("sex is %c\n", stu[i].sex);
		printf("id is %d\n", stu[i].id);
		printf("chinese is %d\n", stu[i].chinese);
		printf("math is %d\n", stu[i].math);
		printf("\n");
	}
}

void Find(st *stu, int len)
{
	int i;
	char target[100];

	printf("Please input which student you want to find:\n");
	scanf("%s", target);

	for(i = 0; i < len; i++)
	{
		if(strcmp(stu[i].name, target) == 0)
		{
			printf("name is %s\n", stu[i].name);
			printf("sex is %c\n", stu[i].sex);
			printf("id is %d\n", stu[i].id);
			printf("chinese is %d\n", stu[i].chinese);
			printf("math is %d\n", stu[i].math);
		}
	}
}

void Sort(st *stu, int len)
{
	int i, j;
	int choice;
	char tempname[100];
	char tempsex;
	int tempid, tempchinese, tempmath;

	printf("Please input your choice: 1. Chinese 2. Math\n");
	scanf("%d", &choice);

	if(1 == choice)
	{
		for(i = 0; i < len - 1; i++)
		{
			for(j = 0; j < len - i - 1; j++)
			{
				if(stu[j].chinese > stu[j + 1].chinese)
				{
					strcpy(tempname, stu[j].name);
					strcpy(stu[j].name, stu[j + 1].name);
					strcpy(stu[j + 1].name, tempname);

					tempsex = stu[j].sex;
					stu[j].sex = stu[j + 1].sex;
					stu[j + 1].sex = tempsex;

					tempid = stu[j].id;
					stu[j].id = stu[j + 1].id;
					stu[j + 1].id = tempid;

					tempchinese = stu[j].chinese;
					stu[j].chinese = stu[j + 1].chinese;
					stu[j + 1].chinese = tempchinese;

					tempmath = stu[j].math;
					stu[j].math = stu[j + 1].math;
					stu[j + 1].math = tempmath;
				}
			}
		}
	
		for(i = 0; i < len; i++)
		{
			printf("name is %s\n", stu[i].name);
			printf("sex is %c\n", stu[i].sex);
			printf("id is %d\n", stu[i].id);
			printf("chinese is %d\n", stu[i].chinese);
			printf("math is %d\n", stu[i].math);
		}
	}

	else if(2 == choice)
	{
		for(i = 0; i < len - 1; i++)
		{
			for(j = 0; j < len - i - 1; j++)
			{
				if(stu[j].math > stu[j + 1].math)
				{
					strcpy(tempname, stu[j].name);
					strcpy(stu[j].name, stu[j + 1].name);
					strcpy(stu[j + 1].name, tempname);

					tempsex = stu[j].sex;
					stu[j].sex = stu[j + 1].sex;
					stu[j + 1].sex = tempsex;

					tempid = stu[j].id;
					stu[j].id = stu[j + 1].id;
					stu[j + 1].id = tempid;

					tempchinese = stu[j].chinese;
					stu[j].chinese = stu[j + 1].chinese;
					stu[j + 1].chinese = tempchinese;

					tempmath = stu[j].math;
					stu[j].math = stu[j + 1].math;
					stu[j + 1].math = tempmath;
				}
			}
		}
	
		for(i = 0; i < len; i++)
		{
			printf("name is %s\n", stu[i].name);
			printf("sex is %c\n", stu[i].sex);
			printf("id is %d\n", stu[i].id);
			printf("chinese is %d\n", stu[i].chinese);
			printf("math is %d\n", stu[i].math);
		}
	}

	else
	{
		;
	}
}

这个系统比较low,用的数组和结构体完成的,实现了四种功能,第一个功能可以输入学生信息,第二个功能可以查看学生信息,第三个功能可以查找学生信息, 第四个功能可以排序。




  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值