C++程序设计-第9周 运算符的重载(之二)
来源:互联网 发布:js点击再点击 编辑:程序博客网 时间:2024/06/05 13:21
课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565
本周目标是:1. 学会定义类中>>和<<运算符的重载函数;2. 理解通过运算符的重载解决实际问题的方法
【上机项目】
项目1-3直接复制第8周的代码,加入要求的运算符重载定义后,作为第9周的项目另外发博文。
【项目1-Complex类】接第8周项目1,定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
【项目2-Time类】接第8周项目2,定义Time类中的<<和>>运算符重载,实现时间的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
【项目3-分数类】接第8周项目3,定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
【项目4-一元一次方程类】设计一元一次方程类,求形如ax+b=0的方程的解。
例如:输入3x-8=0时,输出的方程的解为x=2.66667;
再如:输入5s+18=0时,输出的方程的解为s=-3.6;
class CEquation{private:double a; // 未知数系数double b; // 常数项char unknown; // 未知数的符号public:CEquation(double aa=0,double bb=0); friend istream &operator >> (istream &in,CEquation &e);friend ostream &operator << (ostream &out,CEquation &e);double Solve(); //返回解方程的结果char getUnknown(); //返回未知数用什么符号};int main(){CEquation e;cout<<"请输入一元一次方程(输入格式:3x-8=0):";cin>>e; //在两次测试中,分别输入3x-8=0和5s+18=0cout<<"方程为:"<<e;cout<<"方程的解为:"<<e.getUnknown()<<"="<<e.Solve()<<endl; //对两次测试,分别输出x=...和s=...e.Solve();return 0;}
【项目5(选做)-二维数组类Douary】建立一个二维数组类Douary,使该类中有以下数据成员、成员函数及友员函数,完成矩阵的输入、输出、加、减、相等判断等操作。
class Douary{public:Douary(int m, int n);//构造函数:用于建立动态数组存放m行n列的二维数组(矩阵)元素,并将该数组元素初始化为0Douary::Douary(const Douary &d); //因为有指针数据成员,复制构造函数需要定义~Douary(); //析构函数:用于释放动态数组所占用的存储空间。friend istream &operator>>(istream &input, Douary &d);//重载运算符“>>”输入二维数组friend ostream &operator<<(ostream &output, Douary &d);//重载运算符“<<”以m行n列形式输出friend Douary operator+(const Douary &d1,const Douary &d2);//两个矩阵相加,规则:对应位置上的元素相加friend Douary operator-(const Douary &d1,const Douary &d2);//两个矩阵相减,规则:对应位置上的元素相减bool operator==(const Douary &d);//判断两个矩阵是否相等,即对应位置上的所有元素是否相等private:int *Array; //Array 为动态数组指针,构造函数中分配空间用Array = new int[row*col];int row; //row 为二维数组的行数。int col; //col 为二维数组的列数。}int main(){Douary d1(2,3),d2(2,3)cout<<"输入d1:"<<endl; cin>>d1;cout<<"输入d2:"<<endl; cin>>d2;coutt<<"d1="<<endl; cout<<d1;coutt<<"d2="<<endl; cout<<d2;coutt<<"d1+d2="<<endl; cout<<(d1+d2);coutt<<"d1-d2="<<endl;cout<<(d1-d2);cout<<"d1"<<((d1==d2)?"==":"!=")<<"d2"<<endl;return 0;}
【程序阅读】阅读下面的程序,找出并标明其中出现构造函数、友元函数、运算符重载、静态数据成员等语法现象的位置,仔细体会其用法,在以后的设计中能够灵活应用有关方法和技巧
#include <iostream>#include <Cstring>using namespace std;#define MAX 100class CDate // 定义日期类{private:unsigned short int year; // 年unsigned short int month; // 月unsigned short int day; // 日public:CDate(int y=0,int m=0,int d=0);bool operator < (CDate d);friend istream & operator >> (istream &in,CDate &d);friend ostream & operator<<(ostream &out,CDate &d);friend bool CheckValid(CDate d);friend bool LeapYear(int year);void SetDate(int y,int m,int d);};CDate::CDate(int y,int m,int d):year(y),month(m),day(d){}// 设置日期void CDate::SetDate(int y,int m,int d){year=y;month=m;day=d;}// 重载输入运算符>>istream &operator>>(istream &in,CDate &d){char ch1,ch2;cout<<"请输入日期(输入格式:-3-12):";while(1){cin>>d.year>>ch1>>d.month>>ch2>>d.day;if (ch1=='-' && ch2=='-')if (CheckValid(d)) break;cerr<<"时间格式不正确! 请重新输入\n";}return cin;}// 重载输出运算符<<ostream &operator<<(ostream &out,CDate &d){out<<d.year<<"年"<<d.month<<"月"<<d.day<<"日";return out;}// 判断日期d1<d2bool CDate::operator < (CDate d){if (year<d.year) return true;if (year>d.year) return false;if (month<d.month) return true;if (month>d.month) return false;if (day<d.day) return true;return false;}// 检查日期合法性bool CheckValid(CDate d){int n;if (d.month<1 || d.month>12) return false;if (d.day<1) return false;n=31;switch(d.month){case 2:if (LeapYear(d.year)) n=29;else n=28;break;case 4:case 6:case 9:case 11:n=30;break;}if (d.day>n) return false;return true;}// 检查是否为闰年bool LeapYear(int year){if (year%4==0 && year%100 || year%400==0) return true;return false;}class CStudent{private:char *name; // 姓名bool sex; // 性别CDate date; // 出生日期,类对象作数据成员public:static int num; // 学生人数CStudent();void InputData();friend void Sort(); // 排序friend void FindName(); // 按姓名查询friend void Statistic(); // 按性别统计friend void Display(); // 显示全部信息} stu[MAX];int CStudent::num=0;CStudent::CStudent(){}// 输入信息void CStudent::InputData(){int p;char s[41];cout<<"请输入学生信息(NO."<<num<<"):\n";cout<<"姓名:";cin>>s;name=new char[strlen(s)+1];strcpy(name,s);cout<<"性别(1-男,-女):";cin>>p;if (p) sex=true;else sex=false;cin>>date;cout<<endl;}// 排序void Sort(){int i,j,p,num;char *tn;bool ts;CDate td;num=CStudent::num;for(i=1;i<num;i++){p=i;for(j=i+1;j<=num;j++)if (stu[j].date<stu[p].date) p=j;//找到当前未排序元素中年龄最小的对象的下标if (p==i) continue;//下面交换stu[i]和stu[p]tn=stu[i].name; stu[i].name=stu[p].name; stu[p].name=tn;ts=stu[i].sex; stu[i].sex=stu[p].sex; stu[p].sex=ts;td=stu[i].date; stu[i].date=stu[p].date; stu[p].date=td;}}// 按姓名查询void FindName(){char name[41];int i,num;cout<<"请输入姓名:";cin>>name;num=CStudent::num;for(i=1;i<=num;i++)if (strcmp(stu[i].name,name)==0) break;if (i>num){cout<<"查无此人!"<<endl<<endl;return;}//如果查到了,显示学生信息cout<<"姓名:"<<stu[i].name<<endl;cout<<"性别:";if (stu[i].sex) cout<<"男"<<endl;else cout<<"女"<<endl;cout<<"生日:"<<stu[i].date<<endl;cout<<endl;}// 按性别统计void Statistic(){int i,num,s1,s0;num=CStudent::num;s1=0;s0=0;for(i=1;i<=num;i++)if (stu[i].sex==1)s1++;elses0++;cout<<"男生人数:"<<s1<<endl;cout<<"女生人数:"<<s0<<endl;cout<<endl;}// 显示全部信息void Display(){int i,num;num=CStudent::num;for(i=1;i<=num;i++){cout<<stu[i].name<<" ";if (stu[i].sex) cout<<"男";else cout<<"女";cout<<stu[i].date<<endl;}cout<<endl;}void main(){char *menu[]={ "","输入信息","排序","按姓名查询","按性别统计","显示全部信息","退出" };int i,p;bool end;end=false;while(!end){for(i=1;i<7;i++)cout<<i<<" "<<menu[i]<<endl;cin>>p;switch(p){case 1: // 输入信息CStudent::num++;stu[CStudent::num].InputData(); break;case 2: // 排序Sort(); break;case 3: // 按姓名查询FindName(); break;case 4: // 按性别统计人数Statistic(); break;case 5: // 显示全部信息Display(); break;case 6: // 退出end=true; break;}}}
- C++程序设计-第9周 运算符的重载(之二)
- C++程序设计-第8周 运算符的重载
- C02-程序设计基础提高班(C++)第11周上机任务-运算符重载
- 第9周-运算符重载(二)-复数类中的运算符重载(续)
- 运算符判定(C程序设计进阶第9周)
- C++程序设计(9)之运算符重载
- 第9周-运算符重载(二)-Time类中运算符重载(续)
- 第9周-运算符重载(二)-分数类中的运算符重载(续)
- 第9周-运算符重载(二)-方程也是类
- 第9周-运算符重载(二)-我的向量类
- 第12周项目二—Time类的运算符重载9
- 多态性之运算符重载(二)
- 2013级C++第9周(春)项目——运算符重载(二)
- 运算符重载编程题1(C++程序设计第4周)
- 运算符重载编程题2(C++程序设计第4周)
- 运算符重载编程题3(C++程序设计第4周)
- 从零开始学C++之运算符重载(二):++运算符重载、!运算符重载、赋值运算符重载
- 从零开始学C++之运算符重载(二):++运算符重载、!运算符重载、赋值运算符重载
- 如何测试洗牌程序
- c语言的几个重要知识点
- poj搜索题目
- 5.2
- 关于oracle length()函数的问题 笔记
- C++程序设计-第9周 运算符的重载(之二)
- 编译性语言、脚本语言的区别
- MapReduce:详解Shuffle(copy,sort,merge)过程
- 【备忘】Android模拟小球自由落体(SurfaceView)
- C++ 编译搜索路径(包括include搜索路径和连接搜索路径
- eclipse如何修改dynamic web module version
- SG函数
- uva 10387 - Billiard
- C/C++程序员常用工具集