C/C++實作複數相關運算
C/C++實作複數相關運算,相關解釋在代碼中進行注釋。
原創亓官劼,請勿抄襲,轉載請注明出處。
#pragma
#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 i\n",input[n].Re,input[n].Im);
}