java web class_Java Web ClassProgram_3

针对四则运算,进行了进一步的修改,基于类的思想,将项目拆解成随机数字生成和随机操作符生成和运算两大类

随机数字采用 分子/分母 的表达方式,可以表示所有的数字类型。如分数,整数,小数

由于运算采用了新的数据类型,导致需要编写一定的计算原则;

代码如下:

centrue.class

package pro2;

import java.awt.BorderLayout;

import javax.swing.JFrame;

public class centrue extends JFrame{

NorthPanel north;

public centrue(){

north=new NorthPanel();

this.setSize(800, 600);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.add(north, BorderLayout.NORTH);

this.setVisible(true);

}

public static void main(String[] args) {

centrue c=new centrue();

}

}

Count.java

package pro2;

import java.util.LinkedList;

import java.util.Random;

import java.util.Stack;

public class Count {

int min;

int max;

int list[];

int bound; // 表示list中有几个type数字

boolean isbracket; // 是否有括号

int number; //数字范围2-5

LinkedList

figure;

char[] operatelist;

String shizi;

String daan;

public Count(int min, int max, int list[], int bound, int number,boolean isbracket) {

this.max = max;

this.min = min;

this.list = list;

this.bound = bound;

this.number = number;

this.isbracket = isbracket;

figure=new LinkedList

();

this.getCompound();

this.getString();

System.out.println(this.shizi);

this.calculation();

}

// 获得计算堆栈,包括括号

public void getCompound() {

int braIndex[] ={0,0,0,0};

int braNum=0;

Random ran = new Random();

if(!isbracket){

operatelist=new char[number+1];

}

if (number < 6 && number >= 3 && isbracket) {

braNum = 2;

operatelist=new char[number+braNum+1];

braIndex[0] = ran.nextInt(number - 1) + 1;

operatelist[braIndex[0]]='(';

braIndex[1] = ran.nextInt(number-braIndex[0]) + braIndex[0]+2;

if(braIndex[0]==1&&braIndex[1]==braNum+number-1){

braIndex[1]--;

}

operatelist[braIndex[1]]=')';

}

//数字插入限制受限

// 六个数字的括号插入等待实现

else if (number >= 6 && number >= 3 && isbracket) {

}

// 数字进队列

for (int i = 0; i < number; i++) {

int type = list[ran.nextInt(bound)];

figure.add(new Figure(min, max, type));

}

// 操作符生成

for (int i = 1; i < number+braNum; i++) {

int type = ran.nextInt(4);

char a='+';

switch (type) {

case 0:

a = '+';

break;

case 1:

a = '-';

break;

case 2:

a = '*';

break;

case 3:

a = '/';

break;

}

boolean flag=true;

for(int j=0;j

if(braIndex[j]==i){

flag=false;

}

}

if(flag){

operatelist[i]=a;

}

}

//添加前后两个神奇字符

operatelist[0]='#';

operatelist[number+braNum]='#';

}

public void show(){

System.out.println("操作符数组");

for(int i=0;i

System.out.println(operatelist[i]);

}

for(int i=0;i

System.out.println(figure.get(i));

}

}

public void getString(){

String shi="";

int i=0;

int j=0;

while(true){

shi=shi+operatelist[j];

if(operatelist[j+1]=='('){

j++;

shi=shi+operatelist[j];

}

j++;

shi=shi+figure.get(i);

if(operatelist[j]==')'){

shi=shi+operatelist[j];

j++;

}

i++;

if(j==operatelist.length-1){

break;

}

}

shizi=shi;

}

public String toString(){

return shizi;

}

public void calculation(){

Stack

fiStack=new Stack
(); //figure 数据栈

Stackchar chStack=new Stackchar(); //char 数据栈

int charIndex=0;

int FigureIndex=0;

//#字符压入栈中

chStack.push(operatelist[charIndex++]);

//第一个字符压入

if(operatelist[charIndex]!='('){

chStack.push(operatelist[charIndex++]);

//防止 * (

if(operatelist[charIndex]=='('){

chStack.push(operatelist[charIndex++]);

}

}

else{

chStack.push(operatelist[charIndex++]);

chStack.push(operatelist[charIndex++]);

}

//两个数字压入栈中

fiStack.add(figure.get(FigureIndex++));

fiStack.add(figure.get(FigureIndex++));

while(true){

if(this.compare(chStack.getTop(),operatelist[charIndex])){

System.out.println("tan栈操作");

System.out.println("剩余数字数量"+fiStack.size());

System.out.println("剩余字符数量"+chStack.size());

System.out.println(FigureIndex);

char ope=chStack.pop();

if(ope=='('){

charIndex++;

}

else{

Figure f1=fiStack.pop();

Figure f2=fiStack.pop();

fiStack.push(SimpleCount.count(f1, f2, ope));

}

}

else{

chStack.push(operatelist[charIndex++]);

System.out.println("进栈操作");

System.out.println("剩余数字数量"+fiStack.size());

System.out.println("剩余字符数量"+chStack.size());

System.out.println(FigureIndex);

fiStack.push(figure.get(FigureIndex++));

if(operatelist[charIndex]=='('){

chStack.push(operatelist[charIndex++]);

}

}

if(fiStack.size()==1&&FigureIndex==figure.size()){

Figure ff;

ff=fiStack.pop();

ff.type=1;

ff.huajian();

daan=ff.toString();

break;

}

}

}

public boolean compare(char a,char b) {

int a1=0,b1=0;

if(a=='+'||a=='-'){

a1=2;

}

if(a=='*'||a=='/'){

a1=3;

}

if(a=='('){

a1=1;

}

if(a=='#'){

a1=0;

}

if(b==')'||b=='#'){

b1=1;

}

if(b=='+'||b=='-'){

b1=2;

}

if(b=='*'||b=='/'){

b1=3;

}

if(b=='('){

b1=4;

}

if(a1>=b1){

return true;

}

else{

return false;

}

}

}

Figure.java

/**

* min返回数字的最小值 max 最大值 type返回数字类型

* type=0 返回整数

*/

package pro2;

import java.util.Random;

public class Figure {

int zi;

int mu;

int min;

int max;

int type;

public Figure(int min,int max,int type){

this.max=max;

this.min=min;

this.type=type;

this.returnFigure();

}

public Figure(int zi,int mu){

this.zi=zi;

this.mu=mu;

}

public void returnFigure(){

Random ran=new Random();

switch (type) {

//生成整数

case 0:

zi=(int)((max-min)*Math.random())+min;

mu=1;

break;

//生成分数

case 1:

zi=ran.nextInt((int)((max-min)*Math.random())+min);

mu=ran.nextInt(zi*10+1)+1;

System.out.println("zi="+zi+"mu="+mu);

this.huajian();

break;

//生成double

case 2:

zi=(int)((((max-min)*Math.random())+min)*100);

mu=100;

break;

}

}

public void huajian(){

int chu=1;

for(int i=1;i<=Math.min(zi, mu);i++){

if(zi%i==0&&mu%i==0){

chu=i;

}

}

zi=zi/chu;

mu=mu/chu;

}

public String toString(){

switch (type) {

case 0:

return String.valueOf(zi);

case 1:

return zi+"%"+mu;

case 2:

double ok=((double)zi)/mu;

return ok+"";

default:

return null;

}

}

}

NorthPanel.java

package pro2;

import java.awt.BorderLayout;

import java.awt.Checkbox;

import java.awt.CheckboxGroup;

import java.awt.Component;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

import javax.swing.JTextArea;

import javax.swing.plaf.basic.BasicBorders.RadioButtonBorder;

