导航:首页 > 源码编译 > 负数四则运算源码

负数四则运算源码

发布时间:2022-04-19 15:04:04

① 数据结构课程:用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;}
下面是具体的操作:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#defineERR-1
#defineMAX100/*定义堆栈的大小*/
intstack[MAX];/*用一维数组定义堆栈*/
inttop=0;/*定义堆栈指示*/
intpush(inti)/*存储运算数,入栈操作*/
{
if(top<MAX)
{
stack[++top]=i;/*堆栈仍有空间,栈顶指示上移一个位置*/
return0;
}
else
{
printf("Thestackisfull");
returnERR;
}
}
intpop()/*取出运算数,出栈操作*/
{
intvar;/*定义待返回的栈顶元素*/
if(top!=NULL)/*堆栈中仍有元素*/
{
var=stack[top--];/*堆栈指示下移一个位置*/
returnvar;/*返回栈顶元素*/
}
else
printf("Thestackisempty! ");
returnERR;
}
voidmain()
{
intm,n;
charl;
inta,b,c;
intk;
do{
printf(" AriothmaticOperatesimulator ");/*给出提示信息*/
printf(" Pleaseinputfirstnumber:");/*输入第一个运算数*/
scanf("%d",&m);
push(m);/*第一个运算数入栈*/
printf(" Pleaseinputsecondnumber:");/*输入第二个运算数*/
scanf("%d",&n);
push(n);/*第二个运算数入栈*/
printf(" Chooseoperator(+/-/*//):");
l=getche();/*输入运算符*/
switch(l)/*判断运算符,转而执行相应代码*/
{
case'+':
b=pop();
a=pop();
c=a+b;
printf(" Theresultis%d ",c);
printf(" ");
break;
case'-':
b=pop();
a=pop();
c=a-b;
printf(" Theresultis%d ",c);
printf(" ");
break;
case'*':
b=pop();
a=pop();
c=a*b;
printf(" Theresultis%d ",c);
printf(" ");
break;
case'/':
b=pop();
a=pop();
c=a/b;
printf(" Theresultis%d ",c);
printf(" ");
break;
}
printf(" Continue?(y/n):");/*提示用户是否结束程序*/
l=getche();
if(l=='n')
exit(0);
}while(1);
}

② VC++编程:复数的四则运算

#include <iostream.h>

class complex
{
public:
complex() { real=imag=0; }
complex(double r, double i)
{
real = r, imag = i;
}
complex operator +(const complex &c);
complex operator -(const complex &c);
complex operator *(const complex &c);
complex operator /(const complex &c);
friend void print(const complex &c);
private:
double real, imag;
};

inline complex complex::operator +(const complex &c)
{
return complex(real + c.real, imag + c.imag);
}

inline complex complex::operator -(const complex &c)
{
return complex(real - c.real, imag - c.imag);
}

inline complex complex::operator *(const complex &c)
{
return complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real);
}

inline complex complex::operator /(const complex &c)
{
return complex((real * c.real + imag + c.imag) / (c.real * c.real + c.imag * c.imag),
(imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag));
}

void print(const complex &c)
{
if(c.imag<0)
cout<<c.real<<c.imag<<'i';
else
cout<<c.real<<'+'<<c.imag<<'i';
}

void main()
{
complex c1(2.0, 3.0), c2(4.0, -2.0), c3;
c3 = c1 + c2;
cout<<"\nc1+c2=";
print(c3);
c3 = c1 - c2;
cout<<"\nc1-c2=";
print(c3);
c3 = c1 * c2;
cout<<"\nc1*c2=";
print(c3);
c3 = c1 / c2;
cout<<"\nc1/c2=";
print(c3);
c3 = (c1+c2) * (c1-c2) * c2/c1;
cout<<"\n(c1+c2)*(c1-c2)*c2/c1=";
print(c3);
cout<<endl;
}

③ 用C++编写一个程序,实现复数的加、减、乘、除四则运算

float num1, num2, result;
char opt ,ct;
bool flag = true;
while (flag)
{
cin >> num1;
cin >> opt;
cin >> num2;

switch (opt)
{
case '+':
result = num1 + num2;
cout << result;
break;
case '-':
result = num1 - num2;
cout << result;
break;
case '*':
result = num1 * num2;
cout << result;
break;
case '/':
result = num1 / num2;
cout << result;
break;
default:
cout << "运算符输入错误!";
break;
}
cout << "是否继续(Y/N)?";
cin >> ct;
if (ct != 'y' && ct != 'Y')
{
flag = false;
}

}

大体就主要,自己完善

④ C++复数 类 实现,包含四则运算

template<class type>
class complex
{
protected:
type Re;//实部
type Im;//虚部
public:
complex():Re(),Im(){};//默认构造函数
complex(complex&a):Re(a.Re),Im(a.Im){};//拷贝构造函数
complex(type a,type b=0):Re(a),Im(b){};//构造函数,给实部、虚部赋值
type takeRe(){return Re;}//取实部
type takeIm(){return Im;}//取虚部
complex takeconj(){complex t(Re,-Im);return t;}//求共轭复数
complex operator =(complex&a)
{
Re=a.Re;
Im=a.Im;
return*this;
}
complex operator +()
{
return*this;
}
complex operator -()
{
complex t(-Re,-Im);
return t;
}
complex operator +(complex&a)
{
complex t(Re+a.Re,Im+a.Im);
return t;
}
complex operator -(complex&a)
{
complex t(Re-a.Re,Im-a.Im);
return t;
}
complex operator *(complex&a)
{
complex t(Re*a.Re-Im*a.Im,Re*a.Im+Im*a.Re);
return t;
}
complex operator /(complex&a)
{
type w=a*a+b*b;
if(w)
{
complex t((Re*a.Re+Im*a.Im)/w,(Im*a.Re-Re*a.Im)/w);
return t;
}
else
return;
}
};

⑤ 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);
}

⑥ 复数四则运算 编程

#include <iostream.h>

class Complex{
private:
float _real;
float _image;
public:
Complex(float real=0,float image=0);
Complex operator +(const Complex &rhs);
Complex operator -(const Complex &rhs);
Complex operator *(const Complex &rhs);
float GetReal()const;
float GetImage()const;
};

Complex::Complex(float real,float image)
{
_real=real;
_image=image;
}

Complex Complex::operator +(const Complex &rhs)
{
_real+=rhs.GetReal();
_image+=rhs.GetImage();
return *this;
}

Complex Complex::operator -(const Complex &rhs)
{
_real-=rhs.GetReal();
_image-=rhs.GetImage();
return *this;
}

Complex Complex::operator *(const Complex &rhs)
{
_real=_real*rhs.GetReal()-_image*rhs.GetImage();
_image=_real*rhs.GetImage()+_image*rhs.GetReal();
return *this;
}

float Complex::GetReal()const
{
return _real;
}

float Complex::GetImage()const
{
return _image;
}

void main()
{
cout<<"依次输入两个复数的实部和虚部"<<endl;
float realA,imageA,realB,imageB;
cin>>realA>>imageA>>realB>>imageB;
Complex A(realA,imageA);
Complex B(realB,imageB);
Complex temp;
//减法和乘法类似
temp=A+B;
cout<<"两者之和为:"<<temp.GetReal()<<"+"<<temp.GetImage()<<"i"<<endl;
cout<<"其实部为"<<temp.GetReal()<<"虚部为"<<temp.GetImage()<<endl;
}

⑦ 复数四则运算用C语言代码写出

要是能用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; }

满意请采纳

⑧ 如何用c语言编一个复数的四则运算的程序(完整版的,能运行的)

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);
}

⑨ c++语言对复数的四则运算的实现

#include<iostream.h>
struct complex //定义结构类型
{
double real;
double img ;
};
complex add(complex f1,complex f2); //声明调用函数add()
complex sub(complex f1,complex f2); //声明调用函数sub()
complex mul(complex f1,complex f2); //声明调用函数mul()
complex div(complex f1,complex f2); //声明调用函数div()

void main()
{
char op; //定义操作数
double t;
complex c1,c2,c3; //定义三个复数

cout<<"请输入第一个数的实部和虚部"<<endl;

cin>>c1.real>>c1.img;

cout<<"请输入第二个数的实部和虚部"<<endl;

cin>>c2.real>>c2.img;

cout<<"请选择运算符号 + - * / "<<endl;

cin>>op;
switch(op)
{

case '+':

cout<<"上面两个复数的和是:"<<endl;
c3=add(c1,c2); //调用add函数
if(c3.real>0)
cout<<"c1+c2="<<c3.real<<"+"<<c3.img<<"i"<<endl;
else
cout<<"c1+c2="<<c3.real<<c3.img<<"i"<<endl;
break;

case '-':

cout<<"上面两个复数的差是:"<<endl;
c3=sub(c1,c2); //调用sub函数
if(c3.real>0)
cout<<"c1-c2="<<c3.real<<"+"<<c3.img<<"i"<<endl;
else
cout<<"c1-c2="<<c3.real<<c3.img<<"i"<<endl;
break;

case '*':

cout<<"上面两个复数的乘积是:"<<endl;
c3=mul(c1,c2); //调用mul函数
if(c3.img>0)
cout<<"c1*c2="<<c3.real<<"+"<<c3.img<<"i"<<endl;
else
cout<<"c1*c2="<<c3.real<<c3.img<<"i"<<endl;
break;

case '/':

cout<<"上面两个复数的商是:"<<endl;
c3=div(c1,c2); //调用div函数
t=(c2.real*c2.real+c2.img*c2.img);
if(t!=0)
cout<<"c1/c2="<<c3.real<<"/"<<c3.img<<"i"<<endl;//把div函数的return值付给c1/c2,并输出
else
cout<<"输入错误,第二个复数不能为零";

break;
default:
cout<<"输入运算符错误";

}
}
complex add(complex f1,complex f2 )//定义函数使两复数相加,并返回值
{
complex f3;
f3.real=f1.real+f2.real;
f3.img=f1.img+f2.img;
return f3;
}

