Week2-作业2

代码地址:https://git.coding.net/wangt244/homework1.git

一需求分析

1.随机产生n道四则运算的练习题,数字范围 [ 0,100 ],运算符在3~5之间。

2不得出现负数与非整数

3将你的学号与生成的n道练习题及其对应的正确答案输出到文件“result.txt”中,不要输出额外信息,文件目录与程序目录一致

4附加功能(未实现)、

二功能设计

满足软件基本功能要求的前提下,实现四则运算的显示,输出;

三设计实现

调用不同的方法来处理加减乘除的问题

四算法详解

代码如下   加了一些注释

import java.io.File;
import java.io.FileOutputStream;
import java.util.Random;
import java.util.Scanner;

public class Main {
static Random rand=new Random();
static int up=101;
public static int INT() {//生成1~100随机数
int x=rand.nextInt()%up;
return (x+up)%up;
}

public static int INT2(int x) {
for(int i=2;i<=x;++i)
if(x%i==0)return i;
return 1;
}

public static char op(){//生成随机运算符
int x=INT()%4;
if(x==0)return '-';
if(x==1)return '+';
if(x==2)return '*';
return '/';
}

public static String pro() {//连接生成的表达式
int x=INT(),y=0,z=0,y2=INT();
char c1=op(),c2=op();
if(c1=='/')
{
y=INT2(x);
z=x/y;
}
else if(c1=='+'){
y=INT();
z=x+y;
}
else if(c1=='*'){
y=INT();
z=x*y;
}
else if(c1=='-'){
y=INT()%x;
z=x-y;
}

if(c2=='/')
{
if(c1=='+'||c1=='-')y2=INT2(y);
}
else if(c2=='-'){
y2=INT()%z;
}
return ""+x+c1+y+c2+y2+"=\r\n";
}

public static void main(String[] args) {
try{
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
File output = new File("result.txt");
FileOutputStream fos = new FileOutputStream(output, true);
for(int k=0;k<n;++k)
{
String st = pro();
byte[] b = st.getBytes();
fos.write(b);
//System.out.println(st);
}
fos.close();
}catch (Exception e){ }
}
}

5运行测试

 

六部分代码片段

public static String pro() {//连接生成的表达式
int x=INT(),y=0,z=0,y2=INT();
char c1=op(),c2=op();
if(c1=='/')
{
y=INT2(x);
z=x/y;
}
else if(c1=='+'){
y=INT();
z=x+y;
}
else if(c1=='*'){
y=INT();
z=x*y;
}
else if(c1=='-'){
y=INT()%x;
z=x-y;
}

if(c2=='/')
{
if(c1=='+'||c1=='-')y2=INT2(y);
}
else if(c2=='-'){
y2=INT()%z;
}
return ""+x+c1+y+c2+y2+"=\r\n";
}

public static void main(String[] args) {
try{
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
File output = new File("result.txt");
FileOutputStream fos = new FileOutputStream(output, true);
for(int k=0;k<n;++k)
{
String st = pro();
byte[] b = st.getBytes();
fos.write(b);
//System.out.println(st);
}
fos.close();
}catch (Exception e){ }
}
}

七总结

本来写代码就很差劲,努力试了一下,还是没达到全部的要求,在各位大佬的帮助下,还是没能做成完整的

以前基本没怎么敲过代码  现在感觉挺累的  而且效果也不好   新手   多多包涵   以后要加油了

看那些大佬每天轻轻松松   我们头皮发麻   以后好好整

八psp

PSP2.1

任务内容

计划共完成需要的时间(min)

实际完成需要的时间(min)

Planning

计划

8

15

·        Estimate

·   估计这个任务需要多少时间,并规划大致工作步骤

8

10

Development

开发

82

250

·        Analysis

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

6

300

·        Design Spec

·         生成设计文档

5

10

·        Design Review

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

4

25

·        Coding Standard

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

3

13

·        Design

·         具体设计

10

20

·        Coding

·         具体编码

36

300

·        Code Review

·         代码复审

7

30

·        Test

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

13

20

Reporting

报告

9

15

·         Test Report

·         测试报告

3

6

·         Size Measurement

·         计算工作量

2

5

·         Postmortem & Process Improvement Plan

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

3

4

posted on 2018-03-25 21:42  王涛244  阅读(144)  评论(1编辑  收藏  举报