![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
It does, doesn't it? But then, you don't get Blackadder, so we're even.
|
|
|
|
|
|
#22 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
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. |
|
|
|
|
|
#23 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
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. |
|
|
|
|
|
#24 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 573
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#25 |
|
Programming Guru
![]() ![]() |
Heh, that would be to simple though.
__________________
Profanity is the one language that all programmers understand. Check out my Blog <---updated Nov 30 2007! |
|
|
|
|
|
#26 |
|
Programming Guru
![]() |
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;
}
__________________
|
|
|
|
|
|
#27 |
|
Programming Guru
![]() |
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 |
|
|
|
|
|
#28 | |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 573
Rep Power: 5
![]() |
Quote:
![]()
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4 |
|
|
|
|
|
|
#29 |
|
Programming Guru
![]() |
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.
__________________
|
|
|
|
|
|
#30 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
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:ph34r:
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|