簡體   English   中英

輸入值后代碼停止運行

[英]The code stops running after the entering the values

#include<iostream>
#include<cmath>
using namespace std;

class Complex
{
    private:
        double real;
        double imag;
        double modulus;

    public:
        Complex(){
            cout<<"Default constructor is called"<<endl;
        }
        Complex (double realIn, double imagIn){
            this->real = realIn;
            this->imag = imagIn;
        }
        Complex (const Complex &a ){
            real = a.real;
            imag = a.imag;
            modulus = a.modulus;
        }
        double getModulus(){
            return modulus = sqrt(real * real + imag * imag);
        }
        void display(){
            cout<<real<<" + "<<imag<<" i"<<endl;
        }
        bool operator > (const Complex &c1){
            return modulus > c1.modulus? true : false;
        }
        Complex& operator = (const Complex &c1){
            real = c1.real;
            imag = c1.imag;
            modulus = c1.modulus;
        }               
};

int main(){
    int num, i,j;
    double real, imag;
    cout<<" Input number of complex numbers: ";
    cin>>num;
    Complex* c[num];

    for (i = 0; i < num; i++){
        cout<<"Imput the real part of the complex number no."<<i+1<<endl;
        cin>>real;
        cout<<"Input the imagine part of the complex number no."<<i+1<<endl;
        cin>>imag;
        c[i] = new Complex(real, imag);
        c[i]->display();
    }
    for (i = 0; i < num; i++){
        for (j = i +1; j < num; i++){
            if (c[j]->getModulus() > c[i]->getModulus() ? true:false){
                if (true){
                   Complex* temp = c[j];
                   c[j] = c[i];
                   c[i] = temp;
            } 
            }
        }
    }
    system("CLS");
    cout<<"The decreasing order of the complex numbers is: "<<endl;
    for (i = 0; i < num; i++){
        cout<<i+1<<" : ";
        c[i]->display();
    }

    return 0;
}

我剛開始了解 OOP,特別是重載 function。 我被要求閱讀復數列表並按降序打印(使用它們的模數進行比較)。 但是,在我輸入所有復數后它會停止。 沒有錯誤,所以我不知道我哪里弄錯了。 請幫我。 非常感謝!

首先,添加

#include<cstdlib>

在源代碼的開頭使用system("CLS");

其次,改變

for (j = i +1; j < num; i++){

for (j = i +1; j < num; j++){

(增加j而不是i )以避免使i太大。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM