首页 > 编程语言 >Java 四则运算 继承版

Java 四则运算 继承版

时间:2022-10-03 23:33:09浏览次数:45  
标签:Java get 继承 四则运算 System int static public out

package T1;

import java.util.Scanner;

public class A_Text {

static int title_n=30;
static Scanner cin = new Scanner(System.in);

public static void main(String[] args) {
while(true) {
int choose=Menu();
switch (choose) {
case 1:getRoom();break;
case 2:Second_grade.text();break;
case 3:Third_grade.text();break;
case 4:Fourth_grade.text();break;
case 5:case5();break;
case 6:System.out.println("已成功退出");return;
}
}
}

public static void case5(){
System.out.println("错题:");
for(int i=0;i<Second_grade.wrong;i++) System.out.println(Second_grade.arr[i]+"=");
}

public static int Menu(){
System.out.println("四则运算主界面菜单");
System.out.println("1、参数设置");
System.out.println("2、小学二年级口算题");
System.out.println("3、小学三年级口算题");
System.out.println("4、小学四年级口算题");
System.out.println("5、错题集");
return cin.nextInt();
}

public static void getRoom() {
System.out.print("题目数量:");
title_n = cin.nextInt();
System.out.print("操作数数值范围:");
Second_grade.max = cin.nextInt();
}
}








package T1;

import java.util.Random;
import java.util.Scanner;

public class Second_grade{

static Scanner cin = new Scanner(System.in);
static Random r = new Random();
public static int operand=2,max=0;
public static int parentheses=0;
public static int left,right,between;
public static int wrong=0;
public static String []arr = new String[100000];
public static String []arr2 = new String[100000];

public static void text(){
get_max();
get_title();
int choose=Menu();
if(choose==1)get_title();
else if(choose==2) get_arr();
}

public static void get_arr(){
int number = 0;
for(int i=0;i<wrong;i++) {
int answer = Calculation_Results.compute(arr[i]);
System.out.println(arr[i] + "=");
int get_answer = cin.nextInt();
if (answer != get_answer) {
arr2[number] = arr[i];
number++;
}
}
System.out.println("错题:");
for(int j=0;j<number;j++) System.out.println(arr2[j]+"=");
System.out.println("错了"+number+"道题目");
double value =((double)wrong-(double)number)/(double)wrong*100;
String str = String.format("%.2f",value);
System.out.println("正确率"+ str +"%");
}

public static void get_title() {
wrong = 0;arr = new String[100000];
for(int i=0;i<A_Text.title_n;i++) {
int first = r.nextInt(max);
int then = r.nextInt(max);if(then==0)then++;
char operator = get_operator();
String title = "" + first + operator + then;
int answer = Calculation_Results.compute(title);
System.out.println(title + "=");
int get_answer = cin.nextInt();
if (answer != get_answer) {
arr[wrong] = title;
wrong++;
}
}get_wrong();
}

public static void get_wrong(){
System.out.println("错题:");
for(int i=0;i<wrong;i++) System.out.println(arr[i]+"=");
System.out.println("错了"+wrong+"道题目");
double value =((double)A_Text.title_n-(double)wrong)/(double)A_Text.title_n*100;
String str = String.format("%.2f",value);
System.out.println("正确率"+ str +"%");
}

public static int Menu(){
System.out.println("1.进行下一套");
System.out.println("2.错题本");
System.out.println("3.退出");
return cin.nextInt();
}

public static void get_max(){
System.out.print("操作数数值范围: ");
max= cin.nextInt();
while(max>100){
System.out.println("请输入100以下的数");
max= cin.nextInt();
}
}

public static char get_operator(){
int choose=r.nextInt(4);
if(choose==0)return '+';
else if(choose==1)return '-';
else if(choose==2)return '*';
else return '/';
}
}

















package T1;

public class Third_grade extends Second_grade{

public static void text(){
get_max();
get_title();
int choose=Menu();
if(choose==1)get_title();
else if(choose==2) get_arr();
}

public static void get_title(){
wrong = 0;arr = new String[100000];
for(int i=0;i<A_Text.title_n;i++) {
left=0;right=0;
int number=operand;
int first = r.nextInt(max);
StringBuilder title = new StringBuilder("" + get_left(number) + first + get_right(number));
for(int j=2;j<=operand;j++) {
between++;
number--;
int then = r.nextInt(max);
char operator = get_operator();
title.append(operator).append(get_left(number)).append(then).append(get_right(number));
}
int answer = Calculation_Results.compute(String.valueOf(title));
System.out.println(title + "=");
int get_answer = cin.nextInt();
if (answer != get_answer) {
arr[wrong] = String.valueOf(title);
wrong++;
}
}get_wrong();
}

public static String get_left(int number){
if(parentheses==0)return "";
int choose=r.nextInt(2);
if(left==right)between=0;
if(left==right&&number>1) {
if (choose == 0) {left++;return "(";}
}
return "";
}

public static String get_right(int number){
if(parentheses==0)return "";
int choose=r.nextInt(2);
if(left==right)between=0;
if(left!=right&&number==1){right++;return ")";}
if(left!=right&&between==(operand-2)){right++;return ")";}
if(left!=right&&between>=1){
if (choose == 0) {right++;return ")";}
}
return "";
}

public static void get_max(){
System.out.print("操作数数值范围: ");
max= cin.nextInt();
while(max>1000){
System.out.println("请输入1000以下的数");
max= cin.nextInt();
}
System.out.print("操作数: ");
operand= cin.nextInt();
while(operand>4){
System.out.println("请输入4以下的数");
operand= cin.nextInt();
}
}

}





package T1;

public class Fourth_grade extends Third_grade{

public static void text(){
get_parentheses();
get_title();
int choose=Menu();
if(choose==1)get_title();
else if(choose==2) get_arr();
}

private static void get_parentheses() {
System.out.print("操作数: ");
operand= cin.nextInt();
while(operand>5){
System.out.println("请输入5以下的数");
operand= cin.nextInt();
}
System.out.print("是(1)否(0)添加括号:");
parentheses = cin.nextInt();
}
}








package T1;

public class Calculation_Results {

public static void main(String[] args) {
System.out.println(compute("1+(10+2*20)*3"));
}

public static int compute(String exp){
ArrayStack2 numStack = new ArrayStack2(10);
ArrayStack2 operStack = new ArrayStack2(10);
int index = 0;//用于扫描
int num1;
int num2;
int oper;
int res;
char ch; //扫描的结果
String keepNum = ""; //用于拼接多位数
while (true){
ch = exp.charAt(index);
if (ch==')'){
while (true){
if (operStack.peek()=='('){
operStack.pop();
break;
}
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1, num2, oper);
numStack.push(res);
}
}
if (operStack.isOper(ch)) { //如果是运算符
//判断当前的符号栈是否为空
if (!operStack.isEmpty()){
//如果符号栈有操作符,就进行比较,如果当前的操作符的优先级小于或等于栈中的优先级,就从pop出两个数
//在从符号栈中pop出一个符号,进行运算,将得到结果,入数栈,然后将当前的操作符入符号栈
if (operStack.priority(ch)<=operStack.priority(operStack.peek())&&ch!='('){
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1, num2, oper);
//运算结果入数栈
numStack.push(res);
operStack.push(ch);
}else {
//如果当前操作符的优先级大于栈中的操作符,直接入符号栈
operStack.push(ch);
}
}else {
//如果为空直接入栈。。
operStack.push(ch);
}
}else if (ch!=')'){//如果是数字
// numStack.push(ch-48);
//1、当处理多位数时,不能发现是一个数就立即入栈,因为它可能是多位数
//2、当处理函数,需要向exp表达式的index后再看一位,如果是数就进行扫描,如果是符号才入栈

//处理多位数
keepNum += ch;
//如果ch是最后一位直接入栈
if (index == exp.length()-1){
numStack.push(Integer.parseInt(keepNum));
} else if (operStack.isOper(exp.charAt(index+1))||exp.charAt(index+1)==')'){
//如果后一位是运算符则入栈
numStack.push(Integer.parseInt(keepNum));
//keepNum清空
keepNum="";
}

}
//让index+1,判断是否扫描到最后
index++;
if (index>=exp.length()) break;
}

//当表达式扫描结束,就顺序的从 数栈和符号栈中pop出相应的数和符号,并运算
while (true){
//如果符号栈为空,则计算到最后的结果,数栈中只有一个数字【结果】
if (operStack.isEmpty()){
break;
}
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1, num2, oper);
numStack.push(res);
}
return numStack.pop();
}
}
/**
* 表示栈结构
*/
class ArrayStack2{
private int maxSize; //栈大小
private int[] stack; //数组,数组模拟栈,数据就放在该数组
private int top = -1;

public ArrayStack2(int maxSize) {
this.maxSize = maxSize;
stack = new int[maxSize];
}
//栈满
public boolean isFull(){
return top==maxSize-1;
}
//栈空
public boolean isEmpty(){
return top==-1;
}
//入栈
public void push(int value){
if (isFull()){
System.out.println("栈满 扩容*1.5");
int[] stack1 =new int[(maxSize+maxSize/2)];
System.arraycopy(stack1,0,stack,0,maxSize);
maxSize=maxSize+maxSize/2;
stack=stack1;
System.out.println(maxSize);
}
top++;
stack[top] = value;
}
//出栈-pop
public int pop(){
if (isEmpty()){
throw new RuntimeException("栈空");
}
return stack[top--];
}
//返回栈顶元素的值,不出栈
public int peek(){
return stack[top];
}
//返回的运算符用数字表示,数字越大优先级越高
public int priority(int oper){
if (oper =='*'||oper == '/'){
return 1;
}else if (oper=='+'||oper=='-'){
return 0;
}else{
return -1;
}
}
//判断是不是一个运算符
public boolean isOper(char val){
return val=='+'||val=='-'||val=='*'||val=='/'||val=='(';
}
//计算方法
public int cal(int num1,int num2,int oper){
int res = 0; //用于存计算结果
switch (oper){
case '+':
res =num1+num2;
break;
case '-':
res =num2-num1;
break;
case '*':
res =num1*num2;
break;
case '/':
res =num2/num1;
break;
default:
break;
}
return res;
}
}

标签:Java,get,继承,四则运算,System,int,static,public,out
From: https://www.cnblogs.com/yetang307/p/16751580.html

相关文章