超级多项式计算器

源代码在线查看: function.java

软件大小: 18 K
上传用户: a974555392
关键词: 超级 多项式 计算器
下载地址: 免注册下载 普通下载 VIP

相关代码

				
				import java.util.Vector;
				
				/**Class Fuction
				  *Data structure to save a function.
				  *It has several attributes such as name,arguments and the calculated expression of the fuction express.
				  *Also it contains a method to parse the function expression to get the functionname and the arguments.
				  *@author zhlmmc
				  *@version 1.0
				  */
				
				public class Function
				{
					String s;
					char name;
					Vector arguments = new Vector();
					Vector funcTerms = new Vector();
				
					/**constructors
					  *Fuction() receive no arguments 
					  */
					public Function()
					{
						name = '$';
						s = "";
					}
				
					/**
					  *Fuction(String input)
					  *@param input the expression of the function
					  *@param terms a Vector that contains the polynomial calculated by Calculate.calculateTerms()
					  */
				
					public Function(String input,Vector terms)
					{
						this();
						
						funcTerms = terms;
						analyseInput(input);
						s = "DEF" + toString();
						
					}
				
					/**analyseInput
					  *Analyse the input to separate the
					  *fuction name and argument.
					  *Also this method will store them.
					  *@param input the function express
					  */
					public void analyseInput(String input)
					{
				
						//analyse the name of the function
						name = input.charAt(3);
				
						//analyse the arguments
						int i = 5;//because "DEFA(a,b) = ...",the first argument a's subscript is 5
						do
						{
				
							if (input.charAt(i) == ',')
							{
								continue;
							}
							else
							{
								arguments.add(new Character(input.charAt(i)));
							}
				
						}
						while (input.charAt(++i) != ')');
				
				
					}
				
					/**output the function expression
					  *@return the function expression in String type
					  */
					public String toString()
					{
						if (funcTerms.size() == 0)
						{
							return s.substring(3,s.length());
						}
						String output = "";
						
						output += name + "(" + ((Character)arguments.get(0)).charValue();
				
						for (int i = 1;i < arguments.size();i++)
						{
							output += "," + ((Character)arguments.get(i)).charValue();
						}
						output += ")" + "=";
						
						String tempStr = ((TermNode)funcTerms.get(0)).toString();
						
						if (tempStr.charAt(0) == '+')
						{
							
							output += tempStr.substring(1,tempStr.length());
						}
						else output += tempStr.substring(0,tempStr.length());
				
						int len = funcTerms.size();//the length of terms
						for (int i = 1;i < len ;i++ )
						{
							output += ((TermNode)funcTerms.get(i)).toString();
						}
							
				
						return output;
					}
				
				
				
				};			

相关资源