| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 608 人关注过本帖
标题:[分享]前几天做了个四则运算,现在自己改进了下
只看楼主 加入收藏
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
 问题点数:0 回复次数:3 
[分享]前几天做了个四则运算,现在自己改进了下

package src;

import java.io.*;

public class Calculation {
public static void main(String args[]) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
String oper1="",oper2="";
char c,operation='0';
int operationPosition=0;
double operand1,operand2,result=0;
count:
for(int i=1;i<str.length();i++){
c=str.charAt(i);
switch(c){
case '+':
operation='+';
operationPosition=i;
break count;
case '-':
operation='-';
operationPosition=i;
break count;
case '*':
operation='*';
operationPosition=i;
break count;
case '/':
operation='/';
operationPosition=i;
break count;
}
}
oper1=str.substring(0,operationPosition);
oper2=str.substring(operationPosition+1,str.length());
operand1=Double.parseDouble(oper1);
operand2=Double.parseDouble(oper2);
switch(operation){
case'+':
result=operand1+operand2;
break;
case'-':
result=operand1-operand2;
break;
case'*':
result=operand1*operand2;
break;
case'/':
if(operand2==0) System.out.println("Denominator can't be zero.");
else result=operand1/operand2;
break;
}
System.out.println(result);
}
}
大家看看还有改进的地方吗?

搜索更多相关主题的帖子: 改进 运算 分享 
2007-08-15 10:00
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
得分:0 

import java.io.*;

public class Calculation {
public static void main(String args[]) throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
int length=str.length();
//operand1 is the 1st operand,operand2 is the 2nd
int operand1,operand2,result=0;
//oper1 is the 1st string of 1st operand,oper2 is the 2nd string of 2nd operand
String oper1="",oper2="";
//if afterOper1=true,operand2 will be counted,or operand1 will be counted
boolean afterOper1=false;
//flag is the operation
char c,flag='0';
//these two booleans are used to see whether the operands are minus
boolean firstMinus=false,secondMinus=false;
//to see whether the 1st operand is minus or not
c=str.charAt(0);
if(c=='-'){
firstMinus=true;
str=str.substring(1,length);
length--;
}
for(int i=0;i<length;i++){
c=str.charAt(i);
if(c!='+' && c!='-' && c!='*' && c!='/'){
if(!afterOper1){
oper1+=c;
}else{
oper2+=c;
}
}else{
afterOper1=true;
}
}
//get the operation
flag=str.charAt(oper1.length());
//to see whether the 2nd operand is minus or not
c=str.charAt(oper1.length()+1);
if(c=='-'){
secondMinus=true;
}
operand1=Integer.parseInt(oper1);
if(firstMinus) operand1=-operand1;
operand2=Integer.parseInt(oper2);
if(secondMinus) operand2=-operand2;
switch(flag){
case'+':result=operand1+operand2;break;
case'-':result=operand1-operand2;break;
case'*':result=operand1*operand2;break;
case'/':result=operand1/operand2;break;
}
System.out.println(result);
}
}
这个是上次的程序。


I'm here, as always...
2007-08-15 10:02
a276202460
Rank: 2
等 级:新手上路
威 望:4
帖 子:392
专家分:1
注 册:2007-4-10
收藏
得分:0 

看下了 写的很好 好象没带()运算和进行格式验证吧 如果连续输入运算符会出错吧 用正则表达式和奇偶判断位进行综合判断 在递归调用子运算应该可以运算任意表达式的四则运算


2007-08-15 10:07
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
得分:0 

呵呵,我是新手,你说的我好多都不懂啊,需要学习...


I'm here, as always...
2007-08-15 10:38
快速回复:[分享]前几天做了个四则运算,现在自己改进了下
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025293 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved