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

📄 pcalc.java

📁 超级多项式计算器
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	
	
/**method substitute
  *substitute the variable with the given characters
  *and do calculate
  */
private static String substitute(String input)
{
	if (passSubstituteExp(input))
	{
		
		int i = functionName.indexOf(new Character(input.charAt(0)));
		int a = ((Function)functions.get(i)).s.indexOf('=');
		int len = ((Function)functions.get(i)).s.length();

		String tempExp = addAsterisk(((Function)functions.get(i)).s.substring(a + 1,len));
		Vector arguments = ((Function)functions.get(i)).arguments;
		String[] newStr = getNewStr(input,arguments.size());

		if (newStr == null)
		{
			return null;
		}
		
		//scan the String if the current character is in the arguments table,replace it
		int j = 0;
		while (j < tempExp.length())
		{
		
			int b = arguments.indexOf(new Character(tempExp.charAt(j)));
			if (b != -1)
			{
				tempExp = tempExp.substring(0,j) + newStr[b] + tempExp.substring(j+1,tempExp.length());
				j += newStr[b].length();
			}
			else j++;
			
		}
		
		Vector tempV = new Vector();
		if (judgeExponent(tempExp))
		{
			tempExp = addAsterisk(tempExp);
			
			tempV = Calculate.calculateTerms(tempExp);

			//if the number is too large
			if (tempV == null)
			{
				System.out.println("ERROR : The number you want to calculate is too LARGE!");
				errorPrint(input,0);
				return null;
			}
			
		}
		else 
		{
			return "+E";
		}
		
		if (!tempV.isEmpty() && ((TermNode)tempV.get(0)).toString().equals("+E"))
		{
			System.out.println("ERROR : \"0\" cann't be POWWED!");
			errorPrint(tempExp,0);

			return "+E";

		}

		String reStr = "";

		for (int k = 0;k < tempV.size() ;k++ )
		{
			reStr += ((TermNode)tempV.get(k)).toString();
		}

		if (reStr == "")
		{
			reStr += "0";
		}
		
		return reStr;

	}
	return null;
}

/**getNewStr 
  *get the characters that to replace the original char
  */
private static String[] getNewStr(String input,int n)
{
	
	String[] newStr = new String[n];
	String tempS = "";
	int count = 0;
	
	int i = getI(input,2);
	
	int j = 2;//record start character of the newStr's index
	while (i < input.length())
	{
		if (i - j == 0)
		{
			System.out.println("ERROR : Lose argument!");
			errorPrint(input,i);
			
			return null;
		}
		if (i - j > 1)
		{
			tempS = input.substring(j,i);
			
			if (!passExpression2(tempS))
			{
				return null;
			}
			if (!isEnd(tempS))
			{
				tempS = substituteAll(tempS);	
				if (tempS == null)
				{
					return null;
				}
				newStr[count] = "(" + tempS + ")";
			}
			else newStr[count] = "(" + tempS + ")";	

		}
		else 
		{
			tempS = input.substring(j,i);
			if (!passExpression(tempS))
			{
				return null;
			}
			newStr[count] = tempS;
			
		}
		
		count++;
		j = i + 1;
		i = getI(input,j);
		
	}

	return newStr;

}

/**this method is to judge if the argument contains any funtion
  */
private static boolean isEnd(String argument)
{
	int i = 0;
	while (i < argument.length())
	{
		if (Character.isUpperCase(argument.charAt(i)))
		{
			return false;
		}
		i++;
	}

	return true;
}

/**this method is to get ',' index
  */
private static int getI(String input,int i)
{
	
	int flag = 0;
	while (i < input.length() - 1)
	{
		if (flag == 0 && input.charAt(i) == ',')
		{
			return i;
		}
		else if (input.charAt(i) == '(')
		{
			flag++;
		}
		else if (input.charAt(i) == ')')
		{
			flag--;
		}
		i++;
	}
	return i;
}

/**method passSubstituteExp
  *judge if the evaluate express is correct
  */
private static boolean passSubstituteExp(String input)
{
	
	if (!judgeParentheses(input))
	{
		return false;
	}

	if (input.length() < 4)
	{
		System.out.println("ERROR : Please input the character that you want to substitute the variable!1");
		errorPrint(input,1);
		return false;
	}
	
	int flag = 0;//if a function to substitute ',',int '()' whill not be add
	int argumentNum = 1;
	int a = functionName.indexOf(new Character(input.charAt(0)));
	if ( a != -1)
	{
		for (int i = 2;i < input.length() - 1 ;i++)
		{
			if (flag == 0 && (input.charAt(i) == ','))
			{
				argumentNum++;
			}
			else if (input.charAt(i) == '(')
			{
				flag++;
			}
			else if (input.charAt(i) == ')')
			{
				flag--;
			}
		}
	}
	else 
	{
		System.out.println("ERROR : The function you want to calculate haven't be defined!");
		errorPrint(input,0);
		return false;
	}

	
	if (argumentNum != ((Function)functions.get(a)).arguments.size())
	{
		System.out.println("ERROR : The numbers of the given arguments must be the SAME as the number of the function's arguments!");
		errorPrint(input,0);
		return false;
	}

	return true;
}

