四则运算 结对项目
github 地址:https://github.com/wangshicheng0213/HomeWork02
一、基本要求
1) 實(shí)現(xiàn)一個(gè)帶有用戶界面的四則運(yùn)算。
2) 生成的題目不能重復(fù)。
3) 支持負(fù)數(shù),例如-1,-1/2,-3‘4/5等。
4) 題目的數(shù)量(個(gè)人項(xiàng)目的要求)
5) 數(shù)值的范圍
6) 題目中最多幾個(gè)運(yùn)算符
7) 題目中是否有乘除法
8) 題目中或運(yùn)算過(guò)程中有無(wú)負(fù)數(shù)
根據(jù)老師的要求,我們做的程序中解決了以上的問(wèn)題。因?yàn)樵诘谝淮沃谱鞯乃膭t運(yùn)算中我的代碼就已經(jīng)制作了一個(gè)用戶操作的界面,只是比較粗糙不夠完善,所以這次我和我的組員完善了四則運(yùn)算的界面和一些具體的功能。在這些要求中的運(yùn)算符有幾個(gè)的問(wèn)題里,第一次制作的四則運(yùn)算的運(yùn)算符是做了三個(gè),所以這次依舊使用三個(gè)運(yùn)算符,沒有進(jìn)行大的修改。
二、源代碼
?
源代碼:
?
package com.nenu.calculation;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Map.Entry;
public class Main {
//?public static void main(String[] args) {
//??System.out.println("請(qǐng)輸入n,即想要產(chǎn)生練習(xí)題題目數(shù)量");
//??Scanner scanner = new Scanner(System.in);
//??boolean key1 = true;
//??String temp = null;
//??while(key1){
//???temp = scanner.next();//輸入練習(xí)題的數(shù)量
//???if(!temp.matches("^\\d+$")){
//????System.out.println("輸入錯(cuò)誤,請(qǐng)?jiān)佥斎胍淮?#34;);
//???}
//???else
//????key1 = false;
//??}
//??int account = Integer.valueOf(temp);
//??int questionCount = 0;
//??????? File file = new File("../HomeWork01/result.txt");
//??????? PrintStream ps = null;
//??try {
//???ps = new PrintStream(new FileOutputStream(file));
//??} catch (FileNotFoundException e1) {
//???// TODO Auto-generated catch block
//???e1.printStackTrace();
//??}
//??????? ps.println("2018102996");// 往文件里寫入學(xué)號(hào)字符串
//??//System.out.println(account);
//??while(questionCount != account){
//???//隨機(jī)三到五個(gè)數(shù)字
//???Random random = new Random();
//???int nums = random.nextInt(3) + 3;
//???//System.out.println(nums);
//???ArrayList<Integer> numArrayList = new ArrayList<Integer>();
//???for (int j = 0; j < nums; j++) {
//????Random randomNum = new Random();
//????int number = randomNum.nextInt(101);
//????numArrayList.add(number);
//???}
???System.out.println(numArrayList);
//???//隨機(jī)數(shù)字 - 1個(gè)運(yùn)算符
//???Map<Integer, String> opeMap = new HashMap<Integer, String>();
//???opeMap.put(1, "+");
//???opeMap.put(2, "-");
//???opeMap.put(3, "*");
//???opeMap.put(4, "÷");
//???int opeNum = 0;
//???ArrayList<String> operrayList = new ArrayList<String>();
//???while(opeNum != nums - 1){
//????Random randomOpe = new Random();
//????int key = randomOpe.nextInt(4)+1;
//????String ope01 = opeMap.get(key);
//????operrayList.add(ope01);
//????opeNum ++;
//???}
???System.out.println(operrayList);
???Set<Entry<Integer, Character>> opeSet = opeMap.entrySet();
???Iterator<Entry<Integer, Character>> ope = opeSet.iterator();
???System.out.print("[");
???while(ope.hasNext()){
????Entry<Integer, Character> type = (Entry<Integer, Character>)ope.next();
????int k = type.getKey();
????Character v = type.getValue();
????System.out.print(v + ",");
???}
???System.out.println("]");
//???if(true){
????System.out.println(numArrayList);
????System.out.println(operrayList);
//????ArrayList<String> equString = new ArrayList<String>();
//????equString.add(numArrayList.get(0).toString());
//????for (int i = 1; i < nums; i++) {
//?????equString.add(operrayList.get(i - 1).toString());
//?????equString.add(numArrayList.get(i).toString());
//????}
????System.out.println(equString);
//????StringBuilder stringBuilder = new StringBuilder();
//????for (int i = 0; i < equString.size(); i++) {
//?????stringBuilder.append(equString.get(i));
//????}
//????????? float data = 0;
//????try {
//?????String expression = stringBuilder.toString();
//?????Main nbl = new Main(expression);
//?????????? String nbls_cc = new String();
//?????????? float result = 0;
//?????????? nbl.zz_hz();
?????????? nbl.nbls_bc();
?????????? System.out.println("對(duì)應(yīng)的逆波蘭式為 :" + nbls_cc);
?????????? System.out.println("結(jié)果是:");
//?????????? data = nbl.js_nbl();
//?????????? //分?jǐn)?shù)保留2位
//?????????? DecimalFormat df = new DecimalFormat("0.00");
?????????? String num3 = df.format(num);
//?????????? String dataStr = df.format(data);
?????????? System.out.println(result);
//???????????
?????data = evaluateExpression(expression);
//???????????
//?????stringBuilder.append("=" + dataStr);
//?????????? System.out.println(stringBuilder);
?????????? File dest = new File("../HomeWork01/result.txt");
?????????? BufferedWriter writer = new BufferedWriter(new FileWriter(dest));
?????????? writer.write(stringBuilder.toString());
?????????? writer.flush();
?????????? writer.close();
//????} catch (Exception e) {
//?????questionCount = questionCount - 1;
//????}
//???????????? ps.print(stringBuilder + "\n");// 在已有的基礎(chǔ)上添加字符串
//????questionCount ++;
//???}//每生成一個(gè)題目+1
//??}
//??ps.flush();
//??ps.close();
//?} //? 操作符棧
??? private Stack<String> czf_stack = new Stack<>();??????? // 存放 運(yùn)算符的棧
??? private? ArrayList<String> ysbds_list = new ArrayList<>();???? //存放 原始表達(dá)式的 arraylist
??? private? ArrayList<String> nblbds_list = new ArrayList<>();????? // 存放轉(zhuǎn)換后的 逆波蘭式
??? private static final int One = 1;????? //
??? private static final int Two = 3;???? //
??? private static final int Three = 5;?? //規(guī)定優(yōu)先級(jí)?? Three 最高
???? //? 定義一個(gè)運(yùn)算棧
???? private static Stack<String> ys_stack = new Stack<>();
????
????? // 初始化???????????????????????????? 使用StringTokenizer分割 字符串
???? public Main(String bdString) {
???????? // TODO Auto-generated constructor stub
??????? StringTokenizer stringTokenizer = new StringTokenizer(bdString, "+-*÷()",true);
??????? while(stringTokenizer.hasMoreTokens()){
????????????? ysbds_list.add(stringTokenizer.nextToken());
???????????? //System.out.println(stringTokenizer.nextToken());
???????? }
??? }
????
? ??
???? // 判斷 是否是數(shù)字
???? public boolean isNum(String str){
???????? if(str.matches("[0-9]+")){??? //這里使用正則表達(dá)式 驗(yàn)證是否是數(shù)字
???????????? //System.out.println("Y");
??????????? return true;
???????? }else{
???????????? //System.out.println("N");
???????????? return false;
???????? }
???? }
????
???? // 判斷 是否是操作符
??? public boolean isCzf(String str){
??????? if(str.matches("[\\+\\-\\*\\÷\\(\\)]")){
??????????? //System.out.println("Y");
??????????? return true;
???????? }else{
???????????? //System.out.println("N");
???????????? return false;
???????? }
???? }
???
???? // 獲取 優(yōu)先級(jí)
??? public int getYxj(String str){
????????
???????? switch(str){
???????? case "(":return Three;
???????? case "*":
???????? case "÷":return Two;
???????? case "+":
???????? case "-":return One;
???????? case ")":return 0;
????????
???????? default : return -1;
?????????
???????? }
????????
????? }
? ?
???? // 判斷優(yōu)先級(jí)
????? public boolean isYxj(String str1,String str2){
????????? return getYxj(str1) > getYxj(str2);??
????? }
????? /**
?????? *
?????? * @author 李明陽(yáng)
?????? * @title: stack_czf
?????? * @date 2016年3月28日 上午9:01:12
?????? * @param czf void
?????? */
????? //?? ********* 當(dāng) 當(dāng)前操作元素為 操作符時(shí)**********??? 這里是 核心代碼, 用于操作符棧的判斷
????? public void stack_czf(String czf){
????????
????????? //判斷當(dāng)前棧內(nèi)是否為空
??????? if(czf_stack.isEmpty()){
???????????? czf_stack.push(czf);
????????????? return;
???????? }
?????????
????????? //判斷是否為 (
???????? if("(".equals(czf)){
???????????? czf_stack.push(czf);
???????????? return;
???????? }
????????
???????? //判斷是否為 )
???????? if(")".equals(czf)){
??????????? String string = "";
???????????? while(!"(".equals(string = czf_stack.pop())){
???????????????? nblbds_list.add(string);
???????????? }
???????????? return;
??????? }
?????????
???????? //如果 當(dāng)前棧頂元素是? (? 直接入棧
???????? if("(".equals(czf_stack.peek())){
???????????? czf_stack.push(czf);
???????????? return;
???????? }
????????
???????? // 判斷 與 棧頂元素的優(yōu)先級(jí) , > 為true
???????? if(isYxj(czf, czf_stack.peek())){
??????????? czf_stack.push(czf);
???????????? return;
???????? }
????????
???????? if(!isYxj(czf, czf_stack.peek())){
???????????? nblbds_list.add(czf_stack.pop());
???????????? stack_czf(czf);?? //這里調(diào)用函數(shù) 本身,并將本次的操作數(shù)傳參
???????? }
????????
???? }
? ? ?
???? // 中綴 —> 后綴
??? public void zz_hz(){
????????
???????? // 遍歷原始表達(dá)式list
???????? for(String str:ysbds_list){
????????????
???????????? //System.out.println("->? " + str);
????????????
???????????? if(isNum(str)){
???????????????? nblbds_list.add(str);
???????????? }else if(isCzf(str)){
???????????????? stack_czf(str);
???????????? }else{
???????????????? System.out.println("非法");
???????????? }
????????????
???????? }
????????
???????? // 遍歷完原始表達(dá)式后? 將操作符棧內(nèi)元素 全部添加至 逆波蘭表達(dá)式list
?
???????? while(!czf_stack.isEmpty()){
???????????? //System.out.println("即將 " + czf_stack.peek());
??????????? nblbds_list.add(czf_stack.pop());
???????? }
????????
???? }
? ? ?
??? // 具體計(jì)算方法
???? public Float jsff(String s1,String s2,String s3){
???????? float a = Float.parseFloat(s2);
???????? float b = Float.parseFloat(s1);
???????? switch(s3){
???????? case "+":return a+b;
???????? case "-":return a-b;
???????? case "*":return a*b;
???????? case "÷":return a/b;
???????? default : return (float) 0.0;
??????? }
???? }
??
???? //? 計(jì)算 逆波蘭式
???? public Float js_nbl(){
???????? for(String str:nblbds_list){
???????????? if(isNum(str)){
???????????????? ys_stack.push(str);
???????????? }else{
???????????????? ys_stack.push(String.valueOf(jsff(ys_stack.pop(), ys_stack.pop(), str)));
??????????? }
???????? }
???????? return Float.parseFloat(ys_stack.pop());? //最后 棧中元素 即為結(jié)果
???? }
????
?//??? public void nbls_bc(){
?//??????? for(String string:nblbds_list){
?//??????????? nbls_cc += string;
?//??????? }
?//??? }
}
三.實(shí)驗(yàn)心得
在做的過(guò)程中,出現(xiàn)了很多的問(wèn)題,在逆波蘭式的調(diào)用過(guò)程中接口沒有對(duì)應(yīng),GUi設(shè)計(jì)也很粗糙,對(duì)待學(xué)習(xí)的任務(wù)還是要認(rèn)真一點(diǎn),并不是說(shuō)任務(wù)重要,而是要時(shí)刻保持做任務(wù)的嚴(yán)謹(jǐn)?shù)男膽B(tài),保持心態(tài)是最重要的。
與此同時(shí),通過(guò)上網(wǎng)查閱資料和同學(xué)的幫助讓我順利的解決了自己的問(wèn)題,也讓我慢慢看到了自己的不足之處還有很多,需要學(xué)習(xí)和努力的地方還有很多,并不能因?yàn)樽约簳?huì)一些基礎(chǔ)的編程就驕傲,當(dāng)真正制作東西的時(shí)候這些小問(wèn)題就會(huì)暴露出來(lái),讓我手足無(wú)措。我會(huì)繼續(xù)完善自己的四則運(yùn)算,也會(huì)讓自己的基本功和一些進(jìn)階的編程操作更加扎實(shí)。相信自己下一次制作出來(lái)的東西會(huì)更加完美。
轉(zhuǎn)載于:https://www.cnblogs.com/wangshicheng/p/10007627.html
總結(jié)
- 上一篇: thinkPHP 数据库操作和分页类
- 下一篇: Visual Studio Code搭建