English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

要如何用C++寫複數的四則運算...
希望能簡單易懂,因為我是初學者...
請大家幫個忙,謝謝~~~

2006-08-25 10:09:33 · 4 個解答 · 發問者 非洲雄獅 3 in 電腦與網際網路 程式設計

4 個解答

請問:
(1)typedef struct complexStruct complex; 是什麼意思?
(2)result.real=x.real + y.real; 是什麼意思?
(3)z=complexAdd(x,y); 是什麼意思?
希望你能快點看到,我有點急,用中文解釋它的功能,謝謝你的回答!

2006-08-29 10:04:34 補充:
非常感謝深度痞子...

2006-08-28 14:10:20 · answer #1 · answered by 非洲雄獅 3 · 0 0

Easy and fast way to do complex operation is to use the template "complex".
Following is an example.

#include
#include

using namespace std;
main(){
complex a(2.3,5.6);
complex b(4.1,8.2);
complex c;

cout<<"a=>Real: "< cout<<"b=>Real: "< c=a+b;
cout<<"a+b=>Real: "< c=a-b;
cout<<"a-b=>Real: "< c=a*b;
cout<<"a*b=>Real: "< }

Result:
a=>Real: 2.3 Image: 5.6
b=>Real: 4.1 Image: 8.2
a+b=>Real: 6.4 Image: 13.8
a-b=>Real: -1.8 Image: -2.6
a*b=>Real: -36.49 Image: 41.82

2006-08-26 05:08:26 · answer #2 · answered by ? 4 · 0 0

#include
#include

struct COMP {double r; double i;};

struct COMP cmpxadd(struct COMP *a, struct COMP *b)
{ struct COMP c;
c.r = a->r + b->r;
c.i = a->i + b->i;
return c;
}

struct COMP cmpxmin(struct COMP *a, struct COMP *b)
{ struct COMP c;
c.r = a->r - b->r;
c.i = a->i - b->i;
return c;
}

void cmpxprt(struct COMP c)
{ printf("%f %c %fi", c.r, c.i<0?'-':'+', c.i);
}

voidmain()
{ struct COMP a, b;

a.r = 3, a.i = 5;
b.r = 2, b.i = 4;

cmpxprt(cmpxadd(&a, &b));
cmpxprt(cmpxmin(&a, &b));
}

這樣會了嗎?
若還要寫出乘、除法,請告知。

加油! ^_^

2006-08-25 13:08:09 · answer #3 · answered by ? 7 · 0 0

請參考
http://tw.knowledge.yahoo.com/question/?qid=1406080316903

2006-08-25 10:28:12 · answer #4 · answered by Xiao Lan 4 · 0 0

fedest.com, questions and answers