《Java编写简单计算器(附源代码).pdf》由会员分享,可在线阅读,更多相关《Java编写简单计算器(附源代码).pdf(11页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、 Java 技术与应用大作业 简单计算器的编写 一、选题理由 这个学期我选择Java 技术与应用这门课(其实我的选修课已经修完了),完全是对 java 语言的兴趣和老师的个人魅力使然。这门课让我学到了许多知识,了解到了 java 语言的基本语法、结构和特点。由于个人觉得水平有限,并且没有队友,所以选择了较为容易的简易计算器作为我的大作业。二、系统目标 实现对代数表达式的输入、计算、输出。三、系统分析与设计(1)计算类 cal 设计编写 public class cal public static boolean check(String str)If Jigloo is being used
2、*commercially(ie,by a corporation,company or business for any purpose *whatever)then you should purchase a license for each developer using Jigloo.*Please visit for details.Use of Jigloo implies *acceptance of these licensing terms.A COMMERCIAL LICENSE HAS NOT BEEN *PURCHASED FOR THIS MACHINE,SO JIG
3、LOO OR THIS CODE CANNOT BE USED LEGALLY FOR *ANY CORPORATE OR COMMERCIAL PURPOSE.*/public class NewJFrame extends private JTextField jTextField1;private JButton jButton1;private JButton jButton15;private JButton jButton16;private JButton jButton21;private JButton jButton20;private JButton jButton19;
4、private JButton jButton18;private JButton jButton17;private JButton jButton14;private JButton jButton13;private JButton jButton12;private JButton jButton11;private JButton jButton10;private JButton jButton9;private JButton jButton8;private JButton jButton7;private JButton jButton5;private JButton jB
5、utton4;private JButton jButton3;private JButton jButton2;/*Auto-generated main method to display this JFrame */public static void main(String args)(new Runnable()public void run()NewJFrame inst=new NewJFrame();(null);(true););public NewJFrame()super();initGUI();private void initGUI()try setDefaultCl
6、oseOperation;getContentPane().setLayout(null);(Mycalculation);jTextField1=new JTextField();getContentPane().add(jTextField1,North);(new 72);(0,0,460,72);(new Microsoft YaHei UI,1,16);jButton1=new JButton();getContentPane().add(jButton1);(1);(25,87,82,48);(new Microsoft YaHei UI,0,18);(new ActionList
7、ener()public void actionPerformed(ActionEvent evt),event=+evt);dd(jButton2);(2);(120,87,82,48);(new Microsoft YaHei UI,0,18);(new ActionListener()public void actionPerformed(ActionEvent evt),event=+evt);dd(jButton3);(3);(214,87,82,48);(new Microsoft YaHei UI,0,18);(new ActionListener()public void ac
8、tionPerformed(ActionEvent evt),event=+evt);dd(jButton4);(4);(25,142,82,48);(new Microsoft YaHei UI,0,18);(new ActionListener()public void actionPerformed(ActionEvent evt),event=+evt);dd(jButton5);(5);(122,142,82,48);(new Microsoft YaHei UI,0,18);(new ActionListener()public void actionPerformed(Actio
9、nEvent evt),event=+evt);dd(jButton7);(6);(215,142,82,48);(new Microsoft YaHei UI,0,18);(new ActionListener()public void actionPerformed(ActionEvent evt),event=+evt);dd(jButton8);(7);(26,201,82,50);(new Microsoft YaHei UI,0,18);(new ActionListener()public void actionPerformed(ActionEvent evt),event=+
10、evt);dd(jButton9);(8);(122,201,82,50);(new Microsoft YaHei UI,0,18);(new ActionListener()public void actionPerformed(ActionEvent evt),event=+evt);dd(jButton10);(9);(215,201,82,50);jButton10 .setFont(new Microsoft YaHei UI,0,18);(new ActionListener()public void actionPerformed(ActionEvent evt),event=
11、+evt);dd(jButton11);(0);(26,269,82,48);jButton11 .setFont(new Microsoft YaHei UI,0,18);(new ActionListener()public void actionPerformed(ActionEvent evt),event=+evt);dd(jButton12);(+);(309,147,64,45);jButton12 .setFont(new Microsoft YaHei UI,0,28);(new ActionListener()public void actionPerformed(Acti
12、onEvent evt),event=+evt);dd(jButton13);(-);(384,148,64,44);jButton13 .setFont(new Microsoft YaHei UI,0,28);(new ActionListener()public void actionPerformed(ActionEvent evt),event=+evt);dd(jButton14);(*);(309,208,64,44);jButton14 .setFont(new Microsoft YaHei UI,0,28);(new ActionListener()public void
13、actionPerformed(ActionEvent evt),event=+evt);dd(jButton15);(/);();(308,93,64,44);jButton20 .setFont(new Microsoft YaHei UI,0,28);(new ActionListener()public void actionPerformed(ActionEvent evt),event=+evt);dd(jButton21);(.);(122,270,82,48);jButton21 .setFont(new Microsoft YaHei UI,0,28);(new Action
14、Listener()public void actionPerformed(ActionEvent evt),event=+evt);|(i)=48&(i)=57)continue;else (null,输入错误!);return bool;|(i)!=)&(i+1)=()|(i)=)&(i+1)!=(|(i+1)=.)else (null,输入错误!);return bool;else break;i+;if(num1=num2)bool=true;else(null,输入错误!);return bool;|(i)=0|(i)=1|(i)=2|(i)=3|(i)=4|(i)=5|(i)=6|
15、(i)=7|(i)=8|(i)=9)str2=str2+(i);continue;else if(str2!=)/如果 str2 不为空,既之前有扫描到数字,则进行压栈 (str2);/str2 还原 str2=;/比较当前元素(其实为一操作符)与操作符栈中的栈顶元素的优先级 for(;)/如果当前元素的优先级低于或等于栈顶元素的优先级 if(compare(i),()float temp;/做如下操作 switch()/栈顶元素为+,弹出数字栈的前两个元素,进行运算 case+:temp=()+();(temp);/弹出操作符栈中用过的元素 ();break;/栈顶元素为-,弹出数字栈的前两
16、个元素,进行运算 case-:temp=()+();(temp);/弹出操作符栈中用过的元素 ();break;/栈顶元素为*,弹出数字栈的前两个元素,进行运算 case*:temp=()*();(temp);/弹出操作符栈中用过的元素 ();break;/栈顶元素为/,弹出数字栈的前两个元素,进行运算 case/:temp=1/()*();(temp);/弹出操作符栈中用过的元素 ();break;/栈顶元素为,弹出数字栈的前两个元素,进行运算 case:float n=();float e=();temp=(float)(e,n);/(temp);/弹出操作符栈中用过的元素 ();brea
17、k;/栈顶元素为(,不进行任何运算 case(:/只有当当前元素为“)”时,才弹出“(”,否则,将当前元素压栈,/并进行下一次循环,即扫描字符串的下一个元素 if(i)=)/弹出操作符栈中用过的元素 ();continue loop;else (i);continue loop;else /“)”不入栈 if(i)!=)(i);break;/返回栈顶元素,即算式结果 return();/比较优先级,判断 b 的优先级是否大于等于 a 的优先级 public static boolean compare(char a,char b)boolean bool=false;if(a=#)bool=true;else if(a=$&b!=#)bool=true;else if(a=)&b!=#&b!=$)bool=true;else if(a=+|a=-)&(b=+|b=-|b=*|b=/|b=|b=()bool=true;else if(a=*|a=/)&(b=*|b=/|b=|b=()bool=true;else if(a=)&(b=|b=()bool=true;else if(a=(&b=()bool=true;return bool;
限制150内