.
Отговор в тема
Резултати от 1 до 5 от общо 5

Тема: C++

  1. #1

    C++

    Ако има някой, който може да ми помогне за една програма за последния ми изпит в Неделя, ще съм му много благодарен.
    Ето я и задачата, изискването е да се направи с Class ( дори и структура не дават ).




    Да се реализира проект "Комплексно число", имащо две член-данни, цялата (real) и имагинерната (imaginary) част- цели числа.
    Дефинирайте операции за обектите на този клас: отрицателна стойност,събиране,изваждан е,умножение и деление. Бинарните операции
    да действат и с операнди-комплексно и цяло число. Да се напише член-функция за извеждане на комплексно число във вида: real +/-i imaginary
    В главната функция на програмата да се илюстрират всичките дейности с комплексни числа

  2. #2

  3. #3
    Мега фен Аватара на Chacho
    Регистриран на
    Nov 2006
    Мнения
    15 573
    Утре или макс други ден ще е готова.
    Има 10 вида хора. Тези, които могат да четат двоичен код и тези, които не могат.

  4. #4
    Мега фен Аватара на Chacho
    Регистриран на
    Nov 2006
    Мнения
    15 573
    Код:
    #include <iostream>
    using namespace std;
    
    class ComplexNum
    {
          public:
                 
          /*****Constructors*****/       
          ComplexNum();
          ComplexNum(float);
          ComplexNum(float, float);
          ComplexNum(const ComplexNum&);
          
          /*****Unary operators*****/       
          ComplexNum& operator=(const ComplexNum&);
          ComplexNum operator-();
          
          /*****Binary operators with complex number as rhs*****/  
          ComplexNum operator+(const ComplexNum&);
          ComplexNum operator-(const ComplexNum&);
          ComplexNum operator*(const ComplexNum&);
          ComplexNum operator/(const ComplexNum&);
          
          /*****Binary operators with float as rhs*****/  
          ComplexNum operator+(const float);
          ComplexNum operator-(const float);
          ComplexNum operator*(const float);
          ComplexNum operator/(const float);
          
          /*****Other Methods*****/
          void Print();
          
          
          /*****Members*****/
          private:
          float m_rVal, m_iVal;
    };
    
    //CONSTRUCTORS
    ComplexNum::ComplexNum()
    {
       m_rVal = 0;
       m_iVal = 0;
    }
    
    ComplexNum::ComplexNum(float realNum)
    {
       m_rVal = realNum;
       m_iVal = 0;
    }
    
    ComplexNum::ComplexNum(float realNum, float imagNum)
    {
       m_rVal = realNum;
       m_iVal = imagNum;
    }
    
    ComplexNum::ComplexNum(const ComplexNum& rhs)
    {
       m_rVal = rhs.m_rVal;
       m_iVal = rhs.m_iVal;                
    }
    
    
    ComplexNum& ComplexNum::operator=(const ComplexNum& rhs)
    {
       if(this!=&rhs)
       {
           m_rVal = rhs.m_rVal;
           m_iVal = rhs.m_iVal;  
       }
       return *this;
    }
    
    ComplexNum ComplexNum::operator-()
    {
        ComplexNum result(-(*this).m_rVal, -(*this).m_iVal);
        return result;
    }
    
    
    //UNARY OPERATORS
    ComplexNum ComplexNum::operator+(const ComplexNum& rhs)
    {
        ComplexNum result((*this).m_rVal+rhs.m_rVal, (*this).m_iVal+rhs.m_iVal);
        return result;  
    }
    
    ComplexNum ComplexNum::operator-(const ComplexNum& rhs)
    {
        ComplexNum result((*this).m_rVal-rhs.m_rVal, (*this).m_iVal-rhs.m_iVal);
        return result;                                 
    }
    
    ComplexNum ComplexNum::operator*(const ComplexNum& rhs)
    {
        ComplexNum result((*this).m_rVal*rhs.m_rVal - (*this).m_iVal*rhs.m_iVal, (*this).m_rVal*rhs.m_iVal + (*this).m_iVal*rhs.m_rVal);
        return result;               
    }
    
    ComplexNum ComplexNum::operator/(const ComplexNum& rhs)
    {
        float denom = rhs.m_rVal*rhs.m_rVal + rhs.m_iVal*rhs.m_iVal;
        if(denom!=0)
        {
            ComplexNum result(((*this).m_rVal*rhs.m_rVal + (*this).m_iVal*rhs.m_iVal)/denom, ((*this).m_iVal*rhs.m_rVal - (*this).m_rVal*rhs.m_iVal)/denom);
            return result;
        }   
        else
        {
            cout<<"Error, cannot divide by 0";
            ComplexNum result(0, 0);
            return result;
        }
    }
    
    
    //BINARY OPERATORS
    ComplexNum ComplexNum::operator+(const float rhs)
    {
         ComplexNum tmpVal(rhs, 0);
         return *this+tmpVal;
    }
    
    ComplexNum ComplexNum::operator-(const float rhs)
    {
         ComplexNum tmpVal(rhs, 0);
         return *this-tmpVal;      
    }
    
    ComplexNum ComplexNum::operator*(const float rhs)
    {
         ComplexNum tmpVal(rhs, 0);
         return *this*tmpVal;     
    }
    
    ComplexNum ComplexNum::operator/(const float rhs)
    {
         ComplexNum tmpVal(rhs, 0);
         return *this/tmpVal;      
    }
    
    
    //OTHER METHODS
    void ComplexNum::Print()
    {
         cout<<m_rVal<<" + i("<<m_iVal<<")";
    }
              
    
    //TEST PROGRAM       
    int main()
    {
        int k=13;
        
        ComplexNum tmp(2, 3);
        ComplexNum tmp2(k, 7);
        
        tmp.Print(); //Print
        cout<<"\n";
        tmp2.Print(); //Print
        
        cout<<"\n";
        (tmp+tmp2).Print(); //Addition
        cout<<"\n";
        (tmp-tmp2).Print(); //Subtraction
        cout<<"\n";
        (tmp*tmp2).Print(); //Multiplication
        cout<<"\n";
        (tmp/tmp2).Print(); //Division    
        
        cout<<"\n";
        (tmp+3).Print(); //Addition with int
        cout<<"\n";
        (tmp-4).Print(); //Subtraction with int
        cout<<"\n";
        (tmp*5).Print(); //Multiplication with int
        cout<<"\n";
        (tmp/6).Print(); //Division with int
        
        cout<<"\n";
        (-tmp).Print(); //Unary minus
        cout<<"\n";
        tmp=tmp2; //Assignment
        tmp.Print();
        
        int pause;
        cin>>pause;
        return 0;
    }
    Има 10 вида хора. Тези, които могат да четат двоичен код и тези, които не могат.

  5. #5
    Мега фен Аватара на Chacho
    Регистриран на
    Nov 2006
    Мнения
    15 573
    А, данните не са цели числа, тва не го бях видял, ма ми се струва тъпо. Но можеш лесно да го ограничиш. Малко сменяне на float с int, малко cast-вания до float и закръгляния и си читав.
    Има 10 вида хора. Тези, които могат да четат двоичен код и тези, които не могат.

Правила за публикуване

  • Вие не можете да публикувате теми
  • Вие не можете да отговаряте в теми
  • Вие не можете да прикачвате файлове
  • Вие не можете да редактирате мненията си