Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 22nd, 2004, 1:39 PM   #21
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8 Ooble is on a distinguished road
It does, doesn't it? But then, you don't get Blackadder, so we're even.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Oct 25th, 2004, 1:33 AM   #22
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
the best part about this is that there's already full code examples for a 4-function calculator in multiple languages, including Java, on THIS site.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Oct 28th, 2004, 3:10 AM   #23
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
writing a simple calculator in java can be a great learning excercise b/c you can code it procedurally or make it OO as hell. it was the first real program i wrote where i implemented multiple classes, etc. fun too.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Oct 28th, 2004, 6:52 AM   #24
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 548
Rep Power: 4 Benoit is on a distinguished road
You can just go to the store and buy a calculator and you wouldn't need a PC to use it
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4
Benoit is offline   Reply With Quote
Old Oct 28th, 2004, 8:43 AM   #25
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
Heh, that would be to simple though.
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Old Oct 28th, 2004, 3:02 PM   #26
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
My base price is $200 and $50 an hour. And im one slow ass mother fucker.. itd be more economically efficent to turn in one written in JavaScript... which i'll provide below..

function calc(str) {
 * *eval("var blah = " + str);
 * *return blah;
}
__________________

tempest is offline   Reply With Quote
Old Oct 29th, 2004, 3:32 AM   #27
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 5 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
goto run and type calc woo a calculator.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old Oct 29th, 2004, 7:55 AM   #28
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 548
Rep Power: 4 Benoit is on a distinguished road
Quote:
Originally posted by Pizentios@Oct 28 2004, 01:43 PM
Heh, that would be to simple though.
Why do you have to make things to complicated?
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4
Benoit is offline   Reply With Quote
Old Oct 30th, 2004, 12:49 AM   #29
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
Edit: My apologies for that post, too late now, but i apologize none the less. I was in a really bad mood and shouldnt have taken it out on members of this forum.
__________________

tempest is offline   Reply With Quote
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
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:36 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC