白首少年 2017-10-23 09:43 采纳率: 50%
浏览 515

《C++ primer plus》课后习题11.7,求解。

类定义部分

 #include <iostream>

using namespace std;

class plural
{
    private:
        double A;
        double B;
    public:
        plural();
        plural(double ,double);
        ~plural();
        plural operator+(plural CB);
        plural operator-(plural CB);
        plural operator*(plural CB);
        friend plural operator*(double x,plural CA);
        friend plural operator~(plural CA);
        friend ostream & operator<<(ostream &os,plural CA);
        friend bool operator>>(istream &is,plural &CA);
};

plural::plural()
{
    A=0.0;
    B=0.0;
}

plural::plural(double A_,double B_)
{
    A=A_;
    B=B_;
}

plural::~plural()
{

}

plural plural::operator+(plural CB)
{
    plural CC;
    CC.A=A+CB.A;
    CC.B=B+CB.B;
    return CC;
}

plural plural::operator-(plural CB)
{
    plural CC;
    CC.A=A-CB.A;
    CC.B=B-CB.B;
    return CC;
}

plural plural::operator*(plural CB)
{
    plural CC;
    CC.A=A*CB.A-B*CB.B;
    CC.B=A*CB.B+B*CB.B;
    return CC;
}

plural operator*(double x,plural CA)
{
    plural CB;
    CB.A=x*CA.A;
    CB.B=x*CA.B;
    return CB;
}

plural operator~(plural CA)
{
    plural CC;
    CC.A=CA.A;
    CC.B=-CA.B;
    return CC;
}

ostream & operator<<(ostream &os,plural CA)
{
    os<<CA.A<<'+'<<CA.B<<'i';
    return os;
}

bool operator>>(istream &is,plural &CA)
{
    char ch;
    cin.get(ch);
    if(ch!='q')
    {
        cout<<"real:";
        is>>CA.A;
        cout<<endl<<"imaginary:";
        is>>CA.B;
        return true;
    }
    else
        return false;


}

主函数部分

 #include "11.7_func.cpp"

int main()
{
    plural AA(3.0,4.0);
    plural CC;
    cout<<"enter a plural number (q to quit)"<<endl;
        while(cin>>CC)
        {
        cout<<" CC is "<<CC<<endl;
        cout<<"complex conjugate is "<<~CC<<endl;
        cout<<"a is "<<AA<<endl;
        cout<<"a+c is "<<AA+CC<<endl;
        cout<<"a-c is "<<AA-CC<<endl;
        cout<<"a*c is "<<AA*CC<<endl;
        cout<<"2*c is "<<2*CC<<endl;
        cout<<"enter a plural number (q to quit)"<<endl;
        }
    cout<<"done!"<<endl;
    return 0;
}

运行出现的问题是,无论输入什么字符,q或者不是q,这个循环体都只执行一次。
我觉得是

 bool operator>>(istream &is,plural &CA)
{
    char ch;
    cin.get(ch);
    if(ch!='q')
    {
        cout<<"real:";
        is>>CA.A;
        cout<<endl<<"imaginary:";
        is>>CA.B;
        return true;
    }
    else
        return false;


}

出了问题,但是又看不出问题在哪?求大神解答。

  • 写回答

1条回答 默认 最新

  • 他笑他自己 2017-10-27 09:46
    关注

    感觉。。没问题啊。。

    评论

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元