当前位置:首页 » 编程语言 » c语言怎么求复数解
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言怎么求复数解

发布时间: 2022-10-03 07:51:42

1. c语言复数四则运算

我们设计一个可进行复数运算的演示程序。要求实现下列六种基本运算
:1)由输入的实部和虚部生成一个复数
;2)两个复数求和;
3)两个复数求差;
4)两个复数求积,
5)从已知复数中分离出实部;
6)从已知复数中分离出虚部。
运算结果以相应的复数或实数的表示形式显示(最好用结构体的方法)
要是能用c++和stl,可以这样写#include <complex>#include <iostream>void main(){ using namespace std; complex<double> a(3, 2); complex<double> b(5, 6); complex<double> result(0,0); result = a*b/(a+b); cout << result;}
下面是具体的操作:

stdio.h>
#include<conio.h>
#include<stdlib.h>
#define ERR -1
#define MAX 100 /*定义堆栈的大小*/
int stack[MAX]; /*用一维数组定义堆栈*/
int top=0; /*定义堆栈指示*/

int push(int i) /*存储运算数,入栈操作*/
{
if(top<MAX)
{
stack[++top]=i; /*堆栈仍有空间,栈顶指示上移一个位置*/
return 0;
}
else
{
printf("The stack is full");
return ERR;
}
}
int pop() /*取出运算数,出栈操作*/
{
int var; /*定义待返回的栈顶元素*/
if(top!=NULL) /*堆栈中仍有元素*/
{
var=stack[top--]; /*堆栈指示下移一个位置*/
return var; /*返回栈顶元素*/
}
else
printf("The stack is empty!\n");
return ERR;
}
void main()
{
int m,n;
char l;
int a,b,c;
int k;
do{
printf("\tAriothmatic Operate simulator\n"); /*给出提示信息*/
printf("\n\tPlease input first number:"); /*输入第一个运算数*/
scanf("%d",&m);
push(m); /*第一个运算数入栈*/
printf("\n\tPlease input second number:"); /*输入第二个运算数*/
scanf("%d",&n);
push(n); /*第二个运算数入栈*/
printf("\n\tChoose operator(+/-/*//):");
l=getche(); /*输入运算符*/
switch(l) /*判断运算符,转而执行相应代码*/
{
case '+':
b=pop();
a=pop();
c=a+b;
printf("\n\n\tThe result is %d\n",c);
printf("\n");
break;
case '-':
b=pop();
a=pop();
c=a-b;
printf("\n\n\tThe result is %d\n",c);
printf("\n");
break;
case '*':
b=pop();
a=pop();
c=a*b;
printf("\n\n\tThe result is %d\n",c);
printf("\n");
break;
case '/':
b=pop();
a=pop();
c=a/b;
printf("\n\n\tThe result is %d\n",c);
printf("\n");
break;
}
printf("\tContinue?(y/n):"); /*提示用户是否结束程序*/
l=getche();
if(l=='n')
exit(0);
}while(1);
}

2. C语言 复数表示与求和

在数学中一个复数可以定义为 (z=a + bi) 的形式。 C 语言在 ISO C99 时就引入了复数类型。它是通过 complex.h 中定义的。 我们可以使用 complex , __complex__ , 或 _ComplexI 类型符号来表示。

在C语言中有三种复数类型,分别为 float complex , double complex , long double complex 。他们之间 的区别就是表示复数中实部和虚步的数的数据类型不同。 complex 其实就是一个数组,数组中有两个元素,一个表示复数的实部,一个表示复数的虚部。

源代码如下:

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

double sum(double* x);

void main()
{

double *a,s=0.0;
a=(double*)malloc(sizeof(double));
*a=5;
s=sum(a);

printf("求和的结果是: %lf ",s);

}double sum(double* x)

{

int j=0;
double s=0.0;

for(j=0;j<=3;j++)
{
s=s+pow(*x,j);
}

s=s*2;

return s;
}

(2)c语言怎么求复数解扩展阅读

输入任意两个复数差与商的源代码如下

