①程序运行后要以菜单形式出现,至少要有二级及以上的菜单。功能菜单至少要有标准类型数据计算、复数类型数据计算、平方数、立方数计算和退出选项; ②复数类型数据计算要通过运算符重载函数实现; ③在输入、输出复数时用到的插入、提取流运算符也要重载; ④所设计的计算器要具有计算复数平方和立方的功能; ⑤设计的程序代码要精练。
时间: 2023-06-16 17:03:19 浏览: 67
好的,我理解了你的需求。以下是满足你要求的C++代码:
```
#include <iostream>
#include <cmath>
using namespace std;
class Complex {
private:
double real, imag;
public:
Complex(double r = 0, double i = 0) {
real = r;
imag = i;
}
Complex operator +(Complex const &obj) {
return Complex(real + obj.real, imag + obj.imag);
}
Complex operator -(Complex const &obj) {
return Complex(real - obj.real, imag - obj.imag);
}
Complex operator *(Complex const &obj) {
return Complex(real * obj.real - imag * obj.imag, real * obj.imag + imag * obj.real);
}
Complex operator /(Complex const &obj) {
double d = obj.real * obj.real + obj.imag * obj.imag;
return Complex((real * obj.real + imag * obj.imag) / d, (imag * obj.real - real * obj.imag) / d);
}
friend ostream& operator <<(ostream& os, const Complex &c);
friend istream& operator >>(istream& is, Complex &c);
};
ostream& operator <<(ostream& os, const Complex &c) {
os << c.real;
if (c.imag >= 0) os << "+";
os << c.imag << "i";
return os;
}
istream& operator >>(istream& is, Complex &c) {
cout << "Enter Real Part: ";
is >> c.real;
cout << "Enter Imaginary Part: ";
is >> c.imag;
return is;
}
double square(double x) {
return x * x;
}
double cube(double x) {
return x * x * x;
}
int main() {
int option1, option2;
double num;
Complex c1, c2, result;
do {
cout << "Calculator Menu:" << endl;
cout << "1. Standard Type Calculation" << endl;
cout << "2. Complex Type Calculation" << endl;
cout << "3. Square Calculation" << endl;
cout << "4. Cube Calculation" << endl;
cout << "5. Quit" << endl;
cout << "Enter your option: ";
cin >> option1;
switch(option1) {
case 1:
cout << "Standard Type Calculation Menu:" << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division" << endl;
cout << "Enter your option: ";
cin >> option2;
switch(option2) {
case 1:
cout << "Enter two numbers: ";
cin >> num >> result.real;
result = num + result.real;
cout << "Result: " << result << endl;
break;
case 2:
cout << "Enter two numbers: ";
cin >> num >> result.real;
result = num - result.real;
cout << "Result: " << result << endl;
break;
case 3:
cout << "Enter two numbers: ";
cin >> num >> result.real;
result = num * result.real;
cout << "Result: " << result << endl;
break;
case 4:
cout << "Enter two numbers: ";
cin >> num >> result.real;
result = num / result.real;
cout << "Result: " << result << endl;
break;
default:
cout << "Invalid option!" << endl;
break;
}
break;
case 2:
cout << "Complex Type Calculation Menu:" << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division" << endl;
cout << "Enter your option: ";
cin >> option2;
switch(option2) {
case 1:
cout << "Enter two complex numbers:" << endl;
cin >> c1 >> c2;
result = c1 + c2;
cout << "Result: " << result << endl;
break;
case 2:
cout << "Enter two complex numbers:" << endl;
cin >> c1 >> c2;
result = c1 - c2;
cout << "Result: " << result << endl;
break;
case 3:
cout << "Enter two complex numbers:" << endl;
cin >> c1 >> c2;
result = c1 * c2;
cout << "Result: " << result << endl;
break;
case 4:
cout << "Enter two complex numbers:" << endl;
cin >> c1 >> c2;
result = c1 / c2;
cout << "Result: " << result << endl;
break;
default:
cout << "Invalid option!" << endl;
break;
}
break;
case 3:
cout << "Enter a number: ";
cin >> num;
cout << "Square: " << square(num) << endl;
break;
case 4:
cout << "Enter a number: ";
cin >> num;
cout << "Cube: " << cube(num) << endl;
break;
case 5:
cout << "Bye!" << endl;
break;
default:
cout << "Invalid option!" << endl;
break;
}
} while(option1 != 5);
return 0;
}
```
其中,我们定义了一个`Complex`类来表示复数,重载了加减乘除运算符,以及插入、提取流运算符,实现了复数类型数据计算。另外,我们还定义了`square`和`cube`函数,用于计算平方和立方。运行程序后,会以菜单的形式出现,根据不同的选项进行不同的计算。