C语言数据结构问题 30
C语言数据结构问题编程实现如下功能(1)根据复数x+yi格式建立一个抽象教据类型Complex采用恰当的存储方式记录实部和虑部。(2)在Complex类型上,实现Init...
C语言数据结构问题编程实现如下功能
(1) 根据复数x+yi 格式建立一个抽象教据类型Complex 采用恰当的存储方式记录实部和
虑部。
(2) 在Complex 类型上,实现InitComplex()函数,定义复数类型变量。。
(3) 实现加(add)、减(sub)、乘(multi)、除(div) 操作。。
(4) 输入两个复数类型变量,验证四则运算结果。 展开
(1) 根据复数x+yi 格式建立一个抽象教据类型Complex 采用恰当的存储方式记录实部和
虑部。
(2) 在Complex 类型上,实现InitComplex()函数,定义复数类型变量。。
(3) 实现加(add)、减(sub)、乘(multi)、除(div) 操作。。
(4) 输入两个复数类型变量,验证四则运算结果。 展开
1个回答
展开全部
#include <stdio.h>
#include <string.h>
struct Complex{
float rmz; //实部
float lmz;//虚部
};
//两个复数求和
struct Complex addComplex(struct Complex complex1,struct Complex complex2)
{
struct Complex Node;
Node.rmz=complex1.rmz+complex2.rmz;
Node.lmz=complex1.lmz+complex2.lmz;
return Node;
}
//求两个复数的差
struct Complex subComplex(struct Complex complex1,struct Complex complex2)
{
struct Complex Node;
Node.rmz=complex1.rmz-complex2.rmz;
Node.lmz=complex1.lmz-complex2.lmz;
return Node;
}
//求两个复数的积
struct Complex multiComplex(struct Complex complex1,struct Complex complex2)
{
struct Complex Node;
Node.rmz=complex1.rmz*complex2.rmz-complex1.lmz*complex2.lmz;
Node.lmz=complex1.lmz*complex2.rmz+complex2.lmz*complex2.rmz;
return Node;
}
//求两个复数的商
struct Complex divComplex(struct Complex complex1,struct Complex complex2)
{
struct Complex Node;
Node.rmz=(complex1.rmz*complex2.rmz+complex1.lmz*complex2.lmz)/(complex2.rmz*complex2.rmz+complex2.lmz*complex2.lmz);
Node.lmz=(complex1.lmz*complex2.rmz-complex2.lmz*complex1.rmz)/(complex2.rmz*complex2.rmz+complex2.lmz*complex2.lmz);
return Node;
}
void InitComplex(struct Complex* a) {
printf("input complex:\n");
scanf("%f%f", &a->rmz, &a->lmz);
}
int main() {
struct Complex complex1, complex2,result;
char sym;
memset(&complex1, 0x0, sizeof(struct Complex));
memset(&complex2, 0x0, sizeof(struct Complex));
memset(&result, 0x0, sizeof(struct Complex));
InitComplex(&complex1);
printf("input symbol:\n");
getchar();
scanf("%c", &sym);
getchar();
InitComplex(&complex2);
switch (sym) {
case '+':
result = addComplex(complex1, complex2);
break;
case '-':
result = subComplex(complex1, complex2);
break;
case '*':
result = multiComplex(complex1, complex2);
break;
case '/':
if (0 == complex2.rmz && 0 == complex2.lmz) {
printf("wrong chushu\n");
return -1;
}
result = divComplex(complex1, complex2);
break;
default:
printf("wrong sym\n");
return -1;
}
printf("The result is %f+%fi \n", result.rmz, result.lmz);
return 0;
}
#include <string.h>
struct Complex{
float rmz; //实部
float lmz;//虚部
};
//两个复数求和
struct Complex addComplex(struct Complex complex1,struct Complex complex2)
{
struct Complex Node;
Node.rmz=complex1.rmz+complex2.rmz;
Node.lmz=complex1.lmz+complex2.lmz;
return Node;
}
//求两个复数的差
struct Complex subComplex(struct Complex complex1,struct Complex complex2)
{
struct Complex Node;
Node.rmz=complex1.rmz-complex2.rmz;
Node.lmz=complex1.lmz-complex2.lmz;
return Node;
}
//求两个复数的积
struct Complex multiComplex(struct Complex complex1,struct Complex complex2)
{
struct Complex Node;
Node.rmz=complex1.rmz*complex2.rmz-complex1.lmz*complex2.lmz;
Node.lmz=complex1.lmz*complex2.rmz+complex2.lmz*complex2.rmz;
return Node;
}
//求两个复数的商
struct Complex divComplex(struct Complex complex1,struct Complex complex2)
{
struct Complex Node;
Node.rmz=(complex1.rmz*complex2.rmz+complex1.lmz*complex2.lmz)/(complex2.rmz*complex2.rmz+complex2.lmz*complex2.lmz);
Node.lmz=(complex1.lmz*complex2.rmz-complex2.lmz*complex1.rmz)/(complex2.rmz*complex2.rmz+complex2.lmz*complex2.lmz);
return Node;
}
void InitComplex(struct Complex* a) {
printf("input complex:\n");
scanf("%f%f", &a->rmz, &a->lmz);
}
int main() {
struct Complex complex1, complex2,result;
char sym;
memset(&complex1, 0x0, sizeof(struct Complex));
memset(&complex2, 0x0, sizeof(struct Complex));
memset(&result, 0x0, sizeof(struct Complex));
InitComplex(&complex1);
printf("input symbol:\n");
getchar();
scanf("%c", &sym);
getchar();
InitComplex(&complex2);
switch (sym) {
case '+':
result = addComplex(complex1, complex2);
break;
case '-':
result = subComplex(complex1, complex2);
break;
case '*':
result = multiComplex(complex1, complex2);
break;
case '/':
if (0 == complex2.rmz && 0 == complex2.lmz) {
printf("wrong chushu\n");
return -1;
}
result = divComplex(complex1, complex2);
break;
default:
printf("wrong sym\n");
return -1;
}
printf("The result is %f+%fi \n", result.rmz, result.lmz);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询