C语言实例教程_用c编一个分数计算器程序
/*最简单的分数计算器*/
#include<stdio.h>
typedef unsigned long int u4;
u4 fz,fm;
u4 gcd(u4 x,u4 y)/*最大公约数*/
{
return y?gcd(y,x%y):x;
}
u4 lcm(u4 x,u4 y)/*最小公倍数*/
{
return x/gcd(x,y)*y;
}
void yuefen()/*对fz/fm约分*/
{ u4 gys=gcd(fz,fm);
fz/=gys;
fm/=gys;
}
void add(u4 a,u4 b,u4 c,u4 d)/*无符号分数加法*/
{ u4 u1,u2,v=lcm(b,d);
u1=v/b*a;u2=v/d*c;
fz=u1+u2;fm=v;
yuefen();
}
void mul(u4 a,u4 b,u4 c,u4 d)/*无符号分数乘法*/
{
/*请自行完成*/
}
int main( )
{ char op;
u4 a,b,c,d;
scanf("%ld/%ld%c%ld/%ld",&a,&b,&op,&c,&d);
switch(op)
{
case '+': add(a,b,c,d);break;
case '*': mul(a,b,c,d);break;
}
printf("=%ld/%ld\n",fz,fm);
return 0;
}
例二:
main()
{
int a,b,c,d=1;
while(d==1)
{
printf("1.+\n2.-\n3.*\n4./\n5.clrscr and again\n6.exit\n");
printf("please choose a number(1-6)");
scanf("%d",&c);
switch©
{
case 1:printf("please input a,b\n");
scanf("%d,%d",&a,&b);
printf("%d+%d=%d\n",a,b,a+b);
break;
case 2:printf("please input a,b\n");
scanf("%d,%d",&a,&b);
printf("%d-%d=%d\n",a,b,a-b);
break;
case 3:printf("please input a,b\n");
scanf("%d,%d",&a,&b);
printf("%d*%d=%d\n",a,b,a*b);
break;
case 4:printf("please input a,b\n");
scanf("%d,%d",&a,&b);
printf("%d/%d=%f\n",a,b,(float)a/b);break;
case 5:clrscr();break;
case 6:printf("quit\ndo you really want to quit\n");break;
}
printf("press 1 to continue.press 0 to exit>");
scanf("%d",&d);
}
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |