C 物件導向程式設計之複數四則運算器

2021-10-10 18:19:53 字數 2338 閱讀 2885

定義乙個實數類real,類中包括乙個資料成員real,建構函式、display()函式以及過載的 + 、一、 * 、 / 運算子。用公有繼承方式宣告乙個複數類complex,該類包括兩個資料成員real, image,並過載real類的函式和 - 、一、 *、/ 運算子。

編寫乙個複數四則運算計算器。

#

include

#include

using

namespace std;

class

real

real

(double r)

real operator

+(real& c2)

; real operator

-(real& c2)

; real operator

*(real& c2)

; real operator

/(real& c2)

;void

display()

;double real;};

real real::

operator

+(real& c2)

real real::

operator

-(real& c2)

real real::

operator

*(real& c2)

real real::

operator

/(real& c2)

void

real

::display()

class

complex

:public

real

complex

(double r,

double i)

:real

(r) complex operator

+(complex& c2)

; complex operator

-(complex& c2)

; complex operator

*(complex& c2)

; complex operator

/(complex& c2)

;void

display()

;private

:double image;};

complex complex::

operator

+(complex& c2)

complex complex::

operator

-(complex& c2)

complex complex::

operator

*(complex& c2)

complex complex::

operator

/(complex& c2)

else

}void

complex

::display()

intmain()

case

'-':

case

'*':

case

'/':

default

: cout <<

"請確保您輸入的操作符是+-*/之一!"

<< endl;}}

;break

;case2:

case

'-':

case

'*':

case

'/':

default

: cout <<

"輸入運算子錯誤!"

<< endl;}}

;break;}

cout <<

'\n'

<<

"輸入left離開計算介面/輸入continue繼續進行運算"

<< endl;

string choose;

cin >> choose;

if(choose ==

"left"

) cout <<

"————————感謝使用複數四則計算器!——————————"

<< endl;

else

if(choose ==

"continue"

)goto loop;

else

cout <<

"請重新選擇(left or continue)!"

<< endl;

}return0;

}

執行結果如下:

C 四則運算器

leetcode上的題,只有 空格,計算所給表示式的數值 我現在用的辦法是 中綴表示式轉字尾表示式,然後計算 但是leetcode最後乙個示例是長度為20w的表示式,直接給我弄超時了,先把 放在這裡吧 計算器 include include include includeusing namespac...

分數四則運算器

好的,先弄出個類來,如下 view code 1 class fraction2 各成員實現如下 view code 1 建構函式 2fraction fraction intx,inty 310 denominator y 11 1213 fraction fraction 1417 加法 18c...

程式設計練習 複數四則運算

題目描述 編寫乙個程式,其功能為 通過鍵盤讀取兩個複數和運算子,完成複數運算的操作,並輸出計算的結果 問題分析 a bi c di a bi c di a bi c di a bi c di 結果的實部 a ca c a c b d a c b d c c d d 結果的虛部 b db d b c ...