C++ 大学管理系统
在大学中,会对许多数据进行分析,并将结果用于组织的管理。大学管理系统会及时更新学院及其多个专业领域的列表,以及负责考试和结果的部门。该项目结果包含了有序外观的菜单和子菜单。
大学管理系统是基于面向对象平台使用C++编程语言构建的。它主要用于跟踪与大学相关的学院列表以及它们提供的众多课程。此外,该项目还使用了一个良好的菜单系统来维护和管理考试和结果部门。
程序分解
#include<iostream>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<fstream>
#include<algorithm>
using namespace std;
包括所有必要的文件以避免任何类型的错误。
class student
{
public:
long int reg;
char name[80],branch[50];
void input()
{
cout<<"\n Enter name: ";
gets(name);
cout<<"\n Enter roll no: ";
cin>>reg;
fflush(stdin);
cout<<"\n Enter Branch: ";
gets(branch);
}
void display()
{
system("CLS");
cout<<"\t\tDisplay Records";
cout<<"\n";
cout<<"\n Name - "<<name;
cout<<"\n Reg no. - "<<reg;
cout<<"\n Branch - "<<branch;
cout<<"\n";
system("PAUSE");
system("CLS");
}
bool operator == (student a)
{
if(reg==a.reg)
return true;
else
return false;
}
};
vector <student>v;
上述代码将用于创建学生类,该类将具有以下数据变量。
int search_reg(long int reg, int &i);
void get_file()
{
student x;
int i=0;
fstream f;
f.open("College.txt",ios::in);
if(f)
{
f.read((char *) &x,sizeof(class student));
while(!f.eof())
{
v.push_back(x);
f.read((char *) &x,sizeof(class student));
}
}
else
;
f.close();
}
以上代码将用于获取保存为college.txt的文件
void bubblesort()
{
int i,j;
student x;
for(i=0;i<v.size();i++)
for(j=0;j<v.size()-i-1;j++)
if(v[j].reg>v[j+1].reg)
{
x=v[j];
v[j]=v[j+1];
v[j+1]=x;
}
}
以上代码将用于以冒泡排序方式对学生记录进行排序,其时间复杂度为O(n^2)。
void bubblesort()
{
int i,j;
student x;
for(i=0;i<v.size();i++)
for(j=0;j<v.size()-i-1;j++)
if(v[j].reg>v[j+1].reg)
{
x=v[j];
v[j]=v[j+1];
v[j+1]=x;
}
}
以上代码将用于向大学生记录中插入新数据。
void bubblesort()
{
int i,j;
student x;
for(i=0;i<v.size();i++)
for(j=0;j<v.size()-i-1;j++)
if(v[j].reg>v[j+1].reg)
{
x=v[j];
v[j]=v[j+1];
v[j+1]=x;
}
}
上述代码将用于将冒泡排序函数对大学生数据的结果写入college.txt文件。
int search_reg(long int reg, int &i)
{
int ta=0;
for(i=0;i<v.size();i++)
if(v[i].reg==reg)
{
ta=1;
break;
}
return ta;
}
上述代码将用于在为学生创建的数据文件中搜索学院中学生的注册信息。
int search_name(char name[], vector<int> &vi)
{
int i,ta=0;
for(i=0;i<v.size();i++)
if(strcmp(v[i].name,name)==0)
{
ta=1;
vi.push_back(i);
}
return ta;
}
上述代码将用于在为学生创建的记录文件中搜索学生的姓名。
int search_branch(char branch[], vector<int> &vj)
{
int i,ta=0;
for(i=0;i<v.size();i++)
if(strcmp(v[i].branch,branch)==0)
{
ta=1;
vj.push_back(i);
}
return ta;
}
以上代码将用于搜索分支。
void search_and_show()
{
int ch,i,ta=0;
char name[80],branch[50];
vector <int>vi;
vector <int>vj;
long int reg;
poi:
cout<<"\n1.Press to search reg no."
<<"\n2.Press to search name"
<<"\n3.Press to search branch";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter reg no.: ";
cin>>reg;
if(search_reg(reg,i)==1)
v[i].display();
else
cout<<"\nRecord NOT FOUND!!!";
break;
case 2:
cout<<"\nEnter name: ";
fflush(stdin);
gets(name);
if(search_name(name,vi)==1)
{
for(int j=0;j<vi.size();j++)
v[vi[j]].display();
}
else
cout<<"\nRecord NOT FOUND!!!";
break;
case 3:
cout<<"\nEnter branch: ";
fflush(stdin);
gets(branch);
if(search_branch(branch,vj)==1)
{
for(int j=0;j<vj.size();j++)
v[vj[j]].display();
}
else
cout<<"\nRecord NOT FOUND!!!";
break;
default:
cout<<"\nWrong CHOICE!!!";
goto poi;
}
}
void show()
{
int i;
for(i=0;i<v.size();i++)
v[i].display();
}
以上的代码将用于显示结果。
void delete_data()
{
int i,j;
long int reg;
vector <student>::iterator p=v.begin();
cout<<"\nEnter Reg. no.: ";
cin>>reg;
if(search_reg(reg,i)==1)
{
student x=v[i];
cout<<"\nThe following data is being deleted";
x.display();
p+=i;
v.erase(p,p+1);
}
}
上述代码将用于删除存储的数据。
void edit_data()
{
int i,j;
long int reg;
vector <student>vi;
cout<<"\nEnter Reg. no.: ";
cin>>reg;
if(search_reg(reg,i)==1)
{
cout<<"\nEnter new data:";
fflush(stdin);
v[i].input();
}
}
以上代码将用于编辑数据。
int main()
{
int i=1;
get_file();
while(i)
{
system("CLS");
cout<<"\t\t\t-----------------------------------------\n";
cout<<"\t\t\t University Management System\n";
cout<<"\t\t\t-----------------------------------------\n";
cout<<"\n\t\t\tEnter <1> to Add new student"
<<"\n\t\t\tEnter <2> to Display all student"
<<"\n\t\t\tEnter <3> to Remove student"
<<"\n\t\t\tEnter <4> to Edit student"
<<"\n\t\t\tEnter <5> to Search student"
<<"\n\t\t\tEnter <0> to Exit\n";
cout<<"\n\n\t\t\tEnter Your Choice:";
cin>>i;
switch(i)
{
case 1 :
insert_new();
break;
case 2 :
show();
break;
case 3 :
delete_data();
break;
case 4 :
edit_data();
break;
case 5 :
search_and_show();
break;
case 0 :
write_file();
break;
default :
cout<<"\nWRONG CHOICE!!!\nTRY AGAIN";
}
}
return 0;
}
上述代码将是系统的开关语句。
输出
-----------------------------------------
University Management System
-----------------------------------------
Enter <1> to Add new student
Enter <2> to Display all student
Enter <3> to Remove student
Enter <4> to Edit student
Enter <5> to Search student
Enter <0> to Exit
Enter Your Choice:1
Enter name: John Miller
Enter roll no: 1001
Enter Branch: Computer Science
Press [Y] to enter more: Y
Enter name: Daisy Smith
Enter roll no: 1002
Enter Branch: Computer Science
Press [Y] to enter more: N
-----------------------------------------
University Management System
-----------------------------------------
Enter <1> to Add new student
Enter <2> to Display all student
Enter <3> to Remove student
Enter <4> to Edit student
Enter <5> to Search student
Enter <0> to Exit
Enter Your Choice:2
Display Records
Name - John Miller
Reg no.- 1001
Branch - Computer Science
Name - Daisy Smith
Reg no. - 1002
Branch - Computer Science
Press any key to continue . . .