View previous topic :: View next topic |
Author |
Message |
Hieroglyphics I post too much Reputation: 0
Joined: 06 Dec 2007 Posts: 2007 Location: Your bedroom
|
Posted: Sun Nov 23, 2008 4:24 pm Post subject: C++ Calculator | Difficulty: MIND CRUSHINGLY AVERAGE |
|
|
Here it is 100% me:
Code: | #include <cstdlib>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <string>
#define PI 3.14159265
using namespace std;
int main()
{
int first, second, answer, circumference, area, perimeter, third, aplusb, bOut, twosides, total;
double pathagorus, peremeter;
char menu;
for (;;){
do {
cout << "Welcome to Hiero Calc!\n";
cout << "by Hieroglyphics\n\n";
cout << "Please press one of the following on your keyboard:\n";
cout << "1 - Addition\n";
cout << "2 - Subtraction\n";
cout << "3 - Division\n";
cout << "4 - Multiplication\n";
cout << "5 - Circles\n";
cout << "6 - Squares\n";
cout << "7 - Rectangles\n";
cout << "8 - Triangles\n";
cout << "9 - Trapezoid\n";
cin >> menu;
}
while ( menu < '1' || menu > '9');
switch (menu) {
case '1':
cout << "Enter the first number you would like to add\n";
cin >> first;
cout << "Enter the second number you would like to add\n";
cin >> second;
answer = first + second;
cout << first << "+" << second << "=" << answer;
cout << "\n";
break;
case '2':
cout << "Please enter a number to subtract from\n";
cin >> first;
cout << "Another number to be subtracted\n";
cin >> second;
answer = first - second;
cout << first << "-" << second << "=" << answer;
cout << "\n";
break;
case '3':
cout << "Please enter a number to divide from\n";
cin >> first;
cout << "Please enter the second number to be divided\n";
cin >> second;
answer = first / second;
cout << first << "/" << second << "=" << answer;
cout << "\n";
break;
case '4':
cout << "Please enter a number to multiply\n";
cin >> first;
cout << "Please enter the second number to be multiplied\n";
cin >> second;
answer = first * second;
cout << first << "*" << second << "=" << answer;
cout << "\n";
break;
case '5':
cout << "Enter Radius\n";
cin >> first;
circumference = 2 * first * PI;
area = first * first * PI;
cout << "Circumference = " << circumference << ".\n";
cout << "Area = " << area << ".\n";
break;
case '6':
cout << "Enter the length of one side\n";
cin >> first;
area = first * first;
perimeter = first * 4;
cout << "Perimeter = " << perimeter << ".\n";
cout << "Area = " << area << ".\n";
break;
case '7':
cout << "Enter the length of one side\n";
cin >> first;
cout << "Enter the length of the second side\n";
cin >> second;
area = first * second;
perimeter = (first * 2) + (second * 2);
cout << "Perimeter = " << perimeter << ".\n";
cout << "Area = " << area << ".\n";
break;
case '8':
cout << "Enter the height\n";
cin >> first;
cout << "Enter the base\n";
cin >> second;
area = (first + second)/2;
pathagorus = (second*second)+(first*first);
peremeter = sqrt( pathagorus );
cout << "Perimeter = " << peremeter << ".\n";
cout << "Area = " << area << ".\n";
break;
case '9':
cout << "Enter the length of the top\n";
cin >> first;
cout << "Enter the length of the bottom\n";
cin >> second;
cout << "Enter the height\n";
cin >> third;
aplusb = (first + second)/2;
area = third*aplusb;
perimeter = first + second;
bOut = (second-first)/2;
pathagorus = (bOut*bOut)+(third*third);
peremeter = sqrt( pathagorus );
twosides = 2*peremeter;
total = perimeter+twosides;
cout << "Perimeter = " << total << ".\n";
cout << "Area = " << area << ".\n";
break;
}
}
return 0;
} |
_________________
|
|
Back to top |
|
|
kitterz Grandmaster Cheater Supreme Reputation: 0
Joined: 24 Dec 2007 Posts: 1268
|
Posted: Sun Nov 23, 2008 4:32 pm Post subject: |
|
|
hmmm why not make a real calculator?
one that you put as input, say
"1+1" ?
_________________
|
|
Back to top |
|
|
Hieroglyphics I post too much Reputation: 0
Joined: 06 Dec 2007 Posts: 2007 Location: Your bedroom
|
Posted: Sun Nov 23, 2008 4:38 pm Post subject: |
|
|
well its pretty much the same, if you noticed you can input 1+1 and it will say the answer
_________________
|
|
Back to top |
|
|
Spawnfestis GO Moderator Reputation: 0
Joined: 02 Nov 2007 Posts: 1746 Location: Pakistan
|
Posted: Sun Nov 23, 2008 4:44 pm Post subject: |
|
|
Well glad to know I can now delete this from my own forum.
_________________
CLICK TO HAX MAPLESTORAY ^ !!!! |
|
Back to top |
|
|
BirdsEye Advanced Cheater Reputation: 0
Joined: 05 Apr 2008 Posts: 94
|
Posted: Sun Nov 23, 2008 6:06 pm Post subject: |
|
|
Good job. I'm soon going to release a Win32 Calculator which basically is an imitation of the default 'Calculator.' I just have to polish a few things up.
|
|
Back to top |
|
|
hcavolsdsadgadsg I'm a spammer Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sun Nov 23, 2008 6:52 pm Post subject: |
|
|
This has left me truly horrified to see what you think is advanced.
|
|
Back to top |
|
|
Overload Master Cheater Reputation: 0
Joined: 08 Feb 2008 Posts: 293
|
Posted: Sun Nov 23, 2008 7:45 pm Post subject: |
|
|
slovach wrote: | This has left me truly horrified to see what you think is advanced. |
That's exactly what I thought.
_________________
Blog
Quote: | Rhys says:
you can be my maid
Rhys says:
ill buy you a french maid outfit
Tyler says:
Sounds good
Rhys says:
ill hold you to that |
|
|
Back to top |
|
|
nog_lorp Grandmaster Cheater Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Mon Nov 24, 2008 7:24 am Post subject: |
|
|
Implement a reverse polish notation calculator.
http://en.wikipedia.org/wiki/Reverse_polish_notation
Also, implement arbitrary precision arithmetic.
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
Back to top |
|
|
Niels8500 Grandmaster Cheater Reputation: 0
Joined: 19 Sep 2007 Posts: 859 Location: The Netherlands
|
Posted: Mon Nov 24, 2008 7:28 am Post subject: |
|
|
1 Error
|
|
Back to top |
|
|
samuri25404 Grandmaster Cheater Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Mon Nov 24, 2008 2:23 pm Post subject: |
|
|
Code: |
#define ISDIGIT( x ) ( x >= '0' && x <= '9' )
#define CHARTONUM( x ) ( x - '0' )
#define ISOPERAND( x ) ( x == '+' || x == '-' || x == '*' || x == '/' )
bool parseRPN( char *szExpression, int *pResult )
{
std::stack< int > Operands;
int currentNum = 0;
for( unsigned i = 0; i < strlen( szExpression ); i++ ) {
if( szExpression[ i ] == ' ' ) {
if( i == 0 )
return false;
else if( ISOPERAND( szExpression[ i - 1 ] ) )
continue;
Operands.push( currentNum );
currentNum = 0;
continue;
}
if( ISDIGIT( szExpression[ i ] ) ) {
currentNum = currentNum * 10 + CHARTONUM( szExpression[ i ] );
continue;
}
if( ISOPERAND( szExpression[ i ] ) ) {
if( i == 0 ||
szExpression[ i - 1 ] != ' ' )
return false;
currentNum = Operands.top( );
Operands.pop( );
currentNum = DoOp( szExpression[ i ], Operands.top( ), currentNum );
Operands.pop( );
Operands.push( currentNum );
currentNum = 0;
continue;
}
}
*pResult = Operands.top( );
return true;
}
int DoOp( char cOperation, int nOperandOne, int nOperandTwo )
{
int nRet;
switch( cOperation ) {
case '+':
nRet = nOperandOne + nOperandTwo;
break;
case '-':
nRet = nOperandOne - nOperandTwo;
break;
case '*':
nRet = nOperandOne * nOperandTwo;
break;
case '/':
nRet = nOperandOne / nOperandTwo;
break;
DEFAULT_UNREACHABLE;
}
return nRet;
}
|
Could maybe be optimized a little, but eh.
Took me roughly 10 minutes, after reading.
_________________
|
|
Back to top |
|
|
nog_lorp Grandmaster Cheater Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Mon Nov 24, 2008 3:26 pm Post subject: |
|
|
Nice. Usually they keep their state - I would be able to run "5 3 +" and then "8 -" and get 0, and keep working from there. That shouldn't be hard to do at all though.
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
Back to top |
|
|
TehBestNewbZ Expert Cheater Reputation: 0
Joined: 28 Jul 2006 Posts: 109
|
Posted: Mon Nov 24, 2008 3:46 pm Post subject: |
|
|
Average? It's pretty basic.
|
|
Back to top |
|
|
nog_lorp Grandmaster Cheater Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Mon Nov 24, 2008 6:01 pm Post subject: |
|
|
Next up: algebraic expression evaluator with correct order of operations!
Bonus points if you allow function definition in the form of "abc(args) = <expression in terms of args"
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
Back to top |
|
|
Noz3001 I'm a spammer Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Mon Nov 24, 2008 6:08 pm Post subject: |
|
|
Add boolean algebra too
|
|
Back to top |
|
|
nog_lorp Grandmaster Cheater Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Mon Nov 24, 2008 7:40 pm Post subject: |
|
|
Matrix algebra would be more useful.
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
Back to top |
|
|
|