#include<stdio.h>
void main() { float x,y,z; char c;
scanf("%f%c%f",&x,&c,&y);
switch ( c ) {
case '+': z=x+y; break;
case '-': z=x-y; break;
case '*': z=x*y; break;
case '/': z=( y==0 )?(0):(x/y); break;
default: z=0; break;
}
printf("%f%c%f=%f\n",x,c,y,z);
}
void add(int a,int b,int c)
{
c=a+b;
printf("%d+%d = %d",a,b,c);
printf("\n");
}
void minus(int a,int b,int c)
{
c=a-b;
printf("%d-%d=%d",a,b,c);
printf("\n");
}
void multiplication(int a,int b,int c)
{
c=a*b;
printf("%d*%d=%d",a,b,c);
printf("\n");
}
void div(int a,int b,int c)
{
(float)c=(float)a/(float)b;
printf("%f/%f=%f",a,b,c);
printf("\n");
}
main()
{
int a,b,c;
char p;
printf("请输入数字 A:");
scanf("%d",&a);
printf("请输入数字 B:");
scanf("%d",&b);
printf("请输入运算的符号:");
getchar();
p=getchar();
if(p=='+') add(a,b,c);else
if(p=='-') minus(a,b,c);else
if(p=='*') multiplication(a,b,c);else
if(p=='/') div(a,b,c);else
puts("没有注册这个运算符号\n");
}
方法一:
#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);
}
2013-08-16
void main()
{
int a,b,result;
char m;
printf("请输入需要计算的数:\n");
scanf("%d %d",&a,&b);
printf("请输入加、减、乘或除\n");
scanf("%c",&m);
if(m=="+") //判断是否进行加法运算,以下同理
result=a+b;
else if(m=="-")
result=a-b;
elsee if(m=="*")
result=a*b;
else if(m=="/")
result=a/b;
else
printf("您输入有误\n"); //如果输入的符号非加减乘或是除,报错
printf("计算结果为:%d\n",result); //最后输出结果
}
2013-08-16