程序构思:将每个功能分块将乘除 和加减法分开,再将整数和分数分开,利用java 的输入来控制数值范围,数量等要求,利用switch语句供用户实现多个功能,在处理分数时要注意分母不能为零,分数要化为最简;会产生负数的只有减法。
源程序:
package arithmetic;
import java.util.Scanner;
public class Arithmetic2 {
public static void main(String[] args) {
int num1=0,num2=0,mark3=0,num4=0,num5=0,mark6=0,mark7=0,i,j,p,h,l;
int nuofop,nuofop2,nuofop3;
int top,lower;
int choose;
int m1,m2,mar1,mar2;
char mark[]={'+','-','*','/'};
char mark1[]={'+','-'};
char mark2[]={'*','/'};
int []m={};
boolean q=false;
Scanner sc=new Scanner(System.in);
System.out.println("请输入需要的运算的个数");
nuofop=sc.nextInt();
System.out.println("请输入参与运算的数的个数上限为10");
nuofop2=sc.nextInt();
System.out.println("请输入运算范围的上限");
top=sc.nextInt();
System.out.println("若想要运算的下限为负请输入运算范围的下限,否则下限为0");
lower=sc.nextInt();
System.out.println("请选择一个需要的功能");
System.out.println("1:整数加减,2:分数加减,3:整数可能有负数,4:整数一定无负数");
choose=sc.nextInt();
System.out.println("--------------------------------------------------------");
for(i=0;i<nuofop;i++)
{
int num11[]=new int[nuofop+1];
int num12[]=new int[nuofop+1];
int num13[]=new int[nuofop+1];
num1=(int)(Math.random()*top+1);
num2=(int)(Math.random()*top+1);
mark3=(int)(Math.random()*mark.length+0);
num11[i]=num1;num12[i]=num2;num13[i]=mark3;
for(j=1;j<i-1;j++)
{
if(num11[i]==num11[j]&&num12[i]==num12[j]&&num13[i]==num13[j])
{
do
{
num1=(int)(Math.random()*nuofop+1);
num2=(int)(Math.random()*nuofop+1);
}while(num11[i]==num11[j]&&num12[i]==num12[j]&&num13[i]==num13[j]);
}
}
}
for(i=0;i<nuofop;i++)
{
num1=(int)(Math.random()*top+1);
num2=(int)(Math.random()*top+1);
num4=(int)(Math.random()*top+1);
num5=(int)(Math.random()*top+1);
mark6=(int)(Math.random()*mark1.length+0);
if(num2==0){num2++;}
if(num5==0){num5++;}
for(p=num1;p>0;p--)
{
if(num1%p==0&&num2==0)
{
num1=num1/p;
num2=num2/p;
}
}
for(h=num4;h>0;h--)
{
if(num4%h==0&&num5%h==0)
{
num4=num4/h;
num5=num5/h;
}
}
switch(choose)
{
case 1:{System.out.println("("+num1+mark1[mark6]+num4+")"+"=");}break;
case 2:{System.out.println("("+num1+"/"+num4+")"+mark1[mark6]+"("+num4+"/"+num5+")"+"=");}break;
case 3:{System.out.println(num1+"-"+num4+"=");}break;
case 4:
{
if(num1<num4)
{
int t;
t=num1;
num1=num4;
num4=t;
System.out.println(num1+"-"+num4+"=");
}
}break;
}
}
System.out.println("请选择需要的功能");
System.out.println("1:整数乘除运算,2:分数乘除运算,3:除法有余数,4:除法无余数,5:有无括号");
nuofop3=sc.nextInt();
System.out.println("------------------------------------------------------------");
for(i=0;i<nuofop;i++)
{
num1=(int)(Math.random()*top+1);
num2=(int)(Math.random()*top+1);
num4=(int)(Math.random()*top+1);
num5=(int)(Math.random()*top+1);
mark7=(int)(Math.random()*mark2.length+0);
if(num2==0){num2++;};
if(num5==0){num5++;};
for(p=num1;p>0;p++)
{
if(num1%p==0&&num2%p==0)
{
num1=num1/p;
num2=num2/p;
}
}
for(h=num4;h>0;h++)
{
if(num4%h==0&&num5%h==0)
{
num4=num4/h;
num5=num5/h;
}
}
for(i=0;i<nuofop2;i++)//有无括号
{
num1=(int)(Math.random()*top+1);
mark3=(int)(Math.random()*mark.length+0);
m[i]=num1;
m[i+1]=mark3;
for(j=0;j<mark1.length;j++)
{
for(l=0;l<mark2.length;l++)
{
m1=m[i];
m2=m[i+2];
mar1=mark1[j];
mar2=mark2[l];
if(m1==mar1&&m2==mar2)
{
q=true;
}
}
}
}
switch(nuofop3)
{
case 1:{System.out.println("("+num1+mark2[mark7]+num4+")"+"=");};break;
case 2:{System.out.println("("+num1+"/"+num2+")"+mark2[mark7]+"("+num4+"/"+num5+")"+"=");};break;
case 3:{
if(num1<num4)
{
int t;
t=num1;
num1=num4;
num4=t;
}
System.out.println(num1+"/"+num4+"=");
};break;
case 4:{
if(num1%num4==0){
System.out.println(num1+"/"+num4+"=");
}
};break;
case 5: {
if(q==true)
{
System.out.println("算式中有括号");
}
else
System.out.println("算式中无括号");
}
}
}
}
}
项目计划总结表:日期:2016/3/19
日期任务 | 听课 | 编写程序 | 阅读课本 | 日总计 | |||
周日 | |||||||
周一 | 140 | 60 | 200 | ||||
周二 | |||||||
周三 | 240 | 240 | |||||
周四 | 30 | 30 | 60 | ||||
周五 | |||||||
周六 | 240 | 60 | 300 | ||||
周总结 |
时间记录日志:日期:2016/3/19
日期 | 开始时间 | 结束时间 | 中断时间 | 净时间 | 活动 | 备注 | ||
3/14 | 8:00 | 9:50 | 10 | 140 | 上课 | |||
13:00 | 14:00 | 10 | 50 | 看书 | 找作业思路 | |||
3/16 | 13:00 | 18:00 | 20 | 280 | 写程序 | 写程序,找资料,休息 | ||
3/17 | 14:30 | 3:00 | 0 | 30 | 写程序 | |||
3/19 | 13:00 | 18:00 | 20 | 280 | 写程序 | 写程序,找资料,思考 | ||
缺陷记录日志:日期:2016/3/19
日期 | 编号 | 引入阶段 | 排除阶段 | 修复时间 | 修复缺陷 | 描述 |
3/16 | 1 | 编码 | 编译 | 10min | 漏掉了”}“ | |
3/17 | 2 | 编码 | 编译 | 2min | case 后面写成了; | |
3/19 | 3 | 编码 | 编译 | 3min | 数组初始化错误 | |