| View previous topic :: View next topic |
| Author |
Message |
adamezra Advanced Cheater
Reputation: 0
Joined: 14 Feb 2007 Posts: 73
|
Posted: Thu Jan 31, 2008 11:48 am Post subject: [C++]My longest code and my best creation so far |
|
|
dont let the title confuse u
im a begginer O_o
but this one took me some time
so i wanted to show it
its a calculator
i included the source
_________________
...............................
.............................
............................
...........................
.........................
asagvsfdhg |
|
| Back to top |
|
 |
HolyBlah Master Cheater
Reputation: 2
Joined: 24 Aug 2007 Posts: 446
|
Posted: Thu Jan 31, 2008 11:55 am Post subject: |
|
|
Very nice.
I suggest you to use switch and check if the second number is not 0 when dividing.
|
|
| Back to top |
|
 |
adamezra Advanced Cheater
Reputation: 0
Joined: 14 Feb 2007 Posts: 73
|
Posted: Thu Jan 31, 2008 11:59 am Post subject: |
|
|
i can use if(y==0) to do that no?
edit: lol it crashes when u divide by zero
_________________
...............................
.............................
............................
...........................
.........................
asagvsfdhg |
|
| Back to top |
|
 |
HolyBlah Master Cheater
Reputation: 2
Joined: 24 Aug 2007 Posts: 446
|
Posted: Thu Jan 31, 2008 12:11 pm Post subject: |
|
|
| yes, 'if (y!=0)'.
|
|
| Back to top |
|
 |
adamezra Advanced Cheater
Reputation: 0
Joined: 14 Feb 2007 Posts: 73
|
Posted: Thu Jan 31, 2008 12:16 pm Post subject: |
|
|
ty for the tip
_________________
...............................
.............................
............................
...........................
.........................
asagvsfdhg |
|
| Back to top |
|
 |
