C/C++实现复数相关运算,相关解释在代码中进行注释。 原创亓官劼,请勿抄袭,转载请注明出处。 实现了复数的ADT,基本操作有创建复数、初始化、获取实部、获取虚部、加法、减法、乘法、打印复数等操作。 大家好,我叫亓官劼(qí guān jié ),在ImapBox中记录学习的点滴历程,时光荏苒,未来可期,加油~博客地址为:亓官劼的博客,B站昵称为: 本文原创为亓官劼,请大家支持原创,部分平台一直在盗取博主的文章!!!
C/C++实现复数相关运算
#pragma warning(disable : 4996) #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<string> #include<algorithm> using namespace std; struct Complex{ double Re;//实部 double Im;//虚部 }; void CreatComplex(struct Complex input[],int Re,int Im,int n);//创建复数 void InitialComplex(struct Complex input[],int n);//初始化; double GetRe(struct Complex input[],int n);//获得实部 double GetIm(struct Complex input[], int n);//获得虚部 Complex AddComplex(struct Complex input[], int n1, int n2);//加法 Complex MinusComplex(struct Complex input[], int n1, int n2);//减法 Complex MultiplicationComplex(struct Complex input[], int n1, int n2);//乘法 void PrintComplex(struct Complex input[],int n);//打印 int main(){ //复数ADT int i; Complex a[100]; int m = 0; printf("请输入需要进行的操作:n"); printf("1:初始化复数n"); printf("2:创建复数n"); printf("3:获得实部n"); printf("4:获得虚部n"); printf("5:两个复数的加法n"); printf("6:两个复数的减法n"); printf("7:两个复数的乘法n"); printf("8:打印已有所有复数n"); printf("9:退出程序n"); while (1) { scanf("%d", &i); switch (i) { case 1: { for (int n = 0; n < 100; n++) { InitialComplex(a,n); } printf("全部初始化完毕,请继续选择操作n"); } break; case 2: { double Re, Im; printf("请输入创建复数的实部,虚部n"); scanf("%lf%lf",&Re,&Im); CreatComplex(a, Re, Im,m); m++; printf("创建完毕,请继续选择操作n"); } break; case 3: { int n; printf("请输入获取第几个复数的实部:n"); scanf("%d", &n); printf("第%d个复数的实部为:%f", n, GetRe(a,n)); } break; case 4: { int n; printf("请输入获取第几个复数的虚部:n"); scanf("%d", &n); printf("第%d个复数的虚部为:%lf", n, GetIm(a,n)); } break; case 5: { int n1, n2; printf("请输入第几个复数和第几个复数相加:n"); scanf("%d%d",&n1,&n2); printf("相加后的结果为:%lf+%lf i", AddComplex(a,n1,n2).Re, AddComplex(a, n1, n2).Im); } break; case 6: { int n1, n2; printf("请输入第几个复数和第几个复数相减:n"); scanf("%d%d", &n1, &n2); printf("相减后的结果为:%f+%f i", MinusComplex(a, n1, n2).Re, MinusComplex(a, n1, n2).Im); } break; case 7: { int n1, n2; printf("请输入第几个复数和第几个复数相乘:n"); scanf("%d%d", &n1, &n2); printf("相乘后的结果为:%f+%f i", MultiplicationComplex(a, n1, n2).Re, MultiplicationComplex(a, n1, n2).Im); } break; case 8: { int n; for (n = 0; n < m; n++) { PrintComplex(a,n); } } break; case 9:return 0; default:printf("请输入正确选项n"); break; } } return 0; } void CreatComplex(struct Complex input[], int Re, int Im,int n) {//创建复数 input[n].Re=Re; input[n].Im=Im; } void InitialComplex(struct Complex input[],int n){//初始化; input[n].Re=0; input[n].Im=0; } double GetRe(struct Complex input[], int n) {//获得实部 return input[n].Re; } double GetIm(struct Complex input[], int n) {//获得虚部 return input[n].Im; } Complex AddComplex(struct Complex input[],int n1,int n2) {//加法 Complex re; re.Re=input[n1].Re+input[n2].Re; re.Im=input[n1].Im+input[n2].Im; return re; } Complex MinusComplex(struct Complex input[], int n1, int n2){//减法 Complex re; re.Re=input[n1].Re-input[n2].Re; re.Im=input[n1].Im-input[n2].Im; return re; } Complex MultiplicationComplex(struct Complex input[], int n1, int n2){//乘法 Complex re; re.Re=((input[n1].Re*input[n2].Re)-(input[n1].Im*input[n2].Im)); re.Im=((input[n1].Im*input[n2].Re)-(input[n1].Re*input[n2].Im)); return re; } void PrintComplex(struct Complex input[],int n){//打印 printf("%lf + %lf in",input[n].Re,input[n].Im); }
亓官劼
,地址为亓官劼的B站
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算