【问题标题】:Addition and subtraction of complex numbers using class in C++使用 C++ 中的类进行复数的加法和减法
【发布时间】:2012-02-16 08:38:22
【问题描述】:

我这里有一个代码应该询问用户两组实数和虚数。

#include <iostream>

using namespace std;

class Complex {
    public:
        double r;
        double i;
    public:
        Complex();
        void add(Complex, Complex);
        void subtract(Complex, Complex);
        void print();
};



Complex::Complex() {
    r = i = 0;
}

void Complex::add (Complex op1, Complex op2) {
    r = op1.r+op2.r;
    i = op1.i+op2.i;
}

void Complex::subtract (Complex op1, Complex op2) {
     r = op1.r-op2.r;
     i = op1.i-op2.i;
}

void Complex::print () {
    cout << r << i;
}

int main () {
    Complex operand1, operand2, result;
    cout << "Input real part for operand one: " << endl;
    cin >> operand1.r;
    cout << "Input imaginary part for operand one: " << endl;
    cin >> operand1.i;
    cout << "Input real part for operand two: " << endl;
    cin >> operand2.r;
    cout << "Input imaginary part for operand two: " << endl;
    cin >> operand2.i;
    result.add(operand1, operand2);
    cout << "The sum is " << result.add << endl;
    result.subtract(operand1, operand2);
    cout << "The difference is " << result.subtract << endl;
}

但是,当我编译程序时,显示了很多我什至没有得到的错误 (std::basic_ostream)。

我遇到的另一个问题是函数 void::Complex print。 cout 本身应该有一个条件。没有如果,否则。但我不知道该怎么做。
程序必须像这样运行:
输入操作数一的实部:5
输入操作数一的虚数部分:2(虚数的 i 不应该写)
输入操作数二的实部:8
输入操作数 2 的虚部:1(同样,不应输入 i)
/然后它会打印输入的数字/
(5, 2i) //这次是 i
(8, 1i)
/然后是答案/
总和是 13+3i。
差是-3, 1i。 //或 -3, i

请帮帮我!我是 C++ 和 stackoverflow 的新手,非常感谢您的帮助。非常感谢!

【问题讨论】:

  • 这是你的学校作业吗?
  • 阅读更多关于运算符重载的内容,您应该能够正确编写加法和减法函数。
  • 是的,彭纳图尔。我想我做了我能做的,但我的知识仍然缺乏。我需要指导。
  • 您使用的是哪个编译器? g++ 可能非常神秘。也许试试铿锵++?如果没有,谷歌个别错误。注入一些精神:D
  • 你好,颠倒了!我使用代码块。谢谢!

标签: c++ class object structure


【解决方案1】:

线

cout

不正确,因为add 是一个方法,所以result.add 将是一个指向该方法的指针,而cout 不知道如何处理它——这使得编译器将其吐出。

换行

cout << "The sum is ";
result.print();
cout << endl;

你需要对这条线做同样的事情

cout << "The difference is " << result.subtract << endl;

就编码风格而言,这两种方法都是覆盖现有的复数。也许有这样的功能会更好

Complex &Complex::add (const Complex &op) { 
    r += op.r; 
    i += op.i;
    return *this;
}

这将使您能够将加法链接在一起,并且只需将复数添加到现有复数。

此外,您可以将类变量 ri 设为私有。这将需要一个替代构造函数:

Complex:Complex(double real, double imaginary) : r(real), i(imaginary) {};

最后,您可能希望考虑运算符重载 - 我相信您可以通过谷歌搜索找到合理的教程。

【讨论】:

    【解决方案2】:

    在 main 中,在调用 result.add 之后,当 cout 流没有返回任何内容时,您将相同的函数放入其中。我想你的意思是写 cout

    【讨论】:

      【解决方案3】:

      稍作修正再试一次

          #include <iostream.h>
          class Complex {
              public:
              double r; //real part
              double i; //imaginary part
              public:
              void add(Complex, Complex);
              void subtract(Complex, Complex);
              void print();
          };
      
          void Complex::add (Complex op1, Complex op2) {
              r = op1.r + op2.r;
              i = op1.i + op2.i;
          }
      
          void Complex::subtract (Complex op1, Complex op2) {
               r = op1.r - op2.r;
               i = op1.i - op2.i;
          }
      
          void Complex::print () {
              cout << "("<<r<<", " << i <<")";
          }
      
          void main () {
              Complex operand1, operand2, result;
              cout << "\nInput real part for operand one: " << endl;
              cin >> operand1.r;
              cout << "Input imaginary part for operand one: " << endl;
              cin >> operand1.i;
              cout << "Input real part for operand two: " << endl;
              cin >> operand2.r;
              cout << "Input imaginary part for operand two: " << endl;
              cin >> operand2.i;
              cout << "\nThe sum is ";
              result.add(operand1, operand2);
              result.print();
              cout << "\nThe difference is ";
              result.subtract(operand1, operand2);
              result.print();
          }
      

      【讨论】:

        【解决方案4】:

        您已经在使用 std:: 命名空间。只需使用其中的复数库,就像这个答案建议的那样:Addition of complex numbers using classes

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-10-22
          • 2019-12-21
          • 1970-01-01
          • 1970-01-01
          • 2011-07-27
          • 2022-01-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多