| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 944 人关注过本帖
标题:关于C++ 判断语句的问题
只看楼主 加入收藏
问问。问
Rank: 1
来 自:三明
等 级:新手上路
帖 子:12
专家分:4
注 册:2011-10-24
结帖率:100%
收藏
 问题点数:0 回复次数:2 
关于C++ 判断语句的问题
今天做了道题目,是关于复数相加的,我被卡在了实现复数相加后的输出问题上。题目要求输出格式如下:

(1-2i)+(3+4i)=4+2i
(1-2i)+(1+2i)=2
(4)+(-2i)=4-2i
(-1+i)+(1)=i

要分别判断每一个复数的实部和虚部是否为0,为0 的话就不输出。以下是我写的程序,想请大家帮我看下,怎么用比较简便的方法来判断每个复数的实部和虚部是否为0,以便实现正确的输出格式,谢谢!


#include <iostream>
using namespace std;

class Complex
{
   double real,image;
public:
  void set( double a,double b)
  {
   real=a;
   image=b;
  }
  Complex operator +(const Complex &z);  
};
Complex Complex::operator +(const Complex &z)
{
   Complex s;
    s.real=real+z.real;
    s.image=image+z.image;
    if(real==0&&image!=0&&z.real!=0&&z.image!=0&&s.real!=0&&s.image!=0)
    cout<<"("<< noshowpos<<image<<"i"<<")"<<"+"<< noshowpos<<"("<<z.real <<showpos<<z.image<<"i"<<")"<<"="<< noshowpos<<s.real<<showpos<<s.image<<"i"<<endl;
    else if(image==0&&real!=0&&z.real!=0&&z.image!=0&&s.real!=0&&s.image!=0)
    cout<<"("<< noshowpos<<real<<")"<<"+"<< noshowpos<<"("<<z.real <<showpos<<z.image<<"i"<<")"<<"="<< noshowpos<<s.real<<showpos<<s.image<<"i"<<endl;
    else if(z.real==0&&image!=0&&real!=0&&z.image!=0&&s.real!=0&&s.image!=0)
    cout<<"("<< noshowpos<<real<<showpos<<image<<"i"<<")"<<"+"<< noshowpos<<"("<<z.image<<"i"<<")"<<"="<< noshowpos<<s.real<<showpos<<s.image<<"i"<<endl;
    else if(z.image==0&&image!=0&&z.real!=0&&real!=0&&s.real!=0&&s.image!=0)
    cout<<"("<< noshowpos<<real<<showpos<<image<<"i"<<")"<<"+"<< noshowpos<<"("<<z.real<<")"<<"="<< noshowpos<<s.real<<showpos<<s.image<<"i"<<endl;
    else if(s.real==0&&image!=0&&z.real!=0&&z.image!=0&&real!=0&&s.image!=0)
    cout<<"("<< noshowpos<<real<<showpos<<image<<"i"<<")"<<"+"<< noshowpos<<"("<<z.real <<showpos<<z.image<<"i"<<")"<<"="<< noshowpos<<s.image<<"i"<<endl;
    else if(s.image==0&&image!=0&&z.real!=0&&z.image!=0&&s.real!=0&&real!=0)
    cout<<"("<< noshowpos<<real<<showpos<<image<<"i"<<")"<<"+"<< noshowpos<<"("<<z.real <<showpos<<z.image<<"i"<<")"<<"="<< noshowpos<<s.real<<endl;   
    else if(z.real==0&&image==0&&real!=0&&z.image!=0&&s.real!=0&&s.image!=0)
    cout<<"("<< noshowpos<<real<<")"<<"+"<< noshowpos<<"("<<z.image <<")"<<"="<< noshowpos<<s.real <<showpos<<s.image<<"i"<<endl;
    else if(z.image==0&&s.real==0&&image!=0&&z.real!=0&&real!=0&&s.image!=0)
    cout<<"("<< noshowpos<<real<<showpos<<image<<"i"<<")"<<"+"<< noshowpos<<"("<<z.real<<")"<<"="<< noshowpos<<s.image<<"i"<<endl;
    else
    cout<<"("<< noshowpos<<real<<showpos<<image<<"i"<<")"<<"+"<< noshowpos<<"("<<z.real<<showpos<<z.image<<"i"<<")"<<"="<< noshowpos<<s.real <<showpos<<s.image<<"i"<<endl;

   return s;
}
int main()
{        double n,m,n1,m1;
    while(cin>>n>>m>>n1>>m1)
    {
        if(n1==0&&m1==0)
            cout<<"非法输入"<<endl;
        else
        {
         Complex t;
         Complex t1;                                                                                                            
          Complex t2;
         t. set(n,m);
         t1. set(n1,m1);
         t2=t+t1;
        }                        
    }
     return 0;
}
搜索更多相关主题的帖子: public include Complex double class 
2011-10-26 16:50
YueWuSS
Rank: 2
等 级:论坛游民
帖 子:15
专家分:96
注 册:2011-10-29
收藏
得分:0 
回复 楼主 问问。问
//可以用ostream& operator<< (ostream &output,const Complex z)
//简化程序

#include <iostream>
#include<ostream>
 using namespace std;
 
class Complex
 {()
    double real,image;
 public:
   void set( double a,double b)
   {
    real=a;
    image=b;
   }
   Complex operator +(const Complex &z);
   friend ostream& operator<< (ostream &output, const Complex);
 };
ostream& operator<< (ostream &output,const Complex z)
{   
    if(z.image == 0)
    {
        output<<z.real;
        return output;
    }
    if(z.real != 0.0)
    {
        output<<z.real;
        if(z.image == 1.0)
        {
            output<<"+i";
            return output;
        }
        if(z.image > 0.0)
        {
            output<<"+"<<z.image<<"i";
            return output;
        }
        if(z.image == -1.0)
        {
            output<<"-i";
            return output;
        }
   
        output<<z.image<<"i";
        return output;
    }
    if(z.real == 0.0)
    {
        if(z.image == 1.0)
        {
            output<<"i";
            return output;
        }
        if(z.image > 0.0)
        {
            output<<z.image<<"i";
            return output;
        }
        if(z.image == -1.0)
        {
            output<<"-i";
            return output;
        }
   
        output<<z.image<<"i";
        return output;
    }

}
Complex Complex::operator +(const Complex &z)
 {
    Complex s;
     s.real=real+z.real;
     s.image=image+z.image;
   return s;
 }
 int main()
 {        double n,m,n1,m1;
     while(cin>>n>>m>>n1>>m1)
     {
         if(n1==0 && m1==0)
             cout<<"非法输入"<<endl;
         else
         {
          Complex t;
          Complex t1;
           Complex t2;
          t. set(n,m);
          t1. set(n1,m1);
          t2=t+t1;
          cout<<"("<<t<<") + ("<<t1<<") = "<<t2;
          cout<<endl;
         }                        
    }
      return 0;
 }


[ 本帖最后由 YueWuSS 于 2011-10-29 08:53 编辑 ]
2011-10-29 08:50
问问。问
Rank: 1
来 自:三明
等 级:新手上路
帖 子:12
专家分:4
注 册:2011-10-24
收藏
得分:0 
回复 2楼 YueWuSS
谢谢!
2011-10-29 21:03
快速回复:关于C++ 判断语句的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027647 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved