程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 編程-大神們,幫我看看代碼哪裡錯了?

編程-大神們,幫我看看代碼哪裡錯了?

編輯:編程解疑
大神們,幫我看看代碼哪裡錯了?
 #include<iostream>
using namespace std;
class Complex
{
private:
    double real,imag;
public:
    Complex(double _real=0.0,double _imag=0.0):real(_real),imag(_imag)
    {
    }
    Complex operator +(Complex &val)
    {
        Complex t;
        t.real=this->real+val.real;
        t.imag=this->imag+val.imag;
        return t;
    }
    /*不能引用局部變量Complex&operator+*/
    Complex operator -(Complex &val)
    {
        return Complex(real-val.real,imag-val.imag);
    }
    Complex operator *(Complex &val)
    {
        Complex t;
        t.real=this->real*val.real-this->imag*val.imag;
        t.imag=this->real*val.imag+this->imag*val.real;
        return t;
    }
    Complex operator *(double x)
    {
        return Complex(x*real,x*imag);
    }
    Complex operator ~()
    {
        return Complex(real,-1*imag);
    }
    friend istream &operator >>(istream &is,Complex &com)
    {
        cout<<"real:";
        is>>com.real;
        if (is)
        {
            cout<<"imag:";
            is>>com.imag;
            if (is)
                return is;
            else
                com=Complex();
        }
        else
             com=Complex();
        return is;
    }
    friend ostream &operator <<(ostream &os,Complex &com)
    {
        os<<"("<<com.real<<","<<com.imag<<"i)"<<endl;
        return os;
    }
};
int main()
{
    Complex a(3.0,4.0);
    Complex c;
    cout<<"輸入一個復數(輸入q就停止):\n";
    while (cin>>c)
    {
        cout<<"c is "<<c<<endl;
        cout<<"共轭復數:"<<~c<<endl;
        cout<<"a is "<<a<<endl;
        cout<<"a+c is "<<a+c<<endl;
        cout<<"a-c is "<<a-c<<endl;
        cout<<"a*c is "<<a*c<<endl;
        cout<<"2*c is "<<2*c<<endl;
        cout<<"輸入一個復數(輸入q就停止):\n";
    }
    cout<<"Done!\n";
    return 0;
}
 #include<stdio.h>
int main()
{
    int i,j=0,f=0,k;
    char s[100],a[100],e[100];
    gets(s);
    for (i=0;s[i]!='\0';i++)
    {
        if (s[i]>='0'&&s[i]<='9')
        {
            a[j++]=s[i];
            if (s[i+1]>='0'&&s[i+1]<='9')
                continue;
            if (j>=f)
            {
                for (k=0,f=0;k<j;k++,f++)
                     e[k]=a[j];
            }
            j=0;
        }
    }
    printf("%d\n",f);
    for (k=0;k<f;k++)
        printf("%c",e[k]);
    printf("\n");
    return 0;
}

最佳回答:


LZ上面那段程序:cout<<"2*c is "<<2*c<<endl; 改為cout<<"2*c is "<<c*2<<endl;
2必須作為右操作數

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved