支持四则运算和小数

import java.util.Scanner;
import java.util.Stack;

/**
* @Author : Li limin
* @Description :
* @Date : Created in 16:05 2017/8/3
*/
public class TestJava {

//具体代表哪个符号可以看getLevel函数
public static char[][] level = {
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=','0'},
{'>','>','>','>','0','>','>'},
{'<','<','<','<','<','0','='},
};

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
String str;
while ((str = in.next()) != "#") {
double sum = cal(str);
System.out.println(sum);
}
}

public static char getLevel(char a,char b) {
int i = 0,j = 0;
switch(a)
{
case'+':i=0;break;
case'-':i=1;break;
case'*':i=2;break;
case'/':i=3;break;
case'(':i=4;break;
case')':i=5;break;
case'=':i=6;break;
}
switch(b)
{
case'+':j=0;break;
case'-':j=1;break;
case'*':j=2;break;
case'/':j=3;break;
case'(':j=4;break;
case')':j=5;break;
case'=':j=6;break;
}
return level[i][j];
}

public static double cal(String str) {

char[] chars = str.toCharArray();
int k = 0;
Stack<Double> numStack = new Stack<>();
Stack<Character> opStack = new Stack<>();
//最开始放一个运算符是为了方便运算
opStack.push('=');
//这个意思表达的是chars[k]和opStack.peek()的值都为=时才退出
//之所以2个都得为=时才退出,举个例子5+4=,求到运算符为=的时候,5+4还没来的及算就退出,最后弹栈得到的值为4
while (chars[k] != '=' || opStack.peek() != '=') {
if (chars[k] >= '0' && chars[k] <= '9') {
double sum = 0;
while (chars[k] >= '0' && chars[k] <= '9') {
sum = sum * 10 + (chars[k] - '0');
k++;
}
if (chars[k] == '.') {
//把.运算符跨过去
k++;
double sum1 = 0;
int total = 0;
while (chars[k] >= '0' && chars[k] <= '9') {
total++;
sum1 = sum1 * 10 + (chars[k] - '0');
k++;
}
sum += sum1 / Math.pow(10,total);
}
numStack.push(sum);
} else {
switch (getLevel(opStack.peek(),chars[k])) {
case '<' :
opStack.push(chars[k]);
k++;
break;
case '=' :
opStack.pop();
k++;
break;
case '>' :
double m = numStack.pop();
double n = numStack.pop();
//因为是栈所以顺序要倒一下,写成n,m,例如5-4,出栈是4,5,4-5肯定不对
//这里之所以k的值不加,是因为(5+4)遇到左括号5+4结束,符号栈中的(还没有弹出来,
//或者(5+3*4),3*4计算完毕,还得继续计算(5+12),以此类推
numStack.push(operate(n,m,opStack.pop()));
}
}
}
return numStack.pop();
}

public static double operate(double a,double b,char c) {
if (c == '+') {
return a + b;
} else if (c == '-') {
return a - b;
} else if (c == '*') {
return a * b;
} else {
return a / b;
}
}
}


偶然翻到了以前用C语言写的表达式计算器,还有刚开始自学时写的代码,满满的回忆啊

#include<iostream.h>
int cf(char a)
{
if(a>='0'&&a<='9')
return 0;
else
return 1;
};
char precede(char a,char b)
{
int i,j;
char pre[][7]={'>','>','<','<','<','>','>',
'>','>','<','<','<','>','>',
'>','>','>','>','<','>','>',
'>','>','>','>','<','>','>',
'<','<','<','<','<','=','0',
'>','>','>','>','0','>','>',
'<','<','<','<','<','0','=',};
switch(a)
{
case'+':i=0;break;
case'-':i=1;break;
case'*':i=2;break;
case'/':i=3;break;
case'(':i=4;break;
case')':i=5;break;
case'=':i=6;break;
}
switch(b)
{
case'+':j=0;break;
case'-':j=1;break;
case'*':j=2;break;
case'/':j=3;break;
case'(':j=4;break;
case')':j=5;break;
case'=':j=6;break;
}
return pre[i][j];
};
int operate(int a,char b,int c)
{
switch(b)
{
case'+':return a+c;
case'-':return a-c;
case'*':return a*c;
case'/':return a/c;
}
return 0;
};
int main()
{
char a[40],b[20],*d;
int c[20],top1=-1,top2=-1,sum=0,az,ax;
cout<<"输入表达式记得最后输入等于号"<<endl;
cin>>a;
d=a;
b[++top2]='=';
while(*d!='\0')
{
if(!cf(*d))
{
sum=*d-'0';
d++;
while(!cf(*d))
{
sum=sum*10+*d-'0';
d++;
}
c[++top1]=sum;
}
else
{
switch(precede(b[top2],*d))
{
case'<':b[++top2]=*d;
d++;
break;
case'=':--top2;
d++;
break;
case'>':az=c[top1--],ax=c[top1--];c[++top1]=operate(az,b[top2--],ax);
}
}
}
cout<<c[top1]<<endl;
return 0;
}


#include<iostream.h>//实现字符串复制
int main()
{
char b[]="abcdef",d[]="hijklmn";
char *a=b,*c=d,*f=b;
while(*a++=*c++);//注意这有一个分号,一个语句实现复制
cout<<f<<endl;//不能直接输出a因为字符串的指针已经指向字符串的末尾了
return 0;
}
#include<iostream.h>//求字符串的长度
int main()
{
int n=0;
char a[]="abcdef";
char *p=a;
while(*p++)
n++;
cout<<"长度为"<<n<<endl;
return 0;
}
#include<iostream.h>//实现字符串比较
int main()
{
char a[]="ae",b[]="ae";
char *c=a,*d=b;
while(*c++==*d++&&*c!=0&&*d!=0);//后面两个条件必须写否则会比较错误
if(*c>*d)
cout<<"a大"<<endl;
else if(*c<*d)
cout<<"b大"<<endl;
else
cout<<"相等"<<endl;
return 0;
}
#include<iostream.h>//实现输出99乘法表
#include<iomanip.h>
int main()
{
int x=0,y=0;
while(x<9)
{
(x==y)?(x=1,y++):(x++);
cout<<x<<"*"<<y<<"="<<setw(3)<<x*y<<" ";
if(x==y)
cout<<endl;
}
return 0;
}
#include<iostream.h>//实现调用函数求阶乘
int cf(int n)
{
return n==0?1:n*cf(n-1);
}
int main()
{
int n;
cout<<"请输入要求数的阶乘"<<endl;
cin>>n;
cout<<cf(n)<<endl;
return 0;
}
#include<iostream.h>//输出三个数的最大值
int main()
{
int a,b,c;
cout<<"请输入三个数"<<endl;
cin>>a>>b>>c;
cout<<(a>b?(a>c?a:c):(b>c?b:c))<<endl;
return 0;
}
#include<iostream.h>//10进制装换为16进制
int main()
{
int a,c=0;
char k[20];
cout<<"输入要装换的10进制数"<<endl;
cin>>a;
for(int i=0;a;i++)
{
c=a%16;
(c>9)?(k[i]=c-10+'A'):(k[i]=c+'0');//把数字当做字符串处理,三个括号都可以去掉
a/=16;
}
for(int j=i-1;j>=0;j--)
{
cout<<k[j];
}
cout<<endl;
return 0;
}