void ComplexNum::printComplexNum()
{
if (imaginary = 1)
{
return ""; //cannot return "" I've also tried imaginary == ""; to no avail
}
cout << "(" << noshowpos << real << showpos << imaginary << "i)" << endl;
}
我有一个复杂的数字程序,当我想显示复数时,它显示错误,因为(4-1i)应该显示为(4-i)。虽然(4-1i)在技术上是正确的,但它不会显示为我希望它显示。我在print方法中创建了一个简单的if语句,但它不起作用,因为变量imaginary不是一个字符串。 *如何使我的变量imaginary
等于1,它返回&#34;&#34;或空白或没有任何东西,以便它可以打印出适当的&#34; i)&#34;我已经设定了。
示例输出:
First Complex Number:
Enter real part of complex number: 2
Enter imaginary part of complex number: -3
Form '(a+bi)': (2-3i)
Second Complex Number:
Enter real part of complex number: 4
Enter imaginary part of complex number: -4
Form '(a+bi)': (4-4i)
The addition of the two Complex Numbers is: (6-7i)
The difference of the two Complex Numbers is: (-2+1i)
The product of the two Complex Numbers is: (-4-20i)
First Complex Number Squared: (-5-12i)
Second Complex Number Squared: (0-32i)
注意差异是(-2 + 1i)......我不喜欢这样。我不想要那个。另外,我不想要那个(0-32i)。所以基本上当它是0或者它是1时我希望打印功能能够反映出来。所以差异看起来像(-2 + i),而第二复杂Num Squared看起来像(32i)
现在我的代码:
class ComplexNum
{
public:
ComplexNum(float = 0.0, float = 0.0); //default constructor that uses default arg. in case no init. are in main
void getComplexNum(); //get real and imaginary numbers from keyboard
void sum(ComplexNum a, ComplexNum b); //method to add two ComplexNum numbers together
void diff(ComplexNum a, ComplexNum b); //method to find the difference of two complex numbers
void prod(ComplexNum a, ComplexNum b); //method to find the product of two complex numbers
void square(); //squares values of a and b when called in main
void printComplexNum(); //print sum, diff, prod, square
void formComplexNum(); //and "a+bi" form
private:
float real; //float data member for real number (to be entered in by user)
float imaginary; //float data member for imaginary number (to be entered in by user)
float realSquare; //squared real number data member for square method
float imaginarySquare; //squared imaginary number data member for square method
};
和司机:
int main()
{
ComplexNum a, b, c, d, e, f, g;
cout << "First Complex Number:" << endl;
a.getComplexNum();
a.formComplexNum();
cout << endl;
cout << "Second Complex Number:" << endl;
b.getComplexNum();
b.formComplexNum();
cout << endl;
c.sum(a, b);
c.printComplexNum();
d.diff(a, b);
d.printComplexNum();
e.prod(a, b);
e.printComplexNum();
cout << "First Complex Number Squared: ";
a.square();
cout << "Second Complex Number Squared: ";
b.square();
cout << endl;
system("PAUSE");
return 0;
}
答案 0 :(得分:1)
您已将此标记为C ++
(4-1i)应显示为(4-i)
您可能会发现std :: stringstream很有帮助。它简化了虚部的特殊处理:
virtual int foo()
{
std::cout << std::endl;
show(4, -2);
show(5, -1);
return(0);
}
void show(int real, int imaginary)
{
std::stringstream ss; // default is blank
if (-1 == imaginary) { ss << "-i)"; }
else /* (-1 != imaginary)*/ { ss << imaginary << "i)"; }
std::cout << "("
<< std::noshowpos << real
<< std::showpos << ss.str()
<< std::endl;
}
带输出
(4-2i) (5-i)
答案 1 :(得分:0)
void ComplexNum::printComplexNum()
{
if (imaginary == -1 && real == 0)
{ cout << "(-i)" << endl; }
else if (imaginary == 1 && real == 0)
{ cout << "(i)" << endl; }
else if (imaginary == -1)
{ cout << "(" << noshowpos << real << "-i)" << endl; }
else if (imaginary == 1)
{ cout << "(" << noshowpos << real << "+i)" << endl; }
else if (real == 0 && imaginary == 0)
{ cout << "(0)" << endl; }
else if (real == 0)
{ cout << "(" << noshowpos << imaginary << "i)" << endl; }
else if (imaginary == 0)
{ cout << "(" << noshowpos << real << ")" << endl; }
else
{ cout << "(" << noshowpos << real << showpos << imaginary << "i)" << endl; }
}
指定-1i,1i,0i,0real,0i和0real 最终的if-else块。除此之外别无他法。
答案 2 :(得分:-1)
首先,当你检查两件事是否相等时,你想要使用==或.equals()。在这种情况下,您需要==,因为您要比较基元
接下来,此函数是void函数。这意味着您已将其定义为不返回任何内容,但您尝试返回空字符串。
您想要做的是在imaginary
为1时打印一个模式,在imaginary
为1时打印不同的模式1.使用打印语句,就像在默认情况下一样,打印imaginary
= 1的特殊情况。