作业11: 类_运算符重载

来源:互联网 发布:费舍尔数据 编辑:程序博客网 时间:2024/04/30 02:16

作业11:  类_运算符重载


1.设向量X = ( x1 ,x2 ,x3) 和 Y = ( y1 , y2 ,y3 ),则它们之间的加、减和积分别定义为:

X + Y = ( x1 + y1 , x2 + y2 , x3 + y3 )

X - Y = ( x1 - y1 , x2 - y2 , x3 - y3 )

X * Y = x1 * y1 + x2 * y2 + x3 * y3

编程序定义向量类vector,重载运算符“+”、“-”、“*”和“=”,实现向量之间的加、减、乘和赋值运算。用重载运算符“>>”、“<<”做向量的输入/输出操作。

 

2.定义一个类nauticalmile_kilometer,它包含两个数据成员kilometer(千米)和meter(米)。还包含一个构造函数对数据成员初始化;成员函数print用于输出数据成员kilometer和meter的值;类型转换函数double()实现把千米和米转换为海里(1海里=1.852千米)。编写main函数,测试类nauticalmile_kilometer。

 

3.教材p343页第3小题。

  定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如:c1+c2, i+c1, c1+i , 均合法(设i为整数,c1,c2 为复数)。编程序,分别求两个复数之和、整数和复数之和。


/************************************** 作业五 第1题  **************************************** 1.设向量X = ( x1 ,x2 ,x3) 和 Y = ( y1 , y2 ,y3 ),则它们之间的加、减和积分别定义为:X + Y = ( x1 + y1 , x2 + y2 , x3 + y3 ) X - Y = ( x1 - y1 , x2 - y2 , x3 - y3 )X * Y = x1 * y1 + x2 * y2 + x3 * y3编程序定义向量类vvector, 重载运算符"+"、"-" 、"*"和"=",实现向量之间的加、减、乘和赋值运算。用重载运算符">>"、"<<"做向量的输入/输出操作。**********************************************************************************************/// H5t1.cpp#include <iostream>#include <string>using namespace std;#include "vvector.h"int main(){vvector first;vvector second;char choice;// 定义string的menu时,=后面的内容直接用双引号“” string menu = "————————————\n" "| 请选择想要执行的运算 |\n" "|  1.两向量间的加法    |\n" "|  2.两向量间的减法    |\n" "|  3.两向量间的乘法    |\n" "|  4.我需要退出系统    |\n" "————————————\n" "我的选择是:";// strart to do-while circulatedo// do-while 循环里面,需要用{},在while后,得加上分号; {cout << menu;enteragain://此处定位,用于重新输入cin >> choice;cout << endl;switch(choice){case '1':{     cout << "第一个向量:" << endl;cin >> first;cout << "第二个向量:" << endl;cin >> second;cout << (first + second);break;}case '2':{        cout << "第一个向量:" << endl;cin >> first;cout << "第二个向量:" << endl;cin >> second;cout << (first - second);break;}case '3': {cout << "第一个向量:" << endl;cin >> first;cout << "第二个向量:" << endl;cin >> second;cout << (first * second);break;}case '4': break;default: {cout << "\n你的输入不正确,请重新输入:";if(1)goto enteragain;break;}    }    cout << "执行成功。" << endl << endl << endl << endl << "请继续:"  << endl;}while(choice != '4');// 不要忘了分号 ;  return 0;}

/************************************** 作业五 第1题  **************************************** 1.设向量X = ( x1 ,x2 ,x3) 和 Y = ( y1 , y2 ,y3 ),则它们之间的加、减和积分别定义为:X + Y = ( x1 + y1 , x2 + y2 , x3 + y3 ) X - Y = ( x1 - y1 , x2 - y2 , x3 - y3 )X * Y = x1 * y1 + x2 * y2 + x3 * y3编程序定义向量类vvector, 重载运算符"+"、"-" 、"*"和"=",实现向量之间的加、减、乘和赋值运算。用重载运算符">>"、"<<"做向量的输入/输出操作。**********************************************************************************************/// vvector.h#include <iostream.h>class vvector{private:double x;double y;double z;public:friend vvector operator + (const vvector&, const vvector&);friend vvector operator - (const vvector&, const vvector&);friend double operator * (const vvector&, const vvector&);friend istream& operator >> (istream& inp, vvector& in);friend ostream& operator << (ostream& outp, const vvector& ou);};// +vvector operator + (const vvector& f, const vvector& s){vvector re;cout << "( " << f.x << "+" << s.x << ", " << f.y << "+" << s.y << ", " << f.z << "+" << s.z << " ) = " ;re.x = f.x + s.x;re.y = f.y + s.y;re.z = f.z + s.z;return re;}// -vvector operator - (const vvector& f, const vvector& s){vvector re;cout << "( " << f.x << "-" << s.x << ", " << f.y << "-" << s.y << ", " << f.z << "-" << s.z << " ) = " ;re.x = f.x - s.x;re.y = f.y - s.y;re.z = f.z - s.z;return re;}// *double operator * (const vvector& f, const vvector& s){cout << f.x << "*" << s.x << " + " << f.y << "*" << s.y << " + " << f.z << "*" << s.z << " = " ;return double (f.x * s.x + f.y * s.y + f.z * s.z);}// >>istream& operator >> (istream& inp, vvector& in){cout << "请输入该向量的横坐标: ";inp >> in.x;cout << "\n请输入该向量的纵坐标:";inp >> in.y;cout << "\n请输入该向量的竖坐标:";inp >> in.z;cout << endl;return inp;} // <<ostream& operator << (ostream& outp, const vvector& ou){outp << "(" << ou.x  << ", " << ou.y  << ", " << ou.z << ")";    outp << endl << endl;     return outp; }


