zoukankan      html  css  js  c++  java
  • 学生成绩管理系统

    学生成绩管理系统

    注意:该系统有BUG,在进行成绩查找时,必须先进行按照成绩排序,否则如果成绩不是按一定序列,则会发生死递归
    另外,该程序不支持按照学号排序,一旦按照姓名或成绩排序后就无法返回原样了

    #include <iostream>
    #include <stdlib.h>
    #include <malloc.h>
    #include <string.h>
    using namespace std;
    int MAXSIZE = 100;
    typedef struct{
    	char stunum[8];
    	char name[20];
    	int price;
    }Student;
    typedef struct{
    	Student *elem;
    	int length;
    }SqList;
    
    //以下为基本操作
    int CreatLink(SqList *L)
    {
    	L->elem = NULL;
    	L->elem = (Student *)malloc(sizeof(Student)*MAXSIZE);
    	if (!L->elem)
    		exit(-1);
    	L->length = 0;
    
    	return 1;
    }
    int InputSomeone(SqList *L)
    {
    	cout << "请输入要选择输入几个学生信息:";
    	int num;
    	cin >> num;
    	for (int i = 0; i<num; i++)
    	{
    		cout << "学号:";
    		cin >> L->elem[i].stunum;
    		cout << "姓名:";
    		cin >> L->elem[i].name;
    		cout << "分数:";
    		cin >> L->elem[i].price;
    		L->length++;
    	}
    	return 1;
    }
    int Insert(SqList *L)
    {
    	int k, i;
    	Student e;
    	cout << "请输入要插入的位置:";
    	cin >> i;
    	cout << "请输入具体信息:" << endl;
    	cout << "学号:";
    	cin >> e.stunum;
    	cout << "姓名:";
    	cin >> e.name;
    	cout << "分数:";
    	cin >> e.price;
    	if (i<1 || i>L->length + 1)
    	{
    		printf("此处有误1");
    		return 0;
    	}
    	if (L->length == MAXSIZE)
    	{
    		printf("此处有误2");
    		return 0;
    	}
    	for (k = L->length - 1; k >= i - 1; k--)
    	{
    		L->elem[k + 1] = L->elem[k];
    	}
    	strcpy(L->elem[i - 1].stunum, e.stunum);
    	strcpy(L->elem[i - 1].name, e.name);
    	L->elem[i - 1].price = e.price;
    	++L->length;
    	return 1;
    }
    int Delete(SqList *L)
    {
    	int pos;
    	printf("请输入要删除的位置:");
    	scanf("%d", &pos);
    	if (pos<1 || pos>L->length)
    		return 0;
    	while (pos<L->length)
    	{
    		L->elem[pos - 1] = L->elem[pos];
    		++pos;
    	}
    	L->length--;
    	return 1;
    }
    int SumPoepleNumber(SqList *L)
    {
    	return L->length;
    }
    void Output(SqList *L)
    {
    	for (int i = 0; i < L->length; i++)
    	{
    		printf("
    学号是:%s", L->elem[i].stunum);
    		printf("
    姓名是:%s", L->elem[i].name);
    		printf("
    分数是:%d", L->elem[i].price);
    		printf("
    
    ");
    	}
    }
    
    //以下为排序操作
    //折半插入排序_根据学号
    void BinaryInsertSort_stunum(SqList *L, int n)
    {
    	int i, j;
    	Student stu;
    	for (i = 1; i < n; i++)
    	{
    		stu = L->elem[i];
    		for (j = i - 1; strcmp(stu.stunum, L->elem[j].stunum) < 0; j--)
    			L->elem[j + 1] = L->elem[j];
    		L->elem[j + 1] = stu;
    	}
    }
    //折半插入排序_根据姓名
    void BinaryInsertSort_Name(SqList *L, int n)   
    {
    	int i, j;
    	Student stu;
    	for (i = 1; i < n; i++)
    	{
    		stu = L->elem[i];
    		for (j = i - 1; strcmp(stu.name, L->elem[j].name) < 0; j--)
    			L->elem[j + 1] = L->elem[j];
    		L->elem[j + 1] = stu;
    	}
    }
    //折半插入排序_根据分数
    void BinaryInsertSort_Core(SqList *L, int n)  
    {
    	int i, j;
    	Student stu;
    	for (i = 1; i < n; i++)
    	{
    		stu = L->elem[i];
    		for (j = i - 1; stu.price<L->elem[j].price; j--)
    			L->elem[j + 1] = L->elem[j];
    		L->elem[j + 1] = stu;
    	}
    }
    
    //以下为查找操作
    //折半查找_根据姓名_非递归算法
    int BinarySearch_Name(SqList *L,char a[],int n) //a[]代表输入的姓名的形参
    {
    	int low = 0, high = n - 1;
    	int mid;
    	while (low <= high)
    	{
    		mid = (low + high) / 2;
    		if (strcmp(a, L->elem[mid].name) == 0)
    			return mid;
    		else if (strcmp(a, L->elem[mid].name) < 0)
    			high = mid - 1;
    		else
    			low = mid + 1;
    	}
    	return -1;
    }
    //折半查找_根据成绩_递归算法
    int BinarySearch_Core2(SqList *L, int num, int low, int high)
    {
    	//引入静态变量使得mid
    	int i = low;
    	int j = high;
    	int mid = (low + high) >> 1;
    	if (i > j) {
    		return -1;
    	}
    	if (num == L->elem[mid].price) {
    		return mid;
    	}
    	else if (num > L->elem[mid].price) {
    		return BinarySearch_Core2(L, num, mid + 1, j);
    	}
    	else {
    		return BinarySearch_Core2(L, num, i, mid - 1);
    	}
    	return -1;
    }
    int BinarySearch_Core(SqList *L, int num, int low, int high) //递归折半查找
    {
    	if (low>high)
    		return -1;
    	int mid = (low + high) / 2;
    	if (L->elem[mid].price == num)
    	{
    		return mid ;
    	}
    	else if (num>L->elem[mid].price)
    	{
    		return BinarySearch_Core(L,  num, mid + 1, high);
    	}
    	else
    	{
    		return BinarySearch_Core(L,  num, low, mid - 1);
    	}
    
    }
    
    //菜单显示
    void menu()
    {
    	printf("*******************学生成绩管理系统*********************
    ");
    	printf("主菜单:
    ");
    	printf("  1.建立顺序表                "); printf("2.输入学生信息
    ");
    	printf("  3.插入学生信息              "); printf("4.删除学生信息
    ");
    	printf("  5.统计现在学生人数          "); printf("6.输出所有人的信息
    ");
    	printf("  7.根据姓名排序              "); printf("8.根据分数排序
    ");
    	printf("  9.根据学生姓名查找          "); printf("10.根据学生成绩查找
    ");
    	printf("  11.屏幕清理                 ");
    	printf("
    
      0.退出该系统      
    ");
    	printf("*******************学生成绩管理系统*********************");
    }
    int main()
    {
    	SqList L;
    	char stunum[8];
    	menu();
    	int len, sum, pos;
    	int choice=-1;
    	cout << endl << endl;
    	while (choice != 0)
    	{
    		cout <<"请选择功能:";
    		cin >> choice;
    		switch (choice)
    		{
    		case 1:
    			CreatLink(&L);
    			break;
    		case 2:
    			InputSomeone(&L);
    			break;
    		case 3:
    			Insert(&L);
    			break;
    		case 4:
    			Delete(&L);
    			break;
    		case 5:
    			sum = SumPoepleNumber(&L);
    			cout << "现在的人数是:" << sum << endl;
    			break;
    		case 6:
    			Output(&L);
    			break;
    		case 7:
    			len = SumPoepleNumber(&L);
    			BinaryInsertSort_Name(&L, len);
    			break;
    		case 8:
    			len = SumPoepleNumber(&L);
    			BinaryInsertSort_Core(&L, len);
    			break;
    		case 9:
    			len = SumPoepleNumber(&L);
    			char a[10];
    			cin >> a;
    			pos = BinarySearch_Name(&L, a, len);
    			if (pos != -1)
    			{
    				cout << endl << "查找的结果是:" << endl << "学号:" << L.elem[pos].stunum << "	";
    				cout << "姓名:" << L.elem[pos].name << "	";
    				cout << "分数:" << L.elem[pos].price << endl;
    			}
    			else
    				cout << "没有查询到该学生信息" << endl << endl;
    			break;
    		case 10:
    			int core;
    			cin >> core;
    			len = SumPoepleNumber(&L);
    			pos = BinarySearch_Core(&L, core, 0, len);
    			if (pos != -1)
    			{
    				cout << endl << "查找的结果是:" << endl << "学号:" << L.elem[pos].stunum << "	";
    				cout << "姓名:" << L.elem[pos].name << "	";
    				cout << "分数:" << L.elem[pos].price << endl << endl;
    			}
    			else
    				cout << "没有查询到该学生信息" << endl;
    			break;
    		case 11:
    			system("cls");
    			menu();
    			cout << endl << endl;
    			break;
    		case 0:
    			printf("**************************谢谢使用本系统*****************************
    ");
    			break;
    		}
    	}
    	system("pause");
    }
    

    基本操作

    排序和查找

    姓名

    成绩

  • 相关阅读:
    Win8系统 Python安装
    一些安卓开源框架整理
    Android 媒体键监听以及模拟媒体键盘的实现 demo
    android View 自动 GONE 问题
    Android 定时器TimerTask 简单使用
    关于Android studio 相对 eclipse 优点
    Java序列化与反序列化
    android shape的使用 边框
    Android Studio 修改 包名 package name
    Android WebView Long Press长按保存图片到手机
  • 原文地址:https://www.cnblogs.com/zhengyu-ahu/p/12078312.html
Copyright © 2011-2022 走看看