『壹』 如何用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);
}
『貳』 用c語言編寫程序:輸入兩個復數,計算並輸出它們的和及乘積
輸入兩個復數,計算並輸出它們的和及乘積c語言編寫:
#include<stdio.h>
#include<math.h>
void main()
{ int i1,i2,j1,j2;
int sum(int i1,int i2,int j1,int j2);
int mul(int i1,int i2,int j1,int j2);
printf("請輸入第一個復數的實部和虛部:");
scanf("%d %d",&i1,&i2);
printf("請輸入第二個復數的實部和虛部:");
scanf("%d %d",&j1,&j2);
sum(i1,i2,j1,j2);
mul(i1,i2,j1,j2);
}
int sum(int i1,int i2,int j1,int j2)
{
int i,j;
i=i1+j1;
j=i2+j2;
printf("兩復數的和為:%d+%di ",i,j);
return 0;
}
int mul(int i1,int i2,int j1,int j2)
{
int i,j;
i=i1*j1-i2*j2;
j=i2*j1+i1*j2;
printf("兩復數的積為:%d+%di ",i,j);
return 0;
}
『叄』 數據結構c語言復數運算
1、首先打開vc6.0, 新建一個項目。
『肆』 C語言中復數的運算怎麼實現
C語言的話,C99標準是支持復數運算的.需要包含頭文件complex.h,
裡面幾乎包含了所有復數計算會用到的基本初等函數, 比如三角函數,冪函數,對數函數等等.
gcc是個常見的支持C99標準的編譯器,
『伍』 C語言函數題 復數的運算
定義成結構體 實部和虛部分別定義成double,然後在自己定義運算……
如果是C++的話,可以重載+、-、*、\操作符的方式
『陸』 用c語言 如何編寫兩個復數的運算啊 都含有虛部 謝謝啊
定義一個struct作為復數,然後分別定義加減剩除運算。可以增加一個函數printcomplex來在主函數中以數學的形式輸出復數。
typedef struct complex
{
double real; //實部
double image; //虛部
} COMPLEX;
COMPLEX add(COMPLEX a,COMPLEX b) //加法
{
COMPLEX sum;
sum.real = a.real+b.real;
sum.image = a.image+b.image;
return sum;
}
COMPLEX sub(COMPLEX a,COMPLEX b) //減法
{
COMPLEX diff;
diff.real = a.real-b.real;
diff.image = a.image-b.image;
return diff;
}
COMPLEX mul(COMPLEX a,COMPLEX b) //乘法
{
COMPLEX acc;
acc.real = a.real*b.real-a.image*b.image;
acc.image = a.real*b.image+a.image*b.real;
return acc;
}
COMPLEX divi(COMPLEX a,COMPLEX b) //除法
//除法去分母可以轉換為乘法
{
COMPLEX quo;
double den = b.real*b.real+b.image*b.image; //分母
/* 先判斷除數是否為0,因為均為double型,所以不能 直接與0作比較,而要用絕對值是否小於某個極小值e(讀伊夫西龍???)來判斷是否為0,這里取e=10e-10 */
if ((abs(b.real)<10e-10) && (abs(b.image)<10e-10))
{
printf("Divivd by Zero");
exit(0); //強制退出程序
}
quo.real = a.real*b.real+a.image*b.image;
quo.real /= den;
quo.image = a.image*b.real+a.real*b.image;
quo.image /= den;
return quo;
}
void printcom(COMPLEX a) //輸出復數a
{
printf("%lf+%lfi",a.real,a.image);
}
『柒』 如何用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語言編寫復數的四則運算
設計一個可進行復數運算的演示程序。要求實現下列六種基本運算
: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);
}
『玖』 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;
『拾』 c語言負數運算
/*
c語言復數運算
*/
#include<stdio.h>
#include<stdlib.h>
typedefstruct
{
doubler,i; //r為實部,i為虛部
}complex;
complex*add(complex*a,complex*b);//復數a+復數b
complex*minus(complex*a,complex*b); //復數a-復數b
complex*multiply(complex*a,complex*b);//復數a*復數b
voidprintComplex(complex*a);//輸出
complex*add(complex*a,complex*b){
complex*add;
add->r=a->r+b->r;
add->i=a->i+b->i;
returnadd;
}
complex*minus(complex*a,complex*b)
{
complex*minus;
minus->r=a->r-b->r;
minus->i=a->i-b->i;
returnminus;
}
complex*multiply(complex*a,complex*b)
{
complex*multiply;
multiply->r=a->r*b->r-a->i*b->i;
multiply->i=a->i*b->r+a->r*b->i;
returnmultiply;
}
voidprintComplex(complex*a)//輸出要注意虛部有負數的情況
{
if(a->r!=0)
printf("%.2lf",a->r); //不要斷行,先輸出實部
else
printf("0.00");
if(a->i>0) //大於0,正常輸出+虛部
printf("+%.2lfi ",a->i);
elseif(a->i<0) //虛部是負數,不用輸出+號了,直接輸出負數
printf("%.2lfi ",a->i);
else
printf("+0.00i ");//虛部為0
}
幫你改了下,再去試試吧。一時之間我也想不出還有什麼漏洞了。祝好運。如果是自己寫主函數的話,改一改那個輸入吧,輸入很容易會造成問題的
這一句: scanf("...%f %c ",...,c),一個不好輸入就會出錯,c可能會獲取到空白。