没有合适的资源?快使用搜索试试~ 我知道了~
第2次作业_57119101_王晨阳1
需积分: 0 0 下载量 139 浏览量
2022-08-08
17:52:02
上传
评论
收藏 75KB DOCX 举报
温馨提示
试读
15页
第2次作业_57119101_王晨阳1
资源详情
资源评论
资源推荐
EX1: Complex Class
Code
Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
#include<iostream>
class Complex
{
friend std::ostream &operator<<(std::ostream &,const Complex &);
friend std::istream &operator>>(std::istream &,Complex &);
public:
Complex(double=0.0,double=0.0);
Complex operator+(const Complex&) const;
Complex operator-(const Complex&) const;
Complex operator*(const Complex&) const;
Complex &operator=(const Complex&);
bool operator==(const Complex&) const;
bool operator!=(const Complex&) const;
private:
double r;
double i;
};
#endif
Complex.cpp
#include "Complex.h"
std::ostream &operator<<(std::ostream &output,const Complex &c)
{
output<<"("<<c.r<<","<<c.i<<")";
return output;
}
std::istream &operator>>(std::istream &input,Complex &c)
{
char cc;
input.ignore();
input>>c.r;
input.ignore();
input>>c.i;
input.ignore();
return input;
}
Complex::Complex(double real,double imaginary)
{
this->r=real;
this->i=imaginary;
}
Complex Complex::operator+(const Complex&c) const
{
Complex temp(this->r + c.r,this->i + c.i);
return temp;
}
Complex Complex::operator-(const Complex&c) const
{
Complex temp(this->r - c.r,this->i - c.i);
return temp;
}
Complex Complex::operator*(const Complex&c) const
{
Complex temp((this->r * c.r)-(this->i * c.i),
(this->r * c.i)+(this->i * c.r));
return temp;
}
Complex &Complex::operator=(const Complex&c)
{
this->r = c.r;
this->i = c.i;
return *this;
}
bool Complex::operator==(const Complex&c) const
{
return ((this->r == c.r)&&(this->i == c.i));
}
bool Complex::operator!=(const Complex&c) const
{
return ((this->r != c.r)||(this->i != c.i));
}
main.cpp
#include "Complex.h"
using std::cin;
using std::cout;
int main()
{
Complex x,y(4.3,8.2),z(3.3,1.1),k;
cout<<"Enter a complex number in the form (a,b): ";
cin>>k;
cout<<"x: "<<x<<"\ny: "<<y<<"\nz: "<<z<<"\nk: "<<k<<'\n';
x=y+z;
cout<<"\nx=y+z:\n"<<x<<"="<<y<<"+"<<z<<'\n';
x=y-z;
cout<<"\nx=y-z:\n"<<x<<"="<<y<<"-"<<z<<'\n';
x=y*z;
cout<<"\nx=y*z:\n"<<x<<"="<<y<<"*"<<z<<"\n\n";
if (x!=k)
cout<<x<<"!="<<k<<'\n';
cout<<'\n';
x=k;
if (x==k)
cout<<x<<"=="<<k<<'\n';
return 0;
}
Demo
EX2: Huge Integer Class
Code
Hugeint.h
#ifndef HUGEINT_H
#define HUGEINT_H
#include<iostream>
剩余14页未读,继续阅读
又可乐
- 粉丝: 62
- 资源: 309
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0