数据结构 (抽象数据类型的定义:以复数为例)
# include<>
#include<iostream>
#include <iomanip>
using namespace std;
# include<>
typedef struct{
float Realpart;//实部
float Imagepart;//虚部
}Complex;
void Create (Complex &C,float x,float y);//创建复数
float GetReal(Complex C);//求实部
float GetImag(Complex C);//求虚部
Complex Add(Complex C1,Complex C2);//求复数之和
Complex Sub(Complex C1,Complex C2);//求复数之差
int main()
{
Complex a,b,sum,sub;
float x,y,x1,y1;
cin>>x>>y>>x1>>y1;//输入
Create(a,x,y);
Create(b,x1,y1);
// GetReal(a);
// GetReal(b);
// GetImag(a);
// GetImag(b);
cout<<a.Realpart <<endl;
cout<<a.Imagepart <<endl;
cout<<a.Realpart <<'+'<<a.Imagepart <<"*i"<<endl;
cout<<b.Realpart <<endl;
cout<<b.Imagepart <<endl;
cout<<b.Realpart <<'+'<<b.Imagepart <<"*i"<<endl;
sum=Add(a,b);
cout<<"两复数智和为:"<<sum.Realpart <<'+'<<sum.Imagepart<<"*i"<<endl;
sub=Sub(a,b);
cout<<"两复数之差为:"<<sub.Realpart <<'+'<<sub.Imagepart <<"*i"<<endl;
return 0;
}
void Create (Complex &C,float x,float y)
{
C.Realpart=x;
C.Imagepart=y;
}
float GetReal(Complex C)
{
return C.Realpart;
}
float GetImag(Complex C)
{
return C.Imagepart;
}
Complex Add(Complex C1,Complex C2)
{
Complex sum;
sum.Realpart =C1.Realpart +C2.Realpart ;
sum.Imagepart =C1.Imagepart +C2.Imagepart ;
return sum;
}
Complex Sub(Complex C1,Complex C2)
{
Complex difference;
difference.Realpart =C1.Realpart -C2.Realpart ;
difference.Imagepart =C1.Imagepart -C2.Imagepart ;
return difference;
}