用c語言寫出復(fù)數(shù)的抽象數(shù)據(jù)類型,,至少完成創(chuàng)建一個復(fù)數(shù),復(fù)數(shù)加減,,虛部等五,求一個復(fù)數(shù)的實部個操作
依據(jù)抽象數(shù)據(jù)類型的定義#include<stdio.h>typedef struct{float r;float v;}complex;complex Initcomplex(float a,float b){complex x;x.r=a;x.v=b;return x;}void printc(complex x){printf("%.2f+&.2fi",x.r,x.v);}complex addcomplex(complex x,complex y){complex result;result.r=x.r-y.r;result.v=x.v+y.r;return result;}void main(){complex x,y;x=Initcomplex(5,2);printc(x);y=Initcomplex(5,2); printc(x);}寫到這不會
只要加減的話,,參考:
#include<stdio.h>typedef struct{float r;float v;} complex;complex Initcomplex(float a,float b){complex x;x.r=a;x.v=b;return x;}void printc(complex x){if (x.v>=0)printf("%.2f+%.2fi",x.r,x.v);elseprintf("%.2f%.2fi",x.r,x.v);}complex Addcomplex(complex x,complex y){complex result;result.r=x.r+y.r;result.v=x.v+y.v;return result;}complex Minuscomplex(complex x,complex y){complex result;result.r=x.r-y.r;result.v=x.v-y.v;return result;}int main(){complex x,y;x=Initcomplex(1,2);printc(x);y=Initcomplex(3,4);printc(y);complex z;z=Addcomplex(x,y);printc(z);z=Minuscomplex(x,y);printc(z);return 0;}