typedefstruct{

floatr;

floatim;

Complex;

Complexres;

Complex*add(Complex*a,Complex*b){

res.r=a->r+b->r;

res.im=a->im+b->im;

return&res;
}
Complex*div(Complex*a,Complex*b){

floatd=(b->r*b->r+b->im*b->im);

res.r=(a->r*b->r+a->im*b->im)/d;

res.im=(a->im*b->r-a->r*b->im)/d;

return&res;

3. 对于C语言中用求根公式求出复数怎么写

#include <stdio.h>

#include <math.h>

int main()

{ double a,b,c,d;

scanf("%lf%lf%lf",&a,&b,&c);

printf("%g %g %g ",a,b,c);

d=b*b-4*a*c;

if(d>0)

printf("x1=%g x2=%g ",(-b+sqrt(d))/(2*a),(-b-sqrt(d))/(2*a));

else if(d==0)

printf("x1=x2=%g ",-b/(2*a));

else

printf("x1=%g+%gi x2=%g-%gi ",-b/(2*a),sqrt(-d)/(2*a),-b/(2*a),sqrt(-d)/(2*a));

return 0;

}

4. C语言中怎么进行复数的定义及运算

定义成结构体 实部和虚部分别定义成double,然后在自己定义运算……
如果是C++的话,可以重载+、-、*、\操作符的方式

5. C语言怎么实现复数运算

这个是一个列子,可以参考下
struct complex{
float rmz; //实部
float lmz;//虚部
};
//产生一个复数.
complex getAComplex(float a,float b){
complex Node=new complex();
Node.rmz=a;
Node.lmz=b;
return Node;}
//两个复数求和
complex addComplex(complex complex1,complex complex2)
{
complex Node=new complex();
Node.rmz=complex1.rmz+complex2.rmz;
Node.lmz=complex1.lmz+complex2.lmz;
return Node;
}
//求两个复数的差
complex subComplex(complex complex1,complex complex2)
{
complex Node=new complex();
Node.rmz=complex1.rmz-complex2.rmz;
Node.lmz=complex1.lmz-complex2.lmz;
return Node;
}
//求两个复数的积
complex proctComplex(complex complex1,complex complex2)
{
complex Node=new complex();
Node.rmz=complex1.rmz*complex2.rmz-complex1.lmz*complex2.lmz;
Node.lmz=complex1.lmz*complex2.rmz+complex2.lmz*complex2.rmz;
return Node;
}
//求实部
float getComplexRmz(complex complex1)
{
return complex1.rmz;
}
//求虚部
float getComplexLmz(complex complex1)
{
return complex1.lmz;
}

6. 用c语言求解复数域一元二次方程

#include<stdio.h>
#include<math.h>
int main(){
int a,b,c;
int t;
//t就是b^2-4ac判断它和0的大小决定是解否是复数
printf("推出请按Ctrl+c,输入非数字后果自付.\n");
//Ctrl+c是 windows下的文件结束符.在命令行下不是拷贝快捷键
while(1){
a=b=c=t=0;
//初始化
scanf("%d %d %d",&a,&b,&c);
//输入数据
if(a==0){printf("input error!\n");continue;}
//如果输入二次项系数为零显示告诉用户
t=b*b-4*a*c;
if (t<0){
printf("x1=%.6lf+%.6lfi\n",-b/2.0/a,sqrt(-t)/2/a);
printf("x1=%.6lf-%.6lfi\n",-b/2.0/a,sqrt(-t)/2/a);

}else {
printf("x1=%.6lf+0i\n",-b/2.0/a+sqrt(t)/2/a);
printf("x1=%.6lf+0i\n",-b/2.0/a-sqrt(t)/2/a);
}
}
return 0;}

7. 用c语言如何输出复数

这不是c语言的问题。
数学上复数的表示方法是 a + bi,按这个形式输出就可以了,比如:
1+9i
-3.45-8.33i

输出方法这样就行:
print("%f+%fi", a, b);

8. 如何用c语言编一个复数的四则运算

创建一个结构体,分为实部和虚部两个成员。进行运算法时,利用成员变量进行做

9. 在C语言环境下实现复数运算

乱七八糟写了一个,肯定有很多不对的地方,懒得找了,如果你找到了发QQ(252290278)给我吧
#include
<stdio.h>
#include<string.h>
int
ope(char
num[])//计算有几个符号
{
int
count=0;
for(int
i=0;num[i]!='\0';i++)
if('+'==num[i]
||
'-'==num[i])
count++;
return
count;
}
void
inone(double
&a,double
&b)//输入一个复数
{
int
i,j,l;
char
num[100];
scanf("%s",num);
l=strlen(num);
if('i'==num[l-1])//有虚部
{
if(ope(num))//如果有符号
{
i=0;
if(2==ope(num))//有两个符号
{
if('+'==num[0])//如果实部是正数
{
for(i=1;i<l;i++)//将整数部分的值存放到整数a中
if('+'==num[i]
||
'-'==num[i])
{
for(j=1;j<i;j++)
a=a*10+num[j]-'0';
break;
}
}
else
if('-'==num[0])//如果实部是负数
{
for(i=1;i<l;i++)//将整数部分的值存放到整数a中
if('+'==num[i]
||
'-'==num[i])
{
for(j=1;j<i;j++)
a=a*10-(num[j]-'0');
break;
}
}
}
else
if(1==ope(num))//一个符号,即实部为正数或者没有实部
{
for(i=0;i<l;i++)//将整数部分的值存放到整数a中
if('+'==num[i]
||
'-'==num[i])
{
for(j=0;j<i;j++)
a=a*10+(num[j]-'0');
break;
}
}
//将虚数部分的值存放到整数b中
if('+'==num[i])//如果虚部是正数
for(j=i+1;j<l-1;j++)
b=b*10+num[j]-'0';
else
if('-'==num[i])
for(j=i+1;j<l-1;j++)
b=b*10-(num[j]-'0');
}
else//没有符号
for(i=0;i<l-1;i++)
b=b*10+num[j]-'0';
}
else//没有虚部
{
if('-'==num[0])//实部是负数
for(i=1;i<l;i++)//将整数部分的值存放到整数a中
a=a*10-(num[i]-'0');
else
{
i=0;
if('+'==num[0])//实部是正数
i++;
for(;i<l;i++)
a=a*10+num[i]-'0';
}
}
}
void
intwo(double
&a,double
&b,double
&c,double
&d)//输入两个复数
{
a=b=c=d=0;
printf("请输入第一个复数:\n");
inone(a,b);
printf("请输入第二个复数:\n");
inone(c,d);
}
void
pri(double
a,double
b)//输出
{
if(0==a
&&
0==b)
{
printf("0\n");
return;
}
if(a)
printf("%d",a);
if(b)
{
if(b>0)
printf("+");
printf("%di",b);
}
printf("\n");
}
void
add()//加
{
double
a,b,c,d;
intwo(a,b,c,d);
printf("这两个复数相加的结果为:");
pri(a+c,b+d);
}
void
sub()//减
{
double
a,b,c,d;
intwo(a,b,c,d);
printf("这两个复数相减的结果为:");
pri(a-c,b-d);
}
void
mul()//乘
{
double
a,b,c,d;
intwo(a,b,c,d);
printf("这两个复数相乘的结果为:");
pri(a*c-b*d,b*c+a*d);
}
void
exc()//除
{
double
a,b,c,d;
intwo(a,b,c,d);
if(c==0
&&
d==0)
{
printf("错误,除数为零!\n");
return;
}
printf("这两个复数相除的结果为:");
pri((a*c+b*d)/(c*c+d*d),(b*c-a*d)/c*c+d*d);
}
void
vei()//幂
{
int
n;
double
a,b,c,d;
intwo(a,b);
c=a;d=b;
printf("请出入n次幂(只能为整数!):\n");
scanf("%d",&n);
for(i=1;i<n;i++)
{
a=a*c-b*d;
b=b*c+a*d;
}
printf("这个复数相减的结果为:");
pri(a,b);
}
int
main()
{
int
n;
char
ch;
while(1)
{
printf("请选择需要的运算:\n");
printf("0.退出\n");
printf("1.加运算\n");
printf("2.减运算\n");
printf("3.乘运算\n");
printf("4.除运算\n");
printf("5.幂运算\n");
scanf("%c",&ch);
while('
'==ch
||
'\n'==ch)
scanf("%c",&ch);
if(ch<'0'
||
ch>'9')
{
printf("输入错误请重新输入!\n");
continue;
}
n=ch-'0';
switch(n)
{
case
0:
return
0;
case
1:
add();
break;
case
2:
sub();
break;
case
3:
mul();
break;
case
4:
exc();
break;
case
5:
vei();
break;
default:
printf("输入错误,请重新输入\n");
break;
}
}
return
0;
}

10. C语言中复数的运算怎么实现

C语言的话,C99标准是支持复数运算的.需要包含头文件complex.h,
里面几乎包含了所有复数计算会用到的基本初等函数, 比如三角函数,幂函数,对数函数等等.

gcc是个常见的支持C99标准的编译器,