Dymo Grandmaster Cheater
Reputation: 0
Joined: 16 Sep 2007 Posts: 604 Location: The Netherlands, Noord-Brabant
|
Posted: Thu Jan 31, 2008 1:23 pm Post subject: |
|
|
| Divide by 0 is a math error.
|
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Thu Jan 31, 2008 1:50 pm Post subject: |
|
|
| Code: | #include "Rev0"
using std::cout;
using std::cin;
using std::endl;
using losBasicI::inpute;
using losBasicI::input;
using losBasicI::inputmm;
using losBasicMath::losRound;
double add (double x, double y);
double mult(double x,double y);
double deduct(double x,double y);
double divide(double x ,double y);
int main()
{
double x = 0.0, y = 0.0;
int choose = 0;
bool loop = true;
while(loop == true)
{
cout << "0:Quit" << endl << "1:adding" << endl << "2:deducting" << endl << "3:multiplying" << "\n" << "4:dividing"<< endl << "Please make your choice: " ;
choose = inputmm(0,5);
switch(choose)
{
case 0:
cout << "Good bye" << endl;
loop = false;
break;
case 1:
cout<<"Please input the first number to be added: ";
x = losRound<double>(input<double>(),4);
cout << "2nd number please:" ;
y =losRound<double>(input<double>(),4);
cout<< x << "+" << y << "=" << losRound<double>(add ( x, y ),4) << endl;
break;
case 2:
cout << "Please input the first number to be deducted: ";
x = losRound<double>(input<double>(),4);
cout << "2nd number please: " ;
y = losRound<double>(input<double>(),4);
cout << x << "-" << y <<"=" << deduct(x , y) << "\n" ;
break;
case 3:
cout << "Please input the first number to be multiplied: " ;
x = losRound<double>(input<double>(),4);
cout << "2nd number please: " ;
y = losRound<double>(input<double>(),4);
cout << x << " times " << y << "=" << losRound(mult(x, y), 4) << endl;
break;
case 4:
cout << "Please input the first number to be divided: " ;
x = losRound<double>(input<double>(),4);
cout << "2nd number please: " ;
y = losRound<double>(inpute<double>(0),4);
cout << x << "/" << y << "=" << losRound(divide(x,y),4) << endl;
break;
}
}
}
double add (double x,double y )
{
return x + y;
}
double mult (double x,double y)
{
return x*y;
}
double deduct(double x ,double y)
{
return x-y;
}
double divide(double x,double y)
{
return x/y;
}
|
Cleaned up the code a little and here is Rev0 make sure it is in the same directory as the .cpp file.
| Code: |
#include <iostream>
#include <math.h>
namespace losBasicI
{
template <typename T>
T input()
{
T returnvalue;
std::cin >> returnvalue;
while (std::cin.fail())
{
std::cout << "Bad input Try again: ";
std::cin.clear(); // Clear failbit
std::cin.sync(); // Sync the stream
std::cin >> returnvalue; // Try again
}
return returnvalue;
}
template <typename T>
T inputmm(T min, T max) //Always use this before a switch No execeptions
{
T returnvalue;
std::cin >> returnvalue;
while(std::cin.fail()||returnvalue < min||returnvalue > max)
{
std::cout << "Bad input Try again: ";
std::cin.clear(); // Clear failbit
std::cin.sync(); // Sync the stream
std::cin >> returnvalue; // Try again
}
return returnvalue;
}
template <typename T>
T inpute(T equal)
{
T returnvalue;
std::cin >> returnvalue;
while(std::cin.fail()||returnvalue == equal)
{
std::cout << "Bad input Try again: ";
std::cin.clear(); // Clear failbit
std::cin.sync(); // Sync the stream
std::cin >> returnvalue; // Try again
}
return returnvalue;
}
} // End of namespace losbasici
namespace losBasicMath
{
template <typename T>
T losRound(T num, int decimalplaces)
{
switch(decimalplaces)
{
case 1:
num = floor(num*10)/10;
break;
case 2:
num = floor(num*100)/100;
break;
case 3:
num = floor(num*1000)/1000;
break;
case 4:
num = floor(num*10000)/10000;
break;
case 5:
num = floor(num*100000)/100000;
break;
case 6:
num = floor(num*1000000)/1000000;
}
return num;
}
}
|
If you need help with Rev0 feel free to ask it is just for rounding and checking your input.
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
adamezra Advanced Cheater
Reputation: 0
Joined: 14 Feb 2007 Posts: 73
|
Posted: Thu Jan 31, 2008 2:43 pm Post subject: |
|
|
actually i didnt understad 70% of what you did there O_o
_________________
...............................
.............................
............................
...........................
.........................
asagvsfdhg |
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Thu Jan 31, 2008 3:07 pm Post subject: |
|
|
| That's because he didn't make the code any more clean or efficient, he's just using this to show off fancy C++ features.
|
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Thu Jan 31, 2008 3:26 pm Post subject: |
|
|
Here i commented it all so you can learn from it easier.
| Code: |
#include "Rev0" // Put the Rev0 file in the same directory as your .cpp file
// Saying what functions i will use so i dont have to put using namespace std
// ,using namespace losBasicI and using losBasicMath.
using std::cout;
using std::cin;
using std::endl;
using losBasicI::inpute;
using losBasicI::input;
using losBasicI::inputmm;
using losBasicMath::losRound;
//Declaring your functions
double add (double x, double y);
double mult(double x,double y);
double deduct(double x,double y);
double divide(double x ,double y);
int main()
{
double x = 0.0, y = 0.0; // Making a initializing x and y
int choose = 0; //Variable that controls the switch
bool loop = true; // Bool can = true(1) or false(0)
while(loop == true) // Says repeat this code until i set loop to false
{
cout << "0:Quit" << endl << "1:adding" << endl << "2:deducting" << endl << "3:multiplying" << "\n" << "4:dividing"<< endl << "Please make your choice: " ;
choose = inputmm<int>(0,5); // Ok this is the first Rev0 function
// input<data type>(min,max) so input<int> is the same as saying
// int input(int min, int max) what it does is gets input then checks it
// against the min and max if it is bad it will show a error message and ask
// for the input again.
switch(choose) // Takes what we input and does stuff with it.
{
case 0: // If the input was 0 it will send a exit message and stop the repeat.
cout << "Good bye" << endl;
loop = false;
break;
case 1: // Same but it adds the numbers.
cout<<"Please input the first number to be added: ";
x = losRound<double>(input<double>(),4); // So it gets input but only checks
// if it is invalid for that data type and rounds it using the next
// 2 Rev0 functions here is how losRound works
// losRound<data type>(num, decimalplaces) so saying something like this
// losRound<double>(82.6668,3) will output 82.667 which rounded it to the
// near'st .001 losround can round up to 6 places or .000001
// That line is actually saying get input then round the input to the near'st
// .0001
cout << "2nd number please:" ;
y =losRound<double>(input<double>(),4); // Same as x from here it is just
// a repeat of this until we use devision
cout<< x << "+" << y << "=" << losRound<double>(add ( x, y ),4) << endl;
// Says x+y= x+y rounded
break; // Stops the switch from going onto the next case
case 2:
//Pretty much the same as the first case execept it subtracts y
cout << "Please input the first number to be deducted: ";
x = losRound<double>(input<double>(),4);
cout << "2nd number please: " ;
y = losRound<double>(input<double>(),4);
cout << x << "-" << y <<"=" << losRound<double>(deduct(x , y),4) << "\n" ;
break;
case 3:
// Yet again the same but it multiplies x by y
cout << "Please input the first number to be multiplied: " ;
x = losRound<double>(input<double>(),4);
cout << "2nd number please: " ;
y = losRound<double>(input<double>(),4);
cout << x << " times " << y << "=" << losRound(mult(x, y), 4) << endl;
break;
case 4:
// Something different now it introduces the final Rev function used in
// this program which is inpute<double>(equal)
cout << "Please input the first number to be divided: " ;
x = losRound<double>(input<double>(),4);
cout << "2nd number please: " ;
y = losRound<double>(inpute<double>(0),4);
// Ok so inpute gets input compares it to equal and checks if it is valid
// So if i put inpute<double>(0) then input 0 it would say
// "Bad input Try again: "
cout << x << "/" << y << "=" << losRound(divide(x,y),4) << endl;
break;
}
}
}
double add (double x,double y )
{
return x + y;
}
double mult (double x,double y)
{
return x*y;
}
double deduct(double x ,double y)
{
return x-y;
}
double divide(double x,double y)
{
return x/y;
}
|
And here is my personal Rev0 I have been working on.
| Code: |
#include <iostream>
#include <math.h>
//This is my personal Rev0 function i add things to it when i think of them
namespace losBasicI //Declares a namespace you need to use namespace losBasicI
//To use anything in this namespace.
{
// All of these use the same thing just 2 differences.
template <typename T> //This says it is a template and to replace T
// With what put in the <> in input<data type>() so if it is <int>
// Then the T in this function is replaced with int.
T input()
{
T returnvalue;
std::cin >> returnvalue;
while (std::cin.fail())
{
std::cout << "Bad input Try again: ";
std::cin.clear(); // Clear failbit
std::cin.sync(); // Sync the stream
std::cin >> returnvalue; // Try again
}
return returnvalue;
}
template <typename T>
T inputmm(T min, T max) // Takes 2 variables
{
T returnvalue;
std::cin >> returnvalue;
// Now it checks if the input is invalid under the min or over the
// max input then it prints a error message and asks for input again
while(std::cin.fail()||returnvalue < min||returnvalue > max)
{
std::cout << "Bad input Try again: ";
std::cin.clear(); // Clear failbit
std::cin.sync(); // Sync the stream
std::cin >> returnvalue; // Try again
}
return returnvalue;
}
template <typename T>
T inputmn(T min) //Takes 1 variable
{
T returnvalue;
std::cin >> returnvalue;
while(std::cin.fail()||returnvalue < min) //Same as above but only
// checks the input for being invalid or under the min.
{
std::cout << "Bad input Try again: ";
std::cin.clear(); // Clear failbit
std::cin.sync(); // Sync the stream
std::cin >> returnvalue; // Try again
}
return returnvalue;
}
template <typename T>
T inputm(T max) //Same as above just checks if over the max
{
T returnvalue;
std::cin >> returnvalue;
while(std::cin.fail()||returnvalue > max)
{
std::cout << "Bad input Try again: ";
std::cin.clear(); // Clear failbit
std::cin.sync(); // Sync the stream
std::cin >> returnvalue; // Try again
}
return returnvalue;
}
template <typename T>
T inpute(T equal) // Yet again the same but checks if the input is
// equal to the variable that it takes.
{
T returnvalue;
std::cin >> returnvalue;
while(std::cin.fail()||returnvalue == equal)
{
std::cout << "Bad input Try again: ";
std::cin.clear(); // Clear failbit
std::cin.sync(); // Sync the stream
std::cin >> returnvalue; // Try again
}
return returnvalue;
}
} // End of namespace losbasici
namespace losBasicMath // Another namespace same as the first just different
// functions
{
template <typename T>
T losRound(T num, int decimalplaces) //Takes a number and a intergar
{
switch(decimalplaces)
{
// If you know how decimals work
// This explains itself almost
case 1:
num = floor(num*10)/10;
//floor rounds it to the close'st whole intergar
// floor comes from math.h
break;
case 2:
num = floor(num*100)/100;
break;
case 3:
num = floor(num*1000)/1000;
break;
case 4:
num = floor(num*10000)/10000;
break;
case 5:
num = floor(num*100000)/100000;
break;
case 6:
num = floor(num*1000000)/1000000;
default:
std::cout << "Only takes 1 - 6 decimal places."
<< std::endl;
break;
}
return num;
}
template <typename T>
T losDTR(T num) // Degrees to radians explains itself
{
const double PI = 3.14159265358979323846;
losRound<T>(num = (num*PI)/180,6);
return num;
}
template <typename T>
T losRTD(T num) // radians to degrees
{
const double PI = 3.14159265358979323846;
losRound<T>(num = (num*180)/PI);
return num;
}
}
|
And appalsap yes i am using this to show off fancy c++ features.
I could do this.
| Code: |
#include <iostream>// Put the Rev0 file in the same directory as your .cpp file
// Saying what functions i will use so i dont have to put using namespace std
// ,using namespace losBasicI and using losBasicMath.
using std::cout;
using std::cin;
using std::endl;
int main()
{
double x = 0.0, y = 0.0;
int choice = 0;
bool loop = true;
while(loop == true)
{
cout << "0:Quit" << endl << "1:adding" << endl << "2:deducting" << endl << "3:multiplying" << "\n" << "4:dividing"<< endl << "Please make your choice: " ;
cin >> choice;
switch(choice)
{
case 0:
cout << "Good bye" << endl;
loop = false;
break;
case 1:
cout<<"Please input the first number to be added: ";
cin >> x;
cout << "2nd number please:" ;
cin >> y;
cout<< x << "+" << y << "=" << x+y << endl;
break;
case 2:
cout << "Please input the first number to be deducted: ";
cin >> x;
cout << "2nd number please: " ;
cin >> y;
cout << x << "-" << y <<"=" << x-y << "\n" ;
break;
case 3:
cout << "Please input the first number to be multiplied: " ;
cin >> x;
cout << "2nd number please: " ;
cin >> y;
cout << x << " times " << y << "=" << x*y << endl;
break;
case 4:
cout << "Please input the first number to be divided: " ;
cin >> x;
cout << "2nd number please: " ;
cin >> y;
cout << x << "/" << y << "=" << x/y << endl;
break;
}
}
}
|
Same size barely any difference in ram
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
rapion124 Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Mar 2007 Posts: 1095
|
Posted: Thu Jan 31, 2008 4:14 pm Post subject: |
|
|
| Try using exception instead of if statement for math errors. I believe C++ has a unit just for that.
|
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Fri Feb 01, 2008 6:10 am Post subject: |
|
|
| if devided by zero (or negative numbers) make it return an error instead of self terminate
|
|
| Back to top |
|
 |
adamezra Advanced Cheater
Reputation: 0
Joined: 14 Feb 2007 Posts: 73
|
Posted: Fri Feb 01, 2008 6:11 am Post subject: |
|
|
ty for commenting on the whole code i will learn a lot
and thanks for all the tips i will try making dividing by zero do somthing else
_________________
...............................
.............................
............................
...........................
.........................
asagvsfdhg |
|
| Back to top |
|
 |
AlienX-P Advanced Cheater
Reputation: 0
Joined: 31 Jan 2008 Posts: 52
|
Posted: Fri Feb 01, 2008 11:55 am Post subject: |
|
|
Hmm, nice.
Crashes when you divides by 0 or some negative numbers lol
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Fri Feb 01, 2008 3:40 pm Post subject: |
|
|
well obviously.
what do you expect to happen when you try to do the impossible? just check what the second number is when the user inputs it and if it's 0, don't do it.
|
|
| Back to top |
|
 |
|