⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cw3d.java

📁 a Java program that reads a file containing instructions written in self-defined file (TPL in this c
💻 JAVA
字号:
import java.io.*;
import java.util.*;	
	
	
class TPL
{
	
	public String type;
	public String name;
	
	//constructor
	public TPL(String a,String b)
	{
		name=a;
		type=b;
	}
	public int getType()
	{
		if((type.equalsIgnoreCase("integer"))==true)
			return 0;
		else if((type.equalsIgnoreCase("string"))==true)
			return 1;
		else
			return 2;
	}
	
}
	
//subclass of TPL for store integer variable
class Tinteger extends TPL
{
	
	public int intvalue;
	public Tinteger(String name, int c)
	{
			
		super(name,"integer");
		intvalue=c;
	}
}

//subclass of TPL for store string varaible
class Tstring extends TPL
{

	public String value;
	public Tstring(String name,String b)
	{
		super(name,"string");
		value=b;
	
	}
}

class cw3d
{	
	static Vector text;
	static Vector data;
	static int pNo=0;
	
	public static void main(String args[])
	{	
		
		text=new Vector();
		data=new Vector();
		boolean x=false;
		boolean y;
		String line;
		//read file into vector text
		if(args.length>=1)
		{	
			try{
					BufferedReader inFile=new BufferedReader(new FileReader(args[0]));
					
						
					while((line=inFile.readLine())!=null)	
					{
					 	text.addElement(line);			 	
					 	
					}
			
				}catch(IOException e){
					System.out.println("an error ["+e+"]");
				}
		}
		else											//catch error if no file name input
		{
			System.out.println("no command line args");
		}
		for(int i=0;i<text.size();i++)					// search for end reserve word
		{
			String temp=(String)text.elementAt(i);
			y=temp.equalsIgnoreCase("end");
			x=y;
		}
		if(x==false)
		 exit("No end statement");						//no end statement program exit
		else
		 dostuff();										//invoke dostuff method if there is end statement
		 
	}
	//program exit whenever unexpected statement occurs
	public static void exit(String n)
	{
		System.out.println(n);
		System.exit(0);
	}
	

	public static void dostuff()
	{
		String fStr=" ";
		String sStr=" ";
	
		for(int i=0;i<text.size();i++)
		{
			String temp=(String)text.elementAt(i);			//cast data out of vector
			StringTokenizer file=new StringTokenizer(temp);	
			if(file.countTokens()>1)						//reserved words follow by statement
			{
				int dx=temp.indexOf(" ");
				fStr=temp.substring(0,dx);					//fstr contain reserved words
				sStr=temp.substring((dx+1),temp.length());	//sStr has content of command
				if((fStr.equals("#"))==true)				//print out statement after #
				{	
					System.out.println(sStr);
					pNo++;
				}
				//	do integer word and store class instance into vector data						
				else if((fStr.equalsIgnoreCase("integer"))==true)	
				{
					Tinteger tin = new Tinteger(sStr,0);
					data.addElement(tin);	
				}
				//do string word and store class instance into vector data
				else if((fStr.equalsIgnoreCase("string"))==true)
				{
					Tstring tstr=new Tstring(sStr,"");
					data.addElement(tstr);
				}
				//do let
				else if((fStr.equalsIgnoreCase("let"))==true)
				{
					int inx=sStr.indexOf("=");					//split by "=" sign, s has variable value
			 		String s,t;
			 		t=sStr.substring(0,inx);					//variable name
			 		s=sStr.substring((inx+1),sStr.length());
			 		//find string value and update string value in vector
			 		if(strToint(s)==-1)					
			 		{
						int eno=search(t);
						Tstring tstr=(Tstring)data.elementAt(eno);
						tstr.value=sStr.substring((inx+2),(sStr.length()-1));
						data.setElementAt(tstr,eno);
					}
				 	//find integer value and updata its value in vector 
					else							
			 		{
			 			int eno=search(t);
			 			Tinteger tin=(Tinteger)data.elementAt(eno);
			 			tin.intvalue=strToint(s);
			 			data.setElementAt(tin,eno);
			 		}
			 	}
			 	//do calculate 
			 	else if((fStr.equalsIgnoreCase("calculate"))==true)
			 	{
			 		int r=sStr.indexOf("=");
			 		String s,t;
			 		t=sStr.substring(0,r);     					//contains variable name before "="
			 		s=sStr.substring((r+1),sStr.length()); 		//contain formula
			 		//do substract calculation 
			 		if(getOperator(s)==1)
			 		{
			 			int z=s.indexOf("-");					//split string by index of sign"-"
			 			String sn,sv;
			 			sv=s.substring(0,z);					//get variable name 
			 			sn=s.substring((z+1),s.length());	
			 			int eno=search(t);						//find out index of factorial
			 			int ano=search(sv);						//find out index of myInt
			 			Tinteger tmp=(Tinteger)data.elementAt(eno);
			 			Tinteger atmp=(Tinteger)data.elementAt(ano);
			 			tmp.intvalue=atmp.intvalue-strToint(sn);	// calculate 
			 			data.setElementAt(tmp,eno);					//update factorial value
			 		}
			 		//do add calculation 
			 		else if(getOperator(s)==2)
			 		{
			 			int z=s.indexOf("+");						//split string by index of sign"+"																		
			 			String sn,sv;
			 			sv=s.substring(0,z);						//get variable name 							
			 			sn=s.substring((z+1),s.length());
			 			int eno=search(t);							//find out index of factorial
			 			int ano=search(sv);							//find out index of myInt
			 			Tinteger tmp=(Tinteger)data.elementAt(eno);		
			 			Tinteger atmp=(Tinteger)data.elementAt(ano);
			 			tmp.intvalue=atmp.intvalue+strToint(sn);	// calculate 
			 			data.setElementAt(tmp,eno);					//update factorial value
			 		}
			 		//do multiplication 
			 		else if(getOperator(s)==3)
			 		{
			 					 			
			 			int z=s.indexOf("*");						//split string by index of sign"*"				
			 			String sn,sv;
			 			sv=s.substring(0,z);						//get variable name 			
			 			sn=s.substring((z+1),s.length());
			 			int eno=search(t);							//find out index of factorial
			 			int ano=search(sv);							//find out index of myInt
			 			Tinteger tmp=(Tinteger)data.elementAt(eno);
			 			Tinteger atmp=(Tinteger)data.elementAt(ano);
			 			tmp.intvalue=atmp.intvalue*strToint(sn);		// calculate 
			 			data.setElementAt(tmp,eno);						//update factorial value
			 	     }
			 	     //do division 
			 	     else if(getOperator(s)==4)
			 	     {
			 			int z=s.indexOf("/");						//split string by index of sign"/"				
			 			String sn,sv;
			 			sv=s.substring(0,z);						//get variable name 				
			 			sn=s.substring((z+1),s.length());
			 			int eno=search(t);							//find out index of factorial
			 			int ano=search(sv);							//find out index of myInt
			 			Tinteger tmp=(Tinteger)data.elementAt(eno);
			 			Tinteger atmp=(Tinteger)data.elementAt(ano);
			 			tmp.intvalue=atmp.intvalue/strToint(sn);		// calculate 
			 			data.setElementAt(tmp,eno);						//update factorial value
			 			
			 		  }
			 		  else if(getOperator(s)==0)						//catch error statement
			 		  	exit("unexpected character");
			 		}
			 		//do print 
			 		else if((fStr.equalsIgnoreCase("print"))==true)
			 		{
						if(sStr.indexOf("\"")>=0)					//statement after print
						{
							System.out.print(sStr.substring(1,(sStr.length()-1)));
							
						}
						else 										//TPL instance after print
						{
							int eno=search(sStr);					//search for element by variable name
							TPL tmp=(TPL)data.elementAt(eno);
							//print out integer instance value
							if(tmp.getType()==0)
							{
								Tinteger tin=(Tinteger)data.elementAt(eno);
								System.out.print(tin.intvalue);
							}
							//print out string instance value
							else if(tmp.getType()==1)
							{
								Tstring tstr=(Tstring)data.elementAt(eno);
								System.out.print(tstr.value);
								
							}
							//print out exception 
							else if(tmp.getType()==2)
								exit("Type is not found");
			 			}
			 		}
			 		//do println 
			 		else if((fStr.equalsIgnoreCase("println"))==true)
					{
						if(sStr.indexOf("\"")>=0)			//statement after println
						{
							
							System.out.println(sStr.substring(1,(sStr.length()-1)));
							pNo++;
						}
						else 								// tpl instance after println
						{
							int eno=search(sStr);			//search for element by variable name
							TPL tmp=(TPL)data.elementAt(eno);
							if(tmp.getType()==0)
							{
								Tinteger tin=(Tinteger)data.elementAt(eno);
								System.out.println(tin.intvalue);
								pNo++;
							}
							else if(tmp.getType()==1)
							{
								Tstring tstr=(Tstring)data.elementAt(eno);
								System.out.println(tstr.value);
								pNo++;
							}
							else if(tmp.getType()==2)
								exit("Type is not found");
			 			}
			 		}
			 	}//end of if
				//do end and println reserved words
				else
				{
					if((temp.equalsIgnoreCase("println"))==true)
					{
						System.out.println(" ");
					}
					else
					{
						System.out.println("TPL finished ok. [ "+(pNo+1)+" ] lines processed");
					}
						
				}
		}
	}//end of dostuff

	//search for TPL variable name
	public static int search(String m)
	{
		int i=0;
		for(int n=0;n<data.size();n++)
		{ 
			TPL tmp=(TPL)data.elementAt(n);
			String tn=tmp.name;
			if((tn.equalsIgnoreCase(m))==true)
				i=data.indexOf(tmp);
		}		
		return i;
	}
	
	//find out operator type	
	public static int getOperator(String n)
	{
	
		String a="+";
		String s="-";
		String m="*";
		String d="/";
		if(n.indexOf(s)>=0)
			return 1;
		else if(n.indexOf(a)>=0)
			return 2;
		else if(n.indexOf(m)>=0)
			return 3;
		else if(n.indexOf(d)>=0)
			return 4;
		else
			return 0;
	}
	
	//get string int value
	public static int strToint(String n) 
	{
	
		int inNo;
		int x=-1;
		Integer I;
		try{
				I=Integer.valueOf(n);
				inNo=I.intValue();
				return inNo;
			}catch(NumberFormatException e){
				return x;
			}
	}//strToint
}
	



	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -