纵有疾风起
人生不言弃

源码

import java.util.Scanner;
public class homework
{
public static void main(String[] args)
{
	
/*
 * name must be printed out at the beginning of your source code
 * */
//introduction of the program and  my mane
   printIntro();

 
 Scanner kb = new Scanner(System.in);//create the scanner to get the input 

String answer = "yes";//set the default value of answer

while ("yes".equals(answer))
{

	
	System.out.println("Enter the list of the operands and the opration:");

   //get the input from the use
	String input = kb.nextLine();
	
  // separate the tokens and store them in variables
    
       char op1 = ' ';
       char op2 = ' ';
       String operation ="";
	   int flag = 0;
	
	   for(int i = 0; i < input.length();i++)
	   {
	
		   if(input.charAt(i)!= ' ')
		   {
			
			   if(flag == 0)
			   {
				   op1 = input.charAt(i);
			   }else if(flag ==1)
			   {
				   op2 = input.charAt(i);
			   }else if(flag ==2)
			   {
				   operation = input.substring(i);
			   }
			   
			   flag++;
		   }
		 
		   
	   }
	     
  
	//(3)call different methods to carry out the task

	   printNumeric(op1,op2,operation);
	   printEnglish(op1,op2,operation);
	
	   

	//(4)get the choice of the user
	System.out.print("Do you have another expression?");
	answer = kb.nextLine();
	
	
}

//when the option the user inputed is not "yes" such as "no" or other words print  this sentence
System.out.println("Have a good day!! BYE");

}





/*this methods gets the operands and the operation and
prints the English version: if we call this method
printEnglish(2,3, plus), then this method will output:
Two plus three = 5 */
public static void printEnglish(char op1, char op2,String operation)
{
	/*1. call the method charToString(op1) to convertlish
	word for example ‘1’ to one or ‘2’ to two,….*/
	String str_num1 = charToString(op1);
	String str_num2 = charToString(op2);
	
	
	
	/*
	2. call the method operandConversionToNumber(op1) to
	get its numeric value. For example if op1 is ‘1’ then
	this method call should return the integer value 1*/
	
	int num1 = operandConversiontoNumber(op1) ;
	int num2 = operandConversiontoNumber(op2) ;
	
	/*
	3. call the method operationConversion(operation) to
	convert the operation to a mathematical operation. For
	example if you call this method with the string plus
	then it will return ‘+’*/
	char mathematical_operation =operationConversion(operation);
	

			/*4. finally call the method calculate to get the result
			of the operation.*/
			double result = calculate(num1,num2, mathematical_operation);
			 if(ZERO ==  result && operation.equals("divide")||mathematical_operation==' ')
			 {
				//do nothing because the information has already printed when the method of printEnglish(char op1, char op2,String operation)was called
			 }else if(mathematical_operation=='^')
			{
				
				System.out.println(num1+" to the power of "+num2 +" = "+result );
			}else
		    {  
				//if the operation id not divide the result should be an integer 
				if(!operation.equals("divide"))
				  {
					  System.out.println(str_num1+" "+operation+" "+str_num2+" = "+(int)result);
				  }else
				   {
				    System.out.println(str_num1+" "+operation+" "+str_num2+" = "+result);
				   }
			 }
	


}



/*this method prints the numeric version which is 2*3 =6*/
public static boolean printNumeric(char op1, char op2, String operation)
{

	
//body of this method is similar to the previous 
	//method
	int num1 = operandConversiontoNumber(op1);
	int num2 = operandConversiontoNumber(op2);
	
	char mathematical_operation =operationConversion(operation);
	if(mathematical_operation!=' ')//to make sure that the operation  has exist
	{
			 double result = calculate(num1,num2, mathematical_operation);
				
			 if(ZERO ==  result && operation.equals("divide"))
				{
					 System.out.println("Invalid opernad, cannot divide by zero");
				}else
			 {
					//if the operation id not divide the result should be an integer 
				 if(!operation.equals("divide"))
				 {
					 System.out.println(num1+" "+mathematical_operation+" "+num2 +" = "+(int)result );
				 }else
				 System.out.println(num1+" "+mathematical_operation+" "+num2 +" = "+result );
				 
			 }


	}else//if the operation is not exist , print this sentence
	{
	   System.out.println("Invalid operation, "+operation+" does not exist");	
	}

	return true;
}





/*this method gets a number as a character and returns
its numeric value as an integer. You must use case
statement for this method*/


public static int operandConversiontoNumber(char operand)
{
	int numeric_value = 0;
	switch(operand)
	{
	
    	case '0': numeric_value = 0; break;
    	case '1': numeric_value = 1; break;
    	case '2': numeric_value = 2; break;
    	case '3': numeric_value = 3; break;
    	case '4': numeric_value = 4; break;
    	case '5': numeric_value = 5; break;
    	case '6': numeric_value = 6; break;
    	case '7': numeric_value = 7; break;
    	case '8': numeric_value = 8; break;
    	case '9': numeric_value = 9; break;
	
		
	}
	return numeric_value;

}



/*this method gets the operation as a string and
return the equivalent operation in math. For example
if it receives “plus” the it will return ‘+’ */
public static char operationConversion(String s)
{
	char operation = ' ';
	
	if(s.equals("plus"))
	{
		operation = '+';
	}else if(s.equals("minus"))
	{
		operation = '-';
	}else if(s.equals("multiply"))
	{
		operation = '*';
	}else if(s.equals("divide"))
	{
		operation = '/';
	}else if(s.equals("power"))
	{
		operation = '^';
	}
	
	return  operation;
	
}
/*this method receives two numbers and the operation and returns the result*/
public static double calculate(int operand1, int operand2, char operation)
{
	double result = 0.0;
	switch(operation)
	{
	case '+' :  result = operand1 + operand2; break;
	case '-' :  result = operand1 - operand2; break;
	case '*' :  result = operand1 * operand2; break;
	case '/' :  
		if(operand2==0)
		{
			result = ZERO;
		}else
		{
		result = (double)operand1 / operand2;}
		break;
	case '^' :  result = Math.pow(operand1, operand2); break;
	
	}
	
	/*if(operation=='+')
	{
		
		 result = operand1 + operand2;
	}else if(operation=='-')
	{
		 result = operand1 - operand2;
	}else if(operation=='*')
	{
		 result = operand1 * operand2;
		
	}else if(operation=='/')
	{
		 result = operand1 / operand2;
	}else if(operation=='^')
	{
		 result = Math.pow(operand1, operand2);
	}*/
	
	return result ;
	
}
/*this method converts a number character to its 
 * English word for example if this method receives ‘1’
  it will return “one” */
public static String charToString(char num)
{
	String word = "";
	
	
	switch(num)
	{
	case '0': word = "zero";break;
	case '1': word = "one";break;
	case '2': word = "two";break;
	case '3': word = "three";break;
	case '4': word = "four";break;
	case '5': word = "five";break;
	case '6': word = "six";break;
	case '7': word = "seven";break;
	case '8': word = "eight";break;
	case '9': word = "nine";break;
	
	}
	
	return word;
	
	
}
/*this method prints the description of this program.*/
public static void printIntro()
{
	System.out.println("My name is ...,This program ...");
}
//when op2 is 0 and operation is divide return this number 
public static final double ZERO = -65535;
}

原文链接:https://blog.csdn.net/w605283073/article/details/8749467

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

未经允许不得转载:起风网 » 源码
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录