c++实现复数计算器

复数计算器

1)由输入的实部和虚部生成一个复数;2)求两个复数的和;3)求两个复数的差;4)求两个复数的乘积;5)求复数的实部;6)求复数的虚部 


代码实现:

#define  _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cmath>
using namespace std;
struct Compiex
{
	double real;
	double imag;
};
void mune()
{
	printf("*************************************\n");
	printf("********1.使用加法    2.使用减法*****\n");
	printf("********3.使用乘法    4.仅求实部*****\n");
	printf("********5.仅求虚部    0.退出计算器***\n");
	printf("*************************************\n");
}
Compiex my_add(Compiex a1, Compiex a2)//实现复数加法
{
	Compiex b = { 0 };
	b.real = a1.real + a2.real;
	b.imag = a1.imag + a2.imag;
	return b;

}

Compiex my_sub(Compiex a1, Compiex a2)//实现复数减法
{
	Compiex b = { 0 };
	b.real = a1.real - a2.real;
	b.imag = a1.imag - a2.imag;
	return b;

}

Compiex my_mul(Compiex a1, Compiex a2)//实现复数乘法
{
	Compiex b = { 0 };
	b.real = a1.real * a2.real-a1.imag*a2.imag;
	b.imag = a1.imag*a2.imag + a2.real*a1.imag;
	return b;
}

int main()
{
	Compiex a1 = { 0 };
	Compiex a2 = { 0 };
	int input = 0;
	do
	{
		mune();
		printf("请输入\n");
		scanf("%d", &input);
		system("cls");
		switch (input)
		{
		case 1:
		{
			printf("开始实现加法\n");
			cout << "请分别输入两个数的实部和虚部";
			cin >> a1.real >> a1.imag;
			cin >> a2.real >> a2.imag;
			cout << "通过加法所得的复数为" << my_add(a1, a2).real << '+' <<  '(' << my_add(a1, a2).imag << 'i' <<')' << endl;
			break;
		}
		case 2:
		{
			printf("开始实现减法\n");
			cout << "请分别输入两个数的实部和虚部";
			cin >> a1.real >> a1.imag;
			cin >> a2.real >> a2.imag;
			cout << "通过减法所得的复数为" << my_sub(a1, a2).real << '+' <<'(' << my_sub(a1, a2).imag << 'i' <<')' <<  endl;
			break;
		}
		case 3:
		{
			printf("开始实现乘法\n");
			cout << "请分别输入两个数的实部和虚部";
			cin >> a1.real >> a1.imag;
			cin >> a2.real >> a2.imag;
			cout << "通过乘法所得的复数为" << my_mul(a1, a2).real << '+' << '(' << my_mul(a1, a2).imag << 'i' << ')' << endl;
			break;
		}
		case 4:
		{
			printf("开始实现求复数的实部\n");
			cout << "请分别输入两个数的实部和虚部";
			cin >> a1.real >> a1.imag;
			cin >> a2.real >> a2.imag;
			cout << "复数的实部分别为" << a1.real << "和" << a2.real << endl;
			break;
		}
		case 5:
		{
			printf("开始实现求复数的虚部\n");
			cout << "请分别输入两个数的实部和虚部";
			cin >> a1.real >> a1.imag;
			cin >> a2.real >> a2.imag;
			cout << "复数的虚部分别为" << a1.imag << 'i' << "和" << a2.imag << 'i' << endl;
			break;
		}
		case 0:
		{
			printf("退出成功\n");
			break;
		}
		default:
			printf("输入错误请重新输入\n");
			break;
		}

	} while (input);

	return 0;
}

运行结果: 

  • 21
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include #include #include #include #include #include #define EPS 1e-5 //定义精度常数 using namespace std; //使用标准空间命名std namespace NameCComplex //定义命名空间NameCComplex { class CComplex ////定义一个CComplex类 { private: double Real,Image; public: CComplex(double real=0,double image=0) //构造函数 { Real=real; Image=image; } friend istream & operator>>(istream &is,CComplex &com); //重载输入 friend ostream & operator<(CComplex &com); int operator(CComplex &com) //重载运算符">",比较模的大小 { if(mod()>com.mod()) return 1; else return 0; } int CComplex::operator<(CComplex &com) { if(mod()>(istream &is,CComplex &com) //重载输入,可以输入a+bi的形式 { cout<>s; //用字符串的形式接受复数 int len=strlen(s); //求出字符串的长度 int n=0,sign=1; //n为当前从字符串中提取出来的数字,初始化为0;sign是难道符号,初始化为正 com.Image=com.Real=0; for(int k=0;k<len;k++) //判断接受的字符串是否合法 { if((s[k] '9') && (s[k]!='+' && s[k]!='-' && s[k]!='i')) { cout<<"error"<<endl; return is; //错误,输出出错信息并返回 } } for(k=0;k<len;) //顺序识别字符串中各字符 { if(n!=0 &&(s[k]=='-'||s[k]=='+')) //当前字符是否是符号位 { com.Real=sign*n; //是符号位,且n!=0,即n已被赋值,表明当前读取的是虚部的符号 n=0; //将原n*sign值赋给实部,将n清零,准备接受虚部的值 } if(s[k]=='-') //当前字符为负号 { sign=-1;k++; //给符号标志赋值 } if(s[k]=='+') //当前字符为正号 { sign=1;k++; //给符号标志赋值 } if(s[k]=='i') //当前字符为'I' { if(k!=len-1) //判断字符'I'是否为字符串中作后一个字符 cout<='0' && s[k]<='9') //当前字符在0~9之间,将数字字符转换成数字数值 { n=n*10+s[k]-'0'; k++; } } if(s[len-1]!='i' && n!=0) //如果最后一个字符不是'I',表示复数对象内只有实部,没有虚部 { com.Real=n*sign; } return is; } ostream & operator<<(ostream &os,CComplex &com) //重载输入 { if(fabs(com.Image)<EPS) // 如果虚部为0 os<<com.Real; //只输出实部 else if((fabs(com.Real)<EPS)) //如果实部为0 os<<com.Image<0) os<<com.Real<<"+"<<com.Image<<"i"; else os<<com.Real<<com.Image<<"i"; //虚部为正 return os; } CComplex CComplex::operator+(CComplex &com) //加法重载 { CComplex sum; sum.Real=Real+com.Real; //实部相加 sum.Image=Image+com.Image; //虚部相加 return sum; } CComplex CComplex::operator*(CComplex &com) //乘法重载 { CComplex multi; multi.Real=Real*com.Real-Image*com.Image; //乘积实部 multi.Image=Real*com.Image+Image*com.Real; //乘积虚部 return multi; } CComplex CComplex::operator-(CComplex &com) //减法重载 { CComplex sub; sub.Real=Real-com.Real; sub.Image=Image-com.Image; return sub; } CComplex CComplex::operator+=(CComplex &com) //重载加法赋值 { Real=Real+com.Real; Image=Image+com.Image; return *this; } CComplex CComplex::operator-=(CComplex &com) //重载减法赋值 { Real=Real-com.Real; Image=Image-com.Image; return *this; } CComplex CComplex::operator*=(CComplex &com) //重载乘法赋值 { double nReal=Real*com.Real-Image*com.Image; double nImage=Real*com.Image+Image*com.Real; Real=nReal; Image=nImage; return *this; } int CComplex::operator==(CComplex &com) //重载等于 { if(Real==com.Real && Image==com.Image) return 1; else return 0; } void Test(void) //测试函数 { user.nTest++; cout<<"共10道题,做100以内的加减运算,满分100分:\n"; double real1,real2,image1,image2,real3,real4,image3,image4; CComplex answer,temp; int score=0; char op; for(int i=0;i<=9;i++) { /////为复数产生随机值 real1=rand()%200-100; image1=rand()%200-100; real2=rand()%200-100; image2=rand()%200-100; CComplex a(real1,image1),b(real2,image2); real3=rand()%20-10; image3=rand()%20-10; real4=rand()%20-10; image4=rand()%20-10; CComplex c(real3,image3),d(real4,image4); op=rand()%3; //产生随机加减乘法运算的三个值 switch(op) { case 0: answer=a+b; cout<<a<<"加上"<<b<<"等于"; break; case 1: answer=a-b; cout<<a<<"减去"<<b<<"等于"; break; case 2: answer=c*d; cout<<c<<"乘以"<<d<>temp; //输入用户计算值 if(answer==temp) //比较用户计算值 { score+=10; } else { cout<<"此题做错了\n"; cout<<"正确答案为:"<<answer<<endl; } } cout<<"你的最后得分是:"<<score<<endl; if(user.nTest<=3) { user.alAve=0; user.dlScore[user.nTest-1]=score; for(int i=0;i<user.nTest;i++) user.alAve+=user.dlScore[i]; user.alAve=user.alAve/user.nTest; } else { user.dlScore[0]=user.dlScore[1]; user.dlScore[1]=user.dlScore[2]; user.dlScore[2]=score; for(i=0,user.alAve=0;i<3;i++) user.alAve+=user.dlScore[i]; user.alAve=user.alAve/3; } cout<<"请按任意键继续\n"; cout.flush(); cin.get(); cin.get(); } void Add() //复数加法运算函数 { user.nAdd++; CComplex num1,num2,sum,Zero(0,0); cout<<"加法计算\n"<<"最少输入两个复数,并且以0结束\n"; cout<>num1; cout<>num2; sum=num1+num2; cout<>num1; int i=4; while(!(num1==Zero)) { sum=sum+num1; cout<<"第"<<i<>num1; i++; } cout<<"加法结果是:"<<sum<<endl; cout<<"请按任意键继续\n"; cout.flush(); cin.get(); cin.get(); } void Sub() //复数减法预算函数 { user.nSub++; CComplex num1,num2,sub,Zero(0,0); cout<<"最少输入两个复数,并且以0结束\n"; cout<>num1; cout<>num2; sub=num1-num2; cout<>num1; int i=4; while(!(num1==Zero)) { sub=sub-num1; cout<<"第"<<i<>num1; i++; } cout<<"减法结果是:"<<sub<<endl; cout<<"请按任意键继续\n"; cout.flush(); cin.get(); cin.get(); } void Mul() //复数乘积函数 { user.nMul++; CComplex num1,num2,mul,Zero(0,0); cout<<"乘法计算\n"<<"最少输入两个复数,并且以零结束\n"; cout<>num1; cout<>num2; mul=num1*num2; cout<>num1; int i=4; while(!(num1==Zero)) { mul*=num1; cout<<"第"<<i<>num1; i++; } cout<<"乘法结果是:"<<mul<<endl; cout<>num1; ++num1; cout<<"自加的结果为"<<num1<<endl; cout<>num1; --num1; cout<<"自减的结果为"<<num1<<endl; cout<<"按任意键结束\n"; cout.flush(); cin.get(); cin.get(); } void compare() //两复数比较函数 { CComplex num1,num2; cout<<"输入两个复数\n"; cout<>num1; cout<>num2; if(num1==num2) cout<num2) cout<<num1<<"的模大于"<<num2<<"的模\n"; else if(num1<num2) cout<<num2<<"的模大于"<<num1<<"的模\n"; else cout<<"这两个复数的模相等\n"; cout<<"按任意键继续\n"; cin.get(); cin.get(); } void userprint() //输出用户信息函数 { cout<<user.szName<<"使用的次数为:"<<user.nTime<<endl; cout<<"其中:\t加法的次数:"<<user.nAdd<<"\t减法的次数:"<<user.nSub<<"\t乘法的次数:"<<user.nMul<<endl; cout<<"\t测试次数:"<<user.nTest<<"\t平均成绩:"<<user.alAve<<endl; } void Login() //当前用户信息函数 { char szName[20]; cout<<"请输入您的姓名:"; cin.getline(szName,20); ifstream infile; User user1; infile.open("user.dat",ios::binary|ios::in); if(!infile) { cout<<"没有原始记录文件,您是第一个用户!\n"; strcpy(user.szName,szName); user.nTest++; return; } infile.read((char *)&user1,sizeof(User)); while(!infile.eof()) { if(strcmp(user1.szName,szName)==0) { user=user1; user.nTime++; cout<<"欢迎您再次使用复数计算器!"; userprint(); cin.get(); infile.close(); return; } infile.read((char *) &user1,sizeof(User)); } cout<<"欢迎您再次使用复数计算器!"; strcpy(user.szName,szName); user.nTime++; infile.close(); return; } void SaveFile() //用户资料保存函数 { userprint(); fstream file; User user1; file.open("user.dat",ios::binary|ios::in|ios::out); if(!file) { cout<<"文件打开错误,不能进行更新!\n"; return; } file.seekp(0,ios::beg); while(!file.eof()) { file.read((char *)&user1,sizeof(User)); if(strcmp(user1.szName,user.szName)==0) { file.seekp(-(sizeof(User)),ios::cur); file.write((char *)&user,sizeof(User)); file.close(); return; } } file.close(); fstream outfile; outfile.open("user.dat",ios::binary|ios::app); outfile.write((char *)&user,sizeof(User)); outfile.close(); return; } } using namespace NameCComplex; int main(void) //主函数开始 { srand(time(NULL)); //初始化随机数“种子”语句 Login(); //当前用户信息函数 char strChoise[20]; //定义字符串名 do { system("cls"); cout<<"\t这是一个简单的复数计算器程序,可以实现以下功能,请按对应的按键(1-7)\n\n\n"; cout<<"\t=========================MENU===========================\n"; cout<<"\t1:多复数加法,以0结束\n"; cout<<"\t2:多复数减法,以0结束\n"; cout<<"\t3:测试100以内的复数加减乘法运算,1次测试10道题\n"; cout<<"\t4:多复数乘法,以0结束\n"; cout<<"\t5:复数自加\n:"; cout<<"\t6:复数自减\n:"; cout<<"\t7:复数比较\n:"; cout<<"\t0:退出程序\n\n:"; cout<>strChoise; if(strcmp(strChoise,"1")==0) //用户选1则调用Add()函数 Add(); else if(strcmp(strChoise,"2")==0) //用户选2则调用Sub()函数 Sub(); else if(strcmp(strChoise,"3")==0) //用户选3则调用Test()函数 Test(); else if(strcmp(strChoise,"4")==0) //用户选4则调用Add()函数 Mul(); else if(strcmp(strChoise,"5")==0) //用户选5调用Add1()函数 Add1(); else if(strcmp(strChoise,"6")==0) //用户选6则调用Sub1()函数 Sub1(); else if(strcmp(strChoise,"7")==0) //用户选7则调用Compare()函数 compare(); else if(strcmp(strChoise,"0")==0) //用户选0则结束调用函数 { cout<<"\n\n\t欢迎下次继续使用复数计算器!\n\n"; break; } else { cout<<"\n\t输入错误,请按任意键继续!\n"; cin.get(); cin.get(); } } while((strcmp(strChoise,"0"))); SaveFile(); //调用用户资料保存函数 return 0; }
复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器复数计算器
本报告将介绍使用C++语言设计复数计算器的过程和实现细节。本报告将分为以下几个部分: 1. 需求分析 2. 设计思路 3. 实现方法 4. 测试和性能优化 ## 1. 需求分析 复数计算器需要支持以下基本操作: - 复数加法 - 复数减法 - 复数乘法 - 复数除法 - 复数的模 - 复数的共轭 为了方便使用,我们还需要支持以下附加功能: - 支持输入输出 - 支持任意精度运算 ## 2. 设计思路 为了实现复数的运算,我们需要设计一个复数类,同时重载运算符以方便使用。在设计复数类时,我们需要考虑的因素包括: - 复数的实部和虚部 - 复数的加减乘除运算 - 复数的模和共轭 - 复数的输入输出 - 复数的精度 为了方便使用,我们还可以设计一个控制台界面来与用户交互,同时可以通过命令行参数来指定精度等设置。 ## 3. 实现方法 ### 复数类 我们首先定义一个复数类,其中包括实部和虚部两个成员变量,以及一些成员函数用于实现复数的基本操作。 ```cpp class Complex { public: Complex(double real = 0, double imag = 0); double getReal() const; double getImag() const; double getMod() const; Complex getConjugate() const; Complex operator+(const Complex &rhs) const; Complex operator-(const Complex &rhs) const; Complex operator*(const Complex &rhs) const; Complex operator/(const Complex &rhs) const; friend std::ostream &operator<<(std::ostream &os, const Complex &c); friend std::istream &operator>>(std::istream &is, Complex &c); private: double real_; double imag_; }; ``` 其中,构造函数用于初始化实部和虚部,getReal()、getImag()、getMod()、getConjugate()分别用于获取实部、虚部、模和共轭。 重载的运算符包括加减乘除四个基本运算符,以及输入输出运算符。 ### 控制台界面 我们可以通过命令行参数来指定精度等设置,同时设计一个控制台界面来与用户交互。 ```cpp int main(int argc, char *argv[]) { int precision = 2; if (argc > 1) { precision = std::atoi(argv[1]); } std::cout << "Welcome to Complex Calculator!" << std::endl; std::cout << "Set precision to " << precision << std::endl; std::string line; while (std::getline(std::cin, line)) { if (line.empty()) { continue; } std::istringstream iss(line); Complex c1, c2; std::string op; iss >> c1 >> op >> c2; if (!iss) { std::cout << "Invalid input!" << std::endl; continue; } Complex result; if (op == "+") { result = c1 + c2; } else if (op == "-") { result = c1 - c2; } else if (op == "*") { result = c1 * c2; } else if (op == "/") { result = c1 / c2; } else { std::cout << "Invalid operator: " << op << std::endl; continue; } std::cout << std::fixed << std::setprecision(precision) << result << std::endl; } return 0; } ``` ## 4. 测试和性能优化 为了测试复数计算器的正确性和性能,我们可以编写一些测试用例,并使用Valgrind等工具来检查内存泄漏等问题。在实际使用中,我们还可以通过多线程等技术来提高计算器的性能。 ## 总结 本报告介绍了使用C++语言设计复数计算器的过程和实现细节,包括复数类的设计、控制台界面的实现、测试和性能优化等方面。复数计算器对于科学计算、工程计算等领域具有重要的应用价值,本报告提供了一种简单易用的实现方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值