声明:
该课程设计代码c++和C使用杂糅,本人运用不当勿喷,欢迎指正,谢谢⭐
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
using namespace std;
typedef struct student{
string fn,ln; //姓名
string Id; //学号
int point,rank; //成绩,排名
string message; //重修信息
string grade; //gpa等级
} stu; //定义结构体
bool judge_ID(string i); //判断ID是否符合规定
void Sort_byscore(); //声明数据处理排序函数
void Sort_add(); //声明在add部分排序的函数
void add(); //声明增加数据函数
void manner();菜单界面
void Delete(string i); ///删除函数
void search(string i); ///查找函数
bool judge_retake(string i);///判断是否为重修的函数
void analysis(); ///数据分析函数
string gpacalc(stu x);
void encrypt(int key);
void decrypt(int key,string x);
stu mesgroup[101]; //定义结构体数组
int score,stu_cnt,choice,k,us; //定义用户输入变量
///stu_cnt为数据库人数 choice为用户选择数字 k为加密解密用到的key
string decry=""; //定义加密所用空字符串
string id ,fin,lan; //定义用户输入变量
int main() ///主函数
{
cout<<"Welcome,please input necessary information."<<endl; ///用户界面引语
cout<<"Okay, data upload finished. What do you what to do next? You can enter a number to tell me"<<endl;
do{ do while循环语句实现菜单的反复弹出以及用户选择界面
if(choice!=0)
{
switch(choice) ///switch函数匹配用户选择,进入对应函数接口
{
case 1:add();break;
case 2:
cout<<"pls input ID:";cin>>id;
Delete(id); break;
case 3:
cout<<"pls input ID:";cin>>id;
search(id);break;
case 4: Sort_byscore(); break;
case 5: cout<<"Pls input key for encryption:"<<endl;
cin>>k;
encrypt(k);break;
case 6: cout<<"Pls input key for encryption:"<<endl;
cin>>k;
cout<<"\nPls input encrypted str to decrypt:"<<endl;
cin>>decry;
decrypt(k,decry);break;
case 7: analysis();break;
case 0: cout<<"Thank you for your use.";break;
default:break;
}
}
cout<<"\n";
if(us>0){cout<<"Do you need to continue? Pls input a number\n";}
manner(); //打印菜单
us++;
}
while(scanf("%d",&choice)!=EOF&&choice!=0); ///当用户选择0时跳出界面
cout<<"Thank you for your use."; //多次使用菜单弹出该对话
return 0;
}
//以下为各个部分功能函数的定义
bool cmp(stu x,stu y) ///排序函数排序规则参数
{
return x.point>=y.point;
}
void Sort_byscore() 按照分数排序
{
Sort_add(); ///将信息先排序
cout<<"Rank\t Name Score\tID\t GPA\tRetake\n"<<endl;
for(int i=1;i<=stu_cnt;i++) //遍历数组输出排序后的信息
{
cout<<mesgroup[i].rank<<"\t"<<mesgroup[i].ln<<" "<<mesgroup[i].fn<<"\t "<<mesgroup[i].point
<<"\t"<<mesgroup[i].Id<<"\t"<<mesgroup[i].grade<<"\t"<<mesgroup[i].message<<endl;
}
}
void Sort_add() ///在Add部分先排序
{
sort(mesgroup+1,mesgroup+stu_cnt+1,cmp); //利用库函数排序
for(int i=1;i<=stu_cnt;i++){mesgroup[i].rank=i;}
}
void add() ///添加新数据
{
if(stu_cnt>=100){printf("Sorry,there was no vacant space to storage");} ///人数超过100人无法再添加
else
{cout<<"Last Name:"; cin>>lan;cout<<"\n"; //姓
cout<<"First Name:"; cin>>fin; cout<<"\n";//名
cout<<"ID:";cin>>id;cout<<"\n";//id
cout<<"Score:"; cin>>score;cout<<"\n";//分数
stu_cnt++;///学生人数加1
mesgroup[stu_cnt].fn=fin;
mesgroup[stu_cnt].ln=lan;
while(!judge_ID(id))
{ cout<<"pls input correct ID:";cin>>id;cout<<"\n"; ///判断输入id是否正确
}
while(score) ///判断输入分数是否正确
{ if(score>=0&&score<=100)break;
else{cout<<"Pls inout correct score:\n";
cin>>score;
}
}
mesgroup[stu_cnt].Id=id; ///数据库添加个人ID
mesgroup[stu_cnt].point=score; ///数据库添加个人分数
if(judge_retake(mesgroup[stu_cnt].Id)) ///判断重修信息
{
mesgroup[stu_cnt].message="Yes";
}
else{mesgroup[stu_cnt].message="No";}
mesgroup[stu_cnt].grade=gpacalc(mesgroup[stu_cnt]); ///数据库添加个人GPA
}
cout<<"Information uploaded."<<endl;
Sort_add(); ///加入新数据后将所有数据排序
}
void manner(void) ///打印菜单
{
cout<<"1 add"<<endl;
cout<<"2 delete"<<endl;
cout<<"3 search"<<endl;
cout<<"4 sort by score"<<endl;
cout<<"5 encrypt"<<endl;
cout<<"6 decrypt"<<endl;
cout<<"7 analysis"<<endl;
cout<<"0 exit"<<endl;
}
bool judge_ID(string i) ///判断id是否符合规定
{ if(i.size()==12)
{ for(int j=0;j<12;j++) ///ID长度需要符合12位
{ if(!isdigit(i[j]))
{
return false;
}
}
string s1=i.substr(0,4);
if(s1=="2022"||s1=="2021"||s1=="2020") ///限定ID开头四位
{return true ;}
}
else{return false;}
return false;
}
void Delete(string i) ///删除数据库信息
{ if(judge_ID(i)) ///判断ID
{
int del=0;
for(int j=1;j<=stu_cnt;j++ ){if(mesgroup[j].Id==i){ del=j; break; }} ///遍历寻找ID对应的数组中的位置
for(int j=del;j<=stu_cnt-1;j++){mesgroup[j]=mesgroup[j+1];} ///将数组中剩下的信息向前移动
stu_cnt--; ///数据库人数减去1
cout<<"Informatin has deleted.";
}
else
{
cout<<"Sorry,your input was wrong.\n";
return;
}
}
void search(string i) /查找对应ID
{
if(judge_ID(i))
{ int ser=0;
for(int j=1;j<=stu_cnt;j++ ){if(mesgroup[j].Id==i){ ser=j; break; }} ///遍历输出打印
cout<<"The message you wanna are as follows."<<endl;
cout<<"Name: "<< mesgroup[ser].ln<<" "<<mesgroup[ser].fn<<" "; cout<<"ID:"<<mesgroup[ser].Id<<endl;
cout<<"Score:"<<mesgroup[ser].point<<" ";cout<<"Rank:"<<mesgroup[ser].rank<<endl;
cout<<"GPA:"<<mesgroup[ser].grade<<" ";cout<<"Retak:"<<mesgroup[ser].message<<endl;
}
else{ cout<<"Sorry,your input was wrong."; return; }
}
bool judge_retake(string i) ///bool函数判断重修信息
{
string s2=i.substr(0,4);
if(s2=="2022"){return false;}
else{return true;}
}
string gpacalc(stu x) ///根据成绩判定对应的GPA
{
if(x.point>=93&&x.point<=100){return "A+";}
else if(x.point>=85&&x.point<=92){return "A";}
else if(x.point>=80&&x.point<=84){return "B+";}
else if(x.point>=75&&x.point<=79){return "B";}
else if(x.point>=70&&x.point<=74){return "C+";}
else if(x.point>=65&&x.point<=69){return "C";}
else if(x.point>=64&&x.point<=60){return "D";}
else if(x.point>=0&&x.point<=59){return "F";}
}
void analysis()
{int a1=0,a=0,b1=0,b=0,c=0,c1=0,d=0,f=0; //初始化
for(int i=1;i<=stu_cnt;i++) 遍历数组使对应变量数值增加最后打印
{
if(mesgroup[i].grade=="A+"){a1++;}
if(mesgroup[i].grade=="A"){a++;}
if(mesgroup[i].grade=="B+"){b1++;}
if(mesgroup[i].grade=="B"){b++;}
if(mesgroup[i].grade=="C+"){c1++;}
if(mesgroup[i].grade=="C"){c++;}
if(mesgroup[i].grade=="D"){d++;}
if(mesgroup[i].grade=="F"){f++;}
}
cout<<"GPA"<<"\t"<<"Number"<<endl;
cout<<"A+"<<"\t "<<a1<<endl;
cout<<"A"<<"\t "<<a<<endl;
cout<<"B+"<<"\t "<<b1<<endl;
cout<<"B"<<"\t "<<b<<endl;
cout<<"C+"<<"\t "<<c1<<endl;
cout<<"C"<<"\t "<<c<<endl;
cout<<"D"<<"\t "<<d<<endl;
cout<<"F"<<"\t "<<f<<endl;
}
void encrypt(int key)
{
string sum="";///空字符串 用来拼接所有成绩
for(int i=1;i<=stu_cnt;i++)
{ string tem= to_string(mesgroup[i].point);
sum+=tem;
}
string s1=""; ///用来接收加密后的字符串
for(auto ch : sum) //遍历拼接后字符串每一个字符
{
s1+=((ch-'0'+key)%10+'0'); ///利用所给的key对应加密再转换为字符
}
cout<<"Encrypted message are as follows:"<<endl;
cout<<s1;
}
void decrypt(int key,string x )
{
string s2=""; ///空字符串用来接收解密后字符串
for(auto ch : x) //atuo语句遍历string中的字符
{
s2+=((ch-'0'+10-key)%10+'0'); 与加密规则对称
}
cout<<"Decrypted message are as follows:"<<endl;
cout<<s2;
}
-----未经过授权请勿随意转载使用-------