Java编写简单计算器(附源代码)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

西安交通大学
《Java技术与应用》大作业
简单计算器的编写
一、选题理由
这个学期我选择《Java技术与应用》这门课(其实我的选
修课已经修完了),完全是对java语言的兴趣和老师的个人魅力
使然。

这门课让我学到了许多知识,了解到了java语言的基本
语法、结构和特点。

由于个人觉得水平有限,并且没有队友,所
以选择了较为容易的简易计算器作为我的大作业。

二、系统目标
实现对代数表达式的输入、计算、输出。

三、系统分析与设计
(1)计算类cal设计编写
\
public class cal {
public static boolean check(String str)
If Jigloo is being used
* 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 JIGLOO 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;
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 jButton4;
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 {
setDefaultCloseOperation;
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 ActionListener() {
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="
dd(jButton3);
("3");
(214, 87, 82, 48);
(new "Microsoft YaHei UI", 0, 18));
(new ActionListener() {
|
public void actionPerformed(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(ActionEvent 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="
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="
+ 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(ActionEvent 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 actionPerformed(ActionEvent evt) {
", event="
+ evt);
dd(jButton15);
("/");
!
(384, 208, 64, 44);
jButton15
.setFont(new "Microsoft YaHei UI", 0, 28));
(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
", event="
+ evt);
dd(jButton16);
("=");
(386, 271, 64, 44);
/
jButton16
.setFont(new "Microsoft YaHei UI", 0, 28));
(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
", event="
+ evt);
dd(jButton17);
("^");
(311, 271, 64, 44);
jButton17
\
.setFont(new "Microsoft YaHei UI", 0, 28));
(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
", event="
+ evt);
dd(jButton18);
("C");
(215, 271, 82, 48);
jButton18
.setFont(new "Microsoft YaHei UI", 0, 18));…
(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
", event="
+ evt);
dd(jButton19);
(")");
(384, 93, 64, 44);
jButton19
.setFont(new "Microsoft YaHei UI", 0, 28));
(new ActionListener() {
~
public void actionPerformed(ActionEvent evt) {
", event="
+ evt);
dd(jButton20);
("(");
(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 ActionListener() {
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'||
(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;
.
//栈顶元素为-,弹出数字栈的前两个元素,进行运算
case '-':
temp=()+();
(temp);
//弹出操作符栈中用过的元素
();
break;
//栈顶元素为*,弹出数字栈的前两个元素,进行运算
case '*':
temp=()*();

(temp);
//弹出操作符栈中用过的元素
();
break;
//栈顶元素为/,弹出数字栈的前两个元素,进行运算
case '/':
temp=1/()*();
(temp);
//弹出操作符栈中用过的元素
();
|
break;
//栈顶元素为^,弹出数字栈的前两个元素,进行运算
case '^':
float n=();
float e=();
temp=(float) (e,n);
// (temp);
//弹出操作符栈中用过的元素
();
break;
~
//栈顶元素为(,不进行任何运算
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;
} }?。

相关文档
最新文档