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

📄 pcalc.java

📁 超级多项式计算器
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

import java.io.*;
import java.util.Vector;

/**Pcalc 1.0 <br/>
  *This program is to calculate the polynomial and to complete the fuction operation.
  *The client input the polynomial or the function express in the console and the
  *program will calculate the result and output it.
  *@version 1.0
  *@author zhlmmc
  */


public class Pcalc
{
	private static Vector functions = new Vector();
	private static Vector functionName = new Vector();
	private static String result = "";
	
	//this String is to help set the HelpMode's calculated polynomials
	private static String tempString = "";

	/**Pcalc alpha_1.0
	  *This program is to calculate the polynomial and to complete the fuction operation.
	  *The client input the polynomial or the function express in the console and the
	  *program will calculate the result and output it.
	  */

	public static void main(String arg[])
	{

		//variable to save the current time
		double t1;
		double t2;

		String input = "";
		String valuedStr = "";

		System.out.println("Pcalc, version 1.0");//name of the program and the version
		System.out.println("H -- Enter the help mode!");
		do
		{
			input = read();
			
			if ((input.equals("EXIT")) || (input.equals("QUIT")))
			{
				break;
			}
			else if (input.equals("H"))
			{
				HelpMode.read();
			}
			else 
			{
				t1 = System.currentTimeMillis();
				valuedStr = modifyInput(input);

				if (valuedStr.equals(""))
				{
					System.out.println("ERROR : You must input a polynomial or define a function!");
				}
				else if ((valuedStr.length() >= 3) && (valuedStr.substring(0,3).equals("DEF")))
				{
					
					dealFuction(valuedStr);
				}
				else if (judgeSubstitute(valuedStr))
				{
					if (passExpression2(valuedStr))
					{
						result = substituteAll(valuedStr);
						
						if (result != null)
						{
							Vector tempV = Calculate.calculateTerms(result);
							if (tempV == null)
							{
								System.out.println("ERROR : The number you want to calculate is too LARGE!");
								errorPrint(input,0);
							}
							else 
							{
								System.out.println(print(tempV));

								t2 = (System.currentTimeMillis() - t1) / 1000;
								System.out.println("Time used : " + t2 + " seconds");
							}
						}
						
					}	
				
				}	
				 else if (passExpression(valuedStr))
				{	
					 //set tempString so to add to HelpMode.calculated
					tempString = "";
					tempString += valuedStr + " ";

					valuedStr = addAsterisk(valuedStr);
					Vector tempV = Calculate.calculateTerms(valuedStr);

					if (tempV == null)
					{
						System.out.println("ERROR : The number you want to calculate is too LARGE!");
						errorPrint(input,0);
					}

					else if (!tempV.isEmpty() && ((TermNode)tempV.get(0)).toString().equals("+E"))
					{
						System.out.println("ERROR : \"0\" cann't be POWWED!");
						errorPrint(valuedStr,0);

					}
					else 
					{

						System.out.println(print(tempV));

						t2 = (System.currentTimeMillis() - t1) / 1000;
						System.out.println("Time used : " + t2 + " seconds");

						tempString += print(tempV);
						HelpMode.addCalculated(tempString);
					}
					
				}
			}

		}
		while (true);
		
				
	}

 /***************************Functions that are Used in the Main Method***********************/
	  
	/**
	  *read method to read in the polynomial
	  */
	private static String read()
	{
		String tempStr = "";//this is to deal the "#"
		int a = 0;

		//declear the reader and the StreamReaderBuffer
		InputStreamReader cin = new InputStreamReader(System.in);
		BufferedReader bf = new BufferedReader(cin);

		String s = "";//string to recive the polynomial
		
		//this block is to do the read task
		//when the client type ";" then finish reading
		
		do
		{
			System.out.print("> ");
			try
			{
				tempStr = bf.readLine();
				a = tempStr.indexOf("#");
				if (a == -1)
				{
					s += tempStr;
				}
				else s += tempStr.substring(0,a);

				if ((s.equals("EXIT")) || (s.equals("QUIT")) || (s.equals("H")))
				{
					break;
				}
			}catch(IOException e)
			{
				System.out.println("Wrong!");
			}
		}while(s.indexOf(";") == -1);			
		
		return s;
		
	}

    /**
	  *this method to modify the polynomial
	  *then the polynomial will be no blanks except the blank between number and number
	  *and the characters after the first ";" whill be ignored
	  *return the modifyed String
	  */
	private static String modifyInput(String input)
	{
		int a;

		//characters after ";" will be cut off
		a = input.indexOf(";");
		input = input.substring(0,a);
		
		//delete the blanks in the head and in the tail of the expression
		input = input.trim();

		while (input.indexOf(" ") != -1)
		{
			//the blanks between number and number will be replaced by "*"
			a = input.indexOf(" ");
			if (Character.isDigit(input.charAt(a -1)) && Character.isDigit(input.charAt(a + 1)))
			{
				input = input.substring(0,a) + "*" + input.substring(a + 1,input.length());
			}
			else 
				input = input.substring(0,a) + input.substring(a + 1,input.length());
	
		}

		return input;
	}

	/**method addAsterisk
	  *this method will add asterisk between number and letter
	  *,number and "(" ,")" and letter,letter and "(",")" and "(".
	  */
	private static String addAsterisk(String input)
	{
		int i = 0;
	
		while (i < input.length() - 1)
		{
	
			if (Character.isDigit(input.charAt(i)) && Character.isLetter(input.charAt(i + 1))) 
			{
				input = input.substring(0,i + 1) + "*" + input.substring(i + 1,input.length());
			}
			else if (Character.isLetter(input.charAt(i)) && Character.isLetter(input.charAt(i + 1)))
			{
				input = input.substring(0,i + 1) + "*" + input.substring(i + 1,input.length());
			}
			else if ((input.charAt(i) == ')') && Character.isLetter(input.charAt(i + 1)))
			{
				input = input.substring(0,i + 1) + "*" + input.substring(i + 1,input.length());
			}
			else if (Character.isLowerCase(input.charAt(i)) && (input.charAt(i + 1)) == '(')
			{
				input = input.substring(0,i + 1) + "*" + input.substring(i + 1,input.length());
			}
			else if ((input.charAt(i) == ')') && (input.charAt(i + 1)) == '(')
			{
				input = input.substring(0,i + 1) + "*" + input.substring(i + 1,input.length());
			}
			else if ((input.charAt(i) == ')') && Character.isDigit(input.charAt(i + 1)))
			{
				input = input.substring(0,i + 1) + "*" + input.substring(i + 1,input.length());
			}
			else if (Character.isLetter(input.charAt(i)) && Character.isDigit(input.charAt(i + 1)))
			{
				input = input.substring(0,i + 1) + "*" + input.substring(i + 1,input.length());
			}
			else if (Character.isDigit(input.charAt(i)) && input.charAt(i + 1) == '(')
			{
				input = input.substring(0,i + 1) + "*" + input.substring(i + 1,input.length());
			}
			else if (input.charAt(i) == '-' && input.charAt(i + 1) == '(')
			{
				input = input.substring(0,i + 1) + "1*" + input.substring(i + 1,input.length());
			}
			
			i++;

		}

		return input;
	}
	/**These methods to judge if the inpput polynomial is legal
	  *pass : check all the judge method
	  *judgeFirst : judge if the first character is an illegal character 
	  *noUnexpectedCharacter : judge if the polynomial contains any unexpected character such as ".","'","$"...
	  *judgeExponent : judge if the polynomial has any negative exponent
	  *judeUpperCase : scan the polynomial and to find if there is any uppercase letter
	  *judgeParentheses : to judge if the parentheses are uses correct
	  *passOperator : judge if the operator is used correct
	  */

	/**method pass
	  *if all the judge methods return true
	  *then pass will return true
	  *else return false
	  */
	private static boolean passExpression(String s)
	{
		
		if (noUnexpectedCharacter(s) && judgeComma(s) && judgeFirst(s)  && judgeUpperCase(s)
			
		&& judgeParentheses(s) && passOperator(s) && judgeExponent(s))
		{
			return true;
		}
		else return false;
	}
	
	/**judgeComma()
	  *judge if the express contains any ","
	  */
	private static boolean judgeComma(String s)
	{
		int a = s.indexOf(",");
		if (a != -1)
		{
			System.out.println("ERROR : You cann't input any \",\" in a plynomial!");
			errorPrint(s,a);
			return false;
		}
		
		return true;
	}
	/**method noUnexpectedCharacter()
	  *if the polynomial dosen't contains any UNEXCEPTED character such as ".","#" and so on
	  *return true 
	  *else return flase
	  */
	private static boolean noUnexpectedCharacter(String s)
	{
		int i = 0;
		char tempChar;

		while (i < s.length())
		{
			
			tempChar = s.charAt(i);
			if (!(Character.isDigit(tempChar) || Character.isLetter(tempChar) || isOperator(tempChar)
				|| (tempChar == '^') || (tempChar == '(') || (tempChar == ')') ||
				(tempChar == ',')))
			{
				System.out.println("ERROR : The polymonial contians UNEXPECTED character : " + s.charAt(i));
				errorPrint(s,i);
				return false;
			}

			i++;
			
		}
		return true;
	}

	/**method judgeExponent
	  *if no exponent is negative
	  *retrun true
	  *else return false
	  */
	private static boolean judgeExponent(String input)
	{
		
		int a = input.indexOf("^");
		while (a != -1)
		{
			if (Character.isLowerCase(input.charAt(a + 1)))
			{
				System.out.println("ERROR : You cann't pow of a LETRER!");
				errorPrint(input,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)))
					{
						System.out.println("ERROR : You cann't pow of a LETRER!");
						errorPrint(input,j);
						return false;
					}
				}
			}
		
			a = input.indexOf("^",a + 1);

		}
		return true;
	}

	/**method judgeUpperCase
	  *if the polynomial contains any uppercase
	  *retrun false
	  *else return true
	  */
	private static boolean judgeUpperCase(String s)
	{
		for (int i = 0;i <= s.length() - 1 ;i++ )
		{
			if (Character.isUpperCase(s.charAt(i)) && (i < s.length() - 1)
				&& (!(s.charAt(i + 1) == '(')))
			{

				System.out.println("ERROR : There shouldn't be any UPPERCASE variable in the polynomial!");
				errorPrint(s,i);
				return false;
					
			}
		}

		return true;
	}

	/**
	  *method judgeFirst
	  *if the first character is not an integer or an letter or "("
	  *return false
	  *else return true
	  */

	private static boolean judgeFirst(String s)
	{
		if (Character.isDigit(s.charAt(0)) || Character.isLetter(s.charAt(0)) || 
				(s.charAt(0) == '-') || (s.charAt(0) == '('))
		{
			return true;
		}
		else 
		{
			System.out.println("ERROR : Check your FIRST character!");
			errorPrint(s,0);
			return false;
		}
	}

	/**method judgeParentheses
	  *if the parentheses are mathched
	  *return true
	  *else return false
	  */
	private static boolean judgeParentheses(String s)
	{
		int judge = 0;
		int i = 0;
		int pos = 0;

		while (i < s.length())
		{
			if (s.charAt(i) == '(')
			{
				if (s.charAt(i + 1) == ')')
				{
					System.out.println("ERROR : You can input a EMPTY parenthese!");
					errorPrint(s,i);
					return false;
				}
				judge++;
			}
			else if (s.charAt(i) == ')')
			{
				pos = i;
				judge--;
				if (judge < 0)
				{
					System.out.println("ERROR : Please check your parenthese!");
					errorPrint(s,i);

⌨️ 快捷键说明

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