数据结构
#include<stdio.h>typedef struct //定义复数的实部和虚部
{}cpxNum;
double real;
double imag;
int main()
{
int k;
cpxNum c1,c2,result;
printf("enter the first cpxNum's real and imag:"); //输入第一个复数的实部和虚部
scanf("\n%f%f",&cpxNum.real,&cpxNum.imag);
c1=cpxNum.real+cpxNum.imag*i
printf("\ndisplay the first cpxNum %f",c1);
printf("\nenter the second cpxNum's real and imag:"); //输入第二个复数的实部和虚部
scanf("\n%f%f",&cpxNum.real,&cpxNum.imag);
c2=cpxNum.real+cpxNum.imag*i;
printf("\ndisplay the second cpxNum %f",c2);
printf("please choose a kind of aarithmetic:1、CPlus,2、Cminus,3、cmultiply,4、cdivide"); // 选择算法:加减乘除
scanf("%d",&k);
if(k<1||k>4)
printf("error,please enter the k again: ");
else if(k==1) //实现加法
{
result.real=c1.real+c2.real;
result.imag=c1.imag+c2.imag;
result=result.real+result.imag*i;
printf("\ndisplay the result %f",result);
}
else if(k==2) //实现减法
{
result.real=c1.real-c2.real;
result.imag=c1.imag-c2.imag;
result=result.real+result.imag*i;
printf("\ndisplay the result %f",result);
}
else if(k==3) //实现乘法
{
result.real=c1.real*c2.real-c1.imag*c2.imag;
result.imag=c1.real*c2.imag+c1.imag*c2.real;
result=result.real+result.imag*i;
printf("\ndisplay the result %f",result);
}
else if(k==4) //实现除法
{
result.real=(c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
result.imag=(c1.real*c2.imag-c1.imag*c2.real)/(c2.real*c2.real+c2.imag*c2.imag);
result=result.real+result.imag*i;
printf("\ndisplay the result %f",result);
}
return 0;
}
实现演示两个复数的加减乘除,里边存在语法错误,不知道错在哪,帮忙看一下,谢谢!
如果可以的话,能否指点一下是学习数据结构的方法%……
[ 本帖最后由 yxy1008611 于 2009-9-8 16:42 编辑 ]