C calculator I made:

Dedicated to "idiots" right here on HTS! Please give them a "thanks" for free LULZ. However, please keep the flaming to yourselves!
Forum rules
Content must be appropriate. Keep the flaming to yourselves! For LULZ purposes only.

C calculator I made:

Post by AgentDerp on Mon Jul 19, 2010 5:07 pm
([msg=42161]see C calculator I made:[/msg])

Well, I managed to code a calculator right here.
And the source code is:
Code: Select all
#include <stdio.h>

int main(void)
{
    int numb1;
    int numb2;
    int numb3;
    int oper;
   
    printf("Enter the first number. ");
    scanf("%d", &numb1);
    printf("Enter the second number. ");
    scanf("%d", &numb2);
    printf("Enter the ASCII operator (43 (adding), 45 (subtracting), 42 (multiplying), or 47 (dividing). ");
    scanf("%d", &oper);
    if (oper == 43)
    {
        numb3 = numb1 + numb2;
   printf("The result is: %d", numb3);
    }
    else if (oper == 45)
    {
        numb3 = numb1 - numb2;
   printf("The result is: %d", numb3);
    }
    else if (oper == 42)
    {
        numb3 = numb1 * numb2;
   printf("The result is: %d", numb3);
    }
    else if (oper == 47)
    {
        if (numb2 > 0)
        {
           numb3 = numb1 / numb2;
      printf("The result is: %d", numb3);
        }
        else
        {
            printf("\nYou cannot enter a number below 1!\n");
        }
    }
    else
    {
   printf("You entered a wrong ASCII char.\n");
    }
   return 0;
}

Damn it.
The forums screw up the code spacing. >:U
I hope you don't mind it.
(I am not posting a download link unless people ask. That means you'll have to compile it yourself (After fixing the indentation that the forums messed up)).
AgentDerp
New User
New User
 
Posts: 13
Joined: Mon Jul 19, 2010 4:56 pm
Blog: View Blog (0)


Re: C calculator I made:

Post by Goatboy on Mon Jul 19, 2010 7:04 pm
([msg=42169]see Re: C calculator I made:[/msg])

While it's definitely functional, there are a lot of things that could be improved:

  • Change "numb3" to "result" for clarity
  • Consider making "oper" a char, so people can type in the characters for addition, subtraction, etc.
  • Handle the choice with a switch, instead of a bunch of if/else if
  • Why can't I enter a number below one?
  • What if I want to use decimals, or if my result would produce a decimal (7 divided by 2)?
  • Make it a loop, so the user does not have to re-run the program (type "0" to quit)
Just some constructive criticism. Calculators have been done so many times that you should really be aiming higher than this. You clearly know your code, so expand on it. If you're really up to the challenge, try letting the user enter something like:

14 * 6

And have the code figure it out. This is more challenging, because you need to break up the string into sections (called tokens, by most people) and have your code take the appropriate actions. Just something to think about.
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2807
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: C calculator I made:

Post by AgentDerp on Mon Jul 19, 2010 7:59 pm
([msg=42177]see Re: C calculator I made:[/msg])

Goatboy wrote:While it's definitely functional, there are a lot of things that could be improved:

    1. Change "numb3" to "result" for clarity
    2. Consider making "oper" a char, so people can type in the characters for addition, subtraction, etc.
    3. Handle the choice with a switch, instead of a bunch of if/else if
    4. Why can't I enter a number below one?
    5. What if I want to use decimals, or if my result would produce a decimal (7 divided by 2)?
    6. Make it a loop, so the user does not have to re-run the program (type "0" to quit)
Just some constructive criticism. Calculators have been done so many times that you should really be aiming higher than this. You clearly know your code, so expand on it. If you're really up to the challenge, try letting the user enter something like:

14 * 6

And have the code figure it out. This is more challenging, because you need to break up the string into sections (called tokens, by most people) and have your code take the appropriate actions. Just something to think about.

1. I don't write code for aesthetics.
2. It doesn't work. Every time I tried that, numb3 would be null no matter what.
3. I seriously don't know how to do switches when entering numbers.
4. I don't want people to divide by zero.
5. I don't know how to handle floats.
6. I can do that.
Constructive criticism: I'm not expert OR intermediate.
I'm just a beginner so the calculator was the best thing I ever made.
AgentDerp
New User
New User
 
Posts: 13
Joined: Mon Jul 19, 2010 4:56 pm
Blog: View Blog (0)


Re: C calculator I made:

Post by Goatboy on Mon Jul 19, 2010 8:16 pm
([msg=42179]see Re: C calculator I made:[/msg])

You're welcome?

1.) You need to get into the habit of making your code readable. This means proper indentation (which you did), meaningful variable names (you sorta did), and commenting (you didn't do, but was not needed for something this small).
2.) It will work. Read up on how to handle chars, use them as ints, etc.
3.) Use the switch to handle the operator, not the numbers. If you made the operator a char, a switch would work perfectly. Or you can just typecast like a beast (see below).
4.) What about negative numbers? My mother is part negative number, and I take offense to this.
5.) Floats are easy. You pretty much use them just like an int. Look up "type casting" for conversion between data types.
6.) Do it.

No excuses now. If you don't know it, learn it.
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2807
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: C calculator I made:

Post by thetan on Mon Jul 19, 2010 9:12 pm
([msg=42181]see Re: C calculator I made:[/msg])

AgentDerp wrote:1. I don't write code for aesthetics.

