二代计算器完成除了同层括号问题之外其他问题都已解决
    可以简单的实现四则运算及括号优先运算(实现失败)

    1. package gouzao;
    2. import java.awt.*;
    3. import java.awt.event.*;
    4. import java.text.DecimalFormat;
    5. import java.text.DecimalFormat;
    6. import javax.swing.*;
    7. import org.omg.CORBA.TCKind;
    8. public class computer extends JFrame implements ActionListener{
    9. String[] str= {"7","8","9","DEL","Replay","4","5","6","×","÷","1","2","3","+","-","0","(",")","=","end"};
    10. JPanel p = new JPanel();
    11. DecimalFormat df = new DecimalFormat("0");
    12. int round=0,round1=0;
    13. char un=' ';
    14. int x=1,o=0,s=0,y=0,r=0,m=0,w=0,l=0,d=0,kl=0;//开关:x表示字符长度,o判断用户在输入运算符之前是否按了0键(o为0时其他数字键可以替代0)
    15. //s,y,m,w判断用户是否输入了+,-,×,÷键(控制用户防止用户输入两次同样的运算符及其他非法操作),r判断用户是否输入加减乘除;
    16. double sum=0,befor=0,k=0;
    17. char c[]= {'/','/','/','/','/','/','/','/','/','/','/','/','/','/','/'};
    18. JTextField textA;
    19. String t="0 ";
    20. public computer() {
    21. setLayout(new BorderLayout());
    22. setFont(new Font("Arial",Font.PLAIN,14));
    23. textA = new JTextField("",5);
    24. textA.setFont(new Font("Arial",Font.PLAIN,30));
    25. textA.disable();
    26. getContentPane().add("North",textA);
    27. p.setLayout(new GridLayout(4,5));
    28. for(int i=0;i<20;i++) {
    29. JButton b = new JButton(str[i]);
    30. b.addActionListener(this);
    31. b.setActionCommand(str[i]);
    32. p.add(b);
    33. }
    34. getContentPane().add("Center",p);
    35. }
    36. public static void main(String[] agrs) {
    37. computer c = new computer();
    38. c.setTitle("计算器");
    39. c.pack();
    40. c.setVisible(true);
    41. c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    42. c.setLocationRelativeTo(null);
    43. while(true) {
    44. c.textA.setText(c.t);
    45. }
    46. }
    47. public void actionPerformed(ActionEvent e) {
    48. if(e.getActionCommand().equals("7")) {
    49. if(x==15||d==1) {
    50. return;
    51. }else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0) {
    52. x=x-1;}
    53. if(o==1) {
    54. x=x-1;
    55. o=0;
    56. }
    57. StringBuilder strBuilder = new StringBuilder(t);
    58. strBuilder.setCharAt(x,'7');
    59. strBuilder.toString();
    60. t=strBuilder.toString();
    61. x=x+1;
    62. s=0;y=0;m=0;w=0;l=0;;
    63. }else if(e.getActionCommand().equals("8")) {
    64. if(x==15||d==1) {
    65. return;
    66. }else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0){//为0时退格
    67. x=x-1;}
    68. if(o==1) {
    69. x=x-1;
    70. o=0;
    71. }
    72. StringBuilder strBuilder = new StringBuilder(t);
    73. strBuilder.setCharAt(x,'8');
    74. strBuilder.toString();
    75. t=strBuilder.toString();
    76. x=x+1;
    77. s=0;y=0;m=0;w=0;l=0;;
    78. }else if(e.getActionCommand().equals("9")) {
    79. if(x==15||d==1) {
    80. return;
    81. }else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0){//为0时退格
    82. x=x-1;}
    83. if(o==1) {
    84. x=x-1;
    85. o=0;
    86. }
    87. StringBuilder strBuilder = new StringBuilder(t);
    88. strBuilder.setCharAt(x,'9');
    89. strBuilder.toString();
    90. t=strBuilder.toString();
    91. x=x+1;
    92. s=0;y=0;m=0;w=0;l=0;;
    93. }else if(e.getActionCommand().equals("4")) {
    94. if(x==15||d==1) {
    95. return;
    96. }else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0){//为0时退格
    97. x=x-1;}
    98. if(o==1) {
    99. x=x-1;
    100. o=0;
    101. }
    102. StringBuilder strBuilder = new StringBuilder(t);
    103. strBuilder.setCharAt(x,'4');
    104. strBuilder.toString();
    105. t=strBuilder.toString();
    106. x=x+1;
    107. s=0;y=0;m=0;w=0;l=0;;
    108. }else if(e.getActionCommand().equals("5")) {
    109. if(x==15||d==1) {
    110. return;
    111. }else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0){//为0时退格
    112. x=x-1;}
    113. if(o==1) {
    114. x=x-1;
    115. o=0;
    116. }
    117. StringBuilder strBuilder = new StringBuilder(t);
    118. strBuilder.setCharAt(x,'5');
    119. strBuilder.toString();
    120. t=strBuilder.toString();
    121. x=x+1;
    122. s=0;y=0;m=0;w=0;l=0;;
    123. }else if(e.getActionCommand().equals("6")) {
    124. if(x==15||d==1) {
    125. return;
    126. }else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0){//为0时退格
    127. x=x-1;}
    128. if(o==1) {
    129. x=x-1;
    130. o=0;
    131. }
    132. StringBuilder strBuilder = new StringBuilder(t);
    133. strBuilder.setCharAt(x,'6');
    134. strBuilder.toString();
    135. t=strBuilder.toString();
    136. x=x+1;
    137. s=0;y=0;m=0;w=0;l=0;;
    138. }else if(e.getActionCommand().equals("1")) {
    139. if(x==15||d==1) {
    140. return;
    141. }else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0){//为0时退格
    142. x=x-1;}
    143. if(o==1) {
    144. x=x-1;
    145. o=0;
    146. }
    147. StringBuilder strBuilder = new StringBuilder(t);
    148. strBuilder.setCharAt(x,'1');
    149. strBuilder.toString();
    150. t=strBuilder.toString();
    151. x=x+1;
    152. s=0;y=0;m=0;w=0;l=0;;
    153. }else if(e.getActionCommand().equals("2")) {
    154. if(x==15||d==1) {
    155. return;
    156. }else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0){//为0时退格//为0时退格
    157. x=x-1;
    158. }
    159. if(o==1) {
    160. x=x-1;
    161. o=0;
    162. }
    163. StringBuilder strBuilder = new StringBuilder(t);
    164. strBuilder.setCharAt(x,'2');
    165. strBuilder.toString();
    166. t=strBuilder.toString();
    167. x=x+1;
    168. s=0;y=0;m=0;w=0;l=0;
    169. }else if(e.getActionCommand().equals("3")) {
    170. if(x==15||d==1) {
    171. return;
    172. }else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0){//为0时退格
    173. x=x-1;}
    174. if(o==1) {
    175. x=x-1;
    176. o=0;
    177. }
    178. StringBuilder strBuilder = new StringBuilder(t);
    179. strBuilder.setCharAt(x,'3');
    180. strBuilder.toString();
    181. t=strBuilder.toString();
    182. x=x+1;
    183. s=0;y=0;m=0;w=0;l=0;
    184. }else if(e.getActionCommand().equals("0")) {
    185. if(x==15||d==1) {
    186. return;
    187. }else if(t.charAt(0)=='0'&&r==0){
    188. return;
    189. }else if(o==1){
    190. return;
    191. }else
    192. {
    193. StringBuilder strBuilder = new StringBuilder(t);
    194. strBuilder.setCharAt(x,'0');
    195. strBuilder.toString();
    196. t=strBuilder.toString();
    197. if(s==1||y==1||m==1) {
    198. o=1;
    199. }
    200. x=x+1;
    201. s=0;y=0;m=0;w=0;l=0;
    202. }
    203. }else if(e.getActionCommand().equals("+")) {
    204. if((x==15||x==14)&&(y==0&&m==0&&w==0)) {
    205. return;
    206. }else if(s==1||l==1) {
    207. return;
    208. }else {
    209. if(y==1||m==1||w==1){
    210. x=x-1;
    211. }
    212. StringBuilder strBuilder = new StringBuilder(t);
    213. strBuilder.setCharAt(x,'+');
    214. strBuilder.toString();
    215. t=strBuilder.toString();
    216. s=1;
    217. y=0;
    218. w=0;
    219. x=x+1;
    220. d=0;
    221. o=0;
    222. r=1;
    223. }
    224. }else if(e.getActionCommand().equals("-")) {
    225. if((x==15||x==14)&&(s==0&&m==0&&w==0)) {
    226. return;
    227. }else if(y==1||l==1) {
    228. return;
    229. }else
    230. {
    231. if(s==1||m==1||w==1){
    232. x=x-1;
    233. }
    234. StringBuilder strBuilder = new StringBuilder(t);
    235. strBuilder.setCharAt(x,'-');
    236. strBuilder.toString();
    237. t=strBuilder.toString();
    238. y=1;
    239. s=0;
    240. m=0;
    241. d=0;
    242. w=0;
    243. x=x+1;
    244. o=0;
    245. r=1;
    246. }
    247. }else if(e.getActionCommand().equals("=")) {
    248. int od=0;
    249. int gl=0;
    250. int kl=0;
    251. for(;round>0;round--) {
    252. if(t.charAt(x-1)=='+'||t.charAt(x-1)=='-'||t.charAt(x-1)=='×'||t.charAt(x-1)=='÷')
    253. x=x-1;
    254. if(t.charAt(x-1)=='(') {
    255. StringBuilder strBuilder = new StringBuilder(t);
    256. strBuilder.setCharAt(x,' ');
    257. strBuilder.toString();
    258. t=strBuilder.toString();
    259. }
    260. StringBuilder strBuilder = new StringBuilder(t);
    261. strBuilder.setCharAt(x,')');
    262. strBuilder.toString();
    263. t=strBuilder.toString();
    264. x=x+1;
    265. }
    266. do {
    267. if(t.charAt(gl)=='(') {
    268. od++;
    269. }
    270. gl++;
    271. }while(t.charAt(gl)!=')'&&t.charAt(gl)!=' ');
    272. int j=0;
    273. double []a=new double[15];
    274. int b=0;
    275. String[] t2=new String[21];
    276. while(t.charAt(j)!=' ') {
    277. String t1="0 ";
    278. for(int i=j;i<20;i++) {
    279. if(t.charAt(i)>='0'&&t.charAt(i)<='9') {
    280. StringBuilder strBuilder1 = new StringBuilder(t1);
    281. strBuilder1.setCharAt(i-j,t.charAt(i));
    282. strBuilder1.toString();
    283. t1=strBuilder1.toString();
    284. }else {
    285. c[b]=t.charAt(i);
    286. j=i+1;
    287. break;
    288. }
    289. }
    290. double all=0;
    291. for(int g=0;g<t1.replace(" ","").length();g++) {
    292. double u=(int)Math.pow(10,t1.replace(" ","").length()-g-1);
    293. all=all+u*Integer.parseInt(t1.charAt(g)+"");
    294. }
    295. t2[b]=t1;
    296. a[b]=all;
    297. b=b+1;
    298. }
    299. if(sum==0||a[0]==befor) {
    300. sum=a[0];
    301. }else {
    302. sum=0;
    303. }
    304. for(int l=0;l<=100;l++) {
    305. System.out.println(round1+" "+c[l]+" "+c[l+1]+" "+l+" "+sum+" "+kl);
    306. if(l==0)
    307. sum=a[0];
    308. if(l!=0) {
    309. if(c[l-1]=='(')
    310. sum=a[l];
    311. }
    312. if(c[l]=='+'&&c[l+1]=='(') {
    313. sum=sum+k;
    314. }else if(c[l]=='-'&&c[l+1]=='(') {
    315. sum=sum-k;
    316. }else if(c[l]=='÷'&&c[l+1]=='(') {
    317. sum=sum/k;
    318. }else
    319. if((c[l]=='('&&od==0)||c[l]=='/') {
    320. break;
    321. }else if(od!=0&&kl==0) {//括号跳转
    322. if(round1!=od) {
    323. round1=round1+1;
    324. while(c[l]!='('){
    325. l++;
    326. }
    327. if(round1==od)
    328. kl=1;
    329. continue;
    330. }
    331. }else if(kl==1&&(c[l]=='('||c[l]==')')) {//括号跳转
    332. if(od==1) {
    333. l=-1;
    334. k=sum;
    335. od--;
    336. round1--;
    337. kl=1;
    338. }else {
    339. round1--;
    340. od--;
    341. kl=1;
    342. l--;
    343. while(c[l]!='('&&l!=0){
    344. l--;
    345. }
    346. l--;
    347. while(c[l]!='('&&l!=0){
    348. l--;
    349. }
    350. k=sum;
    351. continue;
    352. }
    353. }else if(c[l]=='+'&&kl==1) { //加法
    354. if(l!=0) {
    355. if(c[l-1]=='(')
    356. sum=a[l];
    357. }
    358. double u=a[l+1];
    359. while(c[l+1]=='÷'||c[l+1]=='×') {
    360. if(c[l+1]=='×'&&c[l+2]!='(') {
    361. u=u*a[l+2];
    362. }else if(c[l+1]=='÷'&&c[l+2]!='('){
    363. u=u/a[l+2];
    364. }else if(c[l+1]=='×'&&c[l+2]=='(') {
    365. u=u*k;
    366. }else if(c[l+1]=='÷'&&c[l+2]=='(') {
    367. u=u/k;
    368. }
    369. l=l+1;
    370. }
    371. sum=sum+u;
    372. }else if(c[l]=='-'&&kl==1) { //减法
    373. if(c[l-1]=='(')
    374. sum=a[l];
    375. double u=a[l+1];
    376. while(c[l+1]=='÷'||c[l+1]=='×') {
    377. if(c[l+1]=='×'&&c[l+2]!='(') {
    378. u=u*a[l+2];
    379. }else if(c[l+1]=='÷'&&c[l+2]!='('){
    380. u=u/a[l+2];
    381. }else if(c[l+1]=='×'&&c[l+2]=='(') {
    382. u=u*k;
    383. }else if(c[l+1]=='÷'&&c[l+2]=='(') {
    384. u=u/k;
    385. }
    386. l=l+1;
    387. }
    388. sum=sum-u;
    389. }
    390. else if(c[l]=='×'&&kl==1) { //乘法
    391. System.out.println(" "+sum);
    392. if(c[l-1]=='(')
    393. sum=a[l];
    394. double u=a[l+1];
    395. if(c[l+1]=='(')
    396. u=k;
    397. while(c[l+1]=='÷'||c[l+1]=='×') {
    398. if(c[l+1]=='×'&&c[l+2]!='(') {
    399. u=u*a[l+2];
    400. }else if(c[l+1]=='÷'&&c[l+2]!='('){
    401. u=u/a[l+2];
    402. }else if(c[l+1]=='×'&&c[l+2]=='(') {
    403. u=u*k;
    404. }else if(c[l+1]=='÷'&&c[l+2]=='(') {
    405. u=u/k;
    406. }
    407. l=l+1;
    408. }
    409. sum=sum*u;
    410. }else if(c[l]=='÷'&&kl==1) { ///除法
    411. if(c[l-1]=='(')
    412. sum=a[l];
    413. double u=a[l+1];
    414. while(c[l+1]=='÷'||c[l+1]=='×') {
    415. if(c[l+1]=='×'&&c[l+2]!='(') {
    416. u=u*a[l+2];
    417. }else if(c[l+1]=='÷'&&c[l+2]!='('){
    418. u=u/a[l+2];
    419. }else if(c[l+1]=='×'&&c[l+2]=='(') {
    420. u=u*k;
    421. }else if(c[l+1]=='÷'&&c[l+2]=='(') {
    422. u=u/k;
    423. }
    424. l=l+1;
    425. }
    426. sum=sum/u;
    427. }
    428. }
    429. int f=0;
    430. for(int m=0;m<b;m++) {
    431. if(t2[m].replace(" ","").length()>10) {
    432. t="error";
    433. sum=0;
    434. f=1;
    435. }else if(sum==befor&&befor!=0) {
    436. f=1;
    437. }
    438. }
    439. if(f==0) {
    440. t=String.valueOf(df.format(sum));
    441. }else {
    442. t=String.valueOf(df.format(befor));
    443. }
    444. x=t.replace(" ", "").length();
    445. for(int h=0;h<16-(x-1);h++) {
    446. t=t+" ";
    447. }
    448. t=t.replace("+","");
    449. t=t.replace("-","");
    450. t=t.replace("×","");
    451. befor=sum;
    452. double wr=0;
    453. for(int g=0;g<t.replace(" ","").length();g++) {
    454. double u=(int)Math.pow(10,t.replace(" ","").length()-g-1);
    455. wr=wr+u*Integer.parseInt(t.charAt(g)+"");
    456. }
    457. sum=wr;
    458. s=0;y=0;m=0;w=0;l=0;r=0;
    459. }else if(e.getActionCommand().equals("Replay")) { //重置
    460. t="0 ";
    461. x=1;
    462. m=0;
    463. un=' ';
    464. y=0;
    465. s=0;
    466. round=0;
    467. w=0;
    468. k=0;
    469. sum=0;
    470. r=0;
    471. befor=0;
    472. d=0;
    473. l=0;
    474. }else if(e.getActionCommand().equals("×")) {
    475. if((x==15||x==14)&&(s==0&&y==0&&w==0)) {
    476. return;
    477. }else if(m==1||l==1) {
    478. return;
    479. }else
    480. {
    481. if(s==1||y==1||w==1){
    482. x=x-1;
    483. }
    484. StringBuilder strBuilder = new StringBuilder(t);
    485. strBuilder.setCharAt(x,'×');
    486. strBuilder.toString();
    487. t=strBuilder.toString();
    488. m=1;
    489. y=0;
    490. w=0;
    491. s=0;
    492. x=x+1;
    493. o=0;
    494. r=1;
    495. d=0;
    496. }
    497. }else if(e.getActionCommand().equals("÷")) {
    498. if((x==15||x==14)&&(s==0&&y==0&&m==0)) {
    499. return;
    500. }else if(w==1||l==1) {
    501. return;
    502. }else
    503. {
    504. if(s==1||y==1||m==1){
    505. x=x-1;
    506. }
    507. StringBuilder strBuilder = new StringBuilder(t);
    508. strBuilder.setCharAt(x,'÷');
    509. strBuilder.toString();
    510. t=strBuilder.toString();
    511. w=1;
    512. m=0;
    513. y=0;
    514. s=0;
    515. x=x+1;
    516. o=0;
    517. d=0;
    518. r=1;
    519. }
    520. }else if(e.getActionCommand().equals("(")) {
    521. if(x==15||x==14) {
    522. return;
    523. }else if(l==1||(s==0&&y==0&&m==0&&w==0)) {
    524. return;
    525. }else{
    526. StringBuilder strBuilder = new StringBuilder(t);
    527. strBuilder.setCharAt(x,'(');
    528. strBuilder.toString();
    529. t=strBuilder.toString();
    530. round=round+1;
    531. s=0;
    532. l=1;
    533. y=0;
    534. w=0;
    535. x=x+1;
    536. o=0;
    537. r=1;
    538. }
    539. }
    540. else if(e.getActionCommand().equals(")")){
    541. if(x==15){
    542. return;
    543. }else if(round==0||t.charAt(x-1)=='(') {
    544. return;
    545. }else if(s==1||y==1||m==1){
    546. x=x-1;
    547. }
    548. StringBuilder strBuilder = new StringBuilder(t);
    549. strBuilder.setCharAt(x,')');
    550. strBuilder.toString();
    551. t=strBuilder.toString();
    552. s=0;
    553. d=1;
    554. round=round-1;
    555. l=0;
    556. y=0;
    557. w=0;
    558. x=x+1;
    559. o=0;
    560. r=1;
    561. }
    562. else if(e.getActionCommand().equals("DEL")) {
    563. if(x!=0&&x!=1) {
    564. x=x-1;
    565. StringBuilder strBuilder = new StringBuilder(t);
    566. strBuilder.setCharAt(x,' ');
    567. strBuilder.toString();
    568. t=strBuilder.toString();
    569. }else if(x==1&&t.charAt(1)!=0) {
    570. x=x-1;
    571. StringBuilder strBuilder = new StringBuilder(t);
    572. strBuilder.setCharAt(x,'0');
    573. strBuilder.toString();
    574. t=strBuilder.toString();
    575. x++;
    576. }
    577. System.out.println(x);
    578. }
    579. else if(e.getActionCommand().equals("end")){
    580. System.exit(0);
    581. }
    582. }
    583. }