#include<stdio.h>
int gcd(int a,int b)
{
while(a!=0&&b!=0)
if(a>=b)
a=a%b;
else
b=b%a;
if(a>b)
return a;
else
return b;
}
void jia(int a,int b,int c,int d)
{
int fenmu,fenzi,gongyueshu;
fenmu=b*d;fenzi=a*d+b*c;
gongyueshu=gcd(fenmu,fenzi);
fenmu=fenmu/gongyueshu;
fenzi=fenzi/gongyueshu;
if(fenmu==1)
printf("%d\n",fenzi);
else
printf("%d/%d\n",fenzi,fenmu);
}
void jian(int a,int b,int c,int d)
{
int fenmu,fenzi,gongyueshu;
fenmu=b*d;fenzi=a*d-b*c;
gongyueshu=gcd(fenmu,fenzi);
fenmu=fenmu/gongyueshu;
fenzi=fenzi/gongyueshu;
if(fenmu==1||fenzi==0)
printf("%d\n",fenzi);
else
printf("%d/%d\n",fenzi,fenmu);
}
void cheng(int a,int b,int c,int d)
{
int fenmu,fenzi,gongyueshu;
fenmu=b*d;fenzi=a*c;
gongyueshu=gcd(fenmu,fenzi);
fenmu=fenmu/gongyueshu;
fenzi=fenzi/gongyueshu;
if(fenmu==1||fenzi==0)
printf("%d\n",fenzi);
else
printf("%d/%d\n",fenzi,fenmu);
}
void chu(int a,int b,int c,int d)
{
/* int fenmu,fenzi,gongyueshu;
fenmu=b*c;fenzi=a*d;
gongyueshu=gcd(fenmu,fenzi);
fenmu=fenmu/gongyueshu;
fenzi=fenzi/gongyueshu;
if(fenmu==1||fenzi==0)
printf("%d\n",fenzi);
else
printf("%d/%d\n",fenzi,fenmu);*/ void cheng(a,b,d,c);
}
void main()
{
int a,b,c,d;
char e;
scanf("%d/%d%c%d/%d",&a,&b,&e,&c,&d);
switch(e)
{
case '+':jia(a,b,c,d);break;
case '-':jian(a,b,c,d);break;
case '*':cheng(a,b,c,d);break;
case '/':chu(a,b,c,d);break;
default :printf("error\n");
}
}
除法那我想到了相当于乘以倒数,所以想调用乘法的函数,但不会弄了,请大家帮帮忙谢谢!
int gcd(int a,int b) //最大公约数
{
int s;
if(b>a)
{
a=a+b;
b=a-b;
a=a-b;
}//使a中放大的数
s=a%b;
while(s!=0)
{
a=b;
b=s;
s=a%b;
}
return b;
}
void jia(int a,int b,int c,int d)
{
int fenmu,fenzi,gongyueshu;
fenmu=b*d;fenzi=a*d+b*c;
gongyueshu=gcd(fenmu,fenzi);
fenmu=fenmu/gongyueshu;
fenzi=fenzi/gongyueshu;
printf("%d/%d\n",fenzi,fenmu);
}
void cheng(int a,int b,int c,int d)
{
int fenmu,fenzi,gongyueshu;
fenmu=b*d;fenzi=a*c;
gongyueshu=gcd(fenmu,fenzi);
fenmu=fenmu/gongyueshu;
fenzi=fenzi/gongyueshu;
printf("%d/%d\n",fenzi,fenmu);
}
void main()
{
int a,b,c,d;
char e;
printf("计算a/b与c/d的四则运算(+ - * /)\n");
do{
printf("请输入a b c d(如:12 43 24 12 a,b,c,d!=0)\n");
scanf("%d%d%d%d",&a,&b,&c,&d);
getchar();
}while(!(a*b*c*d));//如果abcd中有0重新输入
printf("输入运算符:");
scanf("%c",&e);
printf("结果:");
switch(e)
{
case '+':jia(a,b,c,d);break;
case '-':jia(a,-1*b,c,d);break;//减是加的逆运算
case '*':cheng(a,b,c,d);break;
case '/':cheng(a,b,d,c);break;//除是乘的逆运算
default :printf("error\n");
}
printf("\n\nEND\npress Enter key\n");getchar();
}
int gcd(int a,int b)
{
while(a!=0&&b!=0)
if(a>=b)
a=a%b;
else
b=b%a;
if(a>b)
return a;
else
return b;
}
void jia(int a,int b,int c,int d)
{
int fenmu,fenzi,gongyueshu;
fenmu=b*d;fenzi=a*d+b*c;
gongyueshu=gcd(fenmu,fenzi);
fenmu=fenmu/gongyueshu;
fenzi=fenzi/gongyueshu;
if(fenmu==1)
printf("%d\n",fenzi);
else
printf("%d/%d\n",fenzi,fenmu);
}
void jian(int a,int b,int c,int d)
{
int fenmu,fenzi,gongyueshu;
fenmu=b*d;fenzi=a*d-b*c;
gongyueshu=gcd(fenmu,fenzi);
fenmu=fenmu/gongyueshu;
fenzi=fenzi/gongyueshu;
if(fenmu==1||fenzi==0)
printf("%d\n",fenzi);
else
printf("%d/%d\n",fenzi,fenmu);
}
void cheng(int a,int b,int c,int d)
{
int fenmu,fenzi,gongyueshu;
fenmu=b*d;fenzi=a*c;
gongyueshu=gcd(fenmu,fenzi);
fenmu=fenmu/gongyueshu;
fenzi=fenzi/gongyueshu;
if(fenmu==1||fenzi==0)
printf("%d\n",fenzi);
else
printf("%d/%d\n",fenzi,fenmu);
}
void chu(int a,int b,int c,int d)
{
cheng(a,b,d,c); //这样写就行了啊,就用cheng的里面的输出了
}
void main()
{
int a,b,c,d;
char e;
scanf("%d/%d%c%d/%d",&a,&b,&e,&c,&d);
switch(e)
{
case '+':jia(a,b,c,d);break;
case '-':jian(a,b,c,d);break;
case '*':cheng(a,b,c,d);break;
case '/':chu(a,b,c,d);break;
default :printf("error\n");
}
}
int gcd(int a,int b)
{
while(a!=0&&b!=0)
if(a>=b)
a=a%b;
else
b=b%a;
if(a>b)
return a;
else
return b;
}
void jia(int a,int b,int c,int d)
{
int fenmu,fenzi,gongyueshu;
fenmu=b*d;fenzi=a*d+b*c;
gongyueshu=gcd(fenmu,fenzi);
fenmu=fenmu/gongyueshu;
fenzi=fenzi/gongyueshu;
if(fenmu==1)
printf("%d\n",fenzi);
else
printf("%d/%d\n",fenzi,fenmu);
}
void jian(int a,int b,int c,int d)
{
int fenmu,fenzi,gongyueshu;
fenmu=b*d;fenzi=a*d-b*c;
gongyueshu=gcd(fenmu,fenzi);
fenmu=fenmu/gongyueshu;
fenzi=fenzi/gongyueshu;
if(fenmu==1||fenzi==0)
printf("%d\n",fenzi);
else
printf("%d/%d\n",fenzi,fenmu);
}
void cheng(int a,int b,int c,int d)
{
int fenmu,fenzi,gongyueshu;
fenmu=b*d;fenzi=a*c;
gongyueshu=gcd(fenmu,fenzi);
fenmu=fenmu/gongyueshu;
fenzi=fenzi/gongyueshu;
if(fenmu==1||fenzi==0)
printf("%d\n",fenzi);
else
printf("%d/%d\n",fenzi,fenmu);
}
void chu(int a,int b,int c,int d)
{
/* int fenmu,fenzi,gongyueshu;
fenmu=a*c;fenzi=b*d;
gongyueshu=gcd(fenmu,fenzi);
fenmu=fenmu/gongyueshu;
fenzi=fenzi/gongyueshu;
if(fenmu==1||fenzi==0)
printf("%d\n",fenzi);
else
printf("%d/%d\n",fenzi,fenmu);*/ void cheng(a,b,d,c);
}
void main()
{
int a,b,c,d;
char e;
scanf("%d/%d%c%d/%d",&a,&b,&e,&c,&d);
switch(e)
{
case '+':jia(a,b,c,d);break;
case '-':jian(a,b,c,d);break;
case '*':cheng(a,b,c,d);break;
case '/':chu(a,b,c,d);break;
default :printf("error\n");
}
}