给学生看看

天地一棵树 2009-11-05 11:22:10
#include<iostream.h>
#include<fstream.h>


class student
{
public:
char name[10];
int num;
float mathScore,englishScore;
friend ostream &operator<<(ostream &out,student &s);
friend istream &operator>>(istream &in,student &s);

};


ostream &operator<<(ostream &out,student &s)
{
out<<s.name<<" "<<s.num<<" "<<s.mathScore<<" "<<s.englishScore<<'\n';
return out;
}


istream &operator<<(istream &in,student &s)
{
in>>s.name>>s.num>>s.mathScore>>s.englishScore;
return in;
}


void main()
{
ofstream ofile;
ifstream ifile;
ofile.open("d:\\s.txt");

student s;
for(int i=1;i<=3;i++)
{
cout<<"请输入第i个学生的姓名 学号 数学成绩 英语成绩 "<<endl;
cin>>s;
cout<<s;
// cin>>s.name>>s.num>>s.mathScore>>s.englishScore>>endl;
// cout<<s.name<<s.num<<s.mathScore<<s.englishScore<<endl;
}
ofile.close();
cout<<"\n读出文件内容"<<endl;
// ifile.open("C:\\Documents and Settings\\use\\桌面\\学生信息\\myfile.txt");
ifile.open("d:\\s.txt");
ifile>>s;
//ifile>>s;
while(!ifile.eof())
{
cout<<s.name<<s.num<<s.mathScore<<s.englishScore<<endl;
ifile>>s;
}
ifile.close();


}

...全文
204 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
forster 2009-11-10
  • 打赏
  • 举报
回复
啥老师
kxalpah 2009-11-10
  • 打赏
  • 举报
回复
重载的时候写反了吧
clhposs 2009-11-10
  • 打赏
  • 举报
回复
我用C++些的学生管理系统 我以为楼主要

