判短算式表达式正错

package compiling;
import java.util.Random;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.lang.StringBuilder;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Stack;

/**
 *
 * @author weifeng
 */
public class Compiling {
    
    //private static LinkedList operators=new LinkedList<>(); //用于记录操作符
    //private static LinkedList output=new LinkedList<>();//用来记录输出
    //private static StringBuilder sb=new StringBuilder();//用来记录后缀表达式

    /**
     * @param args the command line arguments
     */
    static String path1 = "C:\\Users\\weifeng\\Desktop\\结果.txt";
    static String path = "C:\\Users\\weifeng\\Desktop\\算术表达式.txt";
    static String path2 = "C:\\Users\\weifeng\\Desktop\\错误.txt";
    static String path3 = "C:\\Users\\weifeng\\Desktop\\检查结果.txt";
    static int n=0;
    
    public static void main(String[] args) throws IOException {
        while(true){
            System.out.println("                          ");
            System.out.println("请做出以下选择:");
            System.out.println("1,生成40道正确的算术表达式");
            System.out.println("2,生成40道错误的算术表达式");
            System.out.println("3,将算术表达式中缀转后缀");
            System.out.println("4,判断错误的正确表达式");
            System.out.println("0,退出");
            System.out.println("                          ");
            int x;
            System.out.print("你的选择是:");
            Scanner input = new Scanner(System.in);
            x = input.nextInt();
            switch(x){
                case 1:
                {
                    FileWriter fc =new FileWriter(path);
                    //FileWriter fcc =FileWriter(fc);
                    //BufferedWriter fccc =BufferedWriter(fcc);
                    int i =0;
                    while(i<=40){
                        i++;
                        String CC ="";
                        String DD =getnumberset(CC);
                        System.out.println(DD);
                        fc.write(DD+"\r\n");
                        //fccc.flush();
                    }
                    fc.close();
                    break;
                }
                case 2:
                    {
                    FileWriter fe =new FileWriter(path2);
                    int i =0;
                    while(i<=40){
                        i++;
                        String MM ="";
                        String NN =wrong(MM);
                        System.out.println(NN);
                        fe.write(NN+"\r\n");
                        //fccc.flush();
                    }
                    fe.close();
                    break;
                }
                    
                case 3:
                    changefirst();
                    break;
                case 4:
                    analysefirst();
                    break;
                case 0:
                    return;
                default:
                    break;
            }
        
        }
       
        //analysefirst();
                
    }

    public static String getnumberset(String AA){     //随机生成算术表达式
        
        String numberset="";
        String BB="";
        String[] A ={"+","-","*","/"};
        //生成最简单的运算式子
        int max=1000;
        int min=0;
        Random random =new Random();
        //随机生成1000以内的数字
        n++;
        if(n<6){
        boolean flag =random.nextBoolean();
        int number =random.nextInt(max)%(max-min+1)+min;
        //int number2 =random.nextInt(max)%(max-min+1)+min;
        int rand =(int)(Math.random()*A.length);
        if (flag==true){
            numberset ="("+String.valueOf(number)+String.valueOf(A[rand])+getnumberset(BB)+")";
            return numberset;
        }
        else{
            numberset =String.valueOf(number)+String.valueOf(A[rand])+getnumberset(BB);
            return numberset;   
        }}
        else{
            n=0;
            int rands =(int)(Math.random()*A.length);
            int number1 =random.nextInt(max)%(max-min+1)+min;
            int number2 =random.nextInt(max)%(max-min+1)+min;
            boolean flag1 =random.nextBoolean();
            if(flag1==true){
            numberset =String.valueOf(number1)+String.valueOf(A[rands])+String.valueOf(number2);
            return numberset;}
            else{
                numberset ="("+String.valueOf(number1)+String.valueOf(A[rands])+String.valueOf(number2)+")";
                return numberset;              
            }
                    }
        //number1=String.valueOf(rands);                             
    }
       
    public static void changefirst() throws FileNotFoundException, IOException{  //从文件中读取并将中缀转后缀写入新的文件
        FileWriter fw = new FileWriter(path1);
        File file = new File(path);
        FileReader reader = new FileReader(file);
        BufferedReader breader = new BufferedReader(reader);
        String C = "";
        while((C = breader.readLine())!=null){
            System.out.println(C);    
            System.out.print("中缀转后缀为:");
            System.out.println(changesecond(C));
            fw.write("中缀:"+C+"\r\n");
            fw.write("后缀:"+changesecond(C)+"\r\n");
        }
        breader.close();
        fw.close();
    }

  
    
    public static String wrong(String EE){//随机生成错误的正确表达式
        String numberset="";
        String FF="";
        String[] A ={"+","-","*","/","(",")","++","+-","**","/*","+*"};
        //生成最简单的运算式子
        int max=1000;
        int min=0;
        Random random =new Random();
        //随机生成1000以内的数字
        n++;
        if(n<6){
        boolean flag =random.nextBoolean();
        int number =random.nextInt(max)%(max-min+1)+min;
        //int number2 =random.nextInt(max)%(max-min+1)+min;
        int rand =(int)(Math.random()*A.length);
        if (flag==true){
            numberset ="("+String.valueOf(number)+String.valueOf(A[rand])+wrong(FF)+")";
            return numberset;
        }
        else{
            numberset =String.valueOf(number)+String.valueOf(A[rand])+wrong(FF);
            return numberset;   
        }}
        else{
            n=0;
            int rands =(int)(Math.random()*A.length);
            int number1 =random.nextInt(max)%(max-min+1)+min;
            int number2 =random.nextInt(max)%(max-min+1)+min;
            boolean flag1 =random.nextBoolean();
            if(flag1==true){
            numberset =String.valueOf(number1)+String.valueOf(A[rands])+String.valueOf(number2);
            return numberset;}
            else{
                numberset ="("+String.valueOf(number1)+String.valueOf(A[rands])+String.valueOf(number2)+")";
                return numberset;              
            }
                    }
    }
    
    public static void analysefirst() throws FileNotFoundException, IOException{   //分析错误的算术表达式
        File a =new File(path2);
        FileReader areader = new FileReader(a);
        BufferedReader abreader = new BufferedReader(areader);
        String W ="";
        while(( W= abreader.readLine())!=null){
            int rcount =0; //右括号数
            int lcount =0; // 
            System.out.println(W);
            System.out.println("判断:");
            for(int i=0;ilcount){System.out.println("第 "+i+" 字符括号 ) 与前面括号不匹配");}
                    if(W.charAt(i+1)!='-'&& W.charAt(i+1)!='+'&& W.charAt(i+1)!='/'&& W.charAt(i+1)!='*'&&W.charAt(i+1)!='('&& W.charAt(i+1)!=')'){
                        System.out.println("第 "+i+" 个字符 ) 后不能直接出现数字");
                    }
                    if(W.charAt(i+1)=='('){System.out.println("第 "+i+" 个字符 ) 后不能直接出现 (");}
                }
                if(W.charAt(i)=='+'||W.charAt(i)=='-'||W.charAt(i)=='/'||W.charAt(i)=='*'||W.charAt(i)=='('){
                    if(W.charAt(i+1)=='+'||W.charAt(i+1)=='-'||W.charAt(i+1)=='/'||W.charAt(i+1)=='*'){
                        System.out.println("第 "+i+"  字符运算符号后缺少运算值");
                    }
                }
                if(W.charAt(i)!='-'&& W.charAt(i)!='+'&& W.charAt(i)!='/'&& W.charAt(i)!='*'&&W.charAt(i)!='('&& W.charAt(i+1)!=')'){
                    if(W.charAt(i+1)=='(')
                        System.out.println("第 "+i+" 数值后面不能直接添加 ( ");
                }
            }
                if(i==W.length()-1){
                    if(W.charAt(i)=='('){lcount++;}
                    if(W.charAt(i)==')'){rcount++;}
                    if(W.charAt(i)=='+'||W.charAt(i)=='-'||W.charAt(i)=='/'||W.charAt(i)=='*'){
                        System.out.println("最后一个运算符号右边缺少运算值");
                    }
                    
                    if(lcount!=rcount)
                        System.out.println("最后一个括号与前面不匹配");
                }
            }
            
            }
             
        abreader.close();
    }
    
    
    public static String changesecond(String exp) throws IOException{
        Stack s = new Stack(); // 要输出的后缀表达式字符串
        String suffix = "";
        int length = exp.length(); // 输入的中缀表达式的长度
        for (int i = 0; i < length; i++) {
            
            char temp;
            char ch = exp.charAt(i);
            switch (ch) {
                case ' ':
                    break;
                case '(':
                    s.push(ch);
                    break;
 
                case '+':
                case '-':
                    while (s.size() != 0) {
                        temp = s.pop();
                        if (temp == '(') {
                            s.push('(');
                           break;
                        }
                        suffix += temp;                     
                    }
                    s.push(ch);
                    break;
                    
                case '*':
                case '/':
                    while (s.size() != 0) {
                        temp = s.pop();
                        if (temp == '+' || temp == '-' || temp == '(') {
                            s.push(temp);
                            break;
                        } 
                        else {suffix += temp; }
                    }
                    s.push(ch);
                    break;
                    
                case ')':
                    while (!s.isEmpty()) {
                        temp = s.pop();
                        if (temp == '(') {
                            break;
                        } 
                        else { suffix += temp;}
                    }
                    break;
                    
                default:
                    suffix +=ch;
                    if(i+1

运行结果:

判短算式表达式正错_第1张图片
图片1.png
判短算式表达式正错_第2张图片
图片2.png
判短算式表达式正错_第3张图片
图片3.png
判短算式表达式正错_第4张图片
图片4.png
判短算式表达式正错_第5张图片
图片5.png
判短算式表达式正错_第6张图片
图片6.png
判短算式表达式正错_第7张图片
图片7.png

你可能感兴趣的:(判短算式表达式正错)