/**method passFucExpression
  *to check if the client input the fuction correct
  */
private static boolean passFuncExpression(String input)
{
	if (basePass(input))
	{
		int a = input.indexOf("=");//to store ")" subscript

		if (a == input.length() - 1)
		{
			System.out.println("ERROE : Lose the function expression!");
			errorPrint(input,a + 1);
			return false;
		}

		String express = input.substring(a + 1,input.length());

		if (!passExpression2(express))
		{
			return false;
		}
		return true;
	}
	else return false;

}

/*if all the judge methods return true
 *then pass will return true
 *else return false
 *espescially this method don't judge exponent!
 */
private static boolean passExpression2(String s)
{
		
	if (noUnexpectedCharacter(s) && judgeFirst(s)  && judgeUpperCase(s)
			
		&& judgeParentheses(s) && passOperator(s))
	{
		return true;
	}
	else return false;
}

/**method basePass
  *namePase,to check the function's name
  *argumentPass,to check the arguments
  *equalPass,to check the "="
  */
private static boolean basePass(String input)
{
	if (namePass(input) && argumentPass(input) && equalPass(input) && argumentsMatchPass(input))
	{
		return true;
	}
	else return false;
}

/**method namePass
  *if the fuction name contains more than one character or the name is not uppercase
  *return false
  *else return true
  */
private static boolean namePass(String s)
{
	if (Character.isLetter(s.charAt(3)))
		if (Character.isUpperCase(s.charAt(3)))
			if (s.charAt(4) == '(')
				return true;

	System.out.println("ERROR : The function name must be a SINGLE UPPERCASE!");
	errorPrint("DEF " + s.substring(3,s.length()),4);
	return false;
}

/**method argumentPass
  *if any argument is not a single lowercase
  *return false
  *else return true
  */
private static boolean argumentPass(String s)
{
	
	Vector arguments = new Vector();
	int i = 5;//because the first argument's subscript is 5
	do
	{
		
		if (Character.isLetter(s.charAt(i)))
		{
			
			if (Character.isUpperCase(s.charAt(i)))
			{
				
				System.out.println("ERROR : The argument must be a LOWERCASE!");
				errorPrint("DEF " + s.substring(3,s.length()),i + 1);
			
				return false;
			}
			else 
			{
				if ((s.charAt(i + 1) != ',') && (s.charAt(i + 1) != ')'))
				{
				
					System.out.println("ERROR : The argument must be a single letter!");
					errorPrint("DEF " + s.substring(3,s.length()),i + 1);
					return false;
				}
				
				else if (arguments.indexOf(new Character(s.charAt(i))) == -1)
				{
					arguments.add(new Character(s.charAt(i)));
				}
				else if (arguments.indexOf(new Character(s.charAt(i))) != -1)
				{
					System.out.println("ERROR : You cann't input the SAME argument!");
					errorPrint("DEF " + s.substring(3,s.length()),i + 1);
				
					return false;
				}

				if (s.charAt(i + 1) == ')')
				{
					return true;
				}
				
			}

		}
		else 
		{
			System.out.println("ERROR : The argument must be a LETTER!");
			errorPrint("DEF " + s.substring(3,s.length()),i + 1);

			return false;
		}

		i += 2;
		
	}
	while (s.charAt(i) != ')');

	return true;

}

/**method equalPass
  *if the function expression contains more than one "="
  *return false
  *else return true
  */
private static boolean equalPass(String s)
{
	int a = s.indexOf("=");
	if (a == -1)
	{
		System.out.println("ERROR : There isn't any \"=\"!");
		errorPrint("DEF " + s.substring(3,s.length()),s.length());
		return false;
	}

	a = s.indexOf("=",a + 1);
	if (a != -1)
	{
		System.out.println("ERROR : There are more than one \"=\"!");
		errorPrint("DEF " + s.substring(3,s.length()),a + 1);
		return false;
	}
	
	return true;
}

/**method argumentsMatchPass
  *to judge if the variables in the function expression are all in the arguments table
  *if not 
  *return false
  *else return true;
  */
private static boolean argumentsMatchPass(String s)
{
	Vector tempArguments = new Vector();
	int a = s.indexOf("(");//a to store the first "(" subscript
	
	while ((s.charAt(a) != ')') && (s.charAt(++a) != ')'))
	{
		
		tempArguments.add(new Character(s.charAt(a)));
		a++;
	}

	a = s.indexOf("=");
	while (++a < s.length())
	{
		if (Character.isLetter(s.charAt(a)) && (!tempArguments.contains(new Character(s.charAt(a)))))
		{
			System.out.println("ERROR : The variable in the function expression "
			                      + "must in the ARGUMENTS TABLE!");
			errorPrint("DEF " + s.substring(3,s.length()),a + 1);
			tempArguments.clear();
			return false;
		}		
		
	}

	tempArguments.clear();
	return true;
}

};

⌨️ 快捷键说明

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