//之前的作业(软件工程个人作业02)由于自己的java出点问题,所以用c++来写
//2016/3/26日由于权限受限无法提交作业导致延时

【团队组合】
张更、陈昌

【设计思路】
将程序分成两大部分:简单二元四则运算,复杂多元四则运算
简单二元四则运算:
简单二元四则运算中实现大部分的要求。
再进行分数运算判断用户输入结果使,判断运算结果是否为整数,若为整数则提示用户输入整数,若不为整,则输入分数判断结果。
复杂多元运算:
随机生成用户定制数量Cno个多项运算式,使用for(int i=0;i<Cno;i++)语句循环,循环体判断运算符是否除号还是乘号,将对应位置的运算数进行相应的运算,并将结果存储到第一个数所在的位置,然后将后面的数向前移一位,长度减一。在使用类似的循环计算加减。最后结果存储在数组的一号位置
【程序源代码】
import java.util.*;
public class Arithmetic
{
public static void main(String args[])
{
/*Isctm(是否定制),Cno(运算题个数),Isfra(系统选择是否有分数),IsPfra(是否真分数),Lb(取值上界),Ub(取值下界),
* ctmType(定制类型),testType(题目类型),Isneg(有无负数),Israd(有无乘除法),Isrem(有无余数), Isbra(有无括号),
* cIsfra(用户选择是否有分数)
*/
int Cno,Isfra,IsPfra,temp,Lb,Ub,Isctm,ctmType,testType,Isneg,Israd,Isrem,Isbra,t,ans,cIsfra,Ismul;
Ismul = 0;
ctmType = 0;
Isneg = 1;
Israd = 1;
Isrem = 0;
Isfra = 0;
IsPfra = 0;
Lb = 0;
Ub = 100;
Cno = 30;
t=0;
Scanner sc = new Scanner(System.in);
System.out.println("!本程序默认生成30道取值在0-100以内的包含整数及假分数的两个数之间的四则运算题!");
//选择是否定制
System.out.println("请选择是否定制:0.不需定制 1.我要定制");
Isctm = sc.nextInt();
//非法输入检测并提醒改正
while(Isctm!=0 && Isctm!=1)
{
System.out.println("警告!非法输入!请重新选择是否定制:0.不需定制 1.我要定制");
Isctm = sc.nextInt();
}
if(Isctm==1)
{
while(ctmType!=4)
{
//若参与定制,执行以下内容
System.out.println("-----------------------------");
System.out.println("|**欢迎使用四则运算需求定制**|");
System.out.println("|** 1.自定义取值范围 **|");
System.out.println("|** 2.自定义题目数量 **|");
System.out.println("|** 3.自定义题目类型 **|");
System.out.println("|** 4.结束定制 **|");
System.out.println("-----------------------------");
System.out.println("请输入你要定制的内容:");
ctmType = sc.nextInt();
//非法输入检测并提醒改正
while(ctmType!=1 && ctmType!=2 && ctmType!=3 && ctmType!=4)
{
System.out.println("警告!非法输入!请重新输入您要定制的内容:");
ctmType = sc.nextInt();
}
switch(ctmType)
{
case 1:
//输入下界值并检验合法性
System.out.println("请输入您要限制的下界数值:");
try{
Lb = sc.nextInt();
}
catch(InputMismatchException e)
{
System.out.println("警告!您的输入不合法!程序退出运行!");
System.exit(0);
}
//输入上界值并检验合法性
System.out.println("请输入您要限制的上界数值:");
try{
Ub = sc.nextInt();
}
catch(InputMismatchException e)
{
System.out.println("警告!您的输入不合法!程序退出运行!");
System.exit(0);
}
break;
case 2:
//输入Cno并检验合理性
System.out.println("请输入您要生成运算题目的数值:");
Cno = sc.nextInt();
while(Cno<=0)
{
System.out.println("输入值越界,请重新输入您要生成的运算题数目的值:");
Cno = sc.nextInt();
}
break;
case 3:
//定制题目类型
System.out.println("--------------------------------");
System.out.println("|**欢迎使用四则运算题目类型定制**|");
System.out.println("|** 1.自定义二元或单元运算 **|");
System.out.println("|** 2.自定义加减有无负数 **|");
System.out.println("|** 3.自定义有无乘除法 **|");
System.out.println("|** 4.自定义有无分数 **|");
System.out.println("|** 5.自定义有无括号 **|");
System.out.println("|** 6.结束定制 **|");
System.out.println("--------------------------------");
System.out.println("请输入您要定制的题目类型:");
testType = sc.nextInt();
//非法输入检测并提醒改正
while(testType!=1 && testType!=2 && testType!=3 && testType!=4 && testType!=5 && testType!=6)
{
System.out.println("警告!非法输入!请重新输入您要定制的题目类型:");
testType = sc.nextInt();
}
switch(testType)
{
case 1:
System.out.println("请定义二元或多元运算:0.二元 1.多元");
Ismul = sc.nextInt();
while(Ismul!=0 && Ismul!=1)
{
System.out.println("警告!非法输入!请重新定义二元或多元运算:0.二元 1.多元");
Ismul = sc.nextInt();
}
break;
case 2:
System.out.println("请定义加减有无负数:0.无负数 1.有负数");
Isneg = sc.nextInt();
//非法输入检测并提醒改正
while(Isneg!=0 && Isneg!=1)
{
System.out.println("警告!非法输入!请重新定义加减有无负数:0.无负数 1.有负数");
Isneg = sc.nextInt();
}
break;
case 3:
System.out.println("请定义有无乘除法及除法有无余数: 0.无乘除法 1.有乘除法");
Israd = sc.nextInt();
//非法输入检测并提醒改正
while(Israd!=0 && Israd!=1)
{
System.out.println("警告!非法输入!请重新定义有无乘除法:0.无乘除法 1.有乘除法");
Israd = sc.nextInt();
}
if(Israd==1)
{
System.out.println("请定义除法有无余数:0.无余数 1.有余数");
Isrem = sc.nextInt();
//非法输入检测并提醒改正
while(Isrem!=0 && Isrem!=1)
{
System.out.println("警告!非法输入!请重新定义除法有无余数:0.无余数 1.有余数");
Isrem = sc.nextInt();
}
}
break;
//NOTICE
case 4:
System.out.println("请定义有无分数:0.有分数 1.无分数");
cIsfra = sc.nextInt();
//非法输入检测并提醒改正
while(cIsfra!=0 && cIsfra!=1)
{
System.out.println("警告!非法输入!请重新定义有无分数:0.无分数 1.有分数");
Isrem = sc.nextInt();
}
if(cIsfra==1)
{
System.out.println("请定义");
}
break;
case 5:
System.out.println("请定义有无括号:0.无括号 1.有括号");
Isbra = sc.nextInt();
//非法输入检测并提醒改正
while(Isbra!=0 && Isbra!=1)
{
System.out.println("警告!非法输入!请重新定义有无括号:0.无括号 1.有括号");
Isbra = sc.nextInt();
}
break;
case 6:
break;
}
break;
case 4:
break;
}
}
}
if(Ismul==0)
{
int a[] = new int[Cno];
String b[] = new String[Cno];
int c[] = new int[Cno];
//生成两个数之间的运算题
for(int i=0;i<Cno;i++)
{
int n1 = (int)(Math.random() * (Ub-Lb)) + Lb;
int n2 = (int)(Math.random() * (Ub-Lb)) + Lb;
//若无负数,判断生成数是否为负,若为负,乘-1
if(Isneg==0)
{
if(n1<0)
{
n1*=-1;
}
if(n2<0)
{
n2*=-1;
}
}
//随机生成运算符
int Shows = (int)(Math.random() * 4);
String sign="";
switch(Shows)
{
case 0:
sign="+";
break;
case 1:
sign="-";
break;
case 2:
sign="*";
break;
case 3:
sign="/";
break;
}
a[0] = n1;
c[0] = n2;
b[0] = "";
for(int j=0;j<i;j++)
{
if(n1==a[j] && n2==c[j] && sign==b[j])
{
n1 = (int)(Math.random() * (Ub-Lb)) + Lb;
n2 = (int)(Math.random() * (Ub-Lb)) + Lb;
}
}
if(Israd==0)
{
if(sign=="*")
{
sign="+";
}
if(sign=="/")
{
sign="-";
}
}
Isfra = (int)(Math.random() * 2);
if(Isfra==1)
{
int n3 = (int)(Math.random() * (Ub-Lb)) + Lb;
int n4 = (int)(Math.random() * (Ub-Lb)) + Lb;
//分数避免出现0
if(n1==0)
{
n1 += (int)(Math.random() * (Ub-Lb)) + Lb;
}
if(n2==0)
{
n2 += (int)(Math.random() * (Ub-Lb)) + Lb;
}
//若无负数,使分母均取正值
if(Isneg==0)
{
if(n3<0)
{
n3*=-1;
}
if(n4<0)
{
n4*=-1;
}
}
//生成真分数并约分
if(IsPfra==1)
{
if(n3<n1)
{
temp = n3;
n3 = n1;
n1 = temp;
}
for(int j=n1;j>0;j--)
{
if(n1%j==0 && n3%j==0)
{
n1/=j;
n3/=j;
break;
}
}
if(n4<n2)
{
temp = n4;
n4 = n2;
n2 = temp;
}
for(int j=n2;j>0;j--)
{
if(n2%j==0 && n4%j==0)
{
n2/=j;
n4/=j;
break;
}
}
}
//生成假分数并约分
else
{
if(n3>n1)
{
temp = n3;
n3 = n1;
n1 = temp;
}
for(int j=n3;j>0;j--)
{
if(n1%j==0 && n3%j==0)
{
n1/=j;
n3/=j;
break;
}
}
if(n4>n2)
{
temp = n4;
n4 = n2;
n2 = temp;
}
for(int j=n4;j>0;j--)
{
if(n2%j==0 && n4%j==0)
{
n2/=j;
n4/=j;
break;
}
}
}
if(n1%n3 == 0 || n2%n4==0 || n3%n1 == 0 || n4%n2==0)
{
i-=1;
continue;
}
System.out.println("(" + n1 + "/" + n3 + ")" + sign + "(" + n2 + "/" + n4 + ")" + "=");
int ans1,ans2;
String ds;
int flag = 1;
//加法输入运算结果并判断
if(sign=="+")
{
int n = n1 * n4 + n2 * n3;
int m = n3*n4;
if(n<0)
{
n*=-1;
flag *= -1;
}
if(m<0)
{
m*=-1;
flag *= -1;
}
int j = m;
if(m>n)
{
j=n;
}
for(j=m;j>0;j--)
{
if(n%j==0 && m%j==0)
{
n/=j;
m/=j;
break;
}
}
if(n%m==0)
{
System.out.println("请输入运算结果:");
ans = sc.nextInt();
if(flag==-1)
{
if(ans==-(n/m))
{
t++;
}
}
else
{
if(ans==(n/m))
{
t++;
}
}
}
else
{
System.out.println("请(以分数形式)输入运算结果:");
ans1 = sc.nextInt();
ds = sc.next();
ans2 = sc.nextInt();
if(flag==-1)
{
if(ds.equals("//") && ((ans1==-n && ans2==m) || (ans1==n && ans2==-m)))
{
t++;
}
}
else
{
if(ans1==n && ans2==m && ds.equals("/"))
{
t++;
}
}
}
}
//加法输入运算结果并判断
//减法运算结果并判断
else if(sign=="-")
{
int n = n1 * n4 - n2 * n3;
int m = n3 * n4;
if(n<0)
{
n*=-1;
flag *= -1;
}
if(m<0)
{
m*=-1;
flag *= -1;
}
int j = m;
if(m>n)
{
j=n;
}
for(j=m;j>0;j--)
{
if(n%j==0 && m%j==0)
{
n/=j;
m/=j;
break;
}
}
if(n%m==0)
{
System.out.println("请输入运算结果:");
ans = sc.nextInt();
if(flag==-1)
{
if(ans==-(n/m))
{
t++;
}
}
else
{
if(ans==(n/m))
{
t++;
}
}
}
else
{
System.out.println("请(以分数形式)输入运算结果:");
ans1 = sc.nextInt();
ds = sc.next();
ans2 = sc.nextInt();
if(flag==-1)
{
if((ans1==-n && ans2==m) || (ans1==n && ans2==-m) && ds.equals("//"))
{
t++;
}
}
else
{
if(ans1==n && ans2==m && ds.equals("//"))
{
t++;
}
}
}
}
//减法运算结果并判断
//乘法运算结果并判断
else if(sign=="*")
{
int n = n1 * n2;
int m = n3 * n4;
if(n<0)
{
n*=-1;
flag *= -1;
}
if(m<0)
{
m*=-1;
flag *= -1;
}
int j = m;
if(m>n)
{
j=n;
}
for(j=m;j>0;j--)
{
if(n%j==0 && m%j==0)
{
n/=j;
m/=j;
break;
}
}
if(n%m==0)
{
System.out.println("请输入运算结果:");
ans = sc.nextInt();
if(flag==-1)
{
if(ans==-(n/m))
{
t++;
}
}
else
{
if(ans==(n/m))
{
t++;
}
}
}
else
{
System.out.println("请(以分数形式)输入运算结果:");
ans1 = sc.nextInt();
ds = sc.next();
ans2 = sc.nextInt();
if(flag==-1)
{
if((ans1==-n && ans2==m) || (ans1==n && ans2==-m) && ds.equals("//"))
{
t++;
}
}
else
{
if(ans1==n && ans2==m && ds.equals("/"))
{
t++;
}
}
}
}
//乘法运算结果并判断
//除法运算结果并判断
else if(sign=="/")
{
int n = n1 * n4;
int m = n2 * n3;
if(n<0)
{
n*=-1;
flag *= -1;
}
if(m<0)
{
m*=-1;
flag *= -1;
}
int j = m;
if(m>n)
{
j=n;
}
for(j=m;j>0;j--)
{
if(n%j==0 && m%j==0)
{
n/=j;
m/=j;
break;
}
}
if(n%m==0)
{
System.out.println("请输入运算结果:");
ans = sc.nextInt();
if(flag==-1)
{
if(ans==-(n/m))
{
t++;
}
}
else
{
if(ans==(n/m))
{
t++;
}
}
}
else
{
System.out.println("请(以分数形式)输入运算结果:");
ans1 = sc.nextInt();
ds = sc.next();
ans2 = sc.nextInt();
if(flag==-1)
{
if((ans1==-n && ans2==m) || (ans1==n && ans2==-m) && ds.equals("/"))
{
t++;
}
}
else
{
if(ans1==n && ans2==m && ds.equals("/"))
{
t++;
}
}
}
}
//除法运算结果并判断
}
//整数四则运算
else
{
if(sign=="/")
{
if(Isrem==0)
{
if(n2>n1)
{
temp = n2;
n2 = n1;
n1 = temp;
}
if(n2==0)
{
n2 = (int)(Math.random() * (Ub-Lb)) + Lb;
}
if(n1%n2==0)
{
System.out.println(n1 + sign + n2 + "=");
a[i] = n1;
c[i] = n2;
b[i] = sign;
System.out.println("请输入运算结果:");
ans = sc.nextInt();
if(ans==n1/n2)
{
t++;
}
}
else
{
System.out.println((n1-n1%n2) + sign + n2 + "=");
a[i] = n1-n1%n2;
c[i] = n2;
b[i] = sign;
System.out.println("请输入运算结果:");
ans = sc.nextInt();
if(ans==n1/n2)
{
t++;
}
}
}
}
else
{
System.out.println(n1 + sign + n2 + "=");
a[i] = n1;
c[i] = n2;
b[i] = sign;
System.out.println("请输入运算结果:");
ans = sc.nextInt();
if(sign=="+")
{
if(ans==n1+n2)
{
t++;
}
}
else if(sign=="-")
{
if(ans==n1-n2)
{
t++;
}
}
else
{
if(ans==n1*n2)
{
t++;
}
}
}
}
}
if(t==Cno)
{
System.out.println("你真是个天才!全对!");
}
else
{
System.out.println("共做对" + t + "道题,做错了" + (Cno-t) + "道题。");
}
}
else
{
for(int i=0;i<Cno;i++)
{
int Mno = (int)(Math.random() * 7) + 3;
int n[] = new int[Mno];
for(int j=0;j<Mno;j++)
{
n[j] = (int)(Math.random() * (Ub-Lb)) + Lb;
}
String s[] = new String[Mno];
s[Mno-1] = "=";
String sign="";
for(int j=0;j<Mno-1;j++)
{
//随机生成运算符
int Shows = (int)(Math.random() * 4);
switch(Shows)
{
case 0:
sign="+";
break;
case 1:
sign="-";
break;
case 2:
sign="*";
break;
case 3:
sign="/";
break;
}
s[j] = sign;
}
for(int j=0;j<Mno;j++)
{
System.out.print(n[j] + s[j]);
}
System.out.println();
int rec = Mno;
for(int j=0;j<Mno;j++)
{
if(s[j].equals("*") || s[j].equals("/"))
{
if(s[j].equals("*"))
{
n[j] = n[j] * n[j+1];
rec--;
}
else if(s[j].equals("/"))
{
n[j] = n[j] / n[j+1];
rec--;
}
for(int k=j+1;k<rec;k++)
{
n[k] = n[k+1];
if(k==(rec-1))
{
break;
}
}
for(int k=j;k<rec;k++)
{
s[k] = s[k+1];
if(k==(rec-1))
{
break;
}
}
j--;
}
}
int v = rec;
for(int j=0;j<v;j++)
{
if(s[j].equals("+"))
{
n[j] = n[j] + n[j+1];
}
else if(s[j].equals("-"))
{
n[j] = n[j] - n[j+1];
}
v--;
for(int k=j+1;k<v;k++)
{
n[k] = n[k+1];
}
for(int k=j;k<v;k++)
{
s[k] = s[k+1];
}
j--;
}
System.out.println("请输入答案:");
ans = sc.nextInt();
if(ans==n[0])
{
t++;
}
}
//循环
if(t==Cno)
{
System.out.println("你真是个天才!全对!");
}
else
{
System.out.println("共做对" + t + "道题,做错了" + (Cno-t) + "道题。");
}
}
//生成多项四则运算
}
}
【程序结果截图】




【项目计划总结表】
|
任 务 (日 期) |
听课 | 编写程序 | 阅读课本 | 准备考试 | 日总计 |
| 周日 | |||||
| 周一 | 100min | 100min | |||
| 周二 | 90min | 90min | |||
| 周三 | |||||
| 周四 | 30min | 30min | |||
| 周五 | 190min | 190min | |||
| 周六 | 360min | 360min | |||
| 周总计 | 100min | 450min | 120min | 670min |
【时间记录日志】
| 日期 | 开始时间 | 结束时间 | 中断时间 | 净时间 | 活动 | 备注 |
| 2016/3/21 | 8:00 | 8:50 | 50min | 上课 | ||
| 9:00 | 9:50 | 50min | 上课 | |||
| 2016/3/22 | 15:00 | 16:10 | 10min | 60min | 看书 | |
| 19:00 | 19:30 | 30min | 看书 | |||
| 2016/3/24 | 19:00 | 19:30 | 30min | 看书 | ||
| 2016/3/25 | 18:30 | 21:50 | 190min | 交流设计思路,编码 | ||
| 2016/3/26 | 9:00 | 11:30 | 30min | 120min | 编程序 | 交流设计思路 |
| 15:00 | 19:20 | 30min | 240min | 编程序 |
原文:http://www.cnblogs.com/Againzg/p/5324966.html