LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-22-2012, 08:16 PM   #1
abcde597
Member
 
Registered: Oct 2012
Location: Alabama, US
Distribution: Several Debian Based Distros
Posts: 57

Rep: Reputation: Disabled
Post Rate my simplified Calculator?


Code:
#include <stdio.h>
#include <stdlib.h>

char input [100];
float result;
float first;
float second;
char started;
char operation;

int main(int argc, char** argv) {
    result = 0;
    first = 0;
    second = 0;
    started = 'n';
    while (1) {
        if (started == 'n') {
            printf("Type all your equations in the correct form, examples: "
                    "2 + 2, 4 * 4, and so on, one space between the first number, the operation symbol, and the second number.\n");
            started = 'y';
            continue;
        }
        printf("Type 2 numbers like an equation, with a q in the middle to quit\n");
        fgets(input, sizeof(input), stdin);
        sscanf(input, "%f %c %f", &first, &operation, &second);
        switch (operation) {
            case '+':
                result = first + second;
                break;
            case '-':
                result = first - second;
                break;
            case '*':
                result = first * second;
                break;
            case '/':
                result = first / second;
                break;
            case 'q':
                break;
            case 'Q':
                break;
            default:
                break;
        }
        printf("Your answer is: %f\n", result);
    }
    return (EXIT_SUCCESS);
}
Any constructive criticism is appreciated.
 
Old 10-23-2012, 01:57 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Hi, that's a nice little program!
A few notes:
1)
Code:
float result;
float first;
float second;
char started;
char operation;

int main(int argc, char** argv) {
    result = 0;
    first = 0;
    second = 0;
    started = 'n';
Why don't you initialize the variables when you declare them?

Code:
float result = 0;
float first = 0;
float second = 0;
char started = 'n';
char operation;
Also, it does not really matter here, but I would make these variables local (maybe just my personal taste). I allways try to declare all variables in the smallest scope possible. In this case, result, first and second and operation could very well be declared even inside of the while loop.

2)
Code:
    started = 'n';
    while (1) {
        if (started == 'n') {
            printf("Type all your equations in the correct form, examples: "
                    "2 + 2, 4 * 4, and so on, one space between the first number, the operation symbol, and the second number.\n");
            started = 'y';
            continue;
        }
        ...
    }
I don't really understand this. If you want this to be done only in the first iteration, why don't you just put it out of the loop? That would also save you the need for the started variable:

Code:
   
    printf("Type all your equations in the correct form, examples: "
           "2 + 2, 4 * 4, and so on, one space between the first number"
           ", the operation symbol, and the second number.\n");

    while (1) {
        ...
3)
Code:
            case 'q':
                break;
            case 'Q':
                break;
            default:
                break;
No reason to put all those in your code when the result is the same. Just the default would suffice.
Also, this code will also not work as expected, since the break statement will take you out of the switch, but not out of the while loop.
 
Old 10-23-2012, 03:20 AM   #3
Celyr
Member
 
Registered: Mar 2012
Location: Italy
Distribution: Slackware+Debian
Posts: 321

Rep: Reputation: 81
I suggest you also to have a look at this
http://en.wikipedia.org/wiki/Reverse_Polish_notation
so it may be easy to actually support expressions
 
Old 10-23-2012, 05:29 PM   #4
abcde597
Member
 
Registered: Oct 2012
Location: Alabama, US
Distribution: Several Debian Based Distros
Posts: 57

Original Poster
Rep: Reputation: Disabled
1: Makes things simpler for me.
2: Wasn't thinking about it.
3: That's what was intended...
Just forgot an if for an exit. -.-
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculating Bounce rate and exit rate from Awstats senthilvael Linux - Server 1 02-29-2012 03:26 AM
Simplified Linux Roger Hunter Linux - General 3 12-11-2005 12:56 AM
help on horiz scan rate/vert synch rate on Toshiba Satellite A35-S159 asilentmurmur *BSD 3 10-14-2005 05:29 PM
Vertical refresh rate, horizontal sync rate. NomDeGuerre Linux - Newbie 7 10-07-2005 02:36 AM
Simplified wget? KendersPlace Programming 3 09-08-2003 05:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration