算法提高 6-17复数四则运算
时间限制:1.0s 内存限制:512.0MB
设计复数库,实现基本的复数加减乘除运算。
输入时只需分别键入实部和虚部,以空格分割,两个复数之间用运算符分隔;输出时按a+bi的格式在屏幕上打印结果。参加样例输入和样例输出。
注意考虑特殊情况,无法计算时输出字符串"error"。
输入时只需分别键入实部和虚部,以空格分割,两个复数之间用运算符分隔;输出时按a+bi的格式在屏幕上打印结果。参加样例输入和样例输出。
注意考虑特殊情况,无法计算时输出字符串"error"。
样例输入
2 4 * -3 2
样例输出
-14-8i
样例输入
3 -2 + -1 3
样例输出
2+1i
这样啊,对于本题目本来是很简单的一道题目却提交了三次确实不应该
第一个问题,题目给你的测试数据是int整数,就默认成整数了,要注意,不过感觉很怪异
第二个就是第二数+号的输出
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
double a,b,c,d;
char m;
scanf("%lf %lf %c %lf %lf",&a,&b,&m,&c,&d);
if(m=='+')
{
cout << a+c;
if(b+d>0)
cout << "+";
cout << b+d;
cout << "i";
}
else if(m=='-')
{
cout << a-c;
if(b-d>0)
cout << "+";
cout << b-d;
cout << "i";
}
else if(m=='*')
{
cout << a*c-b*d;
if(a*d+b*c>0)
cout << "+";
cout << a*d+b*c;
cout << "i";
}
else if(m=='/')
{
if(c*c+d*d==0)
{
printf("error\n");
return 0;
}
else
{
cout << (a*c+b*d)/(c*c+d*d);
if((b*c-a*d)/(c*c+d*d)>0)
cout << "+";
cout << (b*c-a*d)/(c*c+d*d);
cout << "i";
}
//printf("%lf+%lf",(a*c+b*d)/(c*c+d*d),(b*c-a*d)/(c*c+d*d));
}
return 0;
}