分数加法怎么用计算机算,加法分数计算器的运算符

这篇博客探讨了如何在Java程序中实现一个分数计算器,包括解析含有加法、减法、除法和乘法的表达式。作者遇到了在`parseExpression`方法中添加对加法运算符的支持的问题,并寻求解决方案。同时,作者也欢迎对于其他运算符的处理建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我正在写一个分数计算器。我已经编写了一个解析代码的方法,但是很难弄清楚如何将plus运算符添加到parseExpression方法中。例如:1_1 / 2 + 1 = 2_1 / 2。如果你选择回答尽力让我指向正确的方向。如果有人对减号,除法和乘法运算符有任何建议,我也会很感激。

public class FracCalc {

public static void main(String[] args) {

// TODO: Implement.

Scanner console = new Scanner(System.in);

System.out.println("enter an equation: ");

String myInput = console.nextLine();

String simplified = parseExpression(myInput);

System.out.print(simplified);

shouldQuit(myInput);

console.close();

}

public static boolean shouldQuit(String input) {

String quit = input;

if (input.equals("quit")) {

System.out.print("Quit");

return true;

} else {

return false;

}

}

public static String parseExpression(String expr) {

//parses the expression for '_'

if(expr.indexOf("_")!=-1) {

int spot = expr.indexOf("_");

String first = expr.substring(0, spot);

int frac = expr.indexOf("/");

String numer = expr.substring(spot + 1, frac);

String denom = expr.substring(frac + 1);

String mixednum = improperMNum(numer, denom);

String mixed = first + "_" + mixednum;

return mixed;

}

//parses the expression for '/'

if (expr.contains("/")) {

int index = expr.indexOf("/");

String numerator = expr.substring(0, index);

String denominator = expr.substring(index + 1);

System.out.println(numerator + denominator);

String x = improperMNum(numerator, denominator);

return x;

} else {

String wholenumber = expr.substring(0);

return wholenumber;

}

}

public static String improperMNum(String numerator, String denominator) {

int wholenumber = numerator.indexOf("_");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值