public class NorthPanel extends JPanel implements ActionListener{

//题目数据

int min=0;

int max=0;

int list[]=new int[4];

int bound=0; // 表示list中有几个type数字

boolean isbracket=false; // 是否有括号

int number=4; //数字范围2-5

JPanel Northjp;

SouthPanel south;

JCheckBox r1=new JCheckBox("整数", true);

JCheckBox r2=new JCheckBox("分数", false);

JCheckBox r3=new JCheckBox("小数", false);

JRadioButton jr=new JRadioButton("有括号",false);

JLabel jl=new JLabel("题目数字个数");

JTextArea num=new JTextArea("4");

JLabel jll=new JLabel("最大最小值");

JTextArea minText=new JTextArea("10");

JTextArea maxText=new JTextArea("100");

JButton jb=new JButton("出题目");

public NorthPanel(){

this.setLayout(new BorderLayout());

Northjp=new JPanel();

Northjp.setLayout(new GridLayout(2,6));

Northjp.add(r1);

Northjp.add(r2);

Northjp.add(r3);

Northjp.add(jr);

Northjp.add(jl);

Northjp.add(num);

Northjp.add(jll);

Northjp.add(minText);

Northjp.add(maxText);

Northjp.add(jb);

this.add(Northjp, BorderLayout.NORTH);

jb.addActionListener(this);

}

public void chuti(){

if(south!=null){

south.setVisible(false);

}

south=new SouthPanel(min, max, list, bound, number, isbracket);

this.add(south, BorderLayout.SOUTH);

this.setVisible(false);

this.setVisible(true);

}

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

min=Integer.parseInt(minText.getText());

max=Integer.parseInt(maxText.getText());

number=Integer.parseInt(num.getText());

bound=0;

if(jr.isSelected()){

isbracket=true;

}

if(r1.isSelected()){

list[bound++]=0;

}

if(r2.isSelected()){

list[bound++]=1;

}

if(r3.isSelected()){

list[bound++]=2;

}

String message = "";

boolean flag=true;

if(min>max){

message=message+";应该左小又大";

flag=false;

}

if(number<2||number>5){

message=message+";数字数量应该小于6大于2";

flag=false;

}

if(bound<1){

message=message+";至少选中一个数据,如整数";

flag=false;

}

if(flag){

this.chuti();

}

else{

JOptionPane.showMessageDialog(null, message);

}

}

}

SimpleCount.java

package pro2;

public class SimpleCount {

public static Figure count(Figure f1, Figure f2, char a) {

Figure value;

int zi=1;

int mu=1;

switch (a) {

case '+':

zi=f1.zi*f2.mu+f2.zi*f1.mu;

mu=f1.mu*f2.mu;

break;

case '-':

zi=f2.zi*f1.mu-f1.zi*f2.mu;

mu=f1.mu*f2.mu;

break;

case '*':

zi=f1.zi*f2.zi;

mu=f1.mu*f2.mu;

break;

case '/':

zi=f2.zi*f1.mu;

mu=f2.mu*f1.zi;

break;

}

value=new Figure(zi, mu);

return value;

}

}

SouthPanel.java

package pro2;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextArea;

public class SouthPanel extends JPanel implements ActionListener{

int min;

int max;

int list[];

int bound; // 表示list中有几个type数字

boolean isbracket; // 是否有括号

int number; //数字范围2-5

String count[]=new String[30];

String result[]=new String[30];

JLabel jlCount[]=new JLabel[30];

JLabel jResult[]=new JLabel[30];

JTextArea jAnswer[]=new JTextArea[30];

JButton jb=new JButton("提交答案");

public SouthPanel(int min, int max, int list[], int bound, int number,boolean isbracket){

this.max = max;

this.min = min;

this.list = list;

this.bound = bound;

this.number = number;

this.isbracket = isbracket;

this.setLayout(new GridLayout(16,6));

this.addCount();

jb.addActionListener(this);

this.setVisible(false);

this.setVisible(true);

}

public void addCount(){

for(int i=0;i<30;i++){

Count c=new Count(min, max, list, bound, number, isbracket);

count[i]=c.toString();

result[i]=c.daan;

jlCount[i]=new JLabel(count[i]);

this.add(jlCount[i]);

jAnswer[i]=new JTextArea();

this.add(jAnswer[i]);

jResult[i]=new JLabel("");

this.add(jResult[i]);

}

this.add(jb);

}

public void result(){

for(int i=0;i<30;i++){

if(!result[i].equals(jAnswer[i].getText())){

jResult[i].setText(result[i]);

}

}

}

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

this.result();

}

}

Stackchar.java

package pro2;

public class Stackchar {

char a[];

int index;

public Stackchar(){

a=new char[10];

index=0;

}

public void push(char b){

a[index]=b;

index++;

}

public char pop(){

char k=a[index-1];

index--;

return k;

}

public char getTop(){

return a[index-1];

}

public int size(){

return index;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值