I've never met a single good or even decent programmer that doesn't care about the "aesthetics" of their code.

Elegant, easy to read code is almost as important as efficient well performing code.

Never overlook the simple fact that code is read more then it is written. You won't live forever, but your code might out live you and when you're gone and some one else comes along and they see ugly code, they'll probably run away.
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 657
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)


Re: C calculator I made:

Post by AgentDerp on Mon Jul 19, 2010 9:14 pm
([msg=42182]see Re: C calculator I made:[/msg])

Goatboy wrote:You're welcome?

1.) You need to get into the habit of making your code readable. This means proper indentation (which you did), meaningful variable names (you sorta did), and commenting (you didn't do, but was not needed for something this small).
2.) It will work. Read up on how to handle chars, use them as ints, etc.
3.) Use the switch to handle the operator, not the numbers. If you made the operator a char, a switch would work perfectly. Or you can just typecast like a beast (see below).
4.) What about negative numbers? My mother is part negative number, and I take offense to this.
5.) Floats are easy. You pretty much use them just like an int. Look up "type casting" for conversion between data types.
6.) Do it.

No excuses now. If you don't know it, learn it.

1. I'm the only person that makes my code. I understand it clearly.
2. It doesn't. When I make oper a char variable, it just executes the "else" clause and then numb3 would be empty.
3. See 1.
4. I never studied negative numbers so I don't what happens if I divide a number by negative whatever.
5. What if somebody with low intelligence doesn't remember to put the "." in?
6. Fine.

No excuses part: I'm not about to learn advanced algebra just to make somebody happy.

-- Mon Jul 19, 2010 9:16 pm --

thetan wrote:
AgentDerp wrote:1. I don't write code for aesthetics.

I've never met a single good or even decent programmer that doesn't care about the "aesthetics" of their code.

Elegant, easy to read code is almost as important as efficient well performing code.

Never overlook the simple fact that code is read more then it is written. You won't live forever, but your code might out live you and when you're gone and some one else comes along and they see ugly code, they'll probably run away.

I don't make code like #include <stdio.h> int main(void) { printf("Hello, world!"); return 0; }
I want the code to at least be readable by ME so I will be able to program the program.
AgentDerp
New User
New User
 
Posts: 13
Joined: Mon Jul 19, 2010 4:56 pm
Blog: View Blog (0)


Re: C calculator I made:

Post by Goatboy on Mon Jul 19, 2010 9:22 pm
([msg=42183]see Re: C calculator I made:[/msg])

Okay, I can see you obviously have an attitude problem. Two people, one of which is a developer, have given you advice, and you have ignored most of it. If you don't want to take our advice, why would you post a piece of code which obviously needs critiquing?

Not only that, but you are making excuses as to why you won't learn. You don't know what happens with negative division? Take a basic algebra course, use a calculator, or Google it. If someone forgets to put in a period, it's their mistake and they need to be smart enough to remember. That's how a normal calculator works. For your last point, this isn't advanced algebra. This is basic math.

I'm not sure what you expected out of this post. If you wanted to show of your skittlez, you should have purchased some before-hand. If you wanted advice on how to code better, you should stop whining and take it.

/rant
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2807
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: C calculator I made:

Post by AgentDerp on Mon Jul 19, 2010 9:34 pm
([msg=42184]see Re: C calculator I made:[/msg])

I wanted to show off my first actual program in C.
I didn't want to mold my program just so I could make somebody happy.
I just wanted you to try the program out.
And I would have known if my program had errors because I always test them before posting them.
And "take a basic algebra course"?
I'm in 6th grade, I have sum-

Okay fine.
I'm obviously a teenager in high school that is able to comprehend the words of other teenagers because I'm smart enough to.[/sarcasm]
AgentDerp
New User
New User
 
Posts: 13
Joined: Mon Jul 19, 2010 4:56 pm
Blog: View Blog (0)


Re: C calculator I made:

Post by Goatboy on Mon Jul 19, 2010 9:39 pm
([msg=42185]see Re: C calculator I made:[/msg])

You should be molding your program to make yourself happy. We're not telling you what to do because it's what we want, we're telling you what to do because we've been there and we know what works. Whether or not you take this advice is entirely up to you. We can't exactly force you to code correctly properly better.

In any case, the attitude is not needed. I don't care what grade you are in. And what makes you think we are all teenagers in highschool?
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2807
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: C calculator I made:

Post by AgentDerp on Mon Jul 19, 2010 9:44 pm
([msg=42186]see Re: C calculator I made:[/msg])

Goatboy wrote:You should be molding your program to make yourself happy. We're not telling you what to do because it's what we want, we're telling you what to do because we've been there and we know what works. Whether or not you take this advice is entirely up to you. We can't exactly force you to code correctly properly better.

In any case, the attitude is not needed. I don't care what grade you are in. And what makes you think we are all teenagers in highschool?

Why should I make myself happy?
I like making myself unhappy somethings.
I'm also being a prick and don't want help from other people unless I need it.
Which I do not right now.

I don't care about my attitude.
Fine.
Because you are obviously more mature than me.

-- Mon Jul 19, 2010 9:54 pm --

Oh yay.
Apparently my posts are "sarcastically funny".
That's REAL nice.[/moresarcasm]
AgentDerp
New User
New User
 
Posts: 13
Joined: Mon Jul 19, 2010 4:56 pm
Blog: View Blog (0)


Next

Return to Idiots' Corner

Who is online

Users browsing this forum: No registered users and 0 guests