这个是我写的那个系统的2个类
jinghaijingtian 2009-11-10
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 clhposs 的回复:]
C/C++ code#ifndef PEOPLE_CLASS#define PEOPLE_CLASS/*
*人员类
*操作单个人
*只提够默认构造函数,重载了输入输出*/class People
{public://重载输入操作 friend std::istream&operator>>(std::istream&in, People&people);//重载输出操作 friend ?-
[/Quote]
这是什么啊
clhposs 2009-11-10
  • 打赏
  • 举报
回复
#ifndef PEOPLE_CLASS
#define PEOPLE_CLASS
/*
*人员类
*操作单个人
*只提够默认构造函数,重载了输入输出
*/
class People
{
public:
//重载输入操作
friend std::istream& operator>>(std::istream &in, People &people);
//重载输出操作
friend std::ostream& operator<<(std::ostream &os, People &people);
People();
People(const People &people);
People& operator=(const People &people);
//查找是否存在某个关键字,查询失败返回NULL
People* find(std::string strFind);
private:
std::string strNumber; //学号
std::string strName; //姓名
std::string strClass; //班级
std::string strCollege; //学院
std::string strPho; //电话
std::string strQQ; //QQ号码
};
#endif
#include <iostream>
#include <string>
#include "People.h"

People::People():strNumber(""), strName(""), strClass(""), strCollege(""), strPho(""), strQQ("")
{}

People::People(const People &people):strNumber(people.strNumber), strName(people.strName), strClass(people.strClass),strCollege(people.strCollege), strPho(people.strPho), strQQ(people.strQQ)
{}

People& People::operator =(const People &people)
{
strNumber = people.strNumber;
strName = people.strName;
strClass = people.strClass;
strCollege = people.strCollege;
strPho = people.strPho;
strQQ = people.strQQ;

return *this;
}

std::istream& operator>>(std::istream& in, People &people)
{
std::cout << "学号:";
in >> people.strNumber;
std::cout << "姓名:";
in >> people.strName;
std::cout << "班级:";
in >> people.strClass;
std::cout << "学院:";
in >> people.strCollege;
std::cout << "电话:";
in >> people.strPho;
std::cout << "Q Q :";
in >> people.strQQ;

return in;
}

std::ostream& operator<<(std::ostream& os, People &people)
{
os << "学号:" << people.strNumber << "\t姓名:" << people.strName
<< "\t班级:" << people.strClass << "\t学院:" << people.strCollege
<< "\t电话:" << people.strPho << "\tQQ:" << people.strQQ;

return os;
}

People* People::find(const std::string strFind)
{
if (strNumber == strFind || strName == strFind ||
strClass == strFind || strCollege == strFind || strPho == strFind || strQQ == strFind)
return this;
else
return NULL;
}

#ifndef MANAGEPEOPLE_CLASS
#define MANAGEPEOPLE_CLASS
#include "People.h"

/*
* 管理People,对多个People的封装
* 对管理系统的封装
*/
class ManagePeople
{
public:
friend std::istream& operator>>(std::istream &is, ManagePeople &people);
friend std::ostream& operator<<(std::ostream &os, const ManagePeople &people);
ManagePeople();
ManagePeople(const ManagePeople& people);
ManagePeople& operator=(const ManagePeople &people);

void show();
private:
//显示菜单
void showMenu();
//显示所有学生信息
void showStudent();
//增加学生信息
void addStudent();
//删除学生信息
void delStudent();
//查找学生信息,成功查找返回指向改学生信息的迭代器
std::vector<People>::iterator find();
//销毁容器
void destroyManage();
private:
std::vector<People> all_people; //存储所有人信息
int index; //存储你输入的选项
};
#endif

#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
#include "ManagePeople.h"
#include "People.h"

ManagePeople::ManagePeople():all_people()
{}

ManagePeople::ManagePeople(const ManagePeople &people):all_people(people.all_people)
{}

ManagePeople& ManagePeople::operator=(const ManagePeople &people)
{
this->all_people = people.all_people;
return *this;
}

void ManagePeople::showMenu()
{
system("cls");
std::cout << "\t\t\t\t学生管理系统\n" << std::endl;
std::cout << "\t\t\t 1.显示所有学生信息" << std::endl;
std::cout << "\t\t\t 2.添加学生信息" << std::endl;
std::cout << "\t\t\t 3.删除学生信息" << std::endl;
std::cout << "\t\t\t 4.初始化系统(慎用)" << std::endl;
std::cout << "\t\t\t 5.退出系统" << std::endl;
std::cout << "\n\t\t\t请选择(1,2,3,4,5):";
std::cin >> index;
}

std::vector<People>::iterator ManagePeople::find()
{
system("cls");
std::string strFind;
std::cout << "请输入查找关键字:" << std::endl;
std::cin >> strFind;

for (std::vector<People>::iterator iter = all_people.begin(); iter != all_people.end(); ++iter)
{
if (iter->find(strFind))
return iter;
}
return all_people.end();
}
void ManagePeople::delStudent()
{
std::vector<People>::iterator iter = find();
if (iter == all_people.end())
{
std::cout << "没有找到相关信息" << std::endl;
}
else
{
all_people.erase(iter);
}
}

void ManagePeople::showStudent()
{
system("cls");
for (std::vector<People>::iterator iter = all_people.begin(); iter != all_people.end(); ++iter)
{
std::cout << *iter << std::endl;
}
}

void ManagePeople::addStudent()
{
People people;
std::cin >> people;
all_people.push_back(people);
}

void ManagePeople::destroyManage()
{
system("cls");
std::string str;
std::cout << "确认初始化系统?(Y/N)" << std::endl;
std::cin >> str;
if (str == "n" || str == "N")
return;
std::cout << "\n强制关闭系统可结束系统初始化\n" << std::endl;
while (index++ != 83)
{
Sleep(90);
std::cout << "<";
}
all_people.erase(all_people.begin(), all_people.end());
std::cout << "\t\t\t\t系统初始化成功" << std::endl;
system("pause");
}
void ManagePeople::show()
{
while (true)
{
showMenu();

switch(index)
{
case 1:
showStudent();
system("pause");
break;
case 2:
addStudent();
break;
case 3:
delStudent();
break;
case 4:
destroyManage();
break;
case 5:
exit(0);
}
}
}
renjianxin520 2009-11-10
  • 打赏
  • 举报
回复
没有细看你的程序,但是可能问题在类型匹配上
renjianxin520 2009-11-10
  • 打赏
  • 举报
回复
LZ的问题,貌似我了解
zgjxwl 2009-11-10
  • 打赏
  • 举报
回复
e
jzd8000 2009-11-10
  • 打赏
  • 举报
回复
你看着你的声明和实现不别扭?

friend istream &operator>>(istream &in,student &s);

istream &operator < <(istream &in,student &s)
{
in>>s.name>>s.num>>s.mathScore>>s.englishScore;
return in;
}
sandaobaqi 2009-11-10
  • 打赏
  • 举报
回复
代码一多我就晕,又不行了
flei_gu 2009-11-06
  • 打赏
  • 举报
回复
似曾相识,检察下类型匹配。指针什么的。我是在数据流里遇到过。
avalon 2009-11-05
  • 打赏
  • 举报
回复
晕死。我C++学得还不够啊。才看到常对象。搞得这个程序都看不懂!我要加油!
天地一棵树 2009-11-05
  • 打赏
  • 举报
回复
编译时出现这个错误
E:\shixi3\shixi3.cpp(40) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class ostream &(__cdecl *)(class ostream &)' (or there is no acceptable conversion)
给解释一下吧
butwang 2009-11-05
  • 打赏
  • 举报
回复
?
djh512 2009-11-05
  • 打赏
  • 举报
回复
JackLam 2009-11-05
  • 打赏
  • 举报
回复
万能的老师,我来拿分了

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