急!!!复数运算C语言程序
structcomplex{
floatrmz;//实部
floatlmz;//虚部
};
//产生一个复数.
complexgetacomplex(floata,floatb){
complexnode=newcomplex();
node.rmz=a;
node.lmz=b;
returnnode;}
//两个复数求和
complexaddcomplex(complexcomplex1,complexcomplex2)
{
complexnode=newcomplex();
node.rmz=complex1.rmz+complex2.rmz;
node.lmz=complex1.lmz+complex2.lmz;
returnnode;
}
//求两个复数的差
complexsubcomplex(complexcomplex1,complexcomplex2)
{
complexnode=newcomplex();
node.rmz=complex1.rmz-complex2.rmz;
node.lmz=complex1.lmz-complex2.lmz;
returnnode;
}
//求两个复数的积
complexproductcomplex(complexcomplex1,complexcomplex2)
{
complexnode=newcomplex();
node.rmz=complex1.rmz*complex2.rmz-complex1.lmz*complex2.lmz;
node.lmz=complex1.lmz*complex2.rmz+complex2.lmz*complex2.rmz;
returnnode;
}
//求实部
floatgetcomplexrmz(complexcomplex1)
{
returncomplex1.rmz;
}
//求虚部
floatgetcomplexlmz(complexcomplex1)
{
returncomplex1.lmz;
}
C语言(有关复数)
你在VC中得不到正确的结果是因为printf函数的描述符用错了,把%d改为%f
在TC中编译错误是因为TC比VC检查要严格,是一个标准的C编译器,而VC其实是一个VC++编译器
在TC中声明了结构之后定义结构变量不能省略struct关键字,如要省略,则必须给该结构类型起个别名。对于你的程序来说,只要把复数类型声明改为如下方式即可,你试试吧:
typedef struct {
double real;
double imag;
} complex;
如何用c语言编一个复数的四则运算的程序(完整版的,能运行的)
stdio.h
#includeconio.h
#includestdlib.h
#define ERR -1
#define MAX 100 /*定义堆栈的大小*/
int stack[MAX]; /*用一维数组定义堆栈*/
int top=0; /*定义堆栈指示*/
int push(int i) /*存储运算数,入栈操作*/
{
if(topMAX)
{
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语言复数代码
刚学习也 呵呵 写的不细致。
忽忽~ 我错咯~ 我自己看书,半个月,还没看到那去,肯定写搞笑咯,我表示的只是一个形式,非真正的复数也~ 算咯,摆那我也不删它咯。
brbr你可以不用函数,自己改了就行。
brbr
brbr/**
brbrbr* Generate the sum and product of two plural numbers.
brbrbr*/
brbrbr#include stdio.h
brbrbr
brbrbrstruct pluralNum {
brbrbr float x; float y;};
brbrbr
brbrbrint main(void)
brbrbr{
brbrbr struct pluralNum p1, p2;
brbrbr struct pluralNum getPluralNum(void);
brbrbr void calSum(struct pluralNum a, struct pluralNum b);
brbrbr void calProduct(struct pluralNum a, struct pluralNum b);
brbrbr
brbrbr p1 = getPluralNum();
brbrbr p2 = getPluralNum();
brbrbr
brbrbr calSum(p1, p2);
brbrbr calProduct(p1, p2);
brbrbr
brbrbr printf(“Thank You!\n”);
brbrbr return 0;
brbrbr}
brbrbr
brbrbr
brbrbrstruct pluralNum getPluralNum(void)
brbrbr{
brbrbr struct pluralNum temp;
brbrbr static int count = 1;
brbrbr
brbrbr printf(“Please specify the plural number.\n”);
brbrbr printf(“real number: x = “);
brbrbr scanf(“%f”, temp.x);
brbrbr printf(“imaginary number: y = “);
brbrbr scanf(“%f”, temp.y);
brbrbr
brbrbr printf(“You specified the plural number p%i =:\n”, count);
brbrbr printf(“%.2f+%.2fi\n”, temp.x, temp.y);
brbrbr count++;
br
brbrbr return (temp);
brbrbr}
brbrbr
brbrbr
brbrbrvoid calSum(struct pluralNum a, struct pluralNum b)
brbrbr{
brbrbr
brbrbrprintf(“The sum of p1 and p2 is:\n”);
brbrbr printf(“%.2f+%.2fi\n”, a.x + b.x, a.y + b.y);
brbrbr}
brbrbr
brbrbr
brbrbrvoid calProduct(struct pluralNum a, struct pluralNum b)
brbrbr{
brbrbr float realNum, imaginaryNum;
brbrbr
brbrbr realNum = a.x * b.x – a.y * b.y;
brbrbr imaginaryNum = a.x * b.y + b.x * a.y;
brbrbr
brbrbr printf(“The product of p1 and p2 is:\n”);
brbrbr printf(“%.2f+%.2fi\n”, realNum, imaginaryNum);
brbrbr}