導航:首頁 > 源碼編譯 > 負數四則運算源碼

負數四則運算源碼

發布時間: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);
}
閱讀全文

與負數四則運算源碼相關的資料

熱點內容
useradd命令linux 瀏覽:575
語言編程源碼分析 瀏覽:274
溫10怎麼設計加密文件 瀏覽:448
python源碼保護資料庫密碼 瀏覽:990
組織伺服器是什麼玩意 瀏覽:445
linux啟動失敗的原因 瀏覽:177
百度演算法面試 瀏覽:924
谷歌框架伺服器地址 瀏覽:378
女程序員藍天白雲 瀏覽:829
鋼鐵命令解說 瀏覽:252
什麼購物app是正品 瀏覽:478
安卓系統斷網怎麼回事 瀏覽:456
黑馬程序員第9章 瀏覽:703
汽車編程所用的函數 瀏覽:447
雲管理伺服器如何注冊 瀏覽:208
linux下重啟網卡 瀏覽:118
樂橙怎麼加密 瀏覽:262
幸識是個什麼樣的app 瀏覽:54
程序員直男認口紅 瀏覽:37
雕刻機的編程點怎麼算 瀏覽:643