实例介绍
【实例截图】
【核心代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | package com.example.dell.calculator; import android.os.StrictMode; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.NumberPicker; public class MainActivity extends AppCompatActivity implements View.OnClickListener { //这里只能加声明,并不能使用函数 EditText output; Button id0; Button id1; Button id2; Button id3; Button id4; Button id5; Button id6; Button id7; Button id8; Button id9; Button jia; Button jian; Button chen; Button chu; Button del; Button clear; Button cal; boolean clear_flag ; //清空标识 iStack s1= new iStack(); //s1是值栈,s2是符号栈 cStack s2= new cStack(); @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); output=(EditText)findViewById(R.id.output); id0=(Button)findViewById(R.id.id0); id1=(Button)findViewById(R.id.id1); id2=(Button)findViewById(R.id.id2); id3=(Button)findViewById(R.id.id3); id4=(Button)findViewById(R.id.id4); id5=(Button)findViewById(R.id.id5); id6=(Button)findViewById(R.id.id6); id7=(Button)findViewById(R.id.id7); id8=(Button)findViewById(R.id.id8); id9=(Button)findViewById(R.id.id9); jia=(Button)findViewById(R.id.jia); jian=(Button)findViewById(R.id.jian); chen=(Button)findViewById(R.id.chen); chu=(Button)findViewById(R.id.chu); del=(Button)findViewById(R.id.del); clear=(Button)findViewById(R.id.clear); cal=(Button)findViewById(R.id.cal); id0.setOnClickListener( this ); //为此,我的类加了个接口 id1.setOnClickListener( this ); id2.setOnClickListener( this ); id3.setOnClickListener( this ); id4.setOnClickListener( this ); id5.setOnClickListener( this ); id6.setOnClickListener( this ); id7.setOnClickListener( this ); id8.setOnClickListener( this ); id9.setOnClickListener( this ); jia.setOnClickListener( this ); jian.setOnClickListener( this ); chen.setOnClickListener( this ); chu.setOnClickListener( this ); clear.setOnClickListener( this ); del.setOnClickListener( this ); cal.setOnClickListener( this ); } @Override public void onClick(View v) { String str=output.getText().toString(); switch (v.getId()) { case R.id.jia: case R.id.jian: case R.id.chen: case R.id.chu: int len=str.length(); if (len!= 0 ) { char lastcode=str.charAt(len- 1 ); if (lastcode< 48 ||lastcode> 57 ) str=str.substring( 0 ,len- 1 ); } case R.id.id0: case R.id.id1: case R.id.id2: case R.id.id3: case R.id.id4: case R.id.id5: case R.id.id6: case R.id.id7: case R.id.id8: case R.id.id9: str=str ((Button)v).getText(); output.setText(str); break ; case R.id.clear: str= "" ; output.setText(str); break ; case R.id.del: if (clear_flag) { clear_flag = false ; str = "" ; output.setText( "" ); } else if (str!= null &&!str.equals( "" )){ output.setText(str.substring( 0 ,str.length()- 1 )); } break ; case R.id.cal: getAns(); break ; } } public void getAns() { String str=output.getText().toString() '#' ; int len=str.length(); if (len== 1 ) return ; if (str.charAt( 0 ) < 48 || str.charAt( 0 ) > 57 ) { str=str.substring( 1 ,len- 1 ); output.setText(str); return ; } if (str.charAt(len- 2 ) < 48 || str.charAt(len- 2 ) > 57 ) { str=str.substring( 0 ,len- 2 ); output.setText(str); return ; } s2.push( '#' ); int n= 0 ; //n是表示扫描到的数字值为多少,初始化为0 boolean book= false ; //没办法,用来当做n取值的有效值,true有效 for ( int i= 0 ;i<len;i ) { char c = str.charAt(i); if (c >= 48 && c <= 57 ) { n = n * 10 c - 48 ; book = true ; } else if (c == ' ' || c == '-' || c == '*' || c == '/' || c == '#' ) { if (book == true ) { s1.push(n); //遇到符号就入栈,并清零 n = 0 ; book = false ; } if (compare(c)) { //如果当前优先级要低就开始计算 int n2 = s1.top(); //3 5,则n1为3,n2为5 s1.pop(); ; int n1 = s1.top(); s1.pop(); ; char op = s2.top(); s2.pop(); int ans = 0 ; switch (op) { case ' ' : ans = n1 n2; break ; case '-' : ans = n1 - n2; break ; case '*' : ans = n1 * n2; break ; case '/' : ans = n1 / n2; break ; } s1.push(ans); i--; //下一轮继续比 } else { s2.push(c); } } else { output.setText( "" ); return ; } } str=s1.top() "" ; output.setText(str); s1.clear(); s2.clear(); } boolean compare( char t) { int n1,n2; //n1是栈顶的优先级,n2是当前扫到符号的优先级,#为0, 、-为1,*、/为2 if (t== '*' ||t== '/' )n2= 2 ; else if (t== ' ' ||t== '-' ) n2= 1 ; else n2= 0 ; t=s2.top(); if (t== '*' ||t== '/' )n1= 2 ; else if (t== ' ' ||t== '-' ) n1= 1 ; else n1= 0 ; return n2<n1; //当前符号优先级小于栈顶元素的时候开始计算 } } class iStack{ private int value[]= new int [ 100 ]; private int top; iStack() { top=- 1 ; } public void push( int c) { value[ top]=c; } public void pop() { top--; } public boolean empty() { return top==- 1 ? true : false ; } public void clear() { top=- 1 ; } public int top() { return value[top]; } }; class cStack{ private char value[]= new char [ 100 ]; private int top; cStack() { top=- 1 ; } public void push( char c) { value[ top]=c; } public void pop() { top--; } public boolean empty() { return top==- 1 ? true : false ; } public void clear() { top=- 1 ; } public char top() { return value[top]; } }; |
标签: 计算器
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论