c – 重载的postincrement运算符在不实例化运算符的情况下对复数进行平方值

c – 重载的postincrement运算符在不实例化运算符的情况下对复数进行平方值,第1张

概述这是输出: First Complex Number:Enter real part of complex number: 3Enter imaginary part of complex number: 6Second Complex Number:Enter real part of complex number: 5Enter imaginary part of complex 这是输出:

First Complex Number:Enter real part of complex number: 3Enter imaginary part of complex number: 6Second Complex Number:Enter real part of complex number: 5Enter imaginary part of complex number: -5a == (-27.00+36.00i)b == (5.00-5.00i)a+b == (-22.00+31.00i)a-b == (-32.00+41.00i)a*b == (45.00+315.00i)a*a == (-567.00-1944.00i)b*b == (0.00-50.00i)a*a (using postincrement) ==(-27.00+36.00i)

正如你所看到的,并非所有涉及a的都是错误的,因为它取一个(一个复数)的平方作为a.所以,虽然答案“a * a(使用后增量)==( – 27.00 36.00i)是正确的答案…它所说的”a ==( – 27.00 36.00i)“的部分是不正确的,因为它应该是==(3 6i).我相信错误在于我的代码的重载和朋友方面,但我不知道如何解决它因为我没有给出任何错误…这是一个问题我的代码中的逻辑.

这是我的代码:

#include<iostream>#include<iomanip>using namespace std;class Complexnum{public:    Complexnum(float = 0.0,float = 0.0); //default constructor that uses default arg. in case no init. are in main    Complexnum& getComplexnum(); //get real and imaginary numbers from keyboard    Complexnum& sum(Complexnum a,Complexnum b); //method to add two Complexnum numbers together    Complexnum& diff(Complexnum a,Complexnum b); //method to find the difference of two complex numbers    Complexnum& prod(Complexnum a,Complexnum b); //method to find the product of two complex numbers    Complexnum& square(Complexnum a); //method to find square using pre/post increment operators    //overloaded operators    Complexnum& operator =  (const Complexnum& that) = default;    Complexnum& operator += (const Complexnum& that) { return sum(*this,that); }    Complexnum& operator -= (const Complexnum& that) { return diff(*this,that); }    Complexnum& operator *= (const Complexnum& that) { return prod(*this,that); }    Complexnum& operator ++() { return square(*this); } //called for ++num    Complexnum& operator ++(int) { return square(*this); } //called for num++    ostream& print(ostream& stm = cout) const;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)    //non-member overloaded operators    //a is passed by value    frIEnd Complexnum operator+ (Complexnum a,const Complexnum& b) { return a += b; }    frIEnd Complexnum operator- (Complexnum a,const Complexnum& b) { return a -= b; }    frIEnd Complexnum operator* (Complexnum a,const Complexnum& b) { return a *= b; }    frIEnd Complexnum operator++(Complexnum a) { return a++; }    frIEnd ostream& operator<< (ostream& stm,const Complexnum& c) { return c.print(stm); }};Complexnum::Complexnum(float a,float b){    real = a;    imaginary = b;}Complexnum& Complexnum::getComplexnum(){    Complexnum keyboard;    cout << "Enter real part of complex number: ";    cin >> real;    cout << "Enter imaginary part of complex number: ";    cin >> imaginary;    return keyboard; }Complexnum& Complexnum::square(Complexnum a){    this->real = (a.real * a.real) - (a.imaginary * a.imaginary);    this->imaginary = (2 * (a.real * a.imaginary));    return *this;}Complexnum& Complexnum::sum(Complexnum a,Complexnum b){    this->real = a.real + b.real;    this->imaginary = a.imaginary + b.imaginary;    return *this;}Complexnum& Complexnum::diff(Complexnum a,Complexnum b){    this->real = a.real - b.real;    this->imaginary = a.imaginary - b.imaginary;    return *this;}Complexnum& Complexnum::prod(Complexnum a,Complexnum b){    this->real = (a.real * b.real) - (a.imaginary * b.imaginary);    this->imaginary = (a.real * b.imaginary) + (b.real * a.imaginary);    return *this;}ostream& Complexnum::print(ostream& stm) const{    return stm << "(" << noshowpos << real << showpos << imaginary << "i)";}int main(){    Complexnum a,b;    cout << "First Complex Number:" << endl;    a.getComplexnum();    cout << endl;    cout << "Second Complex Number:" << endl;    b.getComplexnum();    cout << endl;    cout << fixed << setprecision(2)        << "a == " << a << '\n'        << "b == " << b << '\n'        << "a+b == " << a + b << '\n'        << "a-b == " << a - b << '\n'        << "a*b == " << a*b << '\n'        << "a*a == " << a*a << '\n'        << "b*b == " << b*b << '\n'        << "a*a (using postincrement) ==" << a++ << '\n';        cout << endl;    system("PAUSE");}
解决方法 这不是关于运算符重载,大概是 order of evaluation.

在声明中:

cout << fixed << setprecision(2)        << "a == " << a << '\n'        << "b == " << b << '\n'        << "a+b == " << a + b << '\n'        << "a-b == " << a - b << '\n'        << "a*b == " << a*b << '\n'        << "a*a == " << a*a << '\n'        << "b*b == " << b*b << '\n'        << "a*a (using postincrement) ==" << a++ << '\n';

参数将按照您的预期从左向右打印,但计算每个参数的各个步骤不必按此顺序完成.

编译器可以按任何顺序计算每个子表达式的值,只要该值在需要打印时“就绪”即可.所以它可以做这样的事情:

auto&& temp1 = a + b;auto&& temp2 = a - b;auto&& temp3 = a*b;auto&& temp4 = a*a;auto&& temp5 = b*b;auto&& temp6 = a++;cout << fixed << setprecision(2)        << "a == " << a << '\n'        << "b == " << b << '\n'        << "a+b == " << temp1 << '\n'        << "a-b == " << temp2 << '\n'        << "a*b == " << temp3 << '\n'        << "a*a == " << temp4 << '\n'        << "b*b == " << temp5 << '\n'        << "a*a (using postincrement) ==" << temp6 << '\n';

如果发生这种情况,您将获得您期望的行为,因为子表达式将按从左到右的顺序进行评估.但是当前的C标准(C 14)允许编译器重新排序评估,这可能允许它更好地优化代码,如果它需要更少的堆栈空间更少的寄存器.该声明的另一个有效执行是:

auto&& temp1 = a++;auto&& temp2 = b*b;auto&& temp3 = a*a;auto&& temp4 = a*b;auto&& temp5 = a - b;auto&& temp6 = a + b;cout << fixed << setprecision(2)        << "a == " << a << '\n'        << "b == " << b << '\n'        << "a+b == " << temp6 << '\n'        << "a-b == " << temp5 << '\n'        << "a*b == " << temp4 << '\n'        << "a*a == " << temp3 << '\n'        << "b*b == " << temp2 << '\n'        << "a*a (using postincrement) ==" << temp1 << '\n';

这次你可以看到a *** 作首先发生,因此稍后的计算将在发生之后完成.

编译器还可以选择子表达式评估的任何其他排序,例如,它可以评估a * a然后a a b等等(但实际上最常见的顺序是从左到右和从右到左)

另外,您的代码还有两个严重问题:

此函数返回对局部变量的引用:

Complexnum& Complexnum::getComplexnum(){    Complexnum keyboard;    // ...    return keyboard; }

函数返回后,该局部变量不再存在,因此任何使用该引用的尝试都将访问不存在的对象.永远不要这样做!你的编译器应该警告你这里有问题,启用编译器的警告,不要忽略它们!

其次,重载运算符是一个非常糟糕的主意,就像意味着完全不同的东西一样,就像将对象本身相乘一样.当运算符对您要执行的 *** 作有意义时,您应该只使用 *** 作符重载.增量不是平方,所以这是一个糟糕的选择.我只想为此定义一个普通(非运算符)函数.

总结

以上是内存溢出为你收集整理的c – 重载的postincrement运算符在不实例化运算符的情况下对复数进行平方值全部内容,希望文章能够帮你解决c – 重载的postincrement运算符在不实例化运算符的情况下对复数进行平方值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1228805.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存