日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

结对编程之四则运算(马仪生、李瑞恒)

發布時間:2025/5/22 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 结对编程之四则运算(马仪生、李瑞恒) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

github項目傳送門:https://github.com/bpgg/FourArithmeticOperation

項目要求

  -?功能列表

  • ?[完成] 使用 -n 參數控制生成題目的個數
  • ?[完成] 使用 -r 參數控制題目中數值的范圍, 。該參數可以設置為1或其他自然數
  • ?[完成] 生成的題目中計算過程不能產生負數
  • ?[完成] 生成的題目中如果存在形如e1?÷ e2的子表達式,那么其結果應是真分數
  • ?[完成] 程序一次運行生成的題目不能重復,生成的題目存入執行程序的當前目錄下的Exercises.txt文件
  • ?[完成] 每道題目中出現的運算符個數不超過3個
  • ?[完成] 在生成題目的同時,計算出所有題目的答案,并存入執行程序的當前目錄下的Answers.txt文件
  • ?[完成] 程序應能支持一萬道題目的生成。
  • ?[未完成] 程序支持對給定的題目文件和答案文件,判定答案中的對錯并進行數量統計
  • ?

    PSP開發耗時

    PSP2.1

    Personal Software Process Stages

    預估耗時(分鐘)

    實際耗時(分鐘)

    Planning

    計劃

    ?30?60

    · Estimate

    · 估計這個任務需要多少時間

    ?30?60

    Development

    開發

    ?1000?2240

    · Analysis

    · 需求分析 (包括學習新技術)

    ?100?180

    · Design Spec

    · 生成設計文檔

    ?60?100

    · Design Review

    · 設計復審 (和同事審核設計文檔)

    ?30?30

    · Coding Standard

    · 代碼規范 (為目前的開發制定合適的規范)

    ?30?30

    · Design

    · 具體設計

    ?120?150

    · Coding

    · 具體編碼

    ?600?1500

    · Code Review

    · 代碼復審

    ?50?60

    · Test

    · 測試(自我測試,修改代碼,提交修改)

    ?150?60

    Reporting

    報告

    ?80?80

    · Test Report

    · 測試報告

    ?30?60

    · Size Measurement

    · 計算工作量

    ?20?10

    · Postmortem & Process Improvement Plan

    · 事后總結, 并提出過程改進計劃

    ?30?20

    合計

    ??1110?2400

    ?

    · 思路分析

    - 生成表達式

      使用對象存儲子表達式,兩個子表達式組成一個總表達式

      表達式對象使用字符串類型存儲操作數和操作符

      控制表達式符號與數字之間保留一個空格,方便后續篩選

    - 檢查是否重復

      每生成一個表達式都會添加到列表里面

      通過遍歷列表的表達式確定是否為相同的表達式子,如果是,重新生成表達。

    - 存儲表達式存儲

      將生成的中綴表達式轉化為后綴表達式

      根據運算優先級將操作數和符號存儲在對應節點中

      父節點存儲運算符號,子節點存儲操作數

      例如: 3+2+1

      

    ?

    - 計算表達式結果

      根據表達式對應的二叉樹進行計算,

      將左右子節點進行操作之后把值賦給父節點

      重復上述操作,直到根節點停止

      根節點的值即為所求

      并將所求值存儲在答案列表里

    - 寫入文件

      遍歷表達式列表和答案列表

      分別寫入對應的文件

    ?

    程序流程圖

    ?

    ?

    ?

    ?

    ?

    用戶使用說明

    舉例:

    -n? ?[數值] ? ? 使用 -n 參數控制生成題目的個數。

    -r? ?[數值] ? ? 使用 -r 參數控制題目中數值(自然數、真分數和真分數分母)的范圍。? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

    ?

    代碼

    項目目錄:

    ?

    ?生成表達式:

    - 生成操作數

    public String generateOperand(int iRange){String sOpreand="";switch(randomer.nextInt(3)) {case 0://生成帶分數sOpreand = (randomer.nextInt(iRange)+1)+"’"+generatePrimes(iRange);break;case 1:sOpreand = generatePrimes(iRange);break;case 2:sOpreand =String.valueOf(randomer.nextInt(iRange)+1);break;}return sOpreand;}

    ?

    ?- 生成分數(分子分母互質,無需約分)

    b private String generatePrimes(int iRange){//分子最大為8int iNumerator=randomer.nextInt(iRange)+1;int iDenominator=iNumerator+randomer.nextInt(iRange)+1;//保證兩個數互質if(iNumerator==0){//分子為0,分母直接使用return iNumerator+"/"+iDenominator;}int comDivisor = getComDivisor(iNumerator, iDenominator);iNumerator/=comDivisor;iDenominator/=comDivisor;return iNumerator+"/"+iDenominator;}

    ?

    ?

    ?- 數值互轉及詳細操作

    1 package utils; 2 3 import exception.IllegalSituationException; 4 5 public class CalculateUtil { 6 7 //加法 8 9 /** 10 * 兩個數的相加 11 * 12 * @param a 加數 13 * @param b 加數 14 * @return 結果 15 */ 16 public static String add(String a, String b) { 17 if (a.equals("0")||b.equals("0")){ 18 return a.equals("0")?b:a; 19 } 20 //兩個都是自然數 21 if (isNaturalNumber(a) && isNaturalNumber(b)) { 22 return String.valueOf(Integer.valueOf(a) + Integer.valueOf(b)); 23 } 24 25 //兩個都是分數 26 if (!isNaturalNumber(a) && !isNaturalNumber(b)) { 27 String m = getRealFraction(a); 28 String n = getRealFraction(b); 29 30 return simplify(addFractionAndFraction(m, n)); 31 32 } 33 //一個是自然數一個是分數 34 if (isNaturalNumber(a) && !isNaturalNumber(b)) { 35 String fraction = getRealFraction(b); 36 return simplify(addFractionAndNatural(a, fraction)); 37 } 38 39 //一個是分數一個是自然數 40 if (!isNaturalNumber(a) && isNaturalNumber(b)) { 41 String fraction = getRealFraction(a); 42 return simplify(addFractionAndNatural(b, fraction)); 43 } 44 45 return null; 46 47 } 48 49 /** 50 * 分數加自然數 ,int[0] 為分子, int[1] 為分母 51 * 52 * @param natural 自然數 53 * @param fraction 分數 54 * @return 結果 55 */ 56 private static String addFractionAndNatural(String natural, String fraction) { 57 int nat = Integer.valueOf(natural); 58 int[] numB = getDenominatorAndMolecule(fraction); 59 60 int molecule = nat * numB[1] + numB[0]; //分子 61 int denominator = numB[1]; // 分母 62 return molecule + "/" + denominator; 63 } 64 65 /** 66 * 兩個分數相加 int[0] 為分子, int[1] 為分母 67 * 68 * @param a 加數 69 * @param b 加數 70 * @return 結果 71 */ 72 private static String addFractionAndFraction(String a, String b) { 73 int[] numA = getDenominatorAndMolecule(a); 74 int[] numB = getDenominatorAndMolecule(b); 75 76 int molecule = numA[0] * numB[1] + numB[0] * numA[1]; 77 int denominator = numA[1] * numB[1]; 78 79 return molecule + "/" + denominator; 80 81 } 82 83 //減法 84 85 /** 86 * 兩個數的減法 87 * 88 * @param a 減數 89 * @param b 被減數 90 * @return 結果 91 */ 92 public static String subtract(String a, String b) { 93 if (a.equals("0")||b.equals("0")){ 94 return a.equals("0")? "-"+b:a; 95 } 96 //兩個都是自然數 97 if (isNaturalNumber(a) && isNaturalNumber(b)) { 98 return String.valueOf(Integer.valueOf(a) - Integer.valueOf(b)); 99 } 100 101 //兩個都是分數 102 if (!isNaturalNumber(a) && !isNaturalNumber(b)) { 103 String m = getRealFraction(a); 104 String n = getRealFraction(b); 105 106 return simplify(subtractFractionAndFraction(m, n)); 107 108 } 109 //一個是自然數一個是分數 110 if (isNaturalNumber(a) && !isNaturalNumber(b)) { 111 String fraction = getRealFraction(b); 112 return simplify(subtractFractionAndFraction(naturalToFraction(a, fraction), fraction)); 113 } 114 115 //一個是分數一個是自然數 116 if (!isNaturalNumber(a) && isNaturalNumber(b)) { 117 String fraction = getRealFraction(a); 118 return simplify(subtractFractionAndFraction(fraction, naturalToFraction(b, fraction))); 119 } 120 121 return null; 122 } 123 124 /** 125 * 自然數轉分數 int[0] 為分子, int[1] 為分母 126 * 127 * @param natural 自然數 128 * @param fraction 分數 129 * @return 結果 130 */ 131 private static String naturalToFraction(String natural, String fraction) { 132 int[] numFrac = getDenominatorAndMolecule(fraction); 133 int molecule = Integer.valueOf(natural) * numFrac[1]; //分子 134 int denominator = numFrac[1]; // 分母 135 return molecule + "/" + denominator; 136 137 } 138 139 /** 140 * 分數減分數 int[0] 為分子, int[1] 為分母 141 * 142 * @param a 分數 143 * @param b 分數 144 * @return 結果 145 */ 146 private static String subtractFractionAndFraction(String a, String b) { 147 int[] numA = getDenominatorAndMolecule(a); 148 int[] numB = getDenominatorAndMolecule(b); 149 150 int molecule = numA[0] * numB[1] - numB[0] * numA[1]; //分子 151 int denominator = numA[1] * numB[1]; //分母 152 153 return molecule + "/" + denominator; 154 } 155 156 //乘法 157 158 /** 159 * 乘法 160 * 161 * @param a 乘數 162 * @param b 乘數 163 * @return 結果 164 */ 165 public static String multiplies(String a, String b) { 166 if (a.equals("0")||b.equals("0")){ 167 return String.valueOf(0); 168 } 169 //兩個都是自然數 170 if (isNaturalNumber(a) && isNaturalNumber(b)) { 171 return String.valueOf(Integer.valueOf(a) * Integer.valueOf(b)); 172 } 173 174 //兩個都是分數 175 if (!isNaturalNumber(a) && !isNaturalNumber(b)) { 176 String m = getRealFraction(a); 177 String n = getRealFraction(b); 178 179 return simplify(multipliesFractionAndFraction(m, n)); 180 181 } 182 //一個是自然數一個是分數 183 if (isNaturalNumber(a) && !isNaturalNumber(b)) { 184 String fraction = getRealFraction(b); 185 return simplify(multipliesFractionAndFraction(naturalToFraction(a, fraction), fraction)); 186 } 187 188 //一個是分數一個是自然數 189 if (!isNaturalNumber(a) && isNaturalNumber(b)) { 190 String fraction = getRealFraction(a); 191 return simplify((multipliesFractionAndFraction(fraction, naturalToFraction(b, fraction)))); 192 } 193 return null; 194 } 195 196 /** 197 * 分數乘分數 198 * 199 * @param a 分數 200 * @param b 分數 201 * @return 結果 202 */ 203 private static String multipliesFractionAndFraction(String a, String b) { 204 int[] numA = getDenominatorAndMolecule(a); 205 int[] numB = getDenominatorAndMolecule(b); 206 207 int molecule = numA[0] * numB[0]; //分子 208 int denominator = numA[1] * numB[1]; // 分母 209 210 return molecule + "/" + denominator; 211 } 212 213 /** 214 * 除法 215 * @param a 除數 216 * @param b 被除數 217 * @return 結果 218 */ 219 public static String divide(String a, String b) throws IllegalSituationException { 220 if (a.equals("0")){ 221 return String.valueOf(0); 222 } 223 if (b.equals("0")){ 224 throw new IllegalSituationException("Argument b can’t be zero"); 225 } 226 //兩個都是自然數 227 if (isNaturalNumber(a) && isNaturalNumber(b)) { 228 return simplify(a + "/" + b); 229 } 230 231 //兩個都是分數 232 if (!isNaturalNumber(a) && !isNaturalNumber(b)) { 233 String m = getRealFraction(a); 234 String n = getRealFraction(b); 235 return simplify(divideFractionAndFraction(m, n)); 236 237 } 238 //一個是自然數一個是分數 239 if (isNaturalNumber(a) && !isNaturalNumber(b)) { 240 String fraction = getRealFraction(b); 241 return simplify(divideFractionAndFraction(naturalToFraction(a, fraction), fraction)); 242 } 243 244 //一個是分數一個是自然數 245 if (!isNaturalNumber(a) && isNaturalNumber(b)) { 246 String fraction = getRealFraction(a); 247 return simplify(divideFractionAndFraction(fraction, naturalToFraction(b, fraction))); 248 } 249 250 return null; 251 } 252 253 /** 254 * 分數除分數 255 * @param a 除數 256 * @param b 被除數 257 * @return 結果 258 */ 259 private static String divideFractionAndFraction(String a, String b) { 260 int[] numA = getDenominatorAndMolecule(a); 261 int[] numB = getDenominatorAndMolecule(b); 262 263 int molecule = numA[0] * numB[1]; 264 int denominator = numA[1] * numB[0]; 265 266 return molecule + "/" + denominator; 267 } 268 269 270 /** 271 * 獲取分數的分子和分母 272 * 273 * @param a 分數 274 * @return 結果,int[0] 為分子, int[1] 為分母 275 */ 276 private static int[] getDenominatorAndMolecule(String a) { 277 String numA[] = a.split("[/]"); 278 int numInt[] = new int[numA.length]; 279 for (int i = 0; i < numInt.length; i++) { 280 numInt[i] = Integer.valueOf(numA[i]); 281 282 } 283 return numInt; 284 } 285 286 /** 287 * 分數形式的轉換 288 * 289 * @param s 分數 290 * @return 結果 291 */ 292 private static String getRealFraction(String s) { 293 if (isFalseFraction(s)) { //1"1/2 294 String numStr[] = s.split("[’/]"); 295 int numInt[] = new int[numStr.length]; 296 for (int i = 0; i < numInt.length; i++) { 297 numInt[i] = Integer.valueOf(numStr[i]); 298 299 } 300 int denominator = numInt[0] * numInt[2] + numInt[1]; 301 return denominator + "/" + numStr[2]; 302 } 303 304 return s; 305 } 306 307 /** 308 * 判斷是否為自然數 309 * 310 * @param s 數 311 * @return 結果 312 */ 313 private static boolean isNaturalNumber(String s) { 314 return !s.contains("/"); 315 } 316 317 /** 318 * 判斷是否為 假分數 319 * 320 * @param s 數 321 * @return 結果 322 */ 323 private static boolean isFalseFraction(String s) { 324 return s.contains("’"); 325 } 326 327 private static String simplify(String fraction){ 328 int[] num = getDenominatorAndMolecule(fraction); 329 int molecule = num[0] ; 330 int denominator = num[1] ; 331 if (molecule==0){ 332 return String.valueOf(0); 333 } 334 if (molecule==denominator){ 335 return "1"; 336 } 337 338 if (molecule<denominator){ 339 int i ; 340 if ((i=gcd(molecule,denominator))==1){ 341 return molecule +"/" +denominator; 342 } 343 molecule = molecule/i; 344 denominator = denominator/i; 345 return molecule +"/" +denominator; 346 } 347 348 if (molecule>denominator){ 349 int i = gcd(molecule,denominator); 350 molecule = molecule/i; 351 denominator = denominator/i; 352 353 if (denominator==1){ 354 return molecule+""; 355 356 } 357 358 return getWithFraction(molecule,denominator); 359 360 } 361 362 return null; 363 364 } 365 366 /** 367 * 獲取帶分數 368 * @param molecule 分子 369 * @param denominator 分母 370 * @return 結果 371 */ 372 private static String getWithFraction(int molecule,int denominator){ 373 int withFraction = (molecule - (molecule%denominator)) / denominator; 374 molecule = molecule%denominator; 375 return withFraction+"’"+molecule+"/"+denominator; 376 } 377 378 /** 379 * 求最大公約數,歐幾里得方法 380 * @param m 數1 381 * @param n 數 382 * @return 結果 383 */ 384 private static int gcd(int m, int n) { 385 return n == 0 ? m : gcd(n, m % n); 386 } 387 388 public static boolean isNegative(String num){ 389 return num.contains("-"); 390 } 391 }

    ?

    ?

    - 后綴表達式處理數據

    public static String InfixToPostfix(String expression) {String str[] = expression.split("\\s+");for (String s : str) {//四個操作符if (isOperator(s)) {handleOperator(s);continue;}//左括號,入棧if (s.equals("(")) {mStack.push(s);continue;}//右括號,彈出并輸出,直到遇到左括號,左括號也彈出if (s.equals(")")) {while (!mStack.peek().equals("(")) {mExp.append(mStack.pop());mExp.append(" ");}mStack.pop();continue;}mExp.append(s);mExp.append(" ");}//棧中還有數據,直接出棧輸出while (!mStack.empty()) {mExp.append(mStack.pop());mExp.append(" ");}return mExp.toString();}

    ?

    - 輸出表達式到Exercises.txt和輸出答案到Answers.txt:

    private static void doWork(int iQuestions,int iRange){while (mList.size() != iQuestions) {String ex = Operation.generateQuestion();Node tree = TreeUtil.createTree(StringUtil.InfixToPostfix(ex));if (generate1(tree).isIllegal() && !isEquals(mResult)) {mResult.setExpression(ex);mList.add(mResult);}}//文件操作對象 FileOutputStream outSTr1 ;FileOutputStream outSTr2 ;BufferedOutputStream Buff1;BufferedOutputStream Buff2;long begin0 = System.currentTimeMillis();try {outSTr1 = new FileOutputStream(new File(".\\Exercises.txt"));outSTr2 = new FileOutputStream(new File(".\\Answers.txt"));Buff1 = new BufferedOutputStream(outSTr1);Buff2 = new BufferedOutputStream(outSTr2);for(int i=0; i<mList.size();i++){Buff1.write(((i+1)+" 、"+mList.get(i).getExpression()+"\r\n").getBytes());Buff2.write(((i+1)+" 、"+mList.get(i).getResult()+"\r\n").getBytes());}Buff1.flush();Buff2.flush();Buff1.close();Buff2.close();long end0 = System.currentTimeMillis();System.out.println("BufferedOutputStream執行耗時:" + (end0 - begin0) + " 毫秒");} catch(Exception e){System.out.println("出現異常:" + e.getMessage());}}

    ?

    ?

    測試運行

    表達式的輸出

    -n 10 -e 10
    9’9/10 + 6’6/11 × 9 - 6 = 62’89/110
    5 × 3’1/7 = 15’5/7
    3’1/9 - 1/2 = 2’11/18
    9 + 10/17 + 2 × 9 = 27’10/17
    9 + 2’3/13 ÷ 1/4 = 17’12/13
    3’5/7 + 8 - 2 = 9’5/7
    6’2/5 ÷ 9’3/4 ÷ 10’1/2 = 256/4095
    4’1/5 + 6/11 ÷ 9 = 4’43/165
    9’7/16 ÷ 3/7 - 5 = 17’1/48
    2’6/13 × 2’5/14 = 5’73/91

    ?

    - 隨機輸出10000條10范圍以內的表達式和答案:

      10000個答案?

      https://github.com/bpgg/FourArithmeticOperation/blob/master/src/Answers.txt

      10000個表達式

      https://github.com/bpgg/FourArithmeticOperation/blob/master/src/Exercises.txt

      

    總結

    在與馬儀生的合作中,我體驗到了直接調用別人寫好的接口的快樂。在本次實驗中,我主要負責生成表達式及總結,儀生主要負責處理表達式。在與儀生的合作中,我了解到更多編碼規范,必須對象類的報名定義為bean,一些固定的常量也需要放在const包中,調用的工具類放在util包里,把各種功能實現分開實現,最后集中使用,便于維護的同時也是代碼更加清晰。

    ?

    轉載于:https://www.cnblogs.com/fyy30/p/9710972.html

    總結

    以上是生活随笔為你收集整理的结对编程之四则运算(马仪生、李瑞恒)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 黄色片网站在线观看 | 国产美女www爽爽爽视频 | avtt在线播放 | 毛片aaa| 亚洲色精品三区二区一区 | 国产伦精品一区 | 国产精品三区在线观看 | 国产微拍一区 | 无码人妻精品一区二区蜜桃色欲 | 动漫美女隐私无遮挡 | 免费观看黄一级视频 | 免费观看nba乐趣影院 | 视频精品一区 | 婷婷激情五月 | 欧美日p视频| 91麻豆影视| 91好色先生tv | 亚洲成人xxx | 老湿机69福利区午夜x片 | 国产精品免费看久久久无码 | 久久亚洲av成人无码国产电影 | 中文字幕日日 | 国产群p视频| 国产精品tv | 欧美黄色一级视频 | 银杏av| 69国产视频 | 久久久久久国产精品免费播放 | 日本人添下边视频免费 | 九九热视频免费观看 | 国产亚洲成av人片在线观看桃 | 黑人精品欧美一区二区蜜桃 | 五月天六月婷 | 久久成人免费视频 | 中文字幕在线不卡视频 | 国产999在线观看 | 黄色观看网站 | 韩国中文字幕在线观看 | 国产精品九九视频 | 精品人妻无码一区 | 91久久人澡人人添人人爽欧美 | 色哟哟网站 | 农村一级毛片 | 黄色片视频在线观看 | 青青久视频 | 国产精品毛片久久久 | 麻豆亚洲一区 | 麻豆视频免费在线观看 | 另类亚洲色图 | 不卡免费视频 | 日批视频| 四虎在线免费视频 | 污污污污污污www网站免费 | 色女综合 | 亚洲国产精品成人 | 日批视频在线播放 | 日韩一区二区三区不卡 | 国产精品视频合集 | 蜜桃av一区二区 | 日本视频不卡 | 亚洲第一视频在线观看 | 欧美午夜性生活 | 亚洲欧美一区二区三区四区五区 | 在线观看免费高清视频 | 日韩一区二区三区精品 | 亚洲精品久 | 国产精品成人va在线观看 | 亚洲第四页 | 在办公室被c到呻吟的动态图 | 国产91高清| 九九热精品在线观看 | 久久久久久久久成人 | 国产精品一区在线播放 | 熟妇人妻中文av无码 | 亚洲黄色在线观看 | www在线视频 | 日韩最新网址 | 久久99精品久久久久久水蜜桃 | 国产三级精品三级 | 图片区偷拍区小说区 | 日韩有码第一页 | 无码国产精品96久久久久 | 国产手机在线播放 | 开心激情五月网 | 自拍偷拍电影 | 日b视频免费看 | 亚洲色欧美 | 中文字幕日韩欧美在线 | 婷婷成人综合网 | 五十路毛片 | 国产伦精品一区二区三区88av | 成人黄色网 | 国产又粗又猛又爽免费视频 | 欧美日韩高清在线 | 韩国一级一片高清免费观看 | 欧美剧场 | 天天干天天操心 | 久久一精品 | 日韩成人免费观看 |