⑴ 怎么用java编程,实现分数的加减乘除运算
package Rational;
import java.util.StringTokenizer;
/**
* @author SunKun
* 分数四则运算
*/
public class Calculator {
int numerator; // 分子
int denominator; // 分母
Calculator(){
}
Calculator(int a,int b){
if(a == 0){
numerator = 0;
denominator = 1;
}
else{
setNumeratorAndDenominator(a,b);
}
}
void setNumeratorAndDenominator(int a, int b){ // 设置分子和分母
int c = f(Math.abs(a),Math.abs(b)); // 计算最大公约数
numerator = a / c;
denominator = b / c;
if(numerator<0 && denominator<0){
numerator = - numerator;
denominator = - denominator;
}
}
int getNumerator(){
return numerator;
}
int getDenominator(){
return denominator;
}
int f(int a,int b){ // 求a和b的最大公约数
if(a < b){
int c = a;
a = b;
b = c;
}
int r = a % b;
while(r != 0){
a = b;
b = r;;
r = a % b;
}
return b;
}
Calculator add(Calculator r){ // 加法运算
int a = r.getNumerator();
int b = r.getDenominator();
int newNumerator = numerator * b + denominator * a;
int newDenominator = denominator * b;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}
Calculator sub(Calculator r){ // 减法运算
int a = r.getNumerator();
int b = r.getDenominator();
int newNumerator = numerator * b - denominator * a;
int newDenominator = denominator * b;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}
Calculator muti(Calculator r){ // 乘法运算
int a = r.getNumerator();
int b = r.getDenominator();
int newNumerator = numerator * a;
int newDenominator = denominator * b;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}
Calculator div(Calculator r){ // 除法运算
int a = r.getNumerator();
int b = r.getDenominator();
int newNumerator = numerator * b;
int newDenominator = denominator * a;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}
static Calculator qiuyu(double left,double right){ // 求余运算
double point_result = Math.IEEEremainder(left, right);
int r1 = (int)point_result;
double r2 = point_result - r1;
int newNumerator = r1*10000 + (int)(r2*10000);
int newDenominator = 10000;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}
static Calculator pow(double left,double right){ // 求幂运算left^right
double point_result = Math.pow(left, right);
int r1 = (int)point_result;
double r2 = point_result - r1;
int newNumerator = r1*10000 + (int)(r2*10000);
int newDenominator = 10000;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}
static Calculator max(double left,double right){ // 求两数中的较大值
double point_result = Math.min(left, right);
int r1 = (int)point_result;
double r2 = point_result - r1;
int newNumerator = r1*10000 + (int)(r2*10000);
int newDenominator = 10000;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}
static Calculator min(double left,double right){ // 求两数中的较小值
double point_result = Math.min(left, right);
int r1 = (int)point_result;
double r2 = point_result - r1;
int newNumerator = r1*10000 + (int)(r2*10000);
int newDenominator = 10000;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}
// 封装了具体运算,主要为对输入进行转换,对输出封装
public static void compute(String data1,String operation,String data2){
StringTokenizer fenxi = new StringTokenizer(data1,"/");
int data1_1 = Integer.parseInt(fenxi.nextToken());
int data1_2 = Integer.parseInt(fenxi.nextToken());
fenxi = new StringTokenizer(data2,"/");
int data2_1 = Integer.parseInt(fenxi.nextToken());
int data2_2 = Integer.parseInt(fenxi.nextToken());
Calculator r1 = new Calculator(data1_1,data1_2);
Calculator r2 = new Calculator(data2_1,data2_2);
Calculator result;
int a,b;
if(operation.equals("+")){ // 两数相加
result = r1.add(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}
if(operation.equals("-")){ // 两数相减
result = r1.sub(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}
if(operation.equals("*")){ // 两数相乘
result = r1.muti(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}
if(operation.equals("/")){ // 两数相除
result = r1.div(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}
if(operation.equals("%")){ // 两数求余
double left = (double)data1_1/(double)data1_2;
double right = (double)data2_1/(double)data2_2;
result = qiuyu(left,right);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}
if(operation.equals("^")){ // 两数求幂
double left = (double)data1_1/(double)data1_2;
double right = (double)data2_1/(double)data2_2;
result = pow(left,right);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}
if(operation.equals("max")){ // 两数中的较大值
double left = (double)data1_1/(double)data1_2;
double right = (double)data2_1/(double)data2_2;
result = max(left,right);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}
if(operation.equals("min")){ // 两数中的较小值
double left = (double)data1_1/(double)data1_2;
double right = (double)data2_1/(double)data2_2;
result = min(left,right);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}
}
}
⑵ c语言中的除怎么表示
C语言中的除用符号 / (正斜杠)表示
用法嫌耐:
1、当被除数和除数均为整型时,结果为整型。
如1/2的结果芹罩春是0,9/2的结果是4
2、当被除数和除数任意一个为浮点数时,结果为浮点数。
如1.0/2的结果是0.50000000000000000(double型),9.0/2.0的结果是4.5000000000000000(double型)
(2)如何用编程写除法扩展阅读:
c语言中其他的计算函数
int isdigit(int ch) 若ch是数字('0'-'9')返回非0值,否则返回0
int isgraph(int ch) 若ch是可打印字符(不含空格)(0x21-0x7E)返回非0值,否则返回0
int islower(int ch) 若ch是小写字母('a'-'z')返回非0值,否则返回0
double fabs(double x) 返回双精度参数x的绝对值
long labs(long n) 返回长整型参数n的绝对值
double exp(double x) 返回指数函数ex的闷历值
⑶ 加减乘除的c语言编程怎么写
c语言的加减乘除可以这样写,假设有int变量a,b,c
加法:c=a+b
减法:c=a-b
乘法:c=a*b
除法:c=a/b
⑷ 输入两个整数,进行加减乘除四则运算的c语言程序怎么写啊,拜托了~
代码抄
#include<stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d ",a + b);
printf("%d ",a - b);
printf("%d ",a * b);
printf("%d ",a / b);
return 0;
}
C语言中的加减乘除和数学中的加减乘除一样,不同在于符号的表示问题,乘号需要用“*”表示。除号需要用“/”表示。新手学习C语言,很容易把除号和取余好混淆,强调一下,取余号是“%”,百分号就是取余的意思。因此在输入两个整数以后,按照数学方法就可以直接输出结果,整数的输入用scanf()函数。