| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 469 人关注过本帖
标题:大家看看,在修改下
只看楼主 加入收藏
lndy
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-6-25
收藏
 问题点数:0 回复次数:4 
大家看看,在修改下

#include <iostream> //包含头文件
#include <string>
#include <windows.h>
#include <fstream>
using namespace std;
//************************************************************************************************
class student //定义student类
{
private:
int s_num; //学生学号
char name[20]; //学生姓名
char major[20]; //学生专业
char adress[30]; //学生宿舍地址
char tel[30]; //学生联系电话
//************************************************************************************************
public:
void create() //建立学生宿舍信息表
{
int i,num;
student stu;
cout<<"请输入学生总人数:"<<"\n";
cin>>num;
fstream stu_file;
stu_file.open("d:\\student.txt",ios::out|ios::app); //打开读写文件
if (!stu_file) //打开失败时输出信息
{
cerr<<"打开文件时出错:"<<"\n";
system("pause");
exit(-1);
}
cout<<"请输入学生信息:"<<"\n";
for (i=0;i<num;i++)
{
cout<<endl<<"学号";
cin>>stu.s_num;
cout<<endl<<"姓名";
cin>>stu.name;
cout<<endl<<"专业";
cin>>stu.major;
cout<<endl<<"宿舍地址";
cin>>stu.adress;
cout<<endl<<"联系电话";
cin>>stu.tel;
stu_file.write((char*)(&stu),sizeof(class student));
}
stu_file.close();

}


void find() //查找学生信息
{
class student s_find[100] ;
int sum=0,num=0;
cout<<"请输入你要查询的学生号:"<<"\n";
cin>>num;
fstream stu_file;
stu_file.open("d:\\student.txt",ios::in);
if (!stu_file)
{
cerr<<"打开文件时出错:"<<"\n";
system("pause");
exit(-1);
}
stu_file.read((char*)&(s_find[0]),sizeof (class student)); //把第一个学生信息读入s_find[]中
while (!stu_file.eof())
{
sum++;
stu_file.read((char*)&(s_find[sum]),sizeof (class student));//把全部学生信息读入到s_find[]中
}
stu_file.close();
for(int i=0;i<sum;i++)
{
if(s_find[i].s_num==num) //依次读取直到找出num号跳出程序
break;
}
if (i==sum) //判断该学号不存在
{
cout<<"没有该生信息请核对学生号:"<<"\n";
system("pause");
exit(-2);
}
else //输出查找到的学生信息
{
cout<<endl<<"您要查找的学生信息是:";
cout<<endl<<"学号:"<<s_find[i].s_num;
cout<<endl<<"姓名:"<<s_find[i].name;
cout<<endl<<"专业:"<<s_find[i].major;
cout<<endl<<"宿舍地址:"<<s_find[i].adress;
cout<<endl<<"联系电话:"<<s_find[i].tel;
}
}


void del() //删除函数
{
class student del[100];
int sum=0,k=0;
cout<<"请输入你要删除的学生号:"<<endl;
cin>>k;
fstream stu_file;
stu_file.open("d:\\student.txt",ios::in); //打开只读文件
if (!stu_file)
{
cerr<<"打开文件时出错:"<<"\n";
system("pause");
exit(-3);
}
stu_file.read((char*)&(del[0]),sizeof (class student)); //把第一个学生信息读入del[]中
while (!stu_file.eof())
{
sum++;
stu_file.read((char*)(&del[sum]),sizeof (class student)); //读取全部学生信息到del[]中
}
stu_file.close();
for(int i=0;i<sum;i++)
{
if(del[i].s_num==k)
break;
}
if (i==sum) //判断该学号不存在
{
cout<<"没有该生信息请核对学生号:"<<"\n";
system("pause");
exit(-4);
}
else if (i<sum-1) //被查找的学生在1~sum之间
{
for (;i<sum-1;i++)
{
del[i]=del[i+1];
}
sum=sum-1;
cout<<"文件正在删除";
for (int i=0;i<10;i++)
{
Sleep(50);
cout<<".";
}
system("cls");
cout<<endl<<"删除成功"<<endl;
}
else //被查找的学生是最后一位
{

sum=sum-1; //直接把sum减去1
cout<<"文件正在删除";
for (int i=0;i<10;i++)
{
Sleep(50);
cout<<".";
}
system("cls");
cout<<endl<<"删除成功"<<endl;
}
stu_file.open("d:\\student.txt",ios::trunc|ios::out); //以截断和写的方式打开文件
if (!stu_file)
{
cerr<<"打开文件时出错:"<<"\n";
system("pause");
exit(-5);
}
for ( i=0;i<sum;i++)
stu_file.write((char*)(&del[i]),sizeof(class student)); //把del[]中的数锯重新写入文件
stu_file.close();
}


void alter() //修改学生信息函数
{
student stu;
int m;
fstream stu_file;
cout<<"请输入你要修改的学生号:"<<endl;
cin>>m;
stu_file.open("d:\\student.txt",ios::out|ios::in); //打开文件用与读写
if (!stu_file)
{
cerr<<"打开文件时出错:"<<"\n";
system("pause");
exit(-6);
}
stu_file.seekg((m-1)*sizeof(class student)); //定位指针到指定位置
stu_file.read((char*)&stu,sizeof(class student));
cout<<endl<<"您要修改的学生信息是:";
cout<<endl<<"学号:"<<stu.s_num;
cout<<endl<<"姓名:"<<stu.name;
cout<<endl<<"专业:"<<stu.major;
cout<<endl<<"宿舍地址:"<<stu.adress;
cout<<endl<<"联系电话:"<<stu.tel;
cout<<"请输入您想要修改的资料:";
cout<<endl<<"修改姓名:";
cin>>stu.name;
cout<<endl<<"修改专业:";
cin>>stu.major;
cout<<endl<<"修改宿舍地址:";
cin>>stu.adress;
cout<<endl<<"修改联系电话:";
cin>>stu.tel;
stu_file.seekg((m-1)*sizeof(class student));
stu_file.write((char*)(&stu),sizeof (class student)); //把修改后的学生信息写入文件
stu_file.close();
}


void display() //显示全部学生信息函数
{
student stu;
fstream stu_file;
stu_file.open("d:\\student.txt",ios::in);
if (!stu_file)
{
cerr<<"打开文件时出错:"<<endl;
system("pause");
exit(-7);
}
stu_file.read((char*)&stu,sizeof (class student));
while (!stu_file.eof()) //逐条读取,直到文件尾部
{
cout<<"学号:"<<stu.s_num<<" "<<"姓名:"<<stu.name<<" "<<"专业:"<<stu.major
<<" "<<"宿舍地址:"<<stu.adress<<" "<<"联系电话:"<<stu.tel;
cout<<endl;
stu_file.read((char*)&stu,sizeof(class student ));
}
stu_file.close();

}


void insert() //插入学生信息函数
{
student array[100];
int sum=0,num=0;
bool flag=1;
fstream stu_file;
stu_file.open("d:\\student.txt",ios::in);
if (!stu_file)
{
cerr<<"打开文件时出错:"<<endl;
system("pause");
exit(-8);
}
stu_file.read((char*)&array[0],sizeof(class student));
while (!stu_file.eof())
{
sum++;
stu_file.read((char*)&array[sum],sizeof (class student));//逐条读取到array[]数组中
}
stu_file.close();
start: cout<<"请输入您要插入的学号:"<<endl;
cin>>num;
for (int i=0;i<sum;i++)
if (array[i].s_num==num) //判断输入的学号是否已存在
{
cout<<"该学号信息已存在,请重新输入"<<endl;
flag=0;
system("pause");
system("cls");
goto start; //要是存在就返回到start
}
if (flag!=0) //不存在就继续输入
{
sum++;
array[sum].s_num=num;
cout<<endl<<"继续输入姓名:";
cin>>array[sum].name;
cout<<endl<<"继续输入专业:";
cin>>array[sum].major;
cout<<endl<<"继续输入地址:";
cin>>array[sum].adress;
cout<<endl<<"继续输入电话:";
cin>>array[sum].tel;
}
stu_file.open("d:\\student.txt",ios::trunc|ios::out);
if (!stu_file)
{
cerr<<"文件出错:"<<endl;
system("pause");
exit(-9);
}
for (i=0;i<sum;i++)
stu_file.write((char*)(&array[i]),sizeof (class student));//重新写入文件
stu_file.close();

}
};

//************************************************************************************************
//****************************************主函数**************************************************
void main ()
{
system("color 06"); //调用系统函数改变窗体颜色
student stud;
string str;
begin:
cout<<" ╭═══════════════╮ "<<endl;
cout<<" ║ ║ "<<endl;
cout<<"╭══════┤ 学生宿舍成员管理系统 ├══════╮"<<endl;
cout<<"║ ║ ║ ║"<<endl;
cout<<"║ ╰═══════════════╯ ║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║ 欢迎使用本成员管理 ║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║ ***************1: 新建宿舍成员数锯库***************** ║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║ ***************2: 查询宿舍人员 ***************** ║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║ ***************3: 删除宿舍成员 ***************** ║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║ ***************4: 修改宿舍成员信息 ***************** ║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║ ***************5: 显示全部成员信息 ***************** ║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║ ***************6: 插入宿舍成员信息 ***************** ║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║ ***************7: 退出系统 ***************** ║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║ 请按相应数字键进行选择 ║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║ ║"<<endl;
cout<<"║ ╭───────────────────────╮ ║"<<endl;
cout<<"╰══┤ 开发者lndyfun 开发时间:2007-8-10 ├══╯"<<endl;
cout<<" ╰───────────────────────╯"<<endl;
cout<<"请输入您的选择:";
cin>>str;
if(str[0]=='1')
{
system("cls");
stud.create();
system("pause");
getchar();
system("cls");
goto begin;
}
else if (str[0]=='2')
{
system("cls");
stud.find();
system("pause");
getchar();
system("cls");
goto begin;

}
else if (str[0]=='3')
{
system("cls");
stud.del();
system("pause");
getchar();
system("cls");
goto begin;
}
else if (str[0]=='4')
{
system("cls");
stud.alter();
system("pause");
getchar();
system("cls");
goto begin;
}
else if (str[0]=='5')
{
system("cls");
stud.display();
system("pause");
getchar();
system("cls");
goto begin;

}
else if (str[0]=='6')
{
system("cls");
stud.insert();
system("pause");
getchar();
system("cls");
goto begin;
}
else if (str[0]=='7')
{
system("cls");
cout<<"谢谢您使用本系统,程序即将退出";
for (int i=0;i<8;i++)
{
cout<<"."<<"\a";
}
cout<<endl;
exit(0);

}
else
{
system("cls");
cout<<"输入错误,请重新输入:"<<endl;
system("pause");
getchar();
system("cls");
goto begin;
}
}

2007-08-22 06:13
圆圆的鸟蛋
Rank: 1
等 级:新手上路
帖 子:216
专家分:0
注 册:2007-4-22
收藏
得分:0 
修改什么??有问题吗??
这么长的代码,不会叫我们去找错误吧??

鸟蛋开始孵化。。。我等待那一天Forever。。
2007-08-22 11:03
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
too long --- if i finished reading it, the earth would be rectangular.

I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-08-22 13:35
woshibuhui
Rank: 2
等 级:论坛游民
帖 子:15
专家分:27
注 册:2007-8-14
收藏
得分:0 

看了不到一半,头就大了,头疼。。。。。。。。。。。

2007-08-22 15:13
qkjenjoy
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2007-8-24
收藏
得分:0 

这么长,一般来说用指针数组比用数组更节约内存,优化下吧

[此贴子已经被作者于2007-8-25 15:40:16编辑过]

2007-08-25 09:15
快速回复:大家看看,在修改下
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017850 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved