MATLAB中计算器中进制,多进制混合运算计算器

之前对附件中的计算器感觉十分不方便,在使用过程中,无法随意更改输入表达式,有时候可能会因为一点输入错误,而面临重新输入的繁琐。尤其是在比较长的表达式的输入中,这种不变体现地更加明显。另外,针对附件中的计算器中的进制转换问题,也很不方便,比如计算中如果既有10进制,又有16进制,就需要将不断的点击进制表示,这无疑降低了用于体验。

在之后很长一段时间,碰到计算问题,我都是用matlab,但是,matlab在进制输入同样存在不便,必须调用转换函数才能实现,与此同时,matlab缓慢的启动时间和较大的内存消耗,无法令人忍受。之后,我就在考虑自己做一个桌面应用工具,取代这些问题。

借着最近一段时间学习Java的契机,我开始动手实践一直以来的想法。

本项目的难点主要在于:1.要想保存记录,并且方便修改,必须使用文本字符串输入,而文本输入在转换为数字和运算符,存在不便。

2.运算符优先级和括号的存在,为处理增加了难度。

3.个人原因,对一些类库和错误还不是很熟悉,走了一点弯路。

解决办法:

问题1:对字符串中的元素进行判断,进行人为分类,因为数与数之间必然存在运算符进行分割,所以在检测到运算符之后对数进行入队操作。

问题2:运算优先级和括号的存在,只要对队列元素相应附上一个优先级,然后处理过程中对高优先级的先进行运算。因为对数和运算符都用同样的队列来存储,一开始觉得是要给队列中数赋予优先级,然后优先级靠前的2个数先操作,但是这里的问题在于比如检测到*时,既要在队列前一个元素和后一个元素都改写优先级,如果是8*9*7,中间的9的优先级就要比8和7都要大,如果要处理这个问题,就还要识别相邻的运算符,造成设计复杂度的增加。后来想到一个比较好的解决方法是

优先级只是针对运算符,而数只是作为操作对象,这点在逻辑上也很好理解,运算符相当于方法,优先的对象是方法而不是操作对象。这个问题就迎刃而解,但是另一个问题是()的优先级和*/优先级存在定量问题,如果2者在原优先级上的累加量一样,就会造成3*(8-9)中*和-的优先级一样,由于我的从前到后的处理规则就导致了最后3*8-9的运算。

问题3:体会到直接从API或者是直接看官网的技术文档或者官方例程而非从别人的经验中获取的好处。如SWT的类库中layout布局,之前看别人的代码,然后乱,但是最后还不是很能理解,也不可避免地走了一点的弯路。但是从官网的Understanding

Layouts in SWT就很快地理解了界面设计的基本方法和步骤。在实践中,也对Java的一些特性有了一定的了解。之前一直看Thinking in

Java,但是没有动手实践,理论知识学过就忘,学C语言、HDL的经验告诉我实践才能促进认识,这次实践又加深了我对这点的看法。

下面是核心代码:

fcecaa27ea5212ceb9bf034c36bfbf34.gif

packagecalculation;importjava.lang.String;importcalculation.CalList;public classMyStrtonum{staticString str;static long num =0;static numtypeenum mynumtype =numtypeenum.decimal;static byte pri = 0;//priority

static CalList callist = newCalList();private enumcaltypeenum {numtype,highopertype,lowopertype,leftbrackettype,rightbrackettype,others};private enumnumtypeenum {hex,decimal,binary};

MyStrtonum(String getstr){

str=getstr.toLowerCase();

System.out.println(str);

callist.deleteAll();intii;for(ii=0;ii

distinguish(str.charAt(ii));

System.out.println(str.charAt(ii));

}

callist.insert(num,(byte)(-1));

num= 0;

}//@SuppressWarnings("unused")

private static caltypeenum isCalChar(intch){if(ch>=‘0‘ && ch<=‘9‘)returncaltypeenum.numtype;else if(ch==‘+‘ || ch ==‘-‘)returncaltypeenum.lowopertype;else if( ch ==‘*‘ ||ch ==‘/‘)returncaltypeenum.highopertype;else if((ch>=‘a‘ && ch<=‘f‘) || ch==‘x‘)returncaltypeenum.numtype;else if(ch == ‘(‘)returncaltypeenum.leftbrackettype;else if(ch == ‘)‘)returncaltypeenum.rightbrackettype;else

returncaltypeenum.others;

}private static void distinguish(intch){switch(isCalChar(ch))

{caseleftbrackettype:

pri= (byte) (pri + 2);break;caserightbrackettype:

pri= (byte) (pri - 2);break;casehighopertype:

callist.insert(num,(byte)(-1));

System.out.println(num);

num= 0;

mynumtype=numtypeenum.decimal;

callist.insert(ch, (byte)(pri+1));break;caselowopertype:

callist.insert(num,(byte)(-1));

System.out.println(num);

num= 0;

mynumtype=numtypeenum.decimal;

callist.insert(ch, pri);break;casenumtype:if(ch == ‘x‘)

mynumtype=numtypeenum.hex;else if(ch == ‘b‘ && (mynumtype ==numtypeenum.decimal))

mynumtype=numtypeenum.binary;else if(mynumtype ==numtypeenum.decimal)

{if(ch>‘9‘)

System.out.println("error decimal");elsenum= num*10 + (ch - ‘0‘);

}else if(mynumtype ==numtypeenum.hex)

{

num= (num<<4) + ((ch>‘9‘)?(ch-‘a‘+10):(ch-‘0‘));

}else if(mynumtype ==numtypeenum.binary)

{if(ch>‘1‘)

System.out.println("error binary");elsenum= (num<<1) + (ch-‘0‘);

}break;case others:break;default:break;

}

}public static longnumoperation(){

CalNode tempnode;//while()

if(callist.Length != 1){do{

tempnode=callist.gethigherOper();switch((int)tempnode.next.data)

{case ‘+‘:

tempnode.data=tempnode.data +tempnode.next.next.data;break;case ‘-‘:

tempnode.data=tempnode.data -tempnode.next.next.data;break;case ‘*‘:

tempnode.data=tempnode.data *tempnode.next.next.data;break;case ‘/‘:

tempnode.data=tempnode.data /tempnode.next.next.data;break;default:

System.out.println("error");break;

}

}while(callist.ReconstructList(tempnode) != 1);

}

callist.reset();

System.out.println(callist.ShowNode());long tempdata =callist.ShowNode();

callist.deleteAll();returntempdata;

}/*private static void main(String [] args)

{

MyStrtonum test = new MyStrtonum("(23-0xa)*20");

test.numoperation();

//test.distinguish(‘3‘);

}*/}

fcecaa27ea5212ceb9bf034c36bfbf34.gif

30cedbb07e0febac1bdcb1a42e68d85c.png

原文:http://www.cnblogs.com/crystal-clear/p/3535679.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值