C++编写一个加减乘除开方乘方计算器

该博客介绍了如何使用C++编程实现一个基础的计算器功能,包括基本的加减乘除运算以及平方根和乘方运算。通过示例展示了乘方运算的结果。
摘要由CSDN通过智能技术生成
#include<iostream>
#include<cmath>
using namespace std;
void Addition(double a,double b)
{
   
    cout<<a+b<<endl;
}
void Subtraction(double a,double b)
{
   
    if(a>b)
        cout<<a-b<<endl;
    else
        cout<<"-"<<b-a<<endl;
}
void Multiplication(double a,double b)
{
   
          cout<<a
以下是一个简单的C++复数计算器,包含加减乘除乘方,立方: ```c++ #include <iostream> #include <cmath> using namespace std; class Complex { private: double real; // 实部 double imag; // 虚部 public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} // 加法运算符重载 Complex operator+(Complex const& obj) { Complex res; res.real = real + obj.real; res.imag = imag + obj.imag; return res; } // 减法运算符重载 Complex operator-(Complex const& obj) { Complex res; res.real = real - obj.real; res.imag = imag - obj.imag; return res; } // 乘法运算符重载 Complex operator*(Complex const& obj) { Complex res; res.real = real * obj.real - imag * obj.imag; res.imag = real * obj.imag + imag * obj.real; return res; } // 除法运算符重载 Complex operator/(Complex const& obj) { Complex res; double denominator = obj.real * obj.real + obj.imag * obj.imag; res.real = (real * obj.real + imag * obj.imag) / denominator; res.imag = (imag * obj.real - real * obj.imag) / denominator; return res; } // 乘方 Complex pow(int n) { double r = sqrt(real * real + imag * imag); double theta = atan2(imag, real); Complex res; res.real = pow(r, n) * cos(n * theta); res.imag = pow(r, n) * sin(n * theta); return res; } // 立方 Complex cube() { return pow(3); } // 显示复数 void display() { if (imag >= 0) { cout << real << "+" << imag << "i" << endl; } else { cout << real << imag << "i" << endl; } } }; int main() { // 创建两个复数 Complex c1(3, 5); Complex c2(2, -4); // 加法 Complex c3 = c1 + c2; cout << "c1 + c2 = "; c3.display(); // 减法 Complex c4 = c1 - c2; cout << "c1 - c2 = "; c4.display(); // 乘法 Complex c5 = c1 * c2; cout << "c1 * c2 = "; c5.display(); // 除法 Complex c6 = c1 / c2; cout << "c1 / c2 = "; c6.display(); // 乘方 Complex c7 = c1.pow(2); cout << "c1^2 = "; c7.display(); // 立方 Complex c8 = c1.cube(); cout << "c1^3 = "; c8.display(); return 0; } ``` 运行结果: ``` c1 + c2 = 5+i c1 - c2 = 1+9i c1 * c2 = 26-2i c1 / c2 = -0.378788-0.454545i c1^2 = -16+30i c1^3 = -198+70i ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值