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++实现复数运算complex a(2,5),b(7,8),c(x,y)实部和实部相加,虚部同理,即x=2+7;y=5+8,即c为(9,13)
是不是让你设计一个复数类?如果是的话就简单了:)
//定义复数类complex
class complex{
private:
int real; //实部
int image; //虚部
public:
complex(int x=0,int y=0){ real = x; image = y; } //构造函数
//重载加号运算符,功能为:两个复数对象相加
friend complex operator + (const complex& a, const complex& b)
{
complex c;
c.real = a.real + b.real;
c.image = a.image + b.image;
return c;
}
}
//使用
complex a(2,5),b(7,8),c;
c = a + b;
3. 写一个c语言程序(输入两个复数(格式如2+3i),输出两个复数相加的结果。)
#include<stdio.h>
int main()
{
int x1,y1, x2, y2;
printf("Please input complex 1:");
scanf("%d+%di", &x1, &y1);
printf("Please input complex 2:");
scanf("%d+%di", &x2, &y2);
printf("The sum is=%d+%di\n", x1+x2, y1+y2);
return 0;
}
以上程序是最简单的形式,输入时要注意只能x+yi这样输入,各个字符间不能有空格
4. c语言 复数的加减法 满足手写规则
输入输出样例里面的分号
,不确定是真会输出,还是只是分隔输入输出。
常见的应该没有分号。输入1+i
2输出3+i
这个程序,
麻烦在于如何将输入的数据,转为复数。
可以先将输入
存为两个字符串,对每个字符串进行解析,转为复数。
对于每个字符串,先查找是否存在i,这个很简单,如果有,那么一定是最后一个。
如果不存在i,那么虚部为0,将字符串转为实部即可。
如果存在i,那么查找是否存在+或者-,如果不存在,说明没有实部,实部设置为0
如果存在只有-,
同时-为字符串第一个元素,
那么一样没有实部。
如果存在+,或者存在不是首元素的-,说明同时有实部和虚部,区分两部分,分别转换。
转换后,得到复数,简单的相加,得到结果。
输出时,一样要判断实部,虚部是否为0,然后再根据复数规则输出。
这部分相对要简单的多了。
5. 编程: 输入两个复数的实部和虚部求和,差积
楼上几位光说不写~~~~~~~
#include<stdio.h>
void sddf(int x1,int y1,int x2,int y2);
void subf(int x1,int x2,int y1,int y2);
void mulf(int x1,int x2,int y1,int y2);
void prtf(int a,int b);
void main()
{
int x1,y1,x2,y2;
printf("输入复数a的实部x1=");
scanf("%d",&x1);
printf("输入复数a的虚部y1=");
scanf("%d",&y1);
printf("输入复数b的实部x2=");
scanf("%d",&x2);
printf("输入复数b的虚部y2=");
scanf("%d",&y2);
printf("\n");
sddf(x1,y1,x2,y2);
subf(x1,y1,x2,y2);
mulf(x1,y1,x2,y2);
}
void prtf(int a,int b) /*输出函数*/
{
printf("%d",a);printf("+");printf("(%d",b);printf("i)\n");
}
void sddf(int x1,int y1,int x2,int y2) /*加运算*/
{
int a,b;
a=x1+x2;
b=y1+y2;
printf("复数之和为:");
prtf(a,b);
}
void subf(int x1,int x2,int y1,int y2) /*减运算*/
{
int a,int b;
a=x1-x2;
b=y1-y2;
printf("复数之差为");
prtf(a,b);
}
void mulf(int x1,int x2,int y1,int y2) /*乘积运算*/
{
int a,b;
a=x1*x2-y1*y2;
b=x1*y2+x2*y1;
printf("复数之积为:");
prtf(a,b);
}
6. 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;
}
(6)实部相加虚部相加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;
7. 输入实部和虚部,求该复数 C语言
#include "stdio.h"
main()
{
float a,b;
printf("input a and b:");
scanf("%f %f",&a,&b);
printf(">>%.g+%.gi\n",a,b);
}
8. 设计一个c++程序,实现两个复数相加。
#include "iostream.h"
typedef struct
{
float real;
float imag;
}complex;\\先定义一个复数型变量
float a=0,b=0;
void complex_add()
{\\定义复数相加的函数
complex z1,z2;
cout<<"请输入第一个复数:"<<endl;
cin>>z1.real>>z1.imag;
if(z1.imag<0)
cout<<"第一个复数为z1="<<z1.real<<z1.imag<<"j"<<endl;
if(z1.imag==0)
cout<<"第一个复数为z1="<<z1.real<<endl;
if(z1.imag>0)
cout<<"第一个复数为z1="<<z1.real<<"+"<<z1.imag<<"j"<<endl;
cout<<endl;
cout<<endl;
cout<<"请输入第二个复数:"<<endl;
cin>>z2.real>>z2.imag;
if(z2.imag<0)
cout<<"第二个复数为z2="<<z2.real<<z2.imag<<"j"<<endl;
if(z2.imag==0)
cout<<"第二个复数为z2="<<z2.real<<endl;
if(z2.imag>0)
cout<<"第二个复数为z2="<<z2.real<<"+"<<z2.imag<<"j"<<endl;
a=z1.real+z2.real;
b=z1.imag+z2.imag;
}
void main()
{
complex_add();
cout<<"两个复数之和为:"<<endl;
if(b<0)
cout<<"z="<<a<<b<<"j"<<endl;
if(b==0)
cout<<"z="<<a<<endl;
if(b>0)
cout<<"z="<<a<<"+"<<b<<"j"<<endl;
}
9. C语言函数问题
complex complex::operator+(const complex &c)
{
return complex(real+c.real,image+c.image);//这个函数传一个对象进去,
这段程序意思是 实数再加一个实数,虚数再加一个虚数?
}
这个函数的意思是操作符重载,通俗点说就是,定义当两个你定义的complex对象用+连接的时候,程序是如何执行的,否则,程序不知道当你用+号连接两个complex对象的时候怎么执行的。
对于复数相加的话,就是实部与实部相加,虚部与虚部相加,这个没有错