View Single Post
Old Oct 31st, 2004, 1:59 AM   #30
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
what the hell...it's halloween (technically) and beer time.

this has the four functions plus a reset and square and square root and power of and whatever else i might have forgotten while under the influence.

note: if the comments seem gay, it's b/c i did all of this while i was about three sheets to the wind. i say dumb shit when intoxicated. but it works.

believe floating-point input disgrees with "power-of" function due to shitty f.p. math rules with the computer.

/*i have tried to provide adequate documentation for this program,
 *believe me i need it more than you!
 *program FFC.(Four-Function Calculator)
 *this has expanded way beyond the four functions...
 *originally a big goal for me to do this,
 *because i'm just learning java (programming in general) 
 *i have books on "C" and "C++" and the general lack of order scares me!!!
 *but then again, i am a programming bitch.
 *jcreator ROCKS for a free download (download.com)
 *v2.1 (09/27/04)
 *2004 benjamin hancock.
 *four-function calculator simulation.
 *has expanded into scientific calculator functions.
 *the 'clear' and 'power-of' functions proved difficult.
 *greatly improved over previous versions.
 *new features include:
 *constant variable change for multiple calculations.
 *improved user interface that includes
 *choosing actual math operators
 *OR a number.
 *in the future looking to apply a GUI.
 *oh yeah, and this stupid comment line.
 *bitch.
 *(09/29/04)expanded to include:
 *additional math functions,
 *and other hoity-toity shit
 *including my illustrious "clear" method
 *bitch.
 *(09/30/04)"fixed" the "power of" function
 *except operates weirdly when power is a floating-point input
 */
 

//import the java input/output library
import java.io.*;


//class that contains the math functions
class MFunctions {
	
	//result variable
	double result;
	
	String inData;
	
	//constructor...not much here...
	//remnant from the days when the user input was supplied to 
	//this class as an argument
	//MFunctions() {
	//}
	
	//addition method
	double add(double v1, double v2) {
 
 result = (v1 + v2);
 return result;
	}
	
	
	//subtraction method
	double subtract(double v1, double v2) {
 
 result = (v1 - v2);
 return result;
	}
	
	
	//multiplication method
	double multiply(double v1, double v2) {
 
 result = (v1 * v2);
 return result;
	}
	
	
	//division method
	double divide(double v1, double v2) {
 
 result = (v1 / v2);
 return result;
	}
	
	
	//clear method
	double clear(double v1) {
 
 result = v1;
 return result;
	}
	
	
	//square method
	double square(double v1) {
 
 result = (v1 * v1);
 return result;
	}
	
	
	//square root method
	double sqroot(double v1) {
 
 result = Math.sqrt(v1);
 return result; 
	}
	
	
	//"power of" method
	//doesn't quite work right when power is a floating-point input
	double power(double v1, double v2, double v3) {
 
 int i = 1;
 
 //get initial input
 //v3 remains as the initial input
 //so that v1 can be accurately calculated
 //increment loop until power is reached
   while (i < v2) {
  v1 = (v1 * v3);
  i++;
  
   }//while loop ends here
 
 result = v1;
 
 return result;
 
  }//"power-of" method ends here
	
	
}//MFunctions class ends here






//class containing the "main" method	
class FFC {
	
