當前位置:首頁 » 編程語言 » c語言計算復數之積
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言計算復數之積

發布時間: 2022-05-14 07:46:45

c語言:輸入倆個復數的實部與虛部,計算倆個復數之積,求解為何錯了聲明怎麼聲明

#include<stdio.h>
floatresult_real,result_imag;
voidcomplex_prod(floatx1,floaty1,floatx2,floaty2);//聲明函數
intmain(void)
{
floatimag1,imag2,real1,real2;
printf("enter1stcomplexnumber(realandimaginary):");
scanf("%f%f",&real1,&imag1);
printf("enter2ndcomplexnumber(realandimaginary):");
scanf("%f%f",&real2,&imag2);

complex_prod(real1,imag1,real2,imag2);//調用函數
printf("proctofcomplexis%f+%fi ",result_real,result_imag);
return0;
}

voidcomplex_prod(floatx1,floaty1,floatx2,floaty2)//這里多了分號,參數定義錯
{
//floatresult_real,result_imag;不要了,用全局變數
result_real=x1*x2-y1*y2;//
result_imag=x1*y2+x2*y1;
//returnresult_real,result_imag;用不到
}

㈡ 數據結構課程:用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;
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;
}

㈣ C語言題目

#include
#include
#include
#include
#include

//定義二叉樹節點數據結構
typedef
struct
node{
struct
node
*lchild;
char
data;
struct
node
*rchild;
}bitnode,*bitree;
//構造截取子串函數,start從零開始.
char
*get_substr(char
*strDest,int
start,int
end)
{
if(start>end)
return
NULL;
//如果左子樹或右子樹為空則返回空串
char
*strSub;
//字串指針
strSub=(char*)malloc((end-start+2)*sizeof(char));
int
i;
for(i=start;i<=end;i++)
strSub[i-start]=strDest[i];
strSub[end-start+1]='\0';
return
strSub;
}
//前序遍歷二叉樹,並輸出
void
preorder_traverse(bitree
bt){
if(bt
!=
NULL)
{
printf("%c",bt->data);
preorder_traverse(bt->lchild);
preorder_traverse(bt->rchild);
}
}
//根據中序和後序遍歷結果遞歸構造二叉樹,並返回根指針
bitree
create_bitree(char
*inorder,char
*postorder)
//inorder和postoeder分別為中序和後序遍歷的結果字元串
{
if(inorder==NULL
||
postorder==NULL)
return
NULL;
//空串則返回空樹
char
root_data;//根節點字元
char
*lchild_instr,*lchild_postr,*rchild_instr,*rchild_postr;//左子樹和右子樹中序及後序字元串
int
i,len;
len=strlen(inorder);
//中序和後序字元串長度相等
bitree
new_bitree=(bitree)malloc(sizeof(bitnode));
root_data=postorder[len-1];//樹的根節點必然為中序遍歷的最後一個字元
new_bitree->data=root_data;
for(i=0;i<len;i++)
//找到根節點在中序遍歷中位置
{
if(inorder[i]==root_data)
break;
}
//確定左子樹和右子樹中序及後序字元串
lchild_instr=get_substr(inorder,0,i-1);
lchild_postr=get_substr(postorder,0,i-1);
rchild_instr=get_substr(inorder,i+1,len-1);
rchild_postr=get_substr(postorder,i,len-2);
//遞歸構造子樹
new_bitree->lchild=create_bitree(lchild_instr,lchild_postr);
new_bitree->rchild=create_bitree(rchild_instr,rchild_postr);
return
new_bitree;
}
void
main(){
char
inorder[20]="BDCEAFHG";
char
postorder[20]="DECBHGFA";
bitree
root;
root=create_bitree(inorder,postorder);
//輸出前序遍歷結果
printf("the
binary
tree
in
preorder
is:\n");
preorder_traverse(root);
_getch();
}
//滿意的話別忘了多加點分哈

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