?? mcalc.java
字號:
break;
case 8:
int matchMulti = myStack.search( "[" );
if ( matchMulti == -1 )
{
if ( myStack.size() >=2 )
{
Object number1 = myStack.pop();
Object number2 = myStack.pop();
if ( number1.getClass() == Double.class )
{
double num1 = ((Double)number1).doubleValue();
if ( number2.getClass() == Double.class )
{
double num2 = ((Double)number2).doubleValue();
myStack.push( Matrix.multi( num2,num1));
}
else if( number2.getClass() == Matrix.class )
{
Matrix num2 = ( Matrix )number2;
myStack.push( Matrix.multi( num2,num1 ));
}
}
else
{
Matrix num1 = ( Matrix )number1;
if ( number2.getClass() == Double.class )
{
double num2 = ((Double)number2).doubleValue();
myStack.push( Matrix.multi( num2,num1));
}
else if( number2.getClass() == Matrix.class )
{
Matrix num2 = ( Matrix )number2;
Object result = Matrix.multi( num2,num1 );
if ( result != null )
{
myStack.push( result );
}
}
}
}
else
{
//運算數字個數不足。
System.out.println(
"Error: There aren't enough operands for multiplication!" );
}
}
else
{
System.out.println(
"Error: Unable to do the multiplication inside the matrix!" );
}
break;
case 9:
int matchUnd = myStack.search( "[" );
if ( matchUnd == -1 )
{
if ( myStack.size() >= 1 )
{
Object temp = myStack.pop();
if ( temp.getClass() == Double.class )
{
myStack.push( Matrix.negative(((Double)temp).doubleValue()));
}
else
{
myStack.push( Matrix.negative((Matrix)temp) );
}
}
else
{
//運算數字個數不足。
System.out.println( "Error: The stack is empty!" );
}
}
else
{
System.out.println(
"Unable to do the multiplication inside the matrix!" );
}
break;
case 10:
int matchTra = myStack.search( "[" );
if ( matchTra == -1 )
{
if ( myStack.size() >= 1)
{
Object temp = myStack.pop();
if ( temp.getClass() == Double.class )
{
myStack.push( temp );
}
else
{
temp = Matrix.transpose((Matrix)temp);
myStack.push( temp );
}
}
else
{
//運算數字個數不足。
System.out.println( "Error: The stack is empty!" );
}
}
else
{
System.out.println(
"Error: Unable to do the operation inside the matrix!" );
}
break;
case 11:
//數字給相反數,矩陣給逆陣。
int matchInv = myStack.search( "[" );
if ( matchInv == -1 )
{
if ( myStack.size() > 0 )
{
Object temp = myStack.pop();
if ( temp.getClass() == Double.class )
{
double number = ((Double)temp).doubleValue();
if ( number == 1.0 || number == 0.0 )
{
System.out.println(
"Error: The number doesn't have an inverse number." );
}
else
{
myStack.push( Matrix.inverse( number ));
}
}
else
{
if ( Matrix.hasInvMatrix( (Matrix)temp ) )
{
myStack.push( Matrix.inverse((Matrix)temp) );
}
else
{
myStack.push( temp );
System.out.println( "Error: The matrix hasn't an inverse matrix!" );
}
}
}
else
{
//運算數字個數不足。
System.out.println( "Error: The stack is empty!" );
}
}
else
{
System.out.println(
"Error: Unable to do the operation inside the matrix!" );
}
break;
case 12:
String variableTemp = variable;
char first;
boolean judge = true;
for ( int i = 0; i < variableTemp.length(); i++ )
{
first = variableTemp.charAt( i );
if ( !Character.isLetterOrDigit( first ) && first != '_' )
{
System.out.println( "Error: The variable's name is invalid!" );
judge = false;
break;
}
}
if ( judge )
{
if ( myStack.size() > 0 )
{
hashtable.put( variable,myStack.pop() );
Object temp = hashtable.get( variable );
}
else
{
//空棧報錯;
System.out.println( "Error: There is no element in the stack!" );
}
}
break;
case 13:
Object temp = hashtable.get( variable );
if ( temp != null )
{
myStack.push( temp );
}
else
{
//報錯,沒有這個變量。
System.out.println( "Error: No such variable!" );
}
break;
case 14:
Object tempValue;
if ( myStack.size() == 0 )
{
System.out.println( "Error: The stack is empty!" );
}
else
{
tempValue = myStack.peek();
myStack.push( tempValue );
}
break;
case 15:
if ( myStack.size() < 2)
{
System.out.println(
"Error: Less than two elements. Unable to do the exchange!" );
}
else
{
Object temp1,temp2;
temp1 = myStack.pop();
temp2 = myStack.pop();
myStack.push( temp1 );
myStack.push( temp2 );
}
break;
case 16:
if ( myStack.size() > 0 )
{
myStack.pop();
}
else
{
System.out.println( "Error: The stack is empty!" );
}
break;
case 17:
System.exit( 0 );
break;
case 18:
System.out.println( "The help for this software:" );
System.out.println( "Please input the commands seperated by blanks.\nThe case of letters would be ignored." );
System.out.println( "The possible commands are as follows:" );
System.out.println( "#: It marks a comment.Everything to the end of the line is ignored." );
System.out.println( "N: Read N from the user. N should be a floating-number." );
System.out.println( "[: Marks the beginning of a matrix or a vector." );
System.out.println( "]: Create a vector or matrix out of what's on the stack." );
System.out.println( "The resulting matrix (if valid) is pushed onto the stack." );
System.out.println( "id N:When N is a positive integer, pushes the N*N identity matrix on the stack." );
System.out.println( "=: Show you the top element of the stack." );
System.out.println( "+ - /:pop the top two items on the stack, perform the indicated arithmetic");
System.out.println( "operation (with the top item of the stack on the right of the operator), " );
System.out.println( "and push the result back on the stack." );
System.out.println( "Addition and subtraction are defined between two numbers or two matrices " );
System.out.println( "of the same dimensions. Division is defined only on numbers. " );
System.out.println( "*: pops the top two items on the stack and multiplies them (with " );
System.out.println( "the top item being the right operand), pushing the result back on the " );
System.out.println( "stack. Multiplication is defined between two numbers, between a number " );
System.out.println( "and a matrix, and between two conforming matrices (i.e., an m * k matrix " );
System.out.println( "on the left and a k * n matrix on the right, yielding an m * n matrix). If the " );
System.out.println( "result is a 1 * 1 matrix, it is treated as a scalar (thus giving dot product)." );
System.out.println( "_: It negates the top element of the stack (same as multiplying by -1)." );
System.out.println( "t: Pop the top element of the stack and pushes its transpose. " );
System.out.println( "inv: Pop the top element of the stack, which must be a square, non-singular " );
System.out.println( "matrix, and pushes its inverse." );
System.out.println( "=: V:Pop the top of the stack and save it in variable V. V is any string " );
System.out.println( "of letters, digits, and underscores that does not start with a digit." );
System.out.println( ":= V:Push the value last stored in variable V on the stack." );
System.out.println( "dup/d:Push a copy of the top of the stack to the stack." );
System.out.println( "exch/x:Exchang the top two items on the stack." );
System.out.println( "pop: Pop the top item of the stack." );
System.out.println( "quit/q: Exit the program." );
System.out.println( "?/help: Print a help message summarizing these commands." );
System.out.println( "Look for more information in the user's handbook..." );
break;
case 19:
System.out.println( "Error: Invalid input! " );
break;
default:
throw new RuntimeException( "Error: Command Not Found! ");
}
}
/**
* The main method for the class.
* It reads the input from the user. Parse it into tokens and
* choose the corresponding mathod to solve the problem.
*
* @exception Throws input/output exception.
*/
public static void main( String[] args ) throws java.io.IOException
{
/** Read the input from the user.*/
BufferedReader bufferedReader =
new BufferedReader( new java.io.InputStreamReader( System.in ) );
/** Parse it by the class streamTokenizer.*/
StreamTokenizer st;
/** Parse it by the class stringTokenizer.*/
StringTokenizer stringTokenizer;
/** The object of the class Mcalc.*/
Mcalc myMcalc = new Mcalc();
System.out.println( "Mcalc, version 1.0" );
System.out.println( "Author: Rachel Chen 0122070" );
while( true )
{
try
{
System.out.print( "> " );
//統一轉換為小寫。
String readLine = bufferedReader.readLine().toLowerCase();
//使用下面的'\\'替換'/',以避免無法識別的問題。
readLine = readLine.replace('/', '\\');
readLine = readLine.replace('/', '\\');
readLine = readLine.replaceAll( "=:", "save" );
readLine = readLine.replaceAll( ":=", "load" );
StringReader readExpression = new StringReader( readLine );
st = new StreamTokenizer( readExpression );
int nextToken = st.nextToken();
//Count the total number of the tokens.
stringTokenizer = new StringTokenizer( readLine );
int count = stringTokenizer.countTokens();
loop:
{
//依次選擇對應的命令,執行相關函數。
for ( int i = 0; i < count; i++)
{
switch( nextToken )
{
case StreamTokenizer.TT_NUMBER:
myMcalc.readCommand(0, st.nval, null);
break;
case StreamTokenizer.TT_WORD:
String cStr0 = "" + st.sval;
if (cStr0.equals("id"))
{
st.nextToken();
i++;
myMcalc.readCommand(3, st.nval, "id " + (int)st.nval);
}
else if (cStr0.equals("t"))
{
myMcalc.readCommand(10, 0.0, "t");
}
else if (cStr0.equals("inv"))
{
myMcalc.readCommand(11, 0.0, "inv");
}
else if (cStr0.equals("save"))
{
int test = st.nextToken();
if ( test != StreamTokenizer.TT_WORD && st.ttype != 95 )
{
System.out.println(
"Error: The variable's name is invalid!" );
}
else
{
if ( st.ttype == 95 ) //95 stands for '_'
{
st.nextToken();
i++;
myMcalc.readCommand(12, 0.0, "_" + st.sval);
}
else
{
i++;
myMcalc.readCommand(12, 0.0, st.sval);
}
}
}
else if (cStr0.equals("load"))
{
int test = st.nextToken();
if ( test != StreamTokenizer.TT_WORD && st.ttype != 95 )
{
System.out.println(
"Error: The variable's name is invalid!" );
}
else
{
if ( st.ttype == 95 )
{
st.nextToken();
i++;
myMcalc.readCommand(13, 0.0, "_" + st.sval);
}
else
{
i++;
myMcalc.readCommand(13, 0.0, st.sval);
}
}
}
else if (cStr0.equals("d") || cStr0.equals("dup"))
{
myMcalc.readCommand(14, 0.0, "dup");
}
else if (cStr0.equals("x") || cStr0.equals("exch"))
{
myMcalc.readCommand(15, 0.0, "exch");
}
else if (cStr0.equals("pop"))
{
myMcalc.readCommand(16, 0.0, "pop");
}
else if (cStr0.equals("help"))
{
myMcalc.readCommand(18, 0.0, "help");
}
else if (cStr0.equals("q") || cStr0.equals("quit"))
{
myMcalc.readCommand(17, 0.0, "quit");
}
else
{
myMcalc.readCommand(19, 0.0, null);
}
break;
default:
char char0 = (char)st.ttype;
String cStr = "" + char0;
if (cStr.equals("["))
{
myMcalc.readCommand(1, 0.0, "[");
}
else if (cStr.equals("]"))
{
myMcalc.readCommand(2, 0.0, "]");
}
else if (cStr.equals("="))
{
myMcalc.readCommand(4, 0.0, "=");
}
else if (cStr.equals("+"))
{
myMcalc.readCommand(5, 0.0, "+");
}
else if (cStr.equals("-"))
{
myMcalc.readCommand(6, 0.0, "-");
}
else if (cStr.equals("\\"))
{
myMcalc.readCommand(7, 0.0, "/");
}
else if (cStr.equals("*"))
{
myMcalc.readCommand(8, 0.0, "*");
}
else if (cStr.equals("_"))
{
myMcalc.readCommand(9, 0.0, "_");
}
else if (cStr.equals("?"))
{
myMcalc.readCommand(18, 0.0, "help");
}
else if (cStr.indexOf('#') == 0)
{
//對以后的所有字符不作處理。
break loop;
}
else
{
myMcalc.readCommand(19, 0.0, null);
}
break;
}
nextToken = st.nextToken();
}
}
}
catch ( Exception e )
{
System.out.println("ERROR: " + e.getLocalizedMessage());
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -