学生寝室管理系统设计与实现 数据结构
- 提问者网友:你给我的爱
- 2021-02-06 20:31
- 五星知识达人网友:底特律间谍
- 2021-02-06 21:12
{
char name[10];
int num;
int dormin;
};cpp文件:// 宿舍管理.cpp : 定义控制台应用程序的入口点。
//
///////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include
#include
#include
using namespace std;
#define max 10
int _tmain(int argc, _TCHAR* argv[])
{void input(student *,int);void display(student *,int);void add(student *,int &,int);
void qsort(student *,int,int);void del(student *,char *,int &);void change(student *,char *,int );
void search(student *,int,int,int);
int stunum,choose;
ifstream infile("stunum.txt");
if(!infile) cout<<"文件中尚未创建信息!"<
infile.close();
char name[10];
cout<<"****************************************************"<
cout<<"1.输入学生宿舍信息"<
cin>>choose;
student *S;S=new student[max];
switch(choose){
case 1:{cout<<"输入数据将使原来数据丢失,确定这样做吗?(y/n)"<
cin>>temp;
if(temp=='y') yn=1;
if(yn){cout<<"请输入学生数目:";
cin>>stunum;
input(S,stunum);
ofstream outfile("stunum.txt");
outfile<
case 2:display(S,stunum);break;
case 3:
{int cchoose;cout<<"1.修改信息"<
switch(cchoose)
{case 1:{cout<<"请输入修改的信息人名"<
case 2:{int ad;cout<<"请输入添加人数:";cin>>ad;add(S,stunum,ad);break;}
case 3:{cout<<"请输入删除信息的人名"<
break;}break;}
case 4:{cout<<"请输入学号:";
int cchoose;cin>>cchoose;search(S,0,stunum,cchoose);
break;}
case 5:{ifstream infile("stuinfor.txt",ios::in);
for(int i=0;i
qsort(S,0,stunum-1);
ofstream outfile("sorted_infor.txt");
for(int i=0;i
outfile.close();infile.close();break;}
case 6:cout<<" 谢谢使用!"<
ofstream outfile("stunum.txt");
outfile<
return 0;
}
void input(student *stu,int all)
{ofstream outfile("stuinfor.txt");
for(int i=0;i>stu[i].name>>stu[i].num>>stu[i].dormin;
outfile<
outfile.close();
}
void display(student *stu,int all)
{
ifstream infile("stuinfor.txt",ios::in);
if(!infile) cerr<<"文件打开失败!"<
for(int i=0;i>stu[i].name>>stu[i].num>>stu[i].dormin;
cout<
infile.close();}
}
void add(student *stu,int &all,int add)
{
ofstream outfile("stuinfor.txt",ios::app);
for(int i=0;i>stu[all+i].name>>stu[all+i].num>>stu[all+i].dormin;
outfile<
all+=add;
outfile.close();
}
//快速排序
int partition(student *a,int low,int high)
{
student tem;int piv=a[low].num;
while(high>low)
{
while(high>low&&a[high].num>=piv) high--;
tem=a[high];a[high]=a[low];a[low]=tem;
while(high>low&&a[low].num<=piv) low++;
tem=a[high];a[high]=a[low];a[low]=tem;
}
return low;
}
void qsort(student *a,int low,int high)
{int pivotloc;
if(low
pivotloc=partition(a,low,high);
qsort(a,low,pivotloc-1);
qsort(a,pivotloc+1,high);
}
}
void del(student *stu,char *name,int &num)
{int delper;char check;
ifstream infile("stuinfor.txt");
for(int i=0;i
infile>>stu[i].name>>stu[i].num>>stu[i].dormin;
if(strcmp(name,stu[i].name)==0) delper=i;}
cout<<"您要删除的信息如下:"<
ofstream outfile("stuinfor.txt");
if(check=='y'||'Y')
{
for(int i=delper;i
for(int i=0;i
outfile.close();
}
void change(student *stu,char *name,int num)
{int change;char check,nam[10];
int cn,cd;
ifstream infile("stuinfor.txt");
for(int i=0;i
infile>>stu[i].name>>stu[i].num>>stu[i].dormin;
if(strcmp(name,stu[i].name)==0) change=i;
}
cout<<"您要修改的信息如下:"<
ofstream outfile("stuinfor.txt");
if(check=='y'||'Y')
{
cout<<"请依次输入姓名 学号 宿舍号"<
strcpy(stu[change].name,nam);stu[change].num=cn;stu[change].dormin=cd;
for(int i=0;i
outfile.close();}void search(student *stu,int low,int high,int check)
{int mid=(low+high)/2;
ifstream infile("sorted_infor.txt");
if(!infile) cerr<<"文件打开失败!"<
{for(int i=0;i
while(stu[mid].num!=check&&high>=low) {mid=(low+high)/2;if(stu[mid].num>check) high=mid-1;
else low=mid+1;}//折半查找
}
if(stu[mid].num==check)
{cout<<"您要查询的信息如下:"<
- 1楼网友:几近狂妄
- 2021-02-06 21:42
查询也是只写了按学号查询,是用的折半查找。排序是用的快速排序,因为时间消耗比较小,喏,那是上个排序综合里有的,所以写这个就用的那个里的快速排序,很简单,就是对copy下而已,改下结构体,complete!呵呵,程序如下:
头文件里的:
struct student { char name[10]; int num; int dormin; };
cpp文件:
// 宿舍管理.cpp : 定义控制台应用程序的入口点。 // /////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// #include "stdafx.h" #include<fstream> #include<stdlib.h> #include<iostream> using namespace std; #define max 10 int _tmain(int argc, _tchar* argv[]) {void input(student *,int);void display(student *,int);void add(student *,int &,int); void qsort(student *,int,int);void del(student *,char *,int &);void change(student *,char *,int ); void search(student *,int,int,int);
int stunum,choose; ifstream infile("stunum.txt"); if(!infile) cout<<"文件中尚未创建信息!"<<endl; else infile>>stunum; infile.close();
char name[10]; cout<<"****************************************************"<<endl; cout<<endl<<endl; cout<<" 欢迎进入学生宿舍管理系统!!"<<endl; cout<<endl<<endl; cout<<"****************************************************"<<endl; while(1){ cout<<"1.输入学生宿舍信息"<<endl; cout<<"2.显示已输入信息"<<endl; cout<<"3.信息修改"<<endl; cout<<"4.信息查询"<<endl; cout<<"5.信息排序"<<endl; cout<<"6.退出系统"<<endl; cout<<"请输入需要执行的任务:"; cin>>choose; student *s;s=new student[max]; switch(choose){ case 1:{cout<<"输入数据将使原来数据丢失,确定这样做吗?(y/n)"<<endl; bool yn=0;char temp; cin>>temp; if(temp=='y') yn=1; if(yn){cout<<"请输入学生数目:"; cin>>stunum; input(s,stunum); ofstream outfile("stunum.txt"); outfile<<stunum; outfile.close();break;}} case 2:display(s,stunum);break; case 3: {int cchoose;cout<<"1.修改信息"<<endl; cout<<"2.添加信息"<<endl;cout<<"3.删除信息"<<endl;cout<<"请输入要执行的任务:"<<endl; cin>>cchoose; switch(cchoose) {case 1:{cout<<"请输入修改的信息人名"<<endl;cin>>name;change(s,name,stunum);break;} case 2:{int ad;cout<<"请输入添加人数:";cin>>ad;add(s,stunum,ad);break;} case 3:{cout<<"请输入删除信息的人名"<<endl;cin>>name;del(s,name,stunum);break;} break;}break;} case 4:{cout<<"请输入学号:"; int cchoose;cin>>cchoose;search(s,0,stunum,cchoose); break;} case 5:{ifstream infile("stuinfor.txt",ios::in); for(int i=0;i<stunum;i++) infile>>s[i].name>>s[i].num>>s[i].dormin; qsort(s,0,stunum-1); ofstream outfile("sorted_infor.txt"); for(int i=0;i<stunum;i++) {outfile<<s[i].name<<' '<<s[i].num<<' '<<s[i].dormin<<endl; cout<<s[i].name<<' '<<s[i].num<<' '<<s[i].dormin<<endl;} outfile.close();infile.close();break;} case 6:cout<<" 谢谢使用!"<<endl;exit(1);
} } ofstream outfile("stunum.txt"); outfile<<stunum; outfile.close(); return 0; } void input(student *stu,int all) {ofstream outfile("stuinfor.txt"); for(int i=0;i<all;i++) {cout<<"请输入第"<<i+1<<"个学生的姓名 学号 宿舍号:"; cin>>stu[i].name>>stu[i].num>>stu[i].dormin; outfile<<stu[i].name<<" "<<stu[i].num<<" "<<stu[i].dormin<<endl; } outfile.close(); } void display(student *stu,int all) { ifstream infile("stuinfor.txt",ios::in); if(!infile) cerr<<"文件打开失败!"<<endl; else{ for(int i=0;i<all;i++) {cout<<"输入的第"<<i+1<<"个学生的姓名 学号 宿舍号分别为:"; infile>>stu[i].name>>stu[i].num>>stu[i].dormin; cout<<stu[i].name<<" "<<stu[i].num<<" "<<stu[i].dormin<<endl; } infile.close();} } void add(student *stu,int &all,int add) { ofstream outfile("stuinfor.txt",ios::app); for(int i=0;i<add;i++) { cout<<"请输入第"<<all+i+1<<"个学生的姓名 学号 宿舍号:"; cin>>stu[all+i].name>>stu[all+i].num>>stu[all+i].dormin; outfile<<stu[all+i].name<<" "<<stu[all+i].num<<" "<<stu[all+i].dormin<<endl; } all+=add; outfile.close(); } //快速排序 int partition(student *a,int low,int high) { student tem;int piv=a[low].num; while(high>low) { while(high>low&&a[high].num>=piv) high--; tem=a[high];a[high]=a[low];a[low]=tem; while(high>low&&a[low].num<=piv) low++; tem=a[high];a[high]=a[low];a[low]=tem; } return low; } void qsort(student *a,int low,int high) {int pivotloc; if(low<high) { pivotloc=partition(a,low,high); qsort(a,low,pivotloc-1); qsort(a,pivotloc+1,high); } }
void del(student *stu,char *name,int &num) {int delper;char check; ifstream infile("stuinfor.txt"); for(int i=0;i<num;i++) { infile>>stu[i].name>>stu[i].num>>stu[i].dormin; if(strcmp(name,stu[i].name)==0) delper=i;} cout<<"您要删除的信息如下:"<<endl; cout<<stu[delper].name<<" "<<stu[delper].num<<" "<<stu[delper].dormin<<endl; cout<<"您确定要删除吗?(y/n)"<<endl; cin>>check; ofstream outfile("stuinfor.txt"); if(check=='y'||'y') { for(int i=delper;i<num;i++) stu[i]=stu[i+1]; num--; for(int i=0;i<num;i++) outfile<<stu[i].name<<" "<<stu[i].num<<" "<<stu[i].dormin<<endl; cout<<"信息已删除!"<<endl;} infile.close(); outfile.close(); } void change(student *stu,char *name,int num) {int change;char check,nam[10]; int cn,cd; ifstream infile("stuinfor.txt"); for(int i=0;i<num;i++) { infile>>stu[i].name>>stu[i].num>>stu[i].dormin; if(strcmp(name,stu[i].name)==0) change=i; } cout<<"您要修改的信息如下:"<<endl; cout<<stu[change].name<<" "<<stu[change].num<<" "<<stu[change].dormin<<endl; cout<<"您确定要修改吗?(y/n)"<<endl; cin>>check; ofstream outfile("stuinfor.txt"); if(check=='y'||'y') { cout<<"请依次输入姓名 学号 宿舍号"<<endl; cin>>nam>>cn>>cd; strcpy(stu[change].name,nam);stu[change].num=cn;stu[change].dormin=cd; for(int i=0;i<num;i++) outfile<<stu[i].name<<" "<<stu[i].num<<" "<<stu[i].dormin<<endl; cout<<"信息已修改!"<<endl;} infile.close(); outfile.close();
}
void search(student *stu,int low,int high,int check) {int mid=(low+high)/2; ifstream infile("sorted_infor.txt"); if(!infile) cerr<<"文件打开失败!"<<endl; else {for(int i=0;i<high;i++) infile>>stu[i].name>>stu[i].num>>stu[i].dormin; while(stu[mid].num!=check&&high>=low) {mid=(low+high)/2;if(stu[mid].num>check) high=mid-1; else low=mid+1;}//折半查找 } if(stu[mid].num==check) {cout<<"您要查询的信息如下:"<<endl; cout<<stu[mid].name<<" "<<stu[mid].num<<" "<<stu[mid].dormin<<endl;} else cout<<"未能查到您所要查询信息!"<<endl; }