Method to convert String to a Token: The string holds exactly one token, with no
ID: 3757496 • Letter: M
Question
Method to convert String to a Token: The string holds exactly one token, with no extra spaces before or after the token. Possible tokens are: Operators: PLUS ("+"), TIMES ("*"), MINUS ("-"), DIV ("/"), MOD ("%"), POWER ("^") Parentheses: OPEN ("("), CLOSE (")") Number: NUMBER (any valid string that represents an integer) Assume that if a token is not an operator or a parenthesis, then it is a number. Use Long.parseLong(tok) to convert string to long.Signature: static Token getToken(String tok) { ... }
Tokens have a field "priority" that can be used to store precedence of operators during parsing. Precedence: {^} > {*, /, %} > {+, -}. Assume that all operators are left associative. Assign your own values to priority of tokens. Field "number" is used to store the value of NUMBER token. A token "NIL" is defined for internal use and it does not correspond to any token in the expressions. It is a convenient token to mark bottom of stack. Please write a sample method Method to convert String to a Token: The string holds exactly one token, with no extra spaces before or after the token. Possible tokens are: Operators: PLUS ("+"), TIMES ("*"), MINUS ("-"), DIV ("/"), MOD ("%"), POWER ("^") Parentheses: OPEN ("("), CLOSE (")") Number: NUMBER (any valid string that represents an integer) Assume that if a token is not an operator or a parenthesis, then it is a number. Use Long.parseLong(tok) to convert string to long.
Signature: static Token getToken(String tok) { ... }
Tokens have a field "priority" that can be used to store precedence of operators during parsing. Precedence: {^} > {*, /, %} > {+, -}. Assume that all operators are left associative. Assign your own values to priority of tokens. Field "number" is used to store the value of NUMBER token. A token "NIL" is defined for internal use and it does not correspond to any token in the expressions. It is a convenient token to mark bottom of stack. Please write a sample method Method to convert String to a Token: The string holds exactly one token, with no extra spaces before or after the token. Possible tokens are: Operators: PLUS ("+"), TIMES ("*"), MINUS ("-"), DIV ("/"), MOD ("%"), POWER ("^") Parentheses: OPEN ("("), CLOSE (")") Number: NUMBER (any valid string that represents an integer) Assume that if a token is not an operator or a parenthesis, then it is a number. Use Long.parseLong(tok) to convert string to long.
Signature: static Token getToken(String tok) { ... }
Tokens have a field "priority" that can be used to store precedence of operators during parsing. Precedence: {^} > {*, /, %} > {+, -}. Assume that all operators are left associative. Assign your own values to priority of tokens. Field "number" is used to store the value of NUMBER token. A token "NIL" is defined for internal use and it does not correspond to any token in the expressions. It is a convenient token to mark bottom of stack. Please write a sample method
Explanation / Answer
import java.util.*;
public class HelloWorld{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
String myString= sc.next(); //User Input for expression
char str[]=myString.toCharArray(); // Convert String to character Array
int count1=0,count2=0,count3=0,others=0;
for(int i=0;i<myString.length();i++){
if(str[i]=='^') //Counting the occurance of each operator
count1++;
else if(str[i]=='%' || str[i]=='*' || str[i]=='/')
count2++;
else if(str[i]=='+' || str[i]=='-')
count3++;
else
others++;
}
int temp1=0,temp2=0,temp3=0,temp4=0;
char str1[]=new char[count1];
char str2[]=new char[count2];
char str3[]=new char[count3];
char str4[]=new char[others];
for(int i=0;i<myString.length();i++){
if(str[i]=='^'){
str1[temp1]='^';
temp1++;}
else if(str[i]=='%' || str[i]=='*' || str[i]=='/'){
str2[temp2]=str[i];
temp2++;}
else if(str[i]=='+' || str[i]=='-'){
str3[temp3]=str[i];
temp3++;}
else{
str4[temp4]=str[i];
temp4++;}
}
System.out.print(Arrays.toString(str1)+" "+Arrays.toString(str2)+" "+Arrays.toString(str3)+" "+Arrays.toString(str4));
}
}