题目位于:1.3;PPT:第一章29;
标准答案:(来源自数据结构与算法基础(青岛大学——王卓) note_Tarench的博客-CSDN博客)
#include
using namespace std;
typedef struct {
float realpart; //实部
float imagpart; //虚部
}Complex;
/*函数声明*/
void assign(Complex* A, float real, float imag); //赋值
//加减乘除
void add(Complex* C, Complex A, Complex B);
void minus(Complex* C, Complex A, Complex B);
void mutiply(Complex* C, Complex A, Complex B);
void divide(Complex* C, Complex A, Complex B);
/*函数实现*/
void assign(Complex* A, float real, float imag)
{
A->realpart = real;
A->imagpart = imag;
}
void add(Complex* C, Complex A, Complex B)
{
C->realpart = A.realpart + B.realpart;
C->imagpart = A.imagpart + B.imagpart;
}
void minus(Complex* C, Complex A, Complex B)
{
C->realpart = A.realpart - B.realpart;
C->imagpart = A.imagpart - B.imagpart;
}
void mutiply(Complex* C, Complex A, Complex B)
{
C->realpart = A.realpart * B.realpart - A.imagpart * B.imagpart;
C->imagpart = A.realpart * B.imagpart + A.imagpart * B.realpart;
}
void divide(Complex* C, Complex A, Complex B)
{
C->realpart = (A.realpart * B.realpart + A.imagpart * B.imagpart) / (B.realpart * B.realpart + B.imagpart * B.imagpart);
C->imagpart = (B.realpart * A.imagpart - B.imagpart * A.realpart) / (B.realpart * B.realpart + B.imagpart * B.imagpart);
}
(学习该块知识过程中)碰到的之前没学过的,之前不会的新学的,零碎的知识点和问题:
一:typedef struct
(1):这是个什么东西?
(2):这个typedef总感觉之前学习C++模块的基础知识时好像看到过
但是具体在哪里真的学过翻书翻了半天好像又找不出来
(3):之前我们在C++里面学习定义一个新的类型(结构体)时不都是用:
struct <新建类型名>
的格式吗?
关于typedef struct的详细解释,详见:typedef struct_宇 -Yu的博客-CSDN博客
在这里,简单来说,我们只需要知道: 模块
typedef struct {
float realpart; //实部
float imagpart; //虚部
}Complex;
等价于
struct Complex {
float realpart; //实部
float imagpart; //虚部
};
最终(已经实践验证)结果一样,把这里的typedef struct模块替换为我们熟悉的struct格式的模块即可;
将大段程序简化拆解(把五种运算拆开来一个一个说):
#include
using namespace std;
struct Complex
{//复杂的<复数>
float realpart; //实部
float imagpart; //虚部
};
/*函数声明*/
void assign(Complex* A, float real, float imag); //赋值
/*函数实现(定义)*/
void assign(Complex* A, float real, float imag)
{
A->realpart = real;
A->imagpart = imag;
}
给复数赋值部分,程序运行步骤相当于:
(A->realpart) = real;
(A->imagpart) = imag;
即:
第一个语句:
指针A访问结构体中的realpart部分
相当于(A->realpart)就是(代表)complex结构体中的realpart实数型变量,把这一部分调用出来
并且(然后),把函数给出的实参real赋给这个realpart部分
同样的,第二个语句:
指针A访问结构体中的imagpart部分
相当于(A->imagpart)就是(代表)complex结构体中的imagpart实数型变量,把这一部分调用出来
并且(然后),把函数给出的实参imag赋给这个imagpart部分
于是,赋值操作就完成啦
将该程序补充完整:
#include
using namespace std;
struct Complex
{//复杂的<复数>
float realpart; //实部
float imagpart; //虚部
};
/*函数声明*/
void assign(Complex* A, float real, float imag); //赋值
/*函数实现(定义)*/
void assign(Complex* A, float real, float imag)
{
(A->realpart) = real;
(A->imagpart) = imag;
}
int main()
{
Complex n;
Complex* p = &n;
assign(p, 1, 2);
cout << "该复数等于:"<realpart<<"+"<imagpart<<"i"<
结果:
至于后面其他的加减乘除的函数定义,和赋值部分相似
(用“->”调用出所输出结果的实部/虚部部分,然后再针对该部分的运算规则分别给其赋值)
在此不再赘述,唯一有一点需要说明的,就是关于复数的除法公式:(如下)
另外,在除法运算中其实还默认要求分母不等于0,补充程序为:(标准答案multiply打错了)
#include
using namespace std;
typedef struct //复杂的<复数>
{
float realpart; //实部
float imagpart; //虚部
}Complex;
/*函数声明*/
void assign(Complex* A, float real, float imag); //赋值
//加减乘除
void add(Complex* C, Complex A, Complex B);
void minus(Complex* C, Complex A, Complex B);
void mutiply(Complex* C, Complex A, Complex B);
void divide(Complex* C, Complex A, Complex B);
/*函数实现*/
void assign(Complex* A, float real, float imag)
{
A->realpart = real;
A->imagpart = imag;
}
void add(Complex* C, Complex A, Complex B)
{
C->realpart = A.realpart + B.realpart;
C->imagpart = A.imagpart + B.imagpart;
}
void minus(Complex* C, Complex A, Complex B)
{
C->realpart = A.realpart - B.realpart;
C->imagpart = A.imagpart - B.imagpart;
}
void multiply(Complex* C, Complex A, Complex B)
{
C->realpart = A.realpart * B.realpart - A.imagpart * B.imagpart;
C->imagpart = A.realpart * B.imagpart + A.imagpart * B.realpart;
}
void divide(Complex* C, Complex A, Complex B)
{
while (B.realpart * B.realpart + B.imagpart * B.imagpart != 0)
C->realpart = (A.realpart * B.realpart + A.imagpart * B.imagpart) / (B.realpart * B.realpart + B.imagpart * B.imagpart);
C->imagpart = (B.realpart * A.imagpart - B.imagpart * A.realpart) / (B.realpart * B.realpart + B.imagpart * B.imagpart);
}
最后,我们再来补充完整标准答案中没有写的主函数:(具体实例实际具体运算部分)
void 输出结果(Complex a)
{
cout << "结果为: " << a.realpart
<< "+" << a.imagpart << "i;" << endl;
}
int main()
{
Complex z1, z2,a,m,result;
assign(&z1, 8, 6);
assign(&z2, 4, 3);
add(&a,z1,z2);
multiply(&m, z1, z2);
divide(&result, m, a);
输出结果(result);
}
最终:
#include
using namespace std;
typedef struct //复杂的<复数>
{
float realpart; //实部
float imagpart; //虚部
}Complex;
/*函数声明*/
void assign(Complex* A, float real, float imag); //赋值
//加减乘除
void add(Complex* C, Complex A, Complex B);
void minus(Complex* C, Complex A, Complex B);
void mutiply(Complex* C, Complex A, Complex B);
void divide(Complex* C, Complex A, Complex B);
/*函数实现*/
void assign(Complex* A, float real, float imag)
{
A->realpart = real;
A->imagpart = imag;
}
void add(Complex* C, Complex A, Complex B)
{
C->realpart = A.realpart + B.realpart;
C->imagpart = A.imagpart + B.imagpart;
}
void minus(Complex* C, Complex A, Complex B)
{
C->realpart = A.realpart - B.realpart;
C->imagpart = A.imagpart - B.imagpart;
}
void multiply(Complex* C, Complex A, Complex B)
{
C->realpart = A.realpart * B.realpart - A.imagpart * B.imagpart;
C->imagpart = A.realpart * B.imagpart + A.imagpart * B.realpart;
}
void divide(Complex* C, Complex A, Complex B)
{
while (B.realpart * B.realpart + B.imagpart * B.imagpart != 0)
C->realpart = (A.realpart * B.realpart + A.imagpart * B.imagpart)
/ (B.realpart * B.realpart + B.imagpart * B.imagpart);
C->imagpart = (B.realpart * A.imagpart - B.imagpart * A.realpart)
/ (B.realpart * B.realpart + B.imagpart * B.imagpart);
}
void 输出结果(Complex a)
{
cout << "结果为: " << a.realpart
<< "+" << a.imagpart << "i;" << endl;
}
int main()
{
Complex z1, z2,a,m,result;
assign(&z1, 8, 6);
assign(&z2, 4, 3);
add(&a,z1,z2);
multiply(&m, z1, z2);
divide(&result, m, a);
输出结果(result);
}
但是为啥运行不出来嘞???
md你一个判断语句,你在这放while循环干什么???这不是陷入死循环么??
把while改成if即可:
#include
using namespace std;
typedef struct //复杂的<复数>
{
float realpart; //实部
float imagpart; //虚部
}Complex;
/*函数声明*/
void assign(Complex* A, float real, float imag); //赋值
//加减乘除
void add(Complex* C, Complex A, Complex B);
void minus(Complex* C, Complex A, Complex B);
void mutiply(Complex* C, Complex A, Complex B);
void divide(Complex* C, Complex A, Complex B);
/*函数实现*/
void assign(Complex* A, float real, float imag)
{
A->realpart = real;
A->imagpart = imag;
}
void add(Complex* C, Complex A, Complex B)
{
C->realpart = A.realpart + B.realpart;
C->imagpart = A.imagpart + B.imagpart;
}
void minus(Complex* C, Complex A, Complex B)
{
C->realpart = A.realpart - B.realpart;
C->imagpart = A.imagpart - B.imagpart;
}
void multiply(Complex* C, Complex A, Complex B)
{
C->realpart = A.realpart * B.realpart - A.imagpart * B.imagpart;
C->imagpart = A.realpart * B.imagpart + A.imagpart * B.realpart;
}
void divide(Complex* C, Complex A, Complex B)
{
if(B.realpart * B.realpart + B.imagpart * B.imagpart != 0)
{
C->realpart = (A.realpart * B.realpart + A.imagpart * B.imagpart)
/ (B.realpart * B.realpart + B.imagpart * B.imagpart);
C->imagpart = (B.realpart * A.imagpart - B.imagpart * A.realpart)
/ (B.realpart * B.realpart + B.imagpart * B.imagpart);
}
}
void 输出结果(Complex a)
{
cout << "结果为: " << a.realpart
<< "+" << a.imagpart << "i;" << endl;
}
int main()
{
Complex z1, z2, a, m, result;
assign(&z1, 8, 6);
assign(&z2, 4, 3);
add(&a, z1, z2);
multiply(&m, z1, z2);
divide(&result, m, a);
输出结果(result);
}
结果: