Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args)
- {
- String[] onp = new String[args.length];
- String[] stack = new String[10];
- int index = -1;
- int j = 0;
- for (String str: args)
- {
- onp[j] = "";
- if(str.endsWith("="))
- {
- for(int c = 0; c < str.length(); ++c)
- {
- if(str.charAt(c) >= '0' && str.charAt(c) <= '9' || str.charAt(c) == '.')
- {
- onp[j] += str.charAt(c);
- if(!(str.charAt(c + 1) >= '0' && str.charAt(c + 1) <= '9') && str.charAt(c + 1) != '.')
- {
- onp[j] += " ";
- }
- }
- else {
- switch(str.charAt(c)){
- case '-':
- case '+':
- while(index >=0 && !stack[index].equals("(")){
- onp[j] += stack[index] + " ";
- index--;
- }
- stack[++index] = str.charAt(c) + "";
- break;
- case '*':
- case '/':
- while(index >=0 && !stack[index].equals("(") && !stack[index].equals("+") && !stack[index].equals("-")){
- onp[j] += stack[index] + " ";
- index--;
- }
- stack[++index] = str.charAt(c) + "";
- break;
- case '^':
- while(index >=0 && !stack[index].equals("(") && !stack[index].equals("+") && !stack[index].equals("-") && !stack[index].equals("*") && !stack[index].equals("/")){
- onp[j] += stack[index] + " ";
- index--;
- }
- stack[++index] = str.charAt(c) + "";
- break;
- case '(':
- stack[++index] = str.charAt(c) + "";
- break;
- case ')':
- while(index >= 0 && !stack[index].equals("(")){
- onp[j] += stack[index] + " ";
- index--;
- }
- index--;
- break;
- case '=':
- while(index >= 0 ){
- onp[j] += stack[index] + " ";
- index--;
- }
- break;
- default:
- break;
- }
- }
- }
- }
- else
- {
- System.out.println("Wprowadzone równanie: '" + str + "' jest blędne.");
- }
- System.out.println(onp[j]);
- j++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement