标签:eof maxsize secure 插入 turn return ios poe some
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
using namespace std;
int MAXSIZE = 100;
typedef struct{
char no[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].no;
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.no;
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].no, e.no);
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("\n学号是:%s", L->elem[i].no);
printf("\n姓名是:%s", L->elem[i].name);
printf("\n分数是:%d", L->elem[i].price);
printf("\n\n");
}
}
//以下为排序操作
//折半插入排序_根据姓名
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_Core(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_Core(L, num, mid + 1, j);
}
else {
return BinarySearch_Core(L, num, i, mid - 1);
}
return -1;
}
//菜单显示
void menu()
{
printf("*******************学生成绩管理系统*********************\n\n");
printf("主菜单:\n");
printf(" 1.建立顺序表 "); printf("2.输入学生信息\n");
printf(" 3.插入学生信息 "); printf("4.删除学生信息\n");
printf(" 5.统计现在学生人数 "); printf("6.输出所有人的信息\n");
printf(" 7.根据姓名排序 "); printf("8.根据分数排序\n");
printf(" 9.根据学生姓名查找 "); printf("10.根据学生成绩查找\n");
printf(" 0.退出该系统 ");
}
int main()
{
SqList L;
char NowName[20];
char no[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].no << " ";
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].no << " ";
cout << "姓名:" << L.elem[pos].name << " ";
cout << "分数:" << L.elem[pos].price << endl << endl;
}
else
cout << "没有查询到该学生信息" << endl;
break;
case 0:
printf("**************************谢谢使用本系统*****************************\n");
break;
}
}
system("pause");
}
标签:eof maxsize secure 插入 turn return ios poe some
原文地址:https://www.cnblogs.com/zhengyu-ahu/p/12078312.html