C语言 要求编写一个简单计算器的程序

Python020

C语言 要求编写一个简单计算器的程序,第1张

方法一:

#include <stdio.h>

#include<string.h>

#include<math.h>

#include<stdlib.h>

struct complex multiply(struct complex x, struct complex y)

struct complex{

int real

int imag

}

int main()

{

struct complex a,b,s

scanf("%d%d%d%d",&a.real,&a.imag,&b.real,&b.imag)

s=multiply(a,b)

printf("(%d+%di)*(%d+%di)=%d+%di\n",a.real,a.imag,b.real,b.imag,s.real,s.imag)

return 0

}

struct complex multiply(struct complex x, struct complex y)

{

struct complex m

m.real=x.real*y.real-x.imag*y.imag

m.imag=x.imag*y.real+x.real*y.imag

return m

}

方法二:

#include<stdio.h>

int main()

{

int a,b,c,d,e,f

scanf("%d %d %d %d",&a,&b,&c,&d)

e = a * c - b * d

f = a * d + b * c

printf("(%d+%di)*(%d+%di)=%d+%di\n",a,b,c,d,e,f)

}

#include<stdio.h>

int main()

{

double num1

double num2

double result

char ch

printf("Please enter express to caculate, 'q' to exit(eg. 1+3):")

while(scanf("%lf%c%lf",&num1,&ch,&num2) == 3)

{

switch(ch)

{

case '+':

{

result = num1 + num2

break

}

case '-':

{

result = num1 - num2

break

}

case '/':

{

if(num2 == 0)

printf("Error:div/0\n")

else

result = num1 / num2

break

}

case '*':

{

result = num1 * num2

break

}

}

printf("%g%c%g=%g\n",num1,ch,num2,result)

printf("Please enter express to caculate, 'q' to exit(eg. 1+3):")

}

return 0

}