	public static void main(String[] args) throws IOException {
  
  //generic input variable	
  String inData, userChoice;
  
  //user-defined input variables
  double initialV, nextV;
  
  double x = 0;
  
  //boolean variable for quitting loop
  boolean sentV = false;
  
  //initiate java IO library
  InputStreamReader isr = new InputStreamReader(System.in);
   BufferedReader br = new BufferedReader(isr);
  
  //instantiate object of "MFunctions" class
  MFunctions mf = new MFunctions();
	
  //initial user input
   System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
   System.out.println("Welcome to the currently expanding calculator simulation!");
   System.out.print("When prompted for a math function choice ");
   System.out.print("please type either: \nthe number of your choice, ");
   System.out.println("\nor the symbol beside the number.");
   System.out.println("\nIf you receive a result of either: ");
   System.out.println("'NaN' or 'infinity'");
   System.out.println("OR repeated calculations reach the UNEXPECTED result of either '1' or '0'");
   System.out.println("Your calculations have exceeded the bounds of the java language.");	
 
   System.out.println("\n\nEnter a number.\n\n\n\n\n\n\n\n\n\n\n");
   inData = br.readLine();
   initialV = Double.parseDouble(inData);
    
   //quitting loop 
   while (sentV != true) {
    
    //determine the math function from the user 
    System.out.println("\n\n\nChoose a basic math function.\n");
    System.out.println("1. +\t(addition)");
    System.out.println("2. -\t(subtraction)");
    System.out.println("3. *\t(multiplication)");
    System.out.println("4. /\t(division)");
    System.out.println("5. c\t(clear)");
    System.out.println("6. =\t(equals/finished)");
    System.out.println("7. s\t(square)");
    System.out.println("8. sq\t(square root)");
    System.out.println("9. pw\t(power of)");
    System.out.println("\n\n\n\n\n");
    userChoice = br.readLine();
    
    
    //if user chooses to add...
    if (userChoice.trim().startsWith("+") || userChoice.trim().startsWith("1") ) {
   	
     //get next number from user
     System.out.println("\n\n" + initialV + " Plus?\n\n");
     inData = br.readLine();
     nextV = Double.parseDouble(inData);     
     //print current value and assign that value to initial value
     //this is repeated in the following...
     System.out.println("\n\n\n\n\n\n\n\n\n\n");
     System.out.println("Current value is: " + mf.add(initialV, nextV) + "\n");
     initialV = mf.add(initialV, nextV);
   	
    }//end user add choice
   
    
    //if user chooses to subtract
    if (userChoice.trim().startsWith("-") || userChoice.trim().startsWith("2") ) {
   	
     System.out.println("\n\n" + initialV + " Minus?\n\n");
     inData = br.readLine();
     nextV = Double.parseDouble(inData);   
     //see above comment in add choice
     System.out.println("\n\n\n\n\n\n\n\n\n\n");
     System.out.println("\n\nCurrent value is: " + mf.subtract(initialV, nextV) + "\n");
     initialV = mf.subtract(initialV, nextV);
   	
    }//end user subtract choice
   
   
    //if user chooses to multiply
    if (userChoice.trim().startsWith("*") || userChoice.trim().startsWith("3") ) {
   	
     System.out.println("\n\n" + initialV + " Multiplied by?\n\n");
     inData = br.readLine();
     nextV = Double.parseDouble(inData);   	
     //see above comment in add choice
     System.out.println("\n\n\n\n\n\n\n\n\n\n");
     System.out.println("\n\nCurrent value is: " + mf.multiply(initialV, nextV) + "\n");
     initialV = mf.multiply(initialV, nextV);
   	
    }//end user multiplication choice
   
    
    //if user chooses to divide
    if (userChoice.trim().startsWith("/") || userChoice.trim().startsWith("4") ) {
   	
     System.out.println("\n\n" + initialV + " Divided by?\n\n");
     inData = br.readLine();
     nextV = Double.parseDouble(inData);
     //see above comment in add choice
     System.out.println("\n\n\n\n\n\n\n\n\n\n");
     System.out.println("\n\nCurrent value is: " + mf.divide(initialV, nextV) + "\n");
     initialV = mf.divide(initialV, nextV);
   	
    }//end user division choice   
  
    //if user chooses to clear
    if (userChoice.trim().startsWith("c") || userChoice.trim().startsWith("5") ) {
    	
    	System.out.println("\n\n\n\n\n\n\n\n\n");
    	System.out.println("The new initial value is zero.\n\n");
    	System.out.println("Enter the new initial value.");
    	System.out.println("\n\n\n\n\n");
    	inData = br.readLine();
    	initialV = Double.parseDouble(inData);
    	System.out.println("\n\n\n\n\n\n\n\n\n");
    	System.out.println("\n\nCurrent value is: " + mf.clear(initialV) + "\n");
    	initialV = mf.clear(initialV);
    	
    }//end user clear choice
    
    
     //if user chooses square
   	if (userChoice.trim().startsWith("s") || userChoice.trim().startsWith("7") ) {
    System.out.println("\n\n\n\n\n\n\n\n\n\n");
    System.out.println("\n\nCurrent value is: " + mf.square(initialV) + "\n");
    initialV = mf.square(initialV);
    
   	}//end user square choice  
   	
   	
   	//if user chooses square root
   	if (userChoice.trim().startsWith("sr") || userChoice.trim().startsWith("8") ) {
    System.out.println("\n\n\n\n\n\n\n\n\n\n");
    System.out.println("\n\nCurrent value is: " + mf.sqroot(initialV) + "\n");
    initialV = mf.sqroot(initialV);
    
   	}//end user square root choice 
    
    
    //if user chooses "power-of"
    
    final double thirdV = initialV;
    
    if (userChoice.trim().startsWith("pw") || userChoice.trim().startsWith("9") ) {
      System.out.println(initialV + "\n\nTo the power of?");
      inData = br.readLine();
      nextV = Double.parseDouble(inData);
      System.out.println("\n\n\n\n\n\n\n\n\n\n");
      System.out.println("\n\nCurrent value is: " + mf.power(initialV, nextV, thirdV) + "\n");
      initialV = mf.power(initialV, nextV, thirdV);
      
    }//end user "power-of" choice
      
    
  
    //if user chooses to quit
    if (userChoice.trim().startsWith("=") || userChoice.trim().startsWith("6") ) {
   	
     sentV = true; 
     
    }//end user final quitting choice
       	
   	
  }//while loop ends here
   
   
   //final value printed on-screen for user	
   System.out.println("\n\n\n\n\n\n\n");	
   System.out.println("The final result is:\t" + initialV);
   System.out.println("\n\n\n\n\n\n\n");	
     
   
	
  }//main ends here
  
  
}//class ends here
:rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock:

:ph34r:
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote