标题:复数计算求检验,自己检验过了不过不完全确定正确
只看楼主
yuchao130
Rank: 2
等 级:论坛游民
帖 子:20
专家分:32
注 册:2014-2-19
结帖率:80%
已结贴  问题点数:10 回复次数:3 
复数计算求检验,自己检验过了不过不完全确定正确
//author: Yuchao liao
//2014/3/11
//a complex number calculator
//The program should ask the user which of the five operations he wants to perform. It then asks the user to
//enter the appropriate values for the complex numbers and performs the necessary computation.

#include<stdio.h>
#include<math.h>
#define PI 3.1415926

void read_complex(float *z_r, float *z_i) // reads a complex number from the keyboard
{
    float a,b;
    printf("Enter a complex number:");
    scanf_s("%f %f",&a,&b);
    *z_r=a;
    *z_i=b;
}

void print_complex(float z_r, float z_i)
{
    printf("result:%.1f+(%.1fi)",z_r,z_i);
}

void add_complex(float z1_r, float z1_i, float z2_r, float z2_i, float *z3_r, float *z3_i) // z3=z1+z2
{
    *z3_r=z1_r+z2_r;
    *z3_i=z1_i+z2_i;
}

void sub_complex(float z1_r, float z1_i, float z2_r, float z2_i, float *z3_r, float *z3_i) // z3=z2-z1
{
    *z3_r=z1_r-z2_r;
    *z3_i=z1_i-z2_i;
}

void mul_complex(float z1_r, float z1_i, float z2_r, float z2_i, float *z3_r, float *z3_i) //z3=z1*z2
{
    *z3_r=z1_r*z2_r-z1_i*z2_i;
    *z3_i=z1_i*z2_r+z2_i*z1_r;
}

void div_complex(float z1_r, float z1_i, float z2_r, float z2_i, float *z3_r, float *z3_i) // z3=z1/z2
{
    *z3_r=(z1_r*z2_r+z1_i*z2_i)/(z2_r*z2_r+z2_i*z2_i);
    *z3_i=(z1_i*z2_r-z2_i*z1_r)/(z2_r*z2_r+z2_i*z2_i);
}

void par_complex(float z1_r, float z1_i, float *mag, float *ph) // magnitude and phase
{
    *mag=z1_r*z1_r+z1_i*z1_i;
    *ph=atan(z1_i/z1_r)*180/PI;
}

[ 本帖最后由 yuchao130 于 2014-3-14 05:11 编辑 ]
搜索更多相关主题的帖子: include complex keyboard necessary calculator calculator necessary keyboard complex include 
2014-03-12 10:34
yuchao130
Rank: 2
等 级:论坛游民
帖 子:20
专家分:32
注 册:2014-2-19
得分:0 
我这样直接发代码是不是不太好啊?
2014-03-12 10:57
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:7 
C语言有原生的复数类(关键字_Complex),功能比你这个多,用法比你这个简单
2014-03-12 11:03
yuchao130
Rank: 2
等 级:论坛游民
帖 子:20
专家分:32
注 册:2014-2-19
得分:0 
回复 3楼 rjsp
老师规定的条件,很坑爹还用指针,我们是学到哪用到哪
2014-03-12 11:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-428350-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.150380 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved