问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

加减乘除运算(Java)

发布网友 发布时间:2022-04-22 13:32

我来回答

3个回答

热心网友 时间:2023-09-28 04:45

    java编程实现分数的加减乘除运算的步骤如下:

    1、打开eclipse,创建一个Java工程,在此工程里新建一个类;

    2、在新建的类中,添加4个运算类;

    3、在主方法中调用对应的方法即可完成分数的加减乘除运算了。

    具体实现代码如下:

     public class Demo {
    public static void main(String[] args) {
        System.out.println(jia(1, 2));
        System.out.println(jian(1, 2));
        System.out.println(cheng(1, 2));
        System.out.println(chu(1, 2));
    }
    //加法运算
    private static float jia(float x,float y) {
        return x + y;
    }
    //减法运算
    private static float jian(float x,float y) {
        return x - y;
    }
    //乘法运算
    private static float cheng(float x,float y) {
        return x * y;
    }
    //除法运算
    private static float chu(float x,float y) {
        return x / y;
    }
}

热心网友 时间:2023-09-28 04:45

1、打开eclipse,创建一个Java工程,在此工程里新建一个类;

2、在新建的类中,添加4个运算类;

3、在主方法中调用对应的方法即可完成分数的加减乘除运算了。

具体实现代码如下:

public class Demo {

public static void main(String[] args) {

System.out.println(jia(1, 2));

System.out.println(jian(1, 2));

System.out.println(cheng(1, 2));

System.out.println(chu(1, 2));

}

//加法运算

private static float jia(float x,float y) {

return x + y;

}

//减法运算

private static float jian(float x,float y) {

return x - y;

}

//乘法运算

private static float cheng(float x,float y) {

return x * y;

}

//除法运算

private static float chu(float x,float y) {

return x / y;

}

}

热心网友 时间:2023-09-28 04:45

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);
}
}
}
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
为什么来大姨妈胸会胀 少儿学什么舞蹈 青年学什么舞蹈好 成年人学什么舞蹈 福州企业最低工资标准 2013年厦门的底薪是多少 生产要素的需求有哪些性质 生产要素的需求有何特点? 什么是生产要素需求 微观经济学要素需求什么是条件要素需求?它和要素需求有什么不同?_百度... 利用栈实现逆波兰表达式求值 新浪分期颜值卡是什么意思 逆波兰式的名字出处是什么?为什么会叫这个名字? 新浪分期可以提现到银行卡吗 思科(中国)技术服务有限公司怎么样? 思科的思科公司概览 CISCO全球供应商有哪些家? 新浪分期和小象优品是一家吗 华为,思科总部各是哪国的? 思科公司在哪 家里怎么才能有wifi?怎么弄的? 家里有wifi还能装个wifi吗 思科在中国哪些城市设有研发中心 自己家里面有wifi却搜索不到,怎么回事呢? 有谁了解思科在中国有哪几个城市的分公司啊? 家里要有wifi需要什么? 家里有网为什么手机连不上wifi 为了防空疫情小区禁止所有外来人员所有外来车辆进去物业客服该怎么回答业主的 现在从陕西去海口需要隔离吗? 404 Not Found 请问有数据结构和计算机英语的高人么? 世界500强里面的IT企业有哪些?? 收到的拼音 C语言的问题 谁能帮我做下 404 Not Found 404 Not Found 在double型常量表达中,35这种表示是正确的吗? 小象优品分期怎么样 利息高不高的? C语言指针解释 十进制与十六进制怎样互相转换? 小象优品认证后多久不打款就完了 一百单八将中,能称为英雄的有几位? 小象优品借8000到账多少 请问十一分之六,怎么用百分数表示。 小象优品可以协商推迟还款么 组策略怎么禁止带有特定文件名的文件或程序啊 ? 秋天白衬衫怎么搭配好看 秋季白衬衫怎么搭配 秋天就要这么穿白衬衫 白色衬衫秋季怎么搭配才好看?你知道吗? 白色衬衫秋季怎么搭配才好看?