complex sub(complex f1,complex f2)//定义函数使两复数相减,并返回值
{
complex f3;
f3.real=f1.real-f2.real;
f3.img=f1.img-f2.img;
return f3;
}

complex mul(complex f1,complex f2)//定义函数使两复数相乘,并返回值
{
complex f3;
f3.real=f1.real*f2.real-f1.img*f2.img;
f3.img=f1.real*f2.img+f1.img*f2.real;
return f3;
}
complex div(complex f1,complex f2)//定义函数使两复数相除,并返回值
{
complex f3;
double t;
t=1/(f2.real*f2.real+f2.img*f2.img);
f3.real=(f1.real*f2.real+f1.img*f2.img)*t;
f3.img=(f2.real*f1.img-f1.real*f2.img)*t;
return f3;
}

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

1、设计一个可进行复数运算的演示程序。要求实现下列六种基本运算
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;}
2、例程:

stdio.h>
#include<conio.h>
#include<stdlib.h>
#defineERR-1
#defineMAX100/*定义堆栈的大小*/
intstack[MAX];/*用一维数组定义堆栈*/
inttop=0;/*定义堆栈指示*/
intpush(inti)/*存储运算数,入栈操作*/
{
if(top<MAX)
{
stack[++top]=i;/*堆栈仍有空间,栈顶指示上移一个位置*/
return0;
}
else
{
printf("Thestackisfull");
returnERR;
}
}
intpop()/*取出运算数,出栈操作*/
{
intvar;/*定义待返回的栈顶元素*/
if(top!=NULL)/*堆栈中仍有元素*/
{
var=stack[top--];/*堆栈指示下移一个位置*/
returnvar;/*返回栈顶元素*/
}
else
printf("Thestackisempty! ");
returnERR;
}
voidmain()
{
intm,n;
charl;
inta,b,c;
intk;
do{
printf(" AriothmaticOperatesimulator ");/*给出提示信息*/
printf(" Pleaseinputfirstnumber:");/*输入第一个运算数*/
scanf("%d",&m);
push(m);/*第一个运算数入栈*/
printf(" Pleaseinputsecondnumber:");/*输入第二个运算数*/
scanf("%d",&n);
push(n);/*第二个运算数入栈*/
printf(" Chooseoperator(+/-/*//):");
l=getche();/*输入运算符*/
switch(l)/*判断运算符,转而执行相应代码*/
{
case'+':
b=pop();
a=pop();
c=a+b;
printf(" Theresultis%d ",c);
printf(" ");
break;
case'-':
b=pop();
a=pop();
c=a-b;
printf(" Theresultis%d ",c);
printf(" ");
break;
case'*':
b=pop();
a=pop();
c=a*b;
printf(" Theresultis%d ",c);
printf(" ");
break;
case'/':
b=pop();
a=pop();
c=a/b;
printf(" Theresultis%d ",c);
printf(" ");
break;
}
printf(" Continue?(y/n):");/*提示用户是否结束程序*/
l=getche();
if(l=='n')
exit(0);
}while(1);
}
阅读全文

与负数四则运算源码相关的资料

热点内容
谷歌框架服务器地址 浏览:378
女程序员蓝天白云 浏览:829
钢铁命令解说 浏览:252
什么购物app是正品 浏览:478
安卓系统断网怎么回事 浏览:456
黑马程序员第9章 浏览:703
汽车编程所用的函数 浏览:447
云管理服务器如何注册 浏览:208
linux下重启网卡 浏览:118
乐橙怎么加密 浏览:262
幸识是个什么样的app 浏览:54
程序员直男认口红 浏览:37
雕刻机的编程点怎么算 浏览:643
ftp服务器的http地址 浏览:940
单位吨标煤最简单算法 浏览:423
企业微信中设置服务器是什么 浏览:385
闪电侠解压视频 浏览:293
rgb灯条51单片机 浏览:768
问道4月5日为什么服务器超时 浏览:991
服务器的url地址是什么 浏览:973