/*********************  作业五 第02题 ************************************2.定义一个类 nauticalmile_kilometer,它包含两个数据成员kilometer(千米)和meter(米)。还包含一个构造函数对数据成员初始化;成员函数print用于输出数据成员kilometer和meter的值;类型转换函数double()实现把千米和米转换为海里(mile)(1海里=1.852千米)。编写main函数,测试类nauticalmile_kilometer。****************************************************************************/// H5t2.cpp#include <iostream>#include "nk.h"using namespace std;// typename nauticalmile_kilometer nk;  // 用个别名,方便编写 int main(){  nauticalmile_kilometer m(18520, 0);   // 构造函数中千米的数据为0   nauticalmile_kilometer km(0, 18520);  // 构造函数中米的数据为0   nauticalmile_kilometer mkm(18520, 18520); // 构造函数中两者数据皆不为0  nauticalmile_kilometer nmkm(0, 0);      // 构造函数中两者数据皆为0  // explain cout << "该实验测试将类类型强制转换成double型。"      << "其中类类型包含了(千米、米)两个数据。 "      << endl << endl;   // 强制转换前的显示  cout << "强制转换前:" << endl << endl; cout << " 当构造函数中千米的数据为0时" << endl;  m.print(); cout << " 当构造函数中米的数据为0时" << endl;  km.print(); cout << " 当构造函数中两者数据皆不为0" << endl;  mkm.print(); cout << " 当构造函数中两者数据皆为0" << endl;  nmkm.print(); cout << endl << endl;   // 实行强制转换  (double) (m); (double) (km); (double) (mkm); (double) (nmkm);  // 强制显示后的显示 cout << "强制转换后:" << endl << endl; cout << " 当构造函数中千米的数据为0时" << endl;  m.print(); cout << " 当构造函数中米的数据为0时" << endl;  km.print(); cout << " 当构造函数中两者数据皆不为0" << endl;  mkm.print(); cout << " 当构造函数中两者数据皆为0" << endl;  nmkm.print(); cout << endl << endl;  while(2);   return 0;} 

/*********************  作业五 第02题 ************************************2.定义一个类 nauticalmile_kilometer,它包含两个数据成员kilometer(千米)和meter(米)。还包含一个构造函数对数据成员初始化;成员函数print用于输出数据成员kilometer和meter的值;类型转换函数double()实现把千米和米转换为海里(mile)(1海里=1.852千米)。编写main函数,测试类nauticalmile_kilometer。****************************************************************************/// nk.h -- to define a class nauticalmile_kilometer#include <iostream.h>class nauticalmile_kilometer{   private:    double meter;    double kilometer;      public:   int print();   nauticalmile_kilometer(double m = 0, double km = 0):meter(m), kilometer(km) {}             operator double();};// printint nauticalmile_kilometer::print(){ cout << "meter = " << meter << "\t"; cout << "kilometer = " << kilometer; cout << endl; return 0;}// operator typeName();nauticalmile_kilometer::operator double(){ // only kilometer if (meter == 0 && kilometer != 0) {    kilometer = kilometer / 1.825; return kilometer;} // only meter else if (meter != 0 && kilometer == 0) { meter = meter / 1000 / 1.825; return meter; } // kilometer && meter     else if (meter != 0 && kilometer != 0)    { meter = meter / 1000 / 1.825; kilometer = kilometer / 1.825;    return 0;}    // nothing to print , when both == 0 else return 0;}



/************************** 作业五 习题三 *********************************  定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算。 参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。 例如:c1+c2, i+c1, c1+i , 均合法(设i为整数,c1,c2 为复数)。 编程序,分别求两个复数之和、整数和复数之和。****************************************************************************/ // H5t3.cpp -- Complex  operation#include <iostream>using namespace std;#include "complex.h"int main(){ // definition complex c1; complex c2; complex c; int i;  // enter cout << "输入数据:" << endl << endl;  cout << "第一个复数:" << endl; c1.enter();cout << "第二个复数:" << endl;  c2.enter(); cout << "请输入一个整数:" ;  cin >> i; cout << endl << endl;  // display & calculate cout << "以下是运算结果:" << endl;cout << "1.两复数相加: "; c1.display();cout << " + ";  c2.display(); cout << " = ";    (c1 + c2).display();  cout << endl; cout << endl; cout << "2.复数与整数相加: " << endl; cout << i << " + "; c1.display(); cout << " = ";(i + c1).display();  cout << endl;  c2.display();cout << " + " << i ;cout << " = ";(c2 + i).display();cout << endl;  while(2); return 0;}

/************************** 作业五 习题三 *********************************  定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算。 参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。 例如:c1+c2, i+c1, c1+i , 均合法(设i为整数,c1,c2 为复数)。 编程序,分别求两个复数之和、整数和复数之和。****************************************************************************/// complex.h#include <iostream>class complex{ private:   double real;   double image; public:  void display();  void enter(); friend complex operator + (const complex& , const complex&); friend complex operator + (const complex& , const int& ); friend complex operator + (const int& , const complex& );};// c+ccomplex operator + (const complex& c1, const complex& c2){  complex c;  c.real = c1.real + c2.real;  c.image = c1.image + c2.image;  return c;}  // c+icomplex operator + (const complex& c, const int& i){  complex c1;  c1.real = c.real + double(i);  c1.image = 0;  return c1;}// i+ccomplex operator + (const int& i, const complex& c){  complex c1;  c1.real = c.real + double(i);  c1.image = 0;  return c1;}// displayvoid complex::display(){ cout << "( " << real << " + " << image << "i" << " )";}// entervoid complex::enter(){ cout << "请输入实部:" ; cin >> real; cout << "请输入虚部:" ;cin >> image;}