java吧 关注:1,299,143贴子:12,837,602

回复:java作业,纠结了好几晚上啊,大神再不帮我破,我就去跳海

只看楼主收藏回复

不就是postfix么?我有c++的代码,自己看着修改吧
# include <iostream>
# define MAX 20
using namespace std; void operation(char c1, int x, int y, int &ans);
void push(int val, int &top);
int pop(int &top );
bool isEmpty(int top);
bool isFull(int top); int stack[MAX]; int main(void)
{
int top = -1;
char c1;
int x, y;
int val, sum, ans; cout << "Please type in postfix equation" <<endl;
while(cin>>c1)
{
/* primitive way to handle a 2-digit token */
int d[2];
if((c1>='0') && (c1<='9'))
{
static int i = 0;
if(i==0)
{
d[i]= c1 - '0';
i++;
}
else if(i==1)
{
d[i]= c1 - '0';
val = d[i-1]*10 + d[i];
--i;
push (val, top);
}
} /* Check to see if char is Operation, +, - or * */
if((c1=='+') || (c1=='-') || (c1=='*'))
{
/* Pop top 2 items */
y = pop(top);
x = pop(top); operation(c1, x, y, ans); /* Push the answer on the stack */
push(ans, top);
} /* Print the result if char is equal sign */
if( c1 == '=' )
{
sum = pop(top);
cout<<"Answer is "<<sum<<endl;
}
} return 0;
} void operation(char c1, int x, int y, int &ans)
{
switch(c1)
{
case '+': ans = x + y;
break; case '-': ans = x - y;
break; case '*': ans = x * y;
break;
}
}
void push(int newitem, int &top)
{
if(!isFull(top))
stack[++top] = newitem;
else
cout << "Error! Stack is full!" <<endl;
} int pop(int &top )
{
if(!isEmpty(top))
return stack[top--];
else
cout<< "Error! Stack is empty!" <<endl;
exit (1);
} bool isEmpty(int top)
{
return top==-1;
} bool isFull(int top)
{
return top==(MAX);
}


IP属地:上海18楼2013-04-11 08:03
回复
    围观跳海


    IP属地:江苏19楼2013-04-11 08:16
    回复
      2026-03-11 04:59:10
      广告
      不感兴趣
      开通SVIP免广告
      import java.util.Scanner; public class Test1Main { public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      while (true) {
      System.out.println("请输入表达式(比如:5+6,按回车)(若只输入0,结束)");
      String s = sc.next();
      if (s.equals("0")) {
      System.out.println("程序结束");
      break;
      }
      if (s.contains("+")) {
      String s1 = s.substring(0, s.indexOf("+"));
      double d1 = Double.parseDouble(s1);
      String s2 = s.substring(s.indexOf("+") + 1, s.length());
      double d2 = Double.parseDouble(s2);
      System.out.println(s + "=" + (d1 + d2));
      }
      if (s.contains("-")) {
      String s1 = s.substring(0, s.indexOf("-"));
      double d1 = Double.parseDouble(s1);
      String s2 = s.substring(s.indexOf("-") + 1, s.length());
      double d2 = Double.parseDouble(s2);
      System.out.println(s + "=" + (d1 - d2));
      }
      if (s.contains("*")) {
      String s1 = s.substring(0, s.indexOf("*"));
      double d1 = Double.parseDouble(s1);
      String s2 = s.substring(s.indexOf("*") + 1, s.length());
      double d2 = Double.parseDouble(s2);
      System.out.println(s + "=" + (d1 * d2));
      }
      if (s.contains("/")) {
      String s1 = s.substring(0, s.indexOf("/"));
      double d1 = Double.parseDouble(s1);
      String s2 = s.substring(s.indexOf("/") + 1, s.length());
      double d2 = Double.parseDouble(s2);
      if (d2 != 0) {
      System.out.println(s + "=" + (d1 / d2));
      } else {
      System.out.println("除数不能为0");
      }
      }
      }
      }
      }


      20楼2013-04-11 09:21
      回复
        这个简单 楼上有正解了...我就不帮你写了.


        21楼2013-04-11 10:28
        回复
          我要崩溃了!!!!
          明明是这样写的,为啥就是run出来是错的呢。。。。。


          22楼2013-04-11 13:26
          收起回复
            妹的 用的着 这么 麻烦吗


            IP属地:河南23楼2013-04-11 15:17
            回复
              好难啊,我也不懂


              24楼2013-04-11 16:41
              回复
                这这这....还没跳海么?


                IP属地:湖南25楼2013-04-11 17:53
                收起回复
                  2026-03-11 04:53:10
                  广告
                  不感兴趣
                  开通SVIP免广告
                  跳吧,明年清明大家会把代码烧给你的


                  IP属地:广东来自手机贴吧26楼2013-04-11 18:04
                  收起回复
                    看跳海,围观


                    IP属地:山西27楼2013-04-11 18:51
                    回复
                      坐等楼主变死猪


                      IP属地:安徽来自手机贴吧29楼2013-04-11 20:05
                      收起回复
                        用switch case 语句就可以了!


                        IP属地:江苏30楼2013-04-11 20:06
                        回复
                          +1


                          31楼2013-04-12 09:36
                          回复