第八周实验报告1
来源:互联网 发布:淘宝申请部分金额退款 编辑:程序博客网 时间:2024/05/14 08:52
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 实现复数类中的运算符重载
* 作 者: 于宸
* 完成日期: 2012 年04 月08 日
* 版 本 号:1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:定义一个复数类重载运算符+、-、*、/,使之能用于复数的加减乘除。
* 程序输出:
* 程序头部的注释结束
*/
- #include<iostream>
- using namespace std;
- class Complex
- {
- public:
- Complex(){real = 0;imag = 0;}
- Complex(double r, double i){real = r; imag =i;}
- Complex operator+(Complex &c2);//声明重载运算符"+"
- Complex operator-(Complex &c2);//声明重载运算符"-"
- Complex operator*(Complex &c2);//声明重载运算符"*"
- Complex operator/(Complex &c2);//声明重载运算符"/"
- void display();
- private:
- double real;
- double imag;
- };
- Complex Complex::operator+(Complex &c2)//定义重载运算符"+"
- {
- Complex c;
- c.real = real + c2.real;
- c.imag = imag + c2.imag;
- return c;
- }
- Complex Complex::operator-(Complex &c2)//定义重载运算符"-"
- {
- Complex c;
- c.real = real - c2.real;
- c.imag = imag - c2.imag;
- return c;
- }
- Complex Complex::operator*(Complex &c2)//定义重载运算符"*"
- {
- Complex c;
- c.real = real * c2.real;
- c.imag = imag * c2.imag;
- return c;
- }
- Complex Complex::operator/(Complex &c2)//定义重载运算符"/"
- {
- Complex c;
- c.real = real / c2.real;
- c.imag = imag / c2.imag;
- return c;
- }
- void Complex::display()
- {
- cout << "(" << real << "," << imag << "i)" << endl;
- }
- int main()
- {
- Complex c1(3,4),c2(5,-10),c3;
- cout<<"c1=";
- c1.display();
- cout<<"c2=";
- c2.display();
- c3=c1+c2;
- cout<<"c1+c2=";
- c3.display();
- c3=c1-c2;
- cout<<"c1-c2=";
- c3.display();
- c3=c1*c2;
- cout<<"c1*c2=";
- c3.display();
- c3=c1/c2;
- cout<<"c1/c2=";
- c3.display();
- system("pause");
- return 0;
- }
- #include<iostream.h>
- //using namespace std;
- class Complex
- {
- public:
- Complex(){real = 0;imag = 0;}
- Complex(double r, double i){real = r; imag =i;}
- friend Complex operator+(Complex &c1, Complex &c2);
- friend Complex operator-(Complex &c1, Complex &c2);
- friend Complex operator*(Complex &c1, Complex &c2);
- friend Complex operator/(Complex &c1, Complex &c2);
- void display();
- private:
- double real;
- double imag;
- };
- Complex operator+(Complex &c1, Complex &c2)
- {
- return Complex(c1.real + c2.real, c1.imag + c2.imag);
- }
- Complex operator-(Complex &c1, Complex &c2)
- {
- return Complex(c1.real - c2.real, c1.imag - c2.imag);
- }
- Complex operator*(Complex &c1, Complex &c2)
- {
- return Complex(c1.real * c2.real, c1.imag * c2.imag);
- }
- Complex operator/(Complex &c1, Complex &c2)
- {
- return Complex(c1.real / c2.real, c1.imag / c2.imag);
- }
- void Complex::display()
- {
- cout << "(" << real << "," << imag << "i)" << endl;
- }
- int main()
- {
- Complex c1(3,4),c2(5,-10),c3;
- cout<<"c1=";
- c1.display();
- cout<<"c2=";
- c2.display();
- c3=c1+c2;
- cout<<"c1+c2=";
- c3.display();
- c3=c1-c2;
- cout<<"c1-c2=";
- c3.display();
- c3=c1*c2;
- cout<<"c1*c2=";
- c3.display();
- c3=c1/c2;
- cout<<"c1/c2=";
- c3.display();
- return 0;
- }
- #include<iostream.h>
- //using namespace std;
- class Complex
- {
- public:
- Complex(){real = 0;imag = 0;}
- Complex(double r, double i){real = r; imag =i;}
- friend Complex operator +(Complex &c, int a);
- friend Complex operator -(Complex &c, int a);
- friend Complex operator *(Complex &c, int a);
- friend Complex operator /(Complex &c, int a);
- void display();
- private:
- double real;
- double imag;
- };
- Complex operator +(Complex &c, int a)
- {
- return Complex(c.real + a, c.imag);
- }
- Complex operator -(Complex &c, int a)
- {
- return Complex(c.real - a, c.imag);
- }
- Complex operator*(Complex &c, int a)
- {
- return Complex(a * c.real, a * c.imag);
- }
- Complex operator/(Complex &c, int a)
- {
- return Complex(c.real / a, c.imag / a);
- }
- void Complex::display()
- {
- cout << "(" << real << "," << imag << "i)" << endl;
- }
- int main()
- {
- Complex c1(4.0, 5.0), c2(2.0, 2.0), c3;
- int a = 2;
- cout << "c1 = ";
- c1.display();
- cout << "c2 = ";
- c2.display();
- c3 = c2 + 2;
- cout<< "c2 + 2 = ";
- c3.display();
- c3 = c2 - 2;
- cout<< "c2 - 2 = ";
- c3.display();
- c3 = c2 * 2;
- cout<< "c2 * 2 = ";
- c3.display();
- c3 = c2 / 2;
- cout<< "c2 / 2 = ";
- c3.display();
- return 0;
- }
- 第八周实验报告1
- 第八周实验报告1
- 第八周实验报告1
- 第八周实验报告1
- 第八周实验报告1
- 第八周实验报告
- 第八周实验报告
- 第八周实验报告
- 第八周实验报告
- 第八周实验报告(1-1)
- 第八周实验报告1-1
- 第八周实验报告(1-1)
- 《第八周实验报告任务1》
- 第八周实验报告(1)
- 第八周实验报告(1-2)
- 第八周实验报告1-2
- 第八周实验报告(1-3)
- 第八周实验报告(1)
- Windows Server下查看当前登录用户命令
- 如何在VS2010环境下编译C++程序
- js异常
- apache + tomcate 服务器架构
- 子类对象的实例化过程
- 第八周实验报告1
- cmake
- 网路编程-TCP-Socket
- Spring HttpInvoker(一)
- 编译 调试 练习
- 如何动态改变input type=submit 的type属性为button
- 关于VC维的整理
- Android项目源码混淆问题解决方法
- 类的继承