日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

分数四则运算_JAVA

發(fā)布時(shí)間:2025/3/21 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 分数四则运算_JAVA 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Description

編寫程序,實(shí)現(xiàn)兩個(gè)分?jǐn)?shù)的加減法
Input

輸入包含多行數(shù)據(jù);

每行數(shù)據(jù)是一個(gè)字符串,格式是"a/boc/d",其中a, b, c, d為數(shù)字(每個(gè)數(shù)字保證為正數(shù)并且不存在正號(hào))。o是運(yùn)算符"+“或者”-","*",""。

數(shù)據(jù)以EOF結(jié)束,輸入數(shù)據(jù)保證合法。
Output

直接輸出結(jié)果,并且注意結(jié)果應(yīng)符合書寫習(xí)慣,沒有多余的符號(hào)、分子、分母,并且化簡(jiǎn)至最簡(jiǎn)分?jǐn)?shù)形式。
Sample
Input

1/100+3/100

1/4-1/2

1/3-1/3

1/2*2/1

1/2\1/2

Output

1/25

-1/4

0

1

import java.util.Scanner;class F{int a;int b;public F(int a, int b) {this.a = a;this.b = b;}public int maxnum(int a, int b) {if (a % b == 0)return b;else {int x = maxnum(b, a % b);return x;}} }class math{int a;int b;public math(int a, int b) {this.a = a;this.b = b;}public void cal(math m, char r) {int x;int y;if(r == '+') {x = this.a * m.b + this.b * m.a;y = this.b * m.b;}else if(r == '-') {x = this.a * m.b - this.b * m.a;y = this.b*m.b;}else if(r == '*') {x = this.a * m.a;y = this.b * m.b;}else {int tt = m.a;m.a = m.b;m.b = tt;x = this.a * m.a;y = this.b * m.b;}if(x == 0)System.out.println("0");else {F ff = new F(x,y);int t = ff.maxnum(x, y);x /= t;y /= t;if(y == -1) {System.out.println(-x);}else if(y == 1) {System.out.println(x);}else if(y < 0) {y = -y;x = -x;System.out.println(x + "/" + y);}elseSystem.out.println(x + "/" + y);}}}public class Main {public static void main(String[] args) {Scanner reader = new Scanner(System.in);while(reader.hasNext()) {String str = reader.next();String[] s=str.split("\\+|-|\\*|/|\\\\");char r = '+';for(int i = 0; i < str.length(); i++) {if(str.charAt(i) == '+' || str.charAt(i) == '-' || str.charAt(i) == '*' || str.charAt(i) == '\\') {r = str.charAt(i);break;}}int a = Integer.parseInt(s[0]);int b = Integer.parseInt(s[1]);int c = Integer.parseInt(s[2]);int d = Integer.parseInt(s[3]);math x = new math(a, b);math y = new math(c, d);x.cal(y, r);}reader.close();} } 《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的分数四则运算_JAVA的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。