0916詞法分析

44.林集團發表於2015-09-23

package compileWork;

import java.util.ArrayList;
import java.util.Scanner;

public class compileWork_1
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("please input code");
String scode=s.nextLine();
s.close();
ArrayList<String> arrayphrase=new ArrayList<String>();
for(int i=0;i<scode.length();i++)
{
char[] stemp=scode.substring(i, i+1).toCharArray();
char cword=stemp[0];
String sphrase="";
int flag=0;
while(i<scode.length()&&cword<='Z'&&cword>='A'||cword<='z'&&cword>='a'||cword>='0'&&cword<='9')
{
i++;
sphrase+=cword;
char[] cnextTemp=scode.substring(i,i+1).toCharArray();
cword=cnextTemp[0];
}
if(flag==1){
arrayphrase.add(sphrase);
sphrase="";
}
if(i<scode.length()&&cword=='<')
{
i++;
sphrase+=cword;
char[] cnextTemp=scode.substring(i,i+1).toCharArray();
cword=cnextTemp[0];
if(i<scode.length()&&i<scode.length()&&cword=='=')
{
i++;
sphrase+=cword;
}
if(i<scode.length()&&cword=='>')
{
i++;
sphrase+=cword;
}
arrayphrase.add(sphrase);
sphrase="";
}
if(i<scode.length()&&cword==':')
{
i++;
sphrase+=cword;
char[] cnextTemp=scode.substring(i,i+1).toCharArray();
cword=cnextTemp[0];
if(i<scode.length()&&i<scode.length()&&cword=='=')
{
i++;
sphrase+=cword;
}
arrayphrase.add(sphrase);
sphrase="";
}
if(i<scode.length()&&cword=='+')
{
i++;
sphrase+=cword;
arrayphrase.add(sphrase);
}
if(i<scode.length()&&cword=='-')
{
i++;
sphrase+=cword;
arrayphrase.add(sphrase);
}
if(i<scode.length()&&cword=='*')
{
i++;
sphrase+=cword;
arrayphrase.add(sphrase);
}
if(i<scode.length()&&cword=='/')
{
i++;
sphrase+=cword;
arrayphrase.add(sphrase);
}
if(i<scode.length()&&cword==':')
{
i++;
sphrase+=cword;
arrayphrase.add(sphrase);
}
if(i<scode.length()){
sphrase+=cword;
arrayphrase.add(sphrase);
}
}
for(int j=0;j<6;j++)
{
System.out.print(arrayphrase.get(j));
}
}
}

老師好 因為能力不足只完成了一半 過段時間會補交

相關文章