| View previous topic :: View next topic |
| Author |
Message |
sponge cake recipe Grandmaster Cheater Supreme
Reputation: 22
Joined: 24 Sep 2007 Posts: 1635
|
Posted: Sun Aug 23, 2009 4:09 am Post subject: Shitty C++ Calc |
|
|
Learning C++ from a 15 y/o book.
| Code: | #include <iostream>
using namespace std;
void add(float x,float y);
void sub(float x,float y);
void mult(float x,float y);
void div(float x,float y);
int main()
{
char selection;
int ans;
float x;
float y;
cout << "Enter your first number here: ";
cin >> x;
cout << "Enter your second number here: ";
cin >> y;
cout << "Do you want to add, subtract, multiply or divide these numbers?" << endl;
cout << "Type a for add, s for subtract, etc." << endl;
cin >> selection;
switch(selection) {
case 'a':
add(x, y);
break;
case 's':
sub(x, y);
break;
case 'm':
mult(x, y);
break;
case 'd':
if (y == 0)
cout << "You divided by 0." << endl;
else div(x, y);
break;
default:
cout << "You selected none of the choices." << endl;
}
system("pause");
return 0;
}
void add(float x, float y)
{
cout << (x+y) << endl;
}
void sub(float x, float y)
{
cout << (x-y) << endl;
}
void mult(float x, float y)
{
cout << (x*y) << endl;
}
void div(float x, float y)
{
cout << (x/y) << endl;
} |
|
|
| Back to top |
|
 |
Zerith Master Cheater
Reputation: 1
Joined: 07 Oct 2007 Posts: 468
|
Posted: Sun Aug 23, 2009 5:08 am Post subject: |
|
|
| ...what are you trying to show here? |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sun Aug 23, 2009 5:11 am Post subject: |
|
|
| Zerith wrote: | | ...what are you trying to show here? |
| Holland wrote: | | Shitty C++ Calc |
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sun Aug 23, 2009 5:19 am Post subject: |
|
|
there is not much point in using functions if the content of a function is a 1 liner and it is only called once. it will just slow your program down and depending on how your compiler is set will make the code slower because of superfluous functions. indeed a call/ret is needed at the least
of course those differences are minimal but it is not good code practice to do as you have done |
|
| Back to top |
|
 |
smartz993 I post too much
Reputation: 2
Joined: 20 Jun 2006 Posts: 2013 Location: USA
|
Posted: Sun Aug 23, 2009 6:29 am Post subject: |
|
|
| Slugsnack wrote: | there is not much point in using functions if the content of a function is a 1 liner and it is only called once. it will just slow your program down and depending on how your compiler is set will make the code slower because of superfluous functions. indeed a call/ret is needed at the least
of course those differences are minimal but it is not good code practice to do as you have done |
Or use __inline, but that's microsoft specific. But of course, the compiler might optimize it to inline for you. |
|
| Back to top |
|
 |
sponge cake recipe Grandmaster Cheater Supreme
Reputation: 22
Joined: 24 Sep 2007 Posts: 1635
|
Posted: Sun Aug 23, 2009 6:44 am Post subject: |
|
|
| Slugsnack wrote: | there is not much point in using functions if the content of a function is a 1 liner and it is only called once. it will just slow your program down and depending on how your compiler is set will make the code slower because of superfluous functions. indeed a call/ret is needed at the least
of course those differences are minimal but it is not good code practice to do as you have done |
I was told hardcoding was bad, perhaps not for this, but it's good to get into the habit of creating separate functions, so if they're called multiple times it'll be better? |
|
| Back to top |
|
 |
smartz993 I post too much
Reputation: 2
Joined: 20 Jun 2006 Posts: 2013 Location: USA
|
Posted: Sun Aug 23, 2009 7:18 am Post subject: |
|
|
| Holland wrote: | | Slugsnack wrote: | there is not much point in using functions if the content of a function is a 1 liner and it is only called once. it will just slow your program down and depending on how your compiler is set will make the code slower because of superfluous functions. indeed a call/ret is needed at the least
of course those differences are minimal but it is not good code practice to do as you have done |
I was told hardcoding was bad, perhaps not for this, but it's good to get into the habit of creating separate functions, so if they're called multiple times it'll be better? |
What compiler are you using?
If you're using VC++, google the '__inline' specifier.
What it does is instead of compiling as call/ret, it will just put the compiled asm right "in-line" where it is called, hence speeding up, good coding practice, etc. |
|
| Back to top |
|
 |
sponge cake recipe Grandmaster Cheater Supreme
Reputation: 22
Joined: 24 Sep 2007 Posts: 1635
|
Posted: Sun Aug 23, 2009 8:37 am Post subject: |
|
|
| smartz993 wrote: | | Holland wrote: | | Slugsnack wrote: | there is not much point in using functions if the content of a function is a 1 liner and it is only called once. it will just slow your program down and depending on how your compiler is set will make the code slower because of superfluous functions. indeed a call/ret is needed at the least
of course those differences are minimal but it is not good code practice to do as you have done |
I was told hardcoding was bad, perhaps not for this, but it's good to get into the habit of creating separate functions, so if they're called multiple times it'll be better? |
What compiler are you using?
If you're using VC++, google the '__inline' specifier.
What it does is instead of compiling as call/ret, it will just put the compiled asm right "in-line" where it is called, hence speeding up, good coding practice, etc. |
Code::Blocks. |
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sun Aug 23, 2009 10:17 am Post subject: |
|
|
it is good practice to use functions when they are called multiple times but in this case they are not so i don't see the point in writing them as such
anyway.. not sure your purpose of posting this here. i am guessing for us to critique it ?
something i would change is when you have your IF conditions to indent the next line by 2 spaces or a tab or something to make it stand out more
other than that, code something more advanced, come back and post that instead |
|
| Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Sun Aug 23, 2009 10:23 am Post subject: |
|
|
You can do...
| Code: |
using namespace std::cout;
using namespace std::cin;
|
instead of using the whole std namespace. |
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sun Aug 23, 2009 10:33 am Post subject: |
|
|
uhh what would be the point in doing that ? gains no particular advantage
namespace is just a compiler directive so at runtime there is no difference and i doubt very much whether even at compiletime there is a difference either
on the other hand, he could just save an extra unnecessary line |
|
| Back to top |
|
 |
TiMBuS Cheater
Reputation: 0
Joined: 27 Jun 2006 Posts: 44
|
Posted: Sun Aug 23, 2009 10:38 am Post subject: Re: Shitty C++ Calc |
|
|
Here i added some killer optimizations to yr code check it:
| Code: | int add(int x, int y)
{
if (y == 0)
return x;
return add(++x, --y);
}
int sub(int x, int y)
{
if (y == 0)
return x;
return sub(--x, --y);
}
int mult(int x, int y)
{
if (y == 0)
return 0;
return add(x, mult(x, sub(y, 1)));
}
int div(int x, int y)
{
if (sub(x, y) < 0)
return 0;
return add(div(sub(x, y), y), 1);
} |
only works with ints tho... _________________
Build something even a fool can use and only a fool will use it. |
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sun Aug 23, 2009 10:44 am Post subject: |
|
|
possibly the mult/div would perform better than classical *// but for the first 2 i can't see them as anything other than potentially highly recursive
still code like this does serve a good educational purpose for a beginner, good job
also a little preference, in case OP does not know, instead of 'if (y == 0)', one can write 'if ( !y )' which i prefer but that is a matter of taste and style |
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sun Aug 23, 2009 11:28 am Post subject: |
|
|
yes because i obviously did not spot the sarcasm. be less presumptious, x0r
on second thoughts i'll let you continue rimming ass |
|
| Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Sun Aug 23, 2009 1:16 pm Post subject: |
|
|
| Slugsnack wrote: | uhh what would be the point in doing that ? gains no particular advantage
namespace is just a compiler directive so at runtime there is no difference and i doubt very much whether even at compiletime there is a difference either
on the other hand, he could just save an extra unnecessary line |
http://ubuntuforums.org/showthread.php?t=595176
I tend to be more explicit on things.
& You tend to like to attempt to troll me on every post that I've post, is it to make you look superior over the interwebz, or is it compensation for the lame life you are currently living? |
|
| Back to top |
|
 |
|