【问题标题】:How to return "" OR empty when value of float is 1当 float 值为 1 时如何返回“”或为空
【发布时间】:2017-08-03 23:09:36
【问题描述】:
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 时返回 "" 或空白或什么都没有,以便它可以打印出我设置的适当的 "i)"。

样本输出:

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 时,我希望 print 函数能够反映这一点。所以差异看起来像 (-2+i) 而第二个复数平方看起来像 (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;
}

【问题讨论】:

  • 您在 if 条件中将值 1 分配给变量。为什么不在 if 中使用另一个输出?
  • 什么意思?
  • 您似乎对return 的含义不熟悉。
  • 我写了return和其他东西。是的,我知道 return 无效。因为 void 不返回任何东西。但还是。只是一个 if 语句在这里也不起作用。
  • 我认为人们只是掩盖了我在那条线上写的评论。我特别提到我尝试过 "imaginary = ""; " 但这也没有用。只是想指出我已经尝试了一切。

标签: c++ output


【解决方案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)

【讨论】:

  • 我必须有条不紊地进行打印。没有捷径。谢谢道格。
  • 我在检查代码时发现您的要求发生了变化。所以我没有处理真实的一些要求,但想法是一样的......将工作分成可管理的部分(可能分为 ssR 和 ssI),以最简单的方式处理每个部分(可能是 if 子句),然后结合字符串。祝你好运。
  • 没有必要让想象在真实之前被“字符串流化”。我只是希望在开始 std::cout 子句之前完成这两个部分。我更喜欢 cout 子句在操作期间没有中断。
【解决方案2】:
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 块。没有其他方法可以解决这个问题。

【讨论】:

  • MMMM....看着自己的答案,我很兴奋。看看这段代码有多棒!我是认真的。有没有更好的方法来解释每一种丑陋的虚数形式的情况?呃,我想如果我不能删除我自己的问题,我还不如赞美我自己的答案。只是看看它。这是介绍编程敬虔的东西。 Whatchu 知道我的 else-if 块吗????
【解决方案3】:

首先,当您检查两个事物是否相等时,您希望使用 == 或 .equals()。在这种情况下,您需要 ==,因为您正在比较原语。
接下来,这个函数是一个void函数。这意味着您已将其定义为不返回任何内容,但您正试图返回一个空字符串。
您要做的是在 imaginary 为 1 时打印一个模式,并在 imaginary 不是 1 时打印一个不同的模式。使用 print 语句,就像您在默认情况下所做的那样,打印 @987654323 的特殊情况@ = 1。

【讨论】:

  • 当我在 imaginary = 1 和 else 时尝试使用传统的 if-else 块时,它不会正确打印出语句,因为 showpos 不是由字符串触发的。这只是一个大混乱。我不知道如何解决它。
  • 发布您正在尝试的内容以及输出。
猜你喜欢
  • 1970-01-01
  • 2019-08-25
  • 2011-05-22
  • 1970-01-01
  • 1970-01-01
  • 2016-10-29
  • 2019-04-25
  • 2019-04-20
  • 1970-01-01
相关资源
最近更新 更多