複數的四則運算(C語言實現)

WTomster發表於2020-11-28

複數的四則運算

問題描述:設計一個可進行復數運算的演示程式。

基本要求:實現下列七種基本運算:1)由輸入的實部和虛部生成一個複數;2)複數的顯示;3)兩個複數求和;4)兩個複數求差;5)兩個複數求積;6)從已知複數中分離出實部;7)從已知複數中分離出虛部。

測試資料:對以下資料實現求和。

(1)0;0;應輸出“0”

(2)3.1,0;4.22,8.9;應輸出“7.32+i8.9”

(3)-1.33.2.34;0.1,-6.5;應輸出“-1.23-i4.16”

(4)0,9.7;-2.1,-9.7;應輸出“-2.1”

(5)7.7,-8;-7.7,0;應輸出“-i8”

實現提示:定義複數為由兩個相互之間存在次序關係的實數構成的抽象資料型別,則可以利用實數的操作來實現複數的操作。

選做內容:實現複數的其他運算,如兩個複數相除,求共軛。

過程:TODO

程式碼

// complex.h
#pragma once

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define OK 1
#define ERROR 0
#define INFINITABLE 2

typedef int Status;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

	typedef struct {
		double m_real;
		double m_imaginal;
	}Complex;	

	/// @brief 由輸入的實部和虛部生成一個複數
	/// @param real		實部
	/// @param imaginal	虛部
	/// @return Status code
	Status create(double real, double imaginal, Complex* cpx);

	/// @brief 複數的顯示
	void print(Complex* complex);


	/// @brief 複數加法
	/// @param com1 複數
	/// @param com2 複數
	/// @return Status code
	Status add(Complex* com1, Complex* com2, Complex* cpx);

	/// @brief 複數減法
	/// @param com1 複數
	/// @param com2 複數
	/// @return Status code
	Status sub(Complex* com1, Complex* com2, Complex* cpx);

	/// @brief 複數乘法
	/// @param Complex* com1 複數
	/// @param Complex* com2 複數
	/// @return Status code
	Status mul(Complex* com1, Complex* com2, Complex* cpx);

	/// @brief 複數求商
	/// @param com1 被除數
	/// @param com2 除數
	/// @param cpx 結果
	/// @return Status code
	Status qua(Complex* com1, Complex* com2, Complex* cpx);

	/// @brief 複數求共軛
	/// @param cpx 複數
	/// @return Status code
	Status conj(Complex* cpx);

	/// @brief 從已知複數中分離出實部
	/// @param Complex* cpx 複數
	/// @return double real 實部
	double real(Complex* cpx);

	/// @brief 從已知複數中分離出虛部
	/// @param Complex* cpx 複數
	/// @return double real 虛部
	double imaginal(Complex* cpx);

#ifdef __cplusplus
}
#endif // __cplusplus

// complex.c
#include "complex.h"


Status create(double real, double imaginal, Complex* cpx)
{
	if (!cpx) return ERROR;
	cpx->m_real = real;
	cpx->m_imaginal = imaginal;
	return OK;
}

void print(Complex* complex)
{
	if (!complex) return;
	
	double r = complex->m_real,
			i = complex->m_imaginal;
	if (r && i)
	{
		printf("%g%ci%g\n",r, complex->m_imaginal > 0 ? '+' : '-', fabs(complex->m_imaginal));
	}
	else if (!r && i)
	{
		printf("%ci%g\n",complex->m_imaginal > 0 ? '+' : '-', fabs(complex->m_imaginal));
	}
	else if (r && !i)
	{
		printf("%g\n", r);
	}
	else if (!r && !i)
	{
		printf("0\n");
	}
	
}

Status add(Complex* com1, Complex* com2, Complex* cpx)
{
	if (!com1 || !com2 || !cpx) return ERROR;
	cpx->m_real = com1->m_real + com2->m_real;
	cpx->m_imaginal = com1->m_imaginal + com2->m_imaginal;
	return OK;
}

Status sub(Complex* com1, Complex* com2, Complex* cpx)
{
	if (!com1 || !com2 || !cpx) return ERROR;
	cpx->m_real = com1->m_real - com2->m_real;
	cpx->m_imaginal = com1->m_imaginal - com2->m_imaginal;
	return OK;
}

Status mul(Complex* com1, Complex* com2, Complex* cpx)
{
	if (!com1 || !com2 || !cpx) return ERROR;
	cpx->m_real = com1->m_real * com2->m_real - com1->m_imaginal * com2->m_imaginal;
	cpx->m_imaginal = com1->m_imaginal * com2->m_real + com1->m_real * com2->m_imaginal;
	return OK;
}

Status qua(Complex* com1, Complex* com2, Complex* cpx)
{
	if (!com1 || !com2 || !cpx) return ERROR;
	if (!com2->m_real && !com2->m_imaginal) return INFINITABLE;
	double tmp = com2->m_real* com2->m_real + com2->m_imaginal * com2->m_imaginal;
	cpx->m_real = (com1->m_real * com2->m_real + com1->m_imaginal * com2->m_imaginal) / tmp;
	cpx->m_imaginal = (com1->m_imaginal * com2->m_real - com1->m_real * com2->m_imaginal) / tmp;
	return OK;
}

Status conj(Complex* cpx)
{
	if (!cpx) return ERROR;
	cpx->m_imaginal = -cpx->m_imaginal;
	return OK;
}

double real(Complex* cpx)
{
	if (!cpx) return ERROR;
	return cpx->m_real;
}

double imaginal(Complex* cpx)
{
	if (!cpx) return ERROR;
	return cpx->m_imaginal;
}

// main.c
#include "complex.h"

void banner()
{
	printf("****************************************************\n\n");
	printf("               實驗一:複數四則運算                  \n\n");
	printf("****************************************************\n\n");
}

/// @brief 一級選單
void menu()
{
	printf("----------------------------------------------------\n");
	printf("\t功***能***菜***單:\n\n");
	printf("\t\t1. 複數求和\n");
	printf("\t\t2. 複數求差\n");
	printf("\t\t3. 複數求積\n");
	printf("\t\t4. 複數求商\n");
	printf("\t\t5. 複數求共軛\n");
	printf("\t\t6. 取實部\n");
	printf("\t\t7. 取虛部\n");
	printf("----------------------------------------------------\n");
}


void complex_calc(Status(*calc)(Complex*, Complex*, Complex*))
{
	Complex c1;
	Complex c2;
	Complex result;
	printf("請輸入兩個複數(格式real,imaginal;real,imaginal):\n");
	double r1, r2, i1, i2;
	scanf("%lf,%lf;%lf,%lf;", &r1, &i1, &r2, &i2);
	create(r1, i1, &c1);
	create(r2, i2, &c2);
	switch (calc(&c1, &c2, &result))
	{
	case ERROR:
		perror("程式異常\n");
		return;
	case INFINITABLE:
		perror("除數不能為0\n");
		return;
	}
	printf("結果為:");
	print(&result);
}

void complex_addition()
{
	complex_calc(add);
}

void complex_substration()
{
	complex_calc(sub);
}

void complex_product()
{
	complex_calc(mul);
}

void complex_quotient()
{
	complex_calc(qua);
}

void complex_conjugation()
{
	Complex cpx;
	printf("請輸入一個複數(格式real,imaginal;):\n");
	double r, i;
	scanf("%lf,%lf;", &r, &i);
	create(r, i, &cpx);
	conj(&cpx);
	print(&cpx);
}

void print_real()
{
	Complex cpx;
	printf("請輸入一個複數(格式real,imaginal;):\n");
	double r, i;
	scanf("%lf,%lf;", &r, &i);
	create(r, i, &cpx);
	printf("實部:%g\n", real(&cpx));
}

void print_imaginal()
{
	Complex cpx;
	printf("請輸入一個複數(格式real,imaginal;):\n");
	double r, i;
	scanf("%lf,%lf;", &r, &i);
	create(r, i, &cpx);
	printf("虛部:%g\n", imaginal(&cpx));
}

void complex_func()
{
	system("cls");
	int choose = 0, sec_n = 0;
	banner();
	menu();
	printf("請輸入選項(1~6): ");
	if (scanf("%d", &choose) != 1) return;
	switch (choose)
	{
	case 1:
		complex_addition();
		break;
	case 2:
		complex_substration();
		break;
	case 3:
		complex_product();
		break;
	case 4:
		complex_quotient();
		break;
	case 5:
		complex_conjugation();
		break;
	case 6:
		print_real();
		break;
	case 7:
		print_imaginal();
		break;
	default:
		printf("選項不存在,請重新輸入!");
	}
	system("pause");
}

int main()
{
	while (1) complex_func();
	return 0;
}

結語

介面設計有問題。

程式邏輯不清晰。

失敗。。。。。。

有空改吧。。。。。

相關文章