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

📄 pcalc.java

📁 超级多项式计算器
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
					return false;
				}
			}
			i++;
		}

		if (judge == 0)
		{
			return true;
		}
		else 
		{
			System.out.println("ERROR : Please check your Parenthese!");
			errorPrint(s,pos);
			return false;	
		}
		
	}

	/**passOperator
	  *if the operator is used correctly return true
	  *else return false
	  */
	private static boolean passOperator(String s)
	{
		int i = 0;
		while (i < s.length())
		{
			if (isOperator(s.charAt(i)))
			{
				if (i == s.length() - 1)
				{
					System.out.println("ERROR : You can't end the polynoimal with an operator!");
					errorPrint(s,i);
					return false;
				}
				else if (isOperator(s.charAt(i + 1)))
				{
					System.out.println("ERROR : You can't put two operators together!");
					errorPrint(s,i);
					return false;
				}
				else if ((s.charAt(i) == '+') && (s.charAt(i - 1) == '('))
				{
					System.out.println("ERROE : You can't put \"+\" after \"(\"!");
					errorPrint(s,i);
					return false;
				}
				else if ((s.charAt(i) == '*') && (s.charAt(i - 1) == '('))
				{
					System.out.println("ERROE : You can't put \"*\" after \"(\"!");
					errorPrint(s,i);
					return false;
				}
				else if ((s.charAt(i) == '^') && (s.charAt(i - 1) == '('))
				{
					System.out.println("ERROE : You can't put \"^\" after \"(\"!");
					errorPrint(s,i);
					return false;
				}
				else if (isOperator(s.charAt(i)) && (s.charAt(i + 1) == ')'))
				{
					System.out.println("ERROE : You can't put an operator BEFORE \")\"!");
					errorPrint(s,i);
					return false;
				}
			}
			i++;
		}

		return true;
	}

	/**isOperator
	  *judge if the char is an operator
	  */
	private static boolean isOperator(char c)
	{
		if (c == '+')
		{
			return true;
		}
		if (c == '-')
		{
			return true;
		}
		if (c== '*')
		{
			return true;
		}
		if (c == '^')
		{
			return true;
		}

		return false;
	}
			
	/**
	  *method print
	  *to print the calculated polynomail.<br/>
	  *@param terms the Vector-type expression to be printed 
	  */
	public static String print(Vector terms)
	{
		
		String output = "";
		output += "= ";

		if (terms.isEmpty() || ((TermNode)terms.get(0)).toString() == "")
		{
			output += "0";
			return output;
		}

		//deal the first term
		String tempStr = ((TermNode)terms.get(0)).toString();
		
		if (tempStr.charAt(0) == '+')
		{

			output += tempStr.substring(1,tempStr.length());
			
		}
		else output += tempStr.substring(0,tempStr.length());

		int len = terms.size();//the length of terms
		for (int i = 1;i < len ;i++ )
		{
			
			output += terms.get(i);

		}
		
		return output;

	}

	/**errorPrint
	  *to print the error message
	  */
	private static void errorPrint(String s,int pos)
	{
		System.out.println(s);
		
		int len = s.length();
		for (int i = 0;i < pos ;i++ )
		{
			System.out.print(" ");
		}
		System.out.println("^");
	}

/*************************************Fuction**********************************************/
private static void dealFuction(String input)
{
	
	if (passFuncExpression(input))
	{
		if (!passCalculatable(input))
		{
			//if the functionname has appeared remove the original element
			int i = functionName.indexOf(new Character(input.charAt(3)));
			if (i != -1)
			{
				functions.removeElementAt(i);
				functionName.removeElementAt(i);
			}
			functions.add(new Function());
			
			((Function)functions.get(functions.size() - 1)).s = input;
			((Function)functions.get(functions.size() - 1)).analyseInput(input); 

			functionName.add(new Character(input.charAt(3)));
			
			//set the HelpMode
			HelpMode.setFunctions(functions);

			System.out.println(input.substring(3,input.length()));
			return;
		}
		
		int a = input.indexOf("=");
		input = input.substring(0,a+1) + addAsterisk(input.substring(a + 1,input.length()));
				
		Vector tempV = Calculate.calculateTerms(input.substring(a + 1,input.length()));

		//to deal the situation that the number is too large!
		if (tempV == null)
		{
			System.out.println("ERROR : The number you want to calculate is too LARGE!");
			errorPrint("DEF " + input.substring(3,input.length()),a + 2);
			return;
		}

		if (!tempV.isEmpty() && ((TermNode)tempV.get(0)).toString().equals("+E"))
			{
				System.out.println("ERROR : \"0\" cann't be POWWED!");
				errorPrint("DEF " + input.substring(3,input.length()),a + 2);

			}
		else
		{
			//if the functionname has appeared remove the original element
			int i = functionName.indexOf(new Character(input.charAt(3)));
			if (i != -1)
			{
				functions.removeElementAt(i);
				functionName.removeElementAt(i);
			}

			functions.add(new Function(input,tempV));
			functionName.add(new Character(input.charAt(3)));

			//set the HelpMode
			HelpMode.setFunctions(functions);

			System.out.println(functions.get(functions.size() - 1));
			
		}
		
	}
	

}

/**psaaCalculatable
  *to judge if the expression can be calculated now!
  */
private static boolean passCalculatable(String input)
{
	int a = input.indexOf("^");
	while (a != -1)
	{
		if (Character.isLetter(input.charAt(a + 1)))
		{
			return false;
		}
		else if (input.charAt(a + 1) == '(')
		{
			int count = 1;
			int i = a + 2;
			while (count != 0)
			{	
				i++;
				if (input.charAt(i) == ')')
				{
					count--;
				}
			}
			for (int j = a + 2;j < i ;j++ )
			{
				if (Character.isLetter(input.charAt(j)))
				{
					return false;
				}
			}
		}
		
		a = input.indexOf("^",a + 1);

	}
	return true;
}

/**method judgeSubstitute
  *judge if the input express is to substitute
  */
private static boolean judgeSubstitute(String input)
{
	int i = 0;

	//anywhere appera UpperCase + "(" will be recegnised as substitute
	while (i < input.length() - 1)
	{
	
		if (Character.isUpperCase(input.charAt(i)))
			if (input.charAt(i + 1) == '(')
				return true;
		i++;
	
	}

	return false;
}	

/**method substituteAll
  *to do the calculate between the functions and the calculate between functions and terms
  */
private static String substituteAll(String input)
{

	input = addAsterisk(input);
	//System.out.println(input);

	String finalExpress = "";
	String tempFunc = "";
	int pos = 0;
	while (pos < input.length())
	{

		//save the left expression except the substitute
		while ((pos < input.length()) && (!Character.isUpperCase(input.charAt(pos))))
		{
		
			finalExpress += input.charAt(pos);
			pos++;
			
		}

		//read the next substitute
		if (pos < input.length())
		{
			tempFunc = readFunc(input,pos);
		}
		else break;
		
		pos += tempFunc.length();
		
		tempFunc = substitute(tempFunc);

		if (tempFunc == null)
		{
			return null;
		}

		if (tempFunc.equals("+E"))
		{
			errorPrint(input,pos - 1);
			return null;
		}

		//ATTENTION:This block is HARD to understand!
		if (tempFunc == "")
		{
			return null;
		}
		else 
		{
			
			if (tempFunc.charAt(0) == '+')
			{
				tempFunc = tempFunc.substring(1,tempFunc.length());
				
				if ((tempFunc.indexOf('+') != -1) || (tempFunc.indexOf('-') != -1))
				{
					tempFunc = "(" + tempFunc + ")";
				}

				finalExpress += tempFunc;
			}
			else if (tempFunc.charAt(0) == '-')
			{
					
					int i = finalExpress.length() - 1;
					while ((i >= 0) && ((Character.isLetter(finalExpress.charAt(i))) ||
						   (Character.isDigit(finalExpress.charAt(i))) || finalExpress.charAt(i) == '*'))
					{
						i--;
					}//search the last operator
					if (i >= 0)
					{

						if (finalExpress.charAt(i) == '+')
						{
							tempFunc = tempFunc.substring(1,tempFunc.length());
							
							if ((tempFunc.indexOf('+') != -1) || (tempFunc.indexOf('-') != -1))
							{
								tempFunc = "(" + tempFunc + ")";
							}

				
							finalExpress = finalExpress.substring(0,i) + "-" + finalExpress.substring(i+1,finalExpress.length())
											+ tempFunc;
							
						}
						else if (finalExpress.charAt(i) == '-')
						{
							tempFunc = tempFunc.substring(1,tempFunc.length());
							if ((tempFunc.indexOf('+') != -1) || (tempFunc.indexOf('-') != -1))
							{
								tempFunc = "(" + tempFunc + ")";
							}
							finalExpress = finalExpress.substring(0,i) + "+" + finalExpress.substring(i+1,finalExpress.length())
											+ tempFunc;
							
						}
						
						else if (finalExpress.charAt(i) == '^')
						{
							System.out.println("ERROR : The exponent cann't be a NEGATIVE!");
							errorPrint(finalExpress+tempFunc,i + 1);
							return null;
						}

					}
					else 
					{
					
						tempFunc = tempFunc.substring(1,tempFunc.length());
						if ((tempFunc.indexOf('+') != -1) || (tempFunc.indexOf('-') != -1))
						{
							
							tempFunc = "(" + tempFunc + ")";
						}

						finalExpress = "-" + finalExpress + tempFunc;
					}
				
			}
			else finalExpress += 0;
						
		}					
		
	}
	
	//if the first character is '+',it should be remove ,so that the express can be calculated
	if (finalExpress.charAt(0) == '+')
	{
		finalExpress = finalExpress.substring(1,finalExpress.length());
	}
	

	finalExpress = addAsterisk(finalExpress);

	return finalExpress;
	
}

/**method readFunc
  *to read the next function of the express
  */
private static String readFunc(String input,int pos)
{
	int pos2 = pos;
	int flag = 0;
	while (pos2 < input.length())
	{
		if (input.charAt(pos2) == '(')
		{
			flag++;
		}
		else if (input.charAt(pos2) == ')')
		{
			flag--;
			if (flag == 0)
			{
				break;
			}
		}

		pos2++;
		
	}

	pos2++;

	return input.substring(pos,pos2);
}

⌨️ 快捷键说明

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