软工第三次作业-结对项目

news/2025/10/22 17:32:57/文章来源:https://www.cnblogs.com/JurenXiao/p/19158554

软工第三次作业

小组成员
3123004287肖锦瑞
3123004268黄泽鹏

Github 仓库链接

这个作业属于哪个课程 软件工程
这个作业要求在哪里作业要求 作业要求
这个作业的目标 实现一个自动生成小学四则运算题目的命令行程序

PSP 表格

Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟)
计划 15 20
估计这个任务需要多少开发时间 10 15
开发 600 700
需求分析(包括学习新技术) 60 65
生成设计文档 20 25
设计复审 30 35
代码规范(为目前的开发制定合适的规范) 20 25
具体设计 30 35
具体编码 60 60
代码复审 20 25
测试(自我测试,修改代码,提交代码) 30 60
报告 30 35
测试报告 20 25
计算工作量 10 15
事后总结 15 20
合计 700 830

计算模块接口的设计与实现过程

计算结果的核心思想

利用栈数据结构,先进后出原则

方法:用两个栈(一个用于保存运算符,一个用于保存操作数),其中的运算符包括:+,-,*,/,(,),

表达式有括号、运算符、和操作数(数字)组成。我们根据以下4种情况从左到右逐个将这些实体送入栈处理

1,将操作数压入操作数栈

2,将运算符压入运算符栈

3,忽略左括号

4,在遇到右括号时,弹出一个运算符,弹出所需数量的操作数,并将运算符和操作数的运算结果压入操作数栈

最后:

在处理完最后一个右括号的时候,操作数栈中只会又一个值,他就是表达式的值

程序流程图

image

各模块具体实现代码

计算模块

public class Calculation {//设置运算符的优先级public static Map<String, Integer> map = new HashMap<String, Integer>() {/*** */private static final long serialVersionUID = 3684443232637633564L;{put("(", 0);put("+", 1);put("-", 1);put("×", 2);put("÷", 2);put(")", 3);put("=", 4);}};//将最终结果进行格式化public static String finalResult(String num) throws Exception {String[] nums = new String[2];int mole, deno;nums = FractionCalculation.change(num);mole = Integer.parseInt(nums[0]);deno = Integer.parseInt(nums[1]);String finalResult = FormatProcess.Format(mole, deno);if(finalResult.contains("-")) {//部分运算结果为负数throw new Exception();}else {return finalResult;}}// 括号内运算符的优先级是从低到高public static void doCalcul(Stack<String> operaStack, Stack<String> numStack, boolean flag) throws Exception {//将两个数值及运算符出栈进行计算String nowOpera = operaStack.pop();String nowNum_2 = numStack.pop();String nowNum_1 = numStack.pop();String result = FractionCalculation.result(nowOpera, nowNum_1, nowNum_2);			if(result.contains("-")) {	//结果为负										throw new Exception();}else {numStack.push(result);			}if(flag) {if("(".equals(operaStack.peek())) {//栈顶为(operaStack.pop();}else {doCalcul(operaStack, numStack, flag);//栈顶不为“(”}}else {if(!operaStack.empty()) {							doCalcul(operaStack, numStack, flag);}}}// 优先级比较public static void comparePriority(Stack<String> operaStack, Stack<String> numStack, String operator) throws Exception{String topOpera = operaStack.peek();int priority = map.get(operator) - map.get(topOpera);//优先级判断if (priority > 0) {operaStack.push(operator);} else {String nowOpera = operaStack.pop();String nowNum_2 = numStack.pop();String nowNum_1 = numStack.pop();String result = FractionCalculation.result(nowOpera, nowNum_1, nowNum_2);numStack.push(result);if (operaStack.empty()) {//运算符栈为空operaStack.push(operator);} else {comparePriority(operaStack, numStack, operator);}}}	
}

题目生成模块

