如题所述
给个简单的程序你
#include<stdio.h>
#include<math.h>
typedef struct Fraction
{
int m; //分子
unsigned int d; //分母
}F;
F Addition(F a,F b); //加法
F Subtraction(F a,F b); //减法
F Multiplication(F a,F b); //乘法
F Division(F a,F b); //除法
int Getm(int u,unsigned int v);//最大公约数
void Output(F c);//显示
void main()
{
F a,b,c;
a.m=-2;
a.d=5;
b.m=3;
b.d=7;
c=Addition(a,b);
Output(c);printf("\n");
c=Subtraction(a,b);
Output(c);printf("\n");
c=Multiplication(a,b);
Output(c);printf("\n");
c=Division(a,b);
Output(c);printf("\n");
}
F Addition(F a,F b)
{
F c;
int t,t1;
unsigned int t2;
t1=a.m*b.d+a.d*b.m;t2=a.d*b.d;
t=Getm(abs(t1),t2);
c.m=t1/t;
c.d=t2/t;
return c;
}
F Subtraction(F a,F b)
{
F c;
c.m=-b.m;
c.d=b.d;
return Addition(a,c);
}
F Multiplication(F a,F b)
{
F c;
int t,t1;
unsigned int t2;
t1=a.m*b.m;t2=a.d*b.d;
t=Getm(abs(t1),t2);
c.m=t1/t;
c.d=t2/t;
return c;
}
F Division(F a,F b)
{
F c;
c.m=int(b.d);
c.d=unsigned int(b.m);
if(b.m<0)
c.m=-c.m;
return Multiplication(a,c);
}
int Getm(int m,unsigned int d)
{
int t;
while(d!=0)
{
t=m%d;
m=d;
d=t;
}
return m;
}
void Output(F c)
{
printf("%d",c.m);
if(c.d!=1)
printf("/%d",c.d);
}
#include<stdio.h>
#include<math.h>
typedef struct Fraction
{
int m; //分子
unsigned int d; //分母
}F;
F Addition(F a,F b); //加法
F Subtraction(F a,F b); //减法
F Multiplication(F a,F b); //乘法
F Division(F a,F b); //除法
int Getm(int u,unsigned int v);//最大公约数
void Output(F c);//显示
void main()
{
F a,b,c;
a.m=-2;
a.d=5;
b.m=3;
b.d=7;
c=Addition(a,b);
Output(c);printf("\n");
c=Subtraction(a,b);
Output(c);printf("\n");
c=Multiplication(a,b);
Output(c);printf("\n");
c=Division(a,b);
Output(c);printf("\n");
}
F Addition(F a,F b)
{
F c;
int t,t1;
unsigned int t2;
t1=a.m*b.d+a.d*b.m;t2=a.d*b.d;
t=Getm(abs(t1),t2);
c.m=t1/t;
c.d=t2/t;
return c;
}
F Subtraction(F a,F b)
{
F c;
c.m=-b.m;
c.d=b.d;
return Addition(a,c);
}
F Multiplication(F a,F b)
{
F c;
int t,t1;
unsigned int t2;
t1=a.m*b.m;t2=a.d*b.d;
t=Getm(abs(t1),t2);
c.m=t1/t;
c.d=t2/t;
return c;
}
F Division(F a,F b)
{
F c;
c.m=int(b.d);
c.d=unsigned int(b.m);
if(b.m<0)
c.m=-c.m;
return Multiplication(a,c);
}
int Getm(int m,unsigned int d)
{
int t;
while(d!=0)
{
t=m%d;
m=d;
d=t;
}
return m;
}
void Output(F c)
{
printf("%d",c.m);
if(c.d!=1)
printf("/%d",c.d);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2010-12-18
#include "stdio.h"
void main()
{
float a,b,res,shang;
char c;
scanf("%f",&a);
scanf("%c",&c);
scanf("%f",&b);
switch(c)
{
case '+': res=a+b;break;
case '-': res=a-b;break;
case '*': res=a*b;break;
case '/': res=a/b;break;
}
printf("\n%f%c%f=%f\n",a,c,b,res);
}
void main()
{
float a,b,res,shang;
char c;
scanf("%f",&a);
scanf("%c",&c);
scanf("%f",&b);
switch(c)
{
case '+': res=a+b;break;
case '-': res=a-b;break;
case '*': res=a*b;break;
case '/': res=a/b;break;
}
printf("\n%f%c%f=%f\n",a,c,b,res);
}