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