//生成题目及计算出答案
public class GenerateItem {public static void createitem(int range, int num) {String operator[] = { "+", "-", "×", "÷" };String allAnswer = "";	//全部答案String allItems = "";	//全部题目for(int i = 0;i < num;i++) {int operatornum = (int) (Math.random() * 3 + 1);String figure = null, operate = null;int undo = 0;int bracketnum = 0;String expression = "";boolean flag = true;String result;Stack<String> operaStack = new Stack<String>();Stack<String> numStack = new Stack<String>();try {// 未包含最后一个数while (operatornum > 0) {int tag = (int) (Math.random() * 2);			//用于判断是否要生成括号figure = FormatProcess.fraction(range);operate = operator[(int) (Math.random() * 4)];if (tag == 0 && bracketnum < operatornum - 1) {expression += "(";operaStack.push("(");bracketnum++;undo++;flag = false;} else if (tag == 1&& flag == true && undo > 0) {expression += figure + ")" + " " + operate + " ";numStack.push(figure);Calculation.doCalcul(operaStack, numStack, true);Calculation.comparePriority(operaStack, numStack, operate);operatornum--;undo--;flag = false;} else { expression += figure + " " + operate + " ";if (operaStack.empty()) {operaStack.push(operate);numStack.push(figure);} else {numStack.push(figure);Calculation.comparePriority(operaStack, numStack, operate);}operatornum--;flag = true;}}// 添加最后一个数if (undo == 0) {figure = FormatProcess.fraction(range);expression += figure;} else if (undo == 1) {if (expression.startsWith("((") || expression.startsWith("(") && expression.contains(")") == false) {figure = FormatProcess.fraction(range);expression = expression.substring(1) + figure;} else {figure = FormatProcess.fraction(range);expression += figure + ")";}} else if (undo == 2) {if (expression.startsWith("((")) {figure = FormatProcess.fraction(range);expression = expression.substring(2) + figure;} else if (expression.startsWith("(")) {figure = FormatProcess.fraction(range);expression = expression.substring(1) + figure + ")";} else {figure = FormatProcess.fraction(range);expression += figure + "))";}}numStack.push(figure);Calculation.doCalcul(operaStack, numStack, false);result = Calculation.finalResult(numStack.peek());expression += " " + "=";if(expression.contains("÷ 0")) {i--;expression = "";}else {allItems +=   (i + 1) + "、  " + expression + "\n" ;allAnswer +=  (i + 1) + "、  " + result + "\n" ;}} catch (Exception e) {// TODO: handle exceptioni--;}}FileOutPut.FileOutPut(allItems, 1);FileOutPut.FileOutPut(allAnswer, 2);}
}

分数计算模块

public class FractionCalculation {public static String result(String nowOpera, String nowNum_1, String nowNum_2){String result = null;int mole_1, deno_1, mole_2, deno_2;String[] value_1 = change(nowNum_1);		mole_1 = Integer.parseInt(value_1[0]);deno_1 = Integer.parseInt(value_1[1]);String[] value_2 = change(nowNum_2);mole_2 = Integer.parseInt(value_2[0]);deno_2 = Integer.parseInt(value_2[1]);int flag = 0;if(nowOpera=="+") flag=1;else if(nowOpera=="-") flag=2;else if(nowOpera=="×") flag=3;else if(nowOpera=="÷") flag=4;switch (flag) {case 1:result = add(mole_1, deno_1, mole_2, deno_2);break;case 2:result = subtraction(mole_1, deno_1, mole_2, deno_2);break;case 3:result = multiplication(mole_1, deno_1, mole_2, deno_2);break;case 4:result = division(mole_1, deno_1, mole_2, deno_2);break;default:break;}			return result;			}	//加法public static String add(int a, int b, int c, int d) {int mole, deno;mole = a * d + c * b;deno = b * d;	return mole + "/" + deno;}//减法public static String subtraction(int a, int b, int c, int d){int mole, deno;mole = a * d - c * b;deno = b * d;return mole + "/" + deno;}//乘法public static String multiplication(int a, int b, int c, int d) {int mole, deno;mole = a * c;deno = b * d;return mole + "/" + deno;}//除法public static String division(int a, int b, int c, int d) {int mole, deno;mole = a * d;deno = b * c;return mole + "/" + deno;}//将分数转换成a/b形式并将a、b添加进array数组中public static String[] change(String num){String [] array = new String [2];if(num.contains("/")) {if(num.contains("'")) {//带分数String[] tarray = num.split("/|'");int[] sarray = new int[3];for(int i = 0;i<3;i++) {sarray[i] = Integer.parseInt(tarray[i]);}					array[0] = String.valueOf(sarray[0] * sarray[2] + sarray[1]);array[1] = String.valueOf(sarray[2]);			}else {					array = num.split("/");				}}else {array[0] = num;array[1] = "1";}return array;}		
}

格式处理模块

//格式处理
public class FormatProcess {public static String fraction(int range) {int mole, deno;mole = (int) (Math.random() * range);deno = (int) (Math.random() * range + 1);return Format(mole, deno);}public static String Format(int mole, int deno) {String fraction = null;int min = 0;min = (mole > deno)   ? deno : mole;if (mole == 0) {fraction = "0";} else if (mole % deno == 0) {fraction = String.valueOf(mole / deno);} else {for (int i = min; i >= 2; i--) {if (mole % i == 0 && deno % i == 0) {mole = mole / i;deno = deno / i;}}if (mole > deno) {fraction = String.valueOf(mole / deno) + "'" + String.valueOf(mole % deno) + "/" + String.valueOf(deno);} else {fraction = mole + "/" + deno;}}return fraction;}
}

文件输出保存模块

public class FileOutPut {public static void FileOutPut(String str, int tag) {String path = "";if(tag == 1) {path = "D:\\Exercises.txt";}else if(tag == 2){path = "D:\\Answers.txt";}File file = new File(path);FileWriter fw;try {fw = new FileWriter(file,false);fw.write(str);fw.close();System.out.println("文件保存成功!");} catch (IOException e) {// TODO Auto-generated catch blockSystem.out.println("文件保存不成功!");}}}

main方法

public class PairPro {public static void main(String[] args) {// TODO Auto-generated method stubint i=args.length;int num,range;long startTime = 0,endTime = 0;if(i==4) {//从命令行接收四个参数 -n,num,-r,rangeif(args[0]=="-n"&&args[2]=="-r") {//生成题目if(isNumeric(args[1])==true&&isNumeric(args[3])==true) {num=Integer.valueOf(args[1]);range=Integer.valueOf(args[3]);if(num>0&&range>0) {startTime = System.currentTimeMillis();GenerateItem.createitem(range, num);endTime = System.currentTimeMillis();}else {System.out.println("您输入的参数不合法!!!");System.exit(0);}}}else {System.out.println("您输入的参数不合法!!!");System.exit(0);}}else{System.out.println("您输入的参数数目不符合要求");System.exit(0);}System.out.println("程序运行时间:" + (endTime - startTime) + "ms");}//正则纯数字判断public static boolean isNumeric(String str) {Pattern pattern = Pattern.compile("[0-9]*");Matcher isNum = pattern.matcher(str);if (!isNum.matches()) {return false;}return true;}}

运行结果

87d4384e1ae9f18ade05dc6bd01960f2
9a69f51340ecb49686610684540d2b50

代码优化

·加入检查题目是否重复的模块
·加入答案检查模块

性能分析

cpu性能分析

image

字符和字符串型数据占用大量资源
image

个人总结

肖锦瑞
优点:二人结对编程情况下会比较认真负责,遇到不懂就一起在网上找资料一起学习
缺点:代码风格不同,组合代码时效率较差
黄泽鹏
优点:两个合作,想法更丰富,多元,计划制定更完善,资料查找更全面,工作量降低,能互相学习对方的写代码优点,专注自己擅长的部分,弱势部分由队友补足。
不足:没有建立良好的沟通渠道,沟通成本较高,没有建立良好统一的代码规范,后期管理会比较麻烦。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/943582.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2025 年中国校服厂家最新推荐榜单权威发布!深度解析优质品牌核心竞争力与选择指南

引言 我国中小学在校生规模已达 2.5 亿人,校服作为学生日常必备服饰,其品质与体验直接关系到千万家庭。然而当前市场仍存诸多痛点:部分产品面料安全不达标、版型不合身,洗后易变形褪色;设计同质化严重,既缺乏校园…

【IEEE出版 | EI检索稳定】第五届IEEE能源工程与电力系统国际学术会议(IEEE-EEPS 2025)

第五届IEEE能源工程与电力系统国际学术会议(IEEE-EEPS 2025),将于2025年10月31-11月2日在深圳召开。【IEEE冠名会议 | EI检索稳定】 【香港中文大学(深圳)主办】 第五届IEEE能源工程与电力系统国际学术会议(IEEE…

2025 年同步带厂家推荐:深入剖析浙江三星胶带有限公司,探寻橡胶带行业的优质之选

行业背景 在当今工业蓬勃发展的时代,汽车、摩托车、工业和农业机械设备、家用电器等领域持续扩张。传动胶带作为这些行业设备运转的关键零部件,其需求呈现出爆发式增长。市场对胶带产品的性能要求愈发严苛,不仅追求…

深夜的调试界面,藏着微信生态的黄金密码

2017年的夏天,广州的出租屋格外闷热。我对着电脑屏幕上的 PHP 代码敲到凌晨两点,烟灰缸里的烟蒂堆成了小山,心里却满是焦虑。作为一名刚入行三年的 PHP 开发者,我正陷入职业瓶颈:接的外包项目要么是重复的企业官网…

【题解】洛谷 P4096 [HEOI2013] Eden 的博弈树 | 更简洁的一种做法

洛谷 P4096 [HEOI2013] Eden 的博弈树,一种由 xzm 大神提供的更简洁做法。 首先需要从下往上求出以 \(i\) 为根的子树先后手的最小必胜集合大小,记为 \(f_{i,0/1}\)(\(0\) 为先手,\(1\) 为后手)。此外在转移同时维…

2025年丝杆升降机厂家最新行业推荐:联动丝杆升降机/螺旋丝杆升降机/蜗杆丝杆升降机/蜗轮丝杆升降机/三家兼顾工艺与适配性的实力厂家推荐

随着工业自动化领域对传动设备精度与适配性需求的不断提升,丝杆升降机作为关键传动部件,其生产厂家的工艺积累与场景适配能力愈发受到关注。以下三家厂家在丝杆升降机、螺旋升降机等品类的研发与生产中表现突出,其核…

智能配电变压器生产厂家口碑榜:基于技术实力、客户服务及市场反馈的专业评估

在智能电网建设快速推进的背景下,智能配电变压器作为配电网的核心设备,其技术水平和产品质量直接影响供电可靠性与能效水平。行业数据显示,2024年我国智能配电变压器市场规模预计将达到186亿元,年均复合增长率保持…

力扣300.最长递增子序列(经典dp)力扣375.猜数字II力扣.329矩阵最长的递增子序列力扣.33搜索旋转排序数组 - 详解

力扣300.最长递增子序列(经典dp)力扣375.猜数字II力扣.329矩阵最长的递增子序列力扣.33搜索旋转排序数组 - 详解pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; di…

Meta DINO系列论文浅读

Meta AI的DINO系列(DINO、DINOv2、DINOv3)代表了自监督视觉表示学习领域的重大进展。本报告系统性地分析了该系列模型的技术演进路径,从自监督学习的基础概念和传统方法的局限性出发,深入阐述了DINOv1、DINOv2和DI…

qemu模拟嵌入式开发板运行linux

单片机的:单片机模拟器 - JeasonBoy - 博客园mini2440imx6ullraspberry

2025年知名的工业铝型材深加工加工厂

2025年知名的工业铝型材深加工加工厂<h1>2025年知名的工业铝型材深加工加工厂推荐指南 </h1>工业铝型材深加工行业在制造业升级和绿色发展的推动下,已成为现代工业的重要支柱。随着新能源汽车、光伏、轨…

Apache Tika严重XXE漏洞分析与修复方案

本文详细分析了Apache Tika PDF解析模块中的关键XXE漏洞(CVE-2025-54988),该漏洞允许攻击者通过特制XFA文件执行XML外部实体注入,可能导致敏感数据泄露和内部资源恶意请求。文章提供了受影响版本列表和升级修复方案。…

防火密封胶条生产厂家口碑榜:基于技术实力、客户服务及市场反馈的专业评估

在建筑消防安全要求不断提升的背景下,防火密封胶条作为关键防火材料,其性能质量直接关系到建筑物的防火安全等级。本文基于行业调研数据,从企业规模、技术实力、产品质量及市场应用等维度,对防火密封胶条生产厂家进…

SAP ALV小数位去除

gt_fieldcat-decimals_o = 0.gt_fieldcat-decimals = 3. * gt_fieldcat-qfieldname = UNIT.gt_fieldcat-quantity = EA.

【WCH蓝牙系列芯片】-基于CH585开发板——BLE蓝牙广播----扩展广播应用

【WCH蓝牙系列芯片】-基于CH585开发板——BLE蓝牙广播----扩展广播应用-------------------------------------------------------------------------------------------------------------------------------------在…

茶桌茶台生产厂家口碑榜:TOP3企业综合实力全景解析

随着茶文化的复兴和家居品质需求的提升,茶桌茶台市场呈现稳定增长态势。据行业数据显示,2024年国内茶桌茶台市场规模已达185亿元,实木类产品占比62%。本文基于企业生产能力、材质工艺、设计创新及市场反馈等维度,对…

回转窑式干燥机生产厂家口碑榜:基于技术实力、客户服务及市场反馈的专业评估

随着工业化进程的不断推进,回转窑式干燥机在化工、冶金、建材等领域的应用日益广泛。行业数据显示,2024年中国干燥设备市场规模已突破350亿元,其中回转窑式干燥机因处理量大、运行稳定等特点,年销量增长率保持在18…

FileZilla Pro 破解版

FileZilla Pro 是一款高效的文件传输工具,支持多种协议,包括普通的FTP、加密的FTP (基于TLS) 和 SFTP。这款软件提供了详尽的PDF用户手册,帮助用户深入了解其丰富的功能和设置。 该版本已破解授权,可以使用全部功能…

详细介绍:【实时Linux实战系列】jemalloc/tcmalloc 与内存池:碎片与暂停时间控制

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

CF1508C tj

CF1508C 你总不能一下午啥事情都没干吧。 题面 作为一名教师,Riko Hakozaki 经常需要帮助她的学生解决各类学科的问题。今天,她被问到了一个编程任务,内容如下: 给定一个无向完全图,包含 \(n\) 个节点,其中部分边…