Skip to content

Instantly share code, notes, and snippets.

@gangmul12
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gangmul12/88ec546971134070787b to your computer and use it in GitHub Desktop.
Save gangmul12/88ec546971134070787b to your computer and use it in GitHub Desktop.
import java.io.*;
public class Console
{
public static void main(String args[])
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true)
{
try
{
String input = br.readLine();
if (input.compareTo("q") == 0)
break;
command(input);
}
catch (Exception e)
{
System.out.println("입력이 잘못되었습니다. 오류 : " + e.toString());
}
}
}
private static void command(String input)
{
Parser p = new Parser(input);
p.printInfixExp();
// TODO : 아래 문장을 삭제하고 구현해라.
System.out.println("<< command 함수에서 " + input + " 명령을 처리할 예정입니다 >>");
}
public static boolean isNumber(char c){
if(Character.getNumericValue(c)<10 &&Character.getNumericValue(c)>-1)
return true;
return false;
}
}
import java.util.ListIterator;
import java.util.Stack;
import java.util.ArrayList;
public class Parser {
ArrayList<String> infixExp;
Stack<String> operandStack;
ArrayList<String> postfixExp;
public Parser(String input){
infixExp = new ArrayList<String>();
setInfix(input);
operandStack = new Stack<String>();
postfixExp = new ArrayList<String>();
}
public void setInfix(String input){
int intCount = 0;
for(int i=0; i<input.length(); i++){
if(!Console.isNumber(input.charAt(i))){
if(input.charAt(i)==' '||input.charAt(i)=='\t')
continue;
infixExp.add(Character.toString(input.charAt(i)));
}
else{
intCount++;
if(i+1==input.length()||!Console.isNumber(input.charAt(i+1))){
infixExp.add(input.substring(i+1-intCount, i+1));
intCount = 0;
}
}
}
}
public void printInfixExp(){
ListIterator<String> it = infixExp.listIterator();
while(it.hasNext())
System.out.print(it.next()+" ~ ");
}
public void changeNotation(){
}
public ArrayList<String> getPostFixExp(){
return postfixExp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment