软件工程第二周笔记+简单四则运算实现

编写高质量代码

1.高质量软件开发之道:高质量的设计、规范的编码、有效的测试

2.软件编码规范:是与特定语言相关的描写如何编写代码的规则集合。

3.软件编程规范的目的

(1)提高编码质量,避免不必要的程序错误。

(2)增强程序的可读性、可重用性和可移植性。

4.高质量的设计:模块化设计、面向对象编程、错误与异常处理

5.模块化设计的目的:降低系统构造的复杂性,增加灵活性

6.模块化程序设计基本思想:将一个大的程序按功能分拆成一系列小模块。

7.模块化程序设计的优点:降低程序的复杂性、提高模块的可靠性和复用性、缩短产品的开发周期、易于维护和功能扩展。

8.模块分解策略:(1)基于易变与稳定:认识和识别变与不变的部分,并将之科学的分离开。(2)基于单一职责:类或函数应该只做一件事,并且做好这件事。

9.错误与异常处理:(1)错误:是导致程序崩溃的问题,例如Python程序的语法错误(解析错误)或者未捕获的异常(运行错误)等。(2)异常:是运行时期检测到的错误,即使一条语句或者表达式在语法上是正确的,当试图执行它时也可能会引发错误。

 

 简单四则运算

题目要求:

a)        C++版:https://www.cnblogs.com/SivilTaram/p/software_pretraining_cpp.html

b)        Java版:https://www.cnblogs.com/SivilTaram/p/software_pretraining_java.html

编程环境:

VS2017

详细代码:

  1 #include"pch.h"
  2 #include"stdlib.h"
  3 #include<iostream>
  4 #include<cstdio>
  5 #include<string>
  6 #include<time.h>
  7 #include<sys/timeb.h>
  8 #include<fstream>
  9 using namespace std;
 10 int num = 3;
 11 string str = "";
 12 int random(double start, double end)  //随机数函数
 13 {
 14     return (int)(start + (end - start)*rand() / (RAND_MAX + 1.0));
 15 }   
 16 
 17 void expression(int ans)           //生成运算式
 18 {
 19     int f = 1 + random(0, 100);
 20     if (f < 25)   // 加法
 21     {   
 22         if (num > 2)
 23         {
 24             num--;
 25             int x = random(1, ans);
 26             expression(x);
 27             int y = ans - x;
 28             if (y >= 0)
 29                 str = to_string(y) + "+" + str;
 30 
 31         }
 32         else 
 33         {
 34             int x = random(1, ans);
 35             int y = ans - x;
 36             if (y >= 0)
 37                 str = str + to_string(y) + "+" + to_string(x);
 38         }
 39     }
 40     if (f >= 25 && f < 50)    //减法
 41     {    
 42         if (num > 2) 
 43         {
 44             num--;
 45             int x = random(1, ans);
 46             expression(x);
 47             int y = ans + x;
 48             str = to_string(y) + "-" + "(" + str + ")";
 49 
 50         }
 51         else 
 52         {
 53             int x = random(1, ans);
 54             int y = ans + x;
 55             str = str + to_string(y) + "-" + to_string(x);
 56         }
 57     }
 58     if (f >= 50 && f < 75)  // 乘法
 59     {  
 60         if (num > 2) 
 61         {
 62             num--;
 63             int x = 0;
 64             for (int i = (int)sqrt(ans); i > 0; i++) {
 65                 if (ans%i == 0) {
 66                     x = i;
 67                     break;
 68                 }
 69             }
 70             int y = ans / x;
 71             expression(x);
 72             str = to_string(y) + "*" + "(" + str + ")";
 73 
 74         }
 75         else 
 76         {
 77             int x = 0;
 78             for (int i = (int)sqrt(ans); i > 0; i++) 
 79             {
 80                 if (ans%i == 0) 
 81                 {
 82                     x = i;
 83                     break;
 84                 }
 85             }
 86             int y = ans / x;
 87             str = str + to_string(y) + "*" + to_string(x);
 88         }
 89     }
 90     if (f >= 75)    // 除法
 91     {       
 92         if (num > 2) 
 93         {
 94             num--;
 95             int x = 1 + random(0, 5);
 96             expression(x);
 97             int y = ans * x;
 98             str = to_string(y) + "÷" + "(" + str + ")";
 99 
100         }
101         else 
102         {
103             int x = 1 + random(0, 5);
104             int y = ans * x;
105             str = str + to_string(y) + "÷" + to_string(x);
106         }
107     }
108 }
109 
110 
111 void run()                     //进行整数运算
112 {
113     int n, i;
114     int rightCount = 0, wrongCount = 0, ansCount = 1, exeCount = 1;
115     ofstream fout("IntAnswers.txt");    
116     ofstream fout1("IntExercises.txt");
117     cout << "请输入运算式数量:";
118     cin >> n;
119     i = n;
120     while (n--)
121     {
122         int answers, ans;
123         struct timeb timeSeed;
124         ftime(&timeSeed);
125         srand(timeSeed.time * 1000 + timeSeed.millitm);
126         ans = random(1, 25);
127         fout << ansCount++ << "." << ans << endl;
128         expression(ans);
129         cout << str << endl;
130         fout1 << exeCount++ << "." << str << endl;
131         str = "";
132         num = 3;
133         cin >> answers;
134         if (answers == ans) 
135             rightCount++;
136         else 
137             wrongCount++;
138         cout << "你完成了:" << i - n << "道题" << "   正确:" << rightCount << "" << "   错误:" << wrongCount << "" << endl;
139 
140     }
141 }
142     
143 int main()
144 {
145     system("cls");
146     run();
147     return 0;
148 }

项目总结:

这个项目的基础要求不是很麻烦,只需考虑整数的加减乘除,运算式和结果不必考虑分数。因为好久没有编程的原因,看到这个题目有点懵,解题思路不是很明确,编程环境是用的VS,之前写C++都是用的Codeblocks,有些地方还是有点小小的区别。主要是编程编的少,很多东西都忘得差不多了,做起来有点吃力。

 

 

 

PSP2.1

Personal Software Process Stages

预估耗时(m

实际耗时(m

Planning

计划

10

15

· Estimate

· 估计这个任务需要多少时间

   

Development

开发

120

180

· Analysis

· 需求分析 (包括学习新技术)

20

60

· Design Spec

· 生成设计文档

   

· Design Review

· 设计复审 (和同事审核设计文档)

   

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

   

· Design

· 具体设计

   

· Coding

· 具体编码

   

· Code Review

· 代码复审

   

· Test

· 测试(自我测试,修改代码,提交修改)

   

Reporting

报告

   

· Test Report

· 测试报告

   

· Size Measurement

· 计算工作量

   

· Postmortem & Process Improvement Plan

· 事后总结, 并提出过程改进计划

   
 

合计

   
posted @ 2019-03-12 14:27  djesse1  阅读(228)  评论(0编辑  收藏  举报