Advanced C calculator

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
5
down vote

favorite












I was asked to create an Advanced calculator in C, I call it advanced because it actually handles arithmetic rules.
It was mentioned that I don't need to handle code exceptions and that I should assume that the code has legal input.



It works as follows:



  1. Enter a number and press enter

  2. Enter an operator and press enter To

  3. To finish user input you press X and then enter.

Again I mention this, I assume correct input was put in the program.



calc.h



#pragma once

/*Sort the equation, deal with multipliers/divisors then sub and add, load it all in one array then recalculate */

/* Includes ******************************************************************/
#include <stdio.h>
#include <Windows.h>
/* Macros ********************************************************************/

/* Types *********************************************************************/

/* Global Variables *********************************************************/

/* Function Declarations *****************************************************/

/*Accept the equation as input and sort*/
void sort(IN int *numbers, IN char *operators);

/*Return result based on operator*/
int getOp(IN int a, IN int b, IN char op);

/*Create equation*/
void createEqu(IN int *numbers, IN char *operators);

/*Prints the equation*/
void print(IN int *numbers, IN char *operators);

/*Calculate array sum*/
void calSum(IN int *sorted, IN int length);


calc.c



/* Includes ******************************************************************/
#include "calc.h"

/* Macros ********************************************************************/

/* Types *********************************************************************/

/* Public Function Definitions ***********************************************/

/* Global Variables **********************************************************/

/*Create equation*/
void createEqu(IN int *numbers, IN char *operators)

/*Intialize equation creater, 0 get number, 1 get operator*/
int currentOperation = -1;

/*Iterate through the arrays*/
int n = 0;
int o = 0;

/*While equation input was not ended by user*/
while (currentOperation != 2)

/*Number or operator*/
if (currentOperation == -1)

printf("Enter numbern");
scanf(" %d", &numbers[n]);

/*If operator is negative, turn number into negative*/
if (o != 0 && operators[o - 1] == '-')

numbers[n] = getOp(numbers[n], NULL, operators[o - 1]);

n++;

else

printf("Enter operatorn");
scanf(" %c", &operators[o]);
o++;


currentOperation = currentOperation*-1;
/*check if the last operator was X to terminate equation input*/
if (operators[o - 1] == 'X')

currentOperation = 2;



/*Each array is terminated with NULL so it would be easy to read through them*/

operators[o-1] = NULL;
numbers[n] = NULL;


/*Prints the equation*/
void print(IN int *numbers, IN char *operators)

int i = 0;
for (;operators[i] != NULL; i++)
printf("%c", operators[i]);


/*Return result based on operator*/
int getOp(IN int a, IN int b, IN char op)

if (op == '*')
return a * b;

if (op == '/')
return a / b;

if (op == '+')
return a;

if (op == '-')
return -a;


/*Calculate array sum*/
void calSum(IN int *sorted, IN int length)

int i = 0;
int finalRes = 0;

for (; i <= length; i++)

printf("%d ", sorted[i]);
finalRes += sorted[i];


printf("%d", finalRes);


/*Accept the equation as input and sort*/
void sort(IN int *numbers, IN char *operators)

int sorted[256];

/*Iterators of arrays*/
int s = 0;
int n = 0;
int o = 0;

/*While expression is not over*/
while (operators[o] != NULL)

/*If operation is + or - then store integers in sorted array for later calculation*/
if (operators[o] == '+'

calSum(sorted, s);


/* Private Function Definitions **********************************************/


main.c



/* Includes ******************************************************************/
#include "calc.h"

/* Function Definitions ******************************************************/
INT wmain(IN SIZE_T nArgc, IN PCWSTR *ppcwszArgv)

int numbers[256];
char equ[256];

createEqu(&numbers, &equ);
sort(numbers, equ);
getch();

/* Succeeded */
return 0;







share|improve this question





















  • Clearly state and add the review goals.
    – chux
    Feb 6 at 6:26










  • Where is IN defined?
    – chux
    Feb 6 at 6:27
















up vote
5
down vote

favorite












I was asked to create an Advanced calculator in C, I call it advanced because it actually handles arithmetic rules.
It was mentioned that I don't need to handle code exceptions and that I should assume that the code has legal input.



It works as follows:



  1. Enter a number and press enter

  2. Enter an operator and press enter To

  3. To finish user input you press X and then enter.

Again I mention this, I assume correct input was put in the program.



calc.h



#pragma once

/*Sort the equation, deal with multipliers/divisors then sub and add, load it all in one array then recalculate */

/* Includes ******************************************************************/
#include <stdio.h>
#include <Windows.h>
/* Macros ********************************************************************/

/* Types *********************************************************************/

/* Global Variables *********************************************************/

/* Function Declarations *****************************************************/

/*Accept the equation as input and sort*/
void sort(IN int *numbers, IN char *operators);

/*Return result based on operator*/
int getOp(IN int a, IN int b, IN char op);

/*Create equation*/
void createEqu(IN int *numbers, IN char *operators);

/*Prints the equation*/
void print(IN int *numbers, IN char *operators);

/*Calculate array sum*/
void calSum(IN int *sorted, IN int length);


calc.c



/* Includes ******************************************************************/
#include "calc.h"

/* Macros ********************************************************************/

/* Types *********************************************************************/

/* Public Function Definitions ***********************************************/

/* Global Variables **********************************************************/

/*Create equation*/
void createEqu(IN int *numbers, IN char *operators)

/*Intialize equation creater, 0 get number, 1 get operator*/
int currentOperation = -1;

/*Iterate through the arrays*/
int n = 0;
int o = 0;

/*While equation input was not ended by user*/
while (currentOperation != 2)

/*Number or operator*/
if (currentOperation == -1)

printf("Enter numbern");
scanf(" %d", &numbers[n]);

/*If operator is negative, turn number into negative*/
if (o != 0 && operators[o - 1] == '-')

numbers[n] = getOp(numbers[n], NULL, operators[o - 1]);

n++;

else

printf("Enter operatorn");
scanf(" %c", &operators[o]);
o++;


currentOperation = currentOperation*-1;
/*check if the last operator was X to terminate equation input*/
if (operators[o - 1] == 'X')

currentOperation = 2;



/*Each array is terminated with NULL so it would be easy to read through them*/

operators[o-1] = NULL;
numbers[n] = NULL;


/*Prints the equation*/
void print(IN int *numbers, IN char *operators)

int i = 0;
for (;operators[i] != NULL; i++)
printf("%c", operators[i]);


/*Return result based on operator*/
int getOp(IN int a, IN int b, IN char op)

if (op == '*')
return a * b;

if (op == '/')
return a / b;

if (op == '+')
return a;

if (op == '-')
return -a;


/*Calculate array sum*/
void calSum(IN int *sorted, IN int length)

int i = 0;
int finalRes = 0;

for (; i <= length; i++)

printf("%d ", sorted[i]);
finalRes += sorted[i];


printf("%d", finalRes);


/*Accept the equation as input and sort*/
void sort(IN int *numbers, IN char *operators)

int sorted[256];

/*Iterators of arrays*/
int s = 0;
int n = 0;
int o = 0;

/*While expression is not over*/
while (operators[o] != NULL)

/*If operation is + or - then store integers in sorted array for later calculation*/
if (operators[o] == '+'

calSum(sorted, s);


/* Private Function Definitions **********************************************/


main.c



/* Includes ******************************************************************/
#include "calc.h"

/* Function Definitions ******************************************************/
INT wmain(IN SIZE_T nArgc, IN PCWSTR *ppcwszArgv)

int numbers[256];
char equ[256];

createEqu(&numbers, &equ);
sort(numbers, equ);
getch();

/* Succeeded */
return 0;







share|improve this question





















  • Clearly state and add the review goals.
    – chux
    Feb 6 at 6:26










  • Where is IN defined?
    – chux
    Feb 6 at 6:27












up vote
5
down vote

favorite









up vote
5
down vote

favorite











I was asked to create an Advanced calculator in C, I call it advanced because it actually handles arithmetic rules.
It was mentioned that I don't need to handle code exceptions and that I should assume that the code has legal input.



It works as follows:



  1. Enter a number and press enter

  2. Enter an operator and press enter To

  3. To finish user input you press X and then enter.

Again I mention this, I assume correct input was put in the program.



calc.h



#pragma once

/*Sort the equation, deal with multipliers/divisors then sub and add, load it all in one array then recalculate */

/* Includes ******************************************************************/
#include <stdio.h>
#include <Windows.h>
/* Macros ********************************************************************/

/* Types *********************************************************************/

/* Global Variables *********************************************************/

/* Function Declarations *****************************************************/

/*Accept the equation as input and sort*/
void sort(IN int *numbers, IN char *operators);

/*Return result based on operator*/
int getOp(IN int a, IN int b, IN char op);

/*Create equation*/
void createEqu(IN int *numbers, IN char *operators);

/*Prints the equation*/
void print(IN int *numbers, IN char *operators);

/*Calculate array sum*/
void calSum(IN int *sorted, IN int length);


calc.c



/* Includes ******************************************************************/
#include "calc.h"

/* Macros ********************************************************************/

/* Types *********************************************************************/

/* Public Function Definitions ***********************************************/

/* Global Variables **********************************************************/

/*Create equation*/
void createEqu(IN int *numbers, IN char *operators)

/*Intialize equation creater, 0 get number, 1 get operator*/
int currentOperation = -1;

/*Iterate through the arrays*/
int n = 0;
int o = 0;

/*While equation input was not ended by user*/
while (currentOperation != 2)

/*Number or operator*/
if (currentOperation == -1)

printf("Enter numbern");
scanf(" %d", &numbers[n]);

/*If operator is negative, turn number into negative*/
if (o != 0 && operators[o - 1] == '-')

numbers[n] = getOp(numbers[n], NULL, operators[o - 1]);

n++;

else

printf("Enter operatorn");
scanf(" %c", &operators[o]);
o++;


currentOperation = currentOperation*-1;
/*check if the last operator was X to terminate equation input*/
if (operators[o - 1] == 'X')

currentOperation = 2;



/*Each array is terminated with NULL so it would be easy to read through them*/

operators[o-1] = NULL;
numbers[n] = NULL;


/*Prints the equation*/
void print(IN int *numbers, IN char *operators)

int i = 0;
for (;operators[i] != NULL; i++)
printf("%c", operators[i]);


/*Return result based on operator*/
int getOp(IN int a, IN int b, IN char op)

if (op == '*')
return a * b;

if (op == '/')
return a / b;

if (op == '+')
return a;

if (op == '-')
return -a;


/*Calculate array sum*/
void calSum(IN int *sorted, IN int length)

int i = 0;
int finalRes = 0;

for (; i <= length; i++)

printf("%d ", sorted[i]);
finalRes += sorted[i];


printf("%d", finalRes);


/*Accept the equation as input and sort*/
void sort(IN int *numbers, IN char *operators)

int sorted[256];

/*Iterators of arrays*/
int s = 0;
int n = 0;
int o = 0;

/*While expression is not over*/
while (operators[o] != NULL)

/*If operation is + or - then store integers in sorted array for later calculation*/
if (operators[o] == '+'

calSum(sorted, s);


/* Private Function Definitions **********************************************/


main.c



/* Includes ******************************************************************/
#include "calc.h"

/* Function Definitions ******************************************************/
INT wmain(IN SIZE_T nArgc, IN PCWSTR *ppcwszArgv)

int numbers[256];
char equ[256];

createEqu(&numbers, &equ);
sort(numbers, equ);
getch();

/* Succeeded */
return 0;







share|improve this question













I was asked to create an Advanced calculator in C, I call it advanced because it actually handles arithmetic rules.
It was mentioned that I don't need to handle code exceptions and that I should assume that the code has legal input.



It works as follows:



  1. Enter a number and press enter

  2. Enter an operator and press enter To

  3. To finish user input you press X and then enter.

Again I mention this, I assume correct input was put in the program.



calc.h



#pragma once

/*Sort the equation, deal with multipliers/divisors then sub and add, load it all in one array then recalculate */

/* Includes ******************************************************************/
#include <stdio.h>
#include <Windows.h>
/* Macros ********************************************************************/

/* Types *********************************************************************/

/* Global Variables *********************************************************/

/* Function Declarations *****************************************************/

/*Accept the equation as input and sort*/
void sort(IN int *numbers, IN char *operators);

/*Return result based on operator*/
int getOp(IN int a, IN int b, IN char op);

/*Create equation*/
void createEqu(IN int *numbers, IN char *operators);

/*Prints the equation*/
void print(IN int *numbers, IN char *operators);

/*Calculate array sum*/
void calSum(IN int *sorted, IN int length);


calc.c



/* Includes ******************************************************************/
#include "calc.h"

/* Macros ********************************************************************/

/* Types *********************************************************************/

/* Public Function Definitions ***********************************************/

/* Global Variables **********************************************************/

/*Create equation*/
void createEqu(IN int *numbers, IN char *operators)

/*Intialize equation creater, 0 get number, 1 get operator*/
int currentOperation = -1;

/*Iterate through the arrays*/
int n = 0;
int o = 0;

/*While equation input was not ended by user*/
while (currentOperation != 2)

/*Number or operator*/
if (currentOperation == -1)

printf("Enter numbern");
scanf(" %d", &numbers[n]);

/*If operator is negative, turn number into negative*/
if (o != 0 && operators[o - 1] == '-')

numbers[n] = getOp(numbers[n], NULL, operators[o - 1]);

n++;

else

printf("Enter operatorn");
scanf(" %c", &operators[o]);
o++;


currentOperation = currentOperation*-1;
/*check if the last operator was X to terminate equation input*/
if (operators[o - 1] == 'X')

currentOperation = 2;



/*Each array is terminated with NULL so it would be easy to read through them*/

operators[o-1] = NULL;
numbers[n] = NULL;


/*Prints the equation*/
void print(IN int *numbers, IN char *operators)

int i = 0;
for (;operators[i] != NULL; i++)
printf("%c", operators[i]);


/*Return result based on operator*/
int getOp(IN int a, IN int b, IN char op)

if (op == '*')
return a * b;

if (op == '/')
return a / b;

if (op == '+')
return a;

if (op == '-')
return -a;


/*Calculate array sum*/
void calSum(IN int *sorted, IN int length)

int i = 0;
int finalRes = 0;

for (; i <= length; i++)

printf("%d ", sorted[i]);
finalRes += sorted[i];


printf("%d", finalRes);


/*Accept the equation as input and sort*/
void sort(IN int *numbers, IN char *operators)

int sorted[256];

/*Iterators of arrays*/
int s = 0;
int n = 0;
int o = 0;

/*While expression is not over*/
while (operators[o] != NULL)

/*If operation is + or - then store integers in sorted array for later calculation*/
if (operators[o] == '+'

calSum(sorted, s);


/* Private Function Definitions **********************************************/


main.c



/* Includes ******************************************************************/
#include "calc.h"

/* Function Definitions ******************************************************/
INT wmain(IN SIZE_T nArgc, IN PCWSTR *ppcwszArgv)

int numbers[256];
char equ[256];

createEqu(&numbers, &equ);
sort(numbers, equ);
getch();

/* Succeeded */
return 0;









share|improve this question












share|improve this question




share|improve this question








edited Feb 4 at 22:39









200_success

123k14143401




123k14143401









asked Feb 4 at 21:35









Potato

261




261











  • Clearly state and add the review goals.
    – chux
    Feb 6 at 6:26










  • Where is IN defined?
    – chux
    Feb 6 at 6:27
















  • Clearly state and add the review goals.
    – chux
    Feb 6 at 6:26










  • Where is IN defined?
    – chux
    Feb 6 at 6:27















Clearly state and add the review goals.
– chux
Feb 6 at 6:26




Clearly state and add the review goals.
– chux
Feb 6 at 6:26












Where is IN defined?
– chux
Feb 6 at 6:27




Where is IN defined?
– chux
Feb 6 at 6:27










2 Answers
2






active

oldest

votes

















up vote
2
down vote














  1. Poor use of NULL. In operators[o] != NULL, NULL is a null pointer constant best used with pointers. operators[o] is a char, not a pointer. Suggest below:



    // while (operators[o] != NULL)
    while (operators[o])
    // or
    while (operators[o] != '')



  2. Similar problem with NULL below. Do not use NULL to indicate the null character.



    // getOp(numbers[n], NULL, operators[o - 1]);
    getOp(numbers[n], '', operators[o - 1]);

    // numbers[n] = NULL;
    numbers[n] = '';


  3. void print(IN int *numbers, IN char *operators) is strange in that it never uses numbers. Certainly incorrect functionality.


  4. Highly suspicious about the correctness of the value of s in calSum(sorted, s);. Such terse variable loses clarity as to it role.


Minor



  1. " " in scanf(" %d", &numbers[n]); serve no purpose. Code can be simplified to scanf("%d", &numbers[n]); and retain the same functionality.


  2. Code that does not exceed the presentation width is more clear and easier to re-view. (Code should not need horizontal scroll bars.)



  3. I'd expect const for pointer to data that is not modified by the code. const better conveys code's intent and allows for select optimizations.



     // void print(IN int *numbers, IN char *operators)
    void print(const int *numbers, const char *operators)


  4. The role of IN is not defined and remains unclear.






share|improve this answer























  • Thank you very much for your feedback! IN indicates INPUT while OUT indicates OUTPUT, these are the conventions i had to use.
    – Potato
    Feb 6 at 20:41










  • the space in the scanf invokes it to ignore the previous 'n'
    – Potato
    Feb 6 at 20:51










  • @Potato The space if not needed to ignore the previous 'n'. scanf("%d", &numbers[n]); will ignore leading white-space, even without a space in the format. Hence, " " in scanf(" %d", &numbers[n]); serves no purpose.
    – chux
    Feb 7 at 2:02

















up vote
1
down vote













Strange use of currentOperation in calc.c: it is a state variable. Its values should be states that should have been defined with #defines.



currentOperation = currentOperation*-1;


This changes the state from the initial state -1 to the "work-in-progress" state 1 and later from state 1 to state -1. Why not use assignment to make it clear? For example:



#define STATE_0 -1
#define STATE_1 1
#define STATE_END 2



getOp: that is a strange name "get operator/operation" where it calaculates an experssion. Shouldn't calc or so be a better name?



Also, this function sees + and - as unary and / and * as binary. Where/how are binary + and - handled?






share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186759%2fadvanced-c-calculator%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote














    1. Poor use of NULL. In operators[o] != NULL, NULL is a null pointer constant best used with pointers. operators[o] is a char, not a pointer. Suggest below:



      // while (operators[o] != NULL)
      while (operators[o])
      // or
      while (operators[o] != '')



    2. Similar problem with NULL below. Do not use NULL to indicate the null character.



      // getOp(numbers[n], NULL, operators[o - 1]);
      getOp(numbers[n], '', operators[o - 1]);

      // numbers[n] = NULL;
      numbers[n] = '';


    3. void print(IN int *numbers, IN char *operators) is strange in that it never uses numbers. Certainly incorrect functionality.


    4. Highly suspicious about the correctness of the value of s in calSum(sorted, s);. Such terse variable loses clarity as to it role.


    Minor



    1. " " in scanf(" %d", &numbers[n]); serve no purpose. Code can be simplified to scanf("%d", &numbers[n]); and retain the same functionality.


    2. Code that does not exceed the presentation width is more clear and easier to re-view. (Code should not need horizontal scroll bars.)



    3. I'd expect const for pointer to data that is not modified by the code. const better conveys code's intent and allows for select optimizations.



       // void print(IN int *numbers, IN char *operators)
      void print(const int *numbers, const char *operators)


    4. The role of IN is not defined and remains unclear.






    share|improve this answer























    • Thank you very much for your feedback! IN indicates INPUT while OUT indicates OUTPUT, these are the conventions i had to use.
      – Potato
      Feb 6 at 20:41










    • the space in the scanf invokes it to ignore the previous 'n'
      – Potato
      Feb 6 at 20:51










    • @Potato The space if not needed to ignore the previous 'n'. scanf("%d", &numbers[n]); will ignore leading white-space, even without a space in the format. Hence, " " in scanf(" %d", &numbers[n]); serves no purpose.
      – chux
      Feb 7 at 2:02














    up vote
    2
    down vote














    1. Poor use of NULL. In operators[o] != NULL, NULL is a null pointer constant best used with pointers. operators[o] is a char, not a pointer. Suggest below:



      // while (operators[o] != NULL)
      while (operators[o])
      // or
      while (operators[o] != '')



    2. Similar problem with NULL below. Do not use NULL to indicate the null character.



      // getOp(numbers[n], NULL, operators[o - 1]);
      getOp(numbers[n], '', operators[o - 1]);

      // numbers[n] = NULL;
      numbers[n] = '';


    3. void print(IN int *numbers, IN char *operators) is strange in that it never uses numbers. Certainly incorrect functionality.


    4. Highly suspicious about the correctness of the value of s in calSum(sorted, s);. Such terse variable loses clarity as to it role.


    Minor



    1. " " in scanf(" %d", &numbers[n]); serve no purpose. Code can be simplified to scanf("%d", &numbers[n]); and retain the same functionality.


    2. Code that does not exceed the presentation width is more clear and easier to re-view. (Code should not need horizontal scroll bars.)



    3. I'd expect const for pointer to data that is not modified by the code. const better conveys code's intent and allows for select optimizations.



       // void print(IN int *numbers, IN char *operators)
      void print(const int *numbers, const char *operators)


    4. The role of IN is not defined and remains unclear.






    share|improve this answer























    • Thank you very much for your feedback! IN indicates INPUT while OUT indicates OUTPUT, these are the conventions i had to use.
      – Potato
      Feb 6 at 20:41










    • the space in the scanf invokes it to ignore the previous 'n'
      – Potato
      Feb 6 at 20:51










    • @Potato The space if not needed to ignore the previous 'n'. scanf("%d", &numbers[n]); will ignore leading white-space, even without a space in the format. Hence, " " in scanf(" %d", &numbers[n]); serves no purpose.
      – chux
      Feb 7 at 2:02












    up vote
    2
    down vote










    up vote
    2
    down vote










    1. Poor use of NULL. In operators[o] != NULL, NULL is a null pointer constant best used with pointers. operators[o] is a char, not a pointer. Suggest below:



      // while (operators[o] != NULL)
      while (operators[o])
      // or
      while (operators[o] != '')



    2. Similar problem with NULL below. Do not use NULL to indicate the null character.



      // getOp(numbers[n], NULL, operators[o - 1]);
      getOp(numbers[n], '', operators[o - 1]);

      // numbers[n] = NULL;
      numbers[n] = '';


    3. void print(IN int *numbers, IN char *operators) is strange in that it never uses numbers. Certainly incorrect functionality.


    4. Highly suspicious about the correctness of the value of s in calSum(sorted, s);. Such terse variable loses clarity as to it role.


    Minor



    1. " " in scanf(" %d", &numbers[n]); serve no purpose. Code can be simplified to scanf("%d", &numbers[n]); and retain the same functionality.


    2. Code that does not exceed the presentation width is more clear and easier to re-view. (Code should not need horizontal scroll bars.)



    3. I'd expect const for pointer to data that is not modified by the code. const better conveys code's intent and allows for select optimizations.



       // void print(IN int *numbers, IN char *operators)
      void print(const int *numbers, const char *operators)


    4. The role of IN is not defined and remains unclear.






    share|improve this answer
















    1. Poor use of NULL. In operators[o] != NULL, NULL is a null pointer constant best used with pointers. operators[o] is a char, not a pointer. Suggest below:



      // while (operators[o] != NULL)
      while (operators[o])
      // or
      while (operators[o] != '')



    2. Similar problem with NULL below. Do not use NULL to indicate the null character.



      // getOp(numbers[n], NULL, operators[o - 1]);
      getOp(numbers[n], '', operators[o - 1]);

      // numbers[n] = NULL;
      numbers[n] = '';


    3. void print(IN int *numbers, IN char *operators) is strange in that it never uses numbers. Certainly incorrect functionality.


    4. Highly suspicious about the correctness of the value of s in calSum(sorted, s);. Such terse variable loses clarity as to it role.


    Minor



    1. " " in scanf(" %d", &numbers[n]); serve no purpose. Code can be simplified to scanf("%d", &numbers[n]); and retain the same functionality.


    2. Code that does not exceed the presentation width is more clear and easier to re-view. (Code should not need horizontal scroll bars.)



    3. I'd expect const for pointer to data that is not modified by the code. const better conveys code's intent and allows for select optimizations.



       // void print(IN int *numbers, IN char *operators)
      void print(const int *numbers, const char *operators)


    4. The role of IN is not defined and remains unclear.







    share|improve this answer















    share|improve this answer



    share|improve this answer








    edited Feb 6 at 6:50


























    answered Feb 6 at 6:45









    chux

    11.4k11238




    11.4k11238











    • Thank you very much for your feedback! IN indicates INPUT while OUT indicates OUTPUT, these are the conventions i had to use.
      – Potato
      Feb 6 at 20:41










    • the space in the scanf invokes it to ignore the previous 'n'
      – Potato
      Feb 6 at 20:51










    • @Potato The space if not needed to ignore the previous 'n'. scanf("%d", &numbers[n]); will ignore leading white-space, even without a space in the format. Hence, " " in scanf(" %d", &numbers[n]); serves no purpose.
      – chux
      Feb 7 at 2:02
















    • Thank you very much for your feedback! IN indicates INPUT while OUT indicates OUTPUT, these are the conventions i had to use.
      – Potato
      Feb 6 at 20:41










    • the space in the scanf invokes it to ignore the previous 'n'
      – Potato
      Feb 6 at 20:51










    • @Potato The space if not needed to ignore the previous 'n'. scanf("%d", &numbers[n]); will ignore leading white-space, even without a space in the format. Hence, " " in scanf(" %d", &numbers[n]); serves no purpose.
      – chux
      Feb 7 at 2:02















    Thank you very much for your feedback! IN indicates INPUT while OUT indicates OUTPUT, these are the conventions i had to use.
    – Potato
    Feb 6 at 20:41




    Thank you very much for your feedback! IN indicates INPUT while OUT indicates OUTPUT, these are the conventions i had to use.
    – Potato
    Feb 6 at 20:41












    the space in the scanf invokes it to ignore the previous 'n'
    – Potato
    Feb 6 at 20:51




    the space in the scanf invokes it to ignore the previous 'n'
    – Potato
    Feb 6 at 20:51












    @Potato The space if not needed to ignore the previous 'n'. scanf("%d", &numbers[n]); will ignore leading white-space, even without a space in the format. Hence, " " in scanf(" %d", &numbers[n]); serves no purpose.
    – chux
    Feb 7 at 2:02




    @Potato The space if not needed to ignore the previous 'n'. scanf("%d", &numbers[n]); will ignore leading white-space, even without a space in the format. Hence, " " in scanf(" %d", &numbers[n]); serves no purpose.
    – chux
    Feb 7 at 2:02












    up vote
    1
    down vote













    Strange use of currentOperation in calc.c: it is a state variable. Its values should be states that should have been defined with #defines.



    currentOperation = currentOperation*-1;


    This changes the state from the initial state -1 to the "work-in-progress" state 1 and later from state 1 to state -1. Why not use assignment to make it clear? For example:



    #define STATE_0 -1
    #define STATE_1 1
    #define STATE_END 2



    getOp: that is a strange name "get operator/operation" where it calaculates an experssion. Shouldn't calc or so be a better name?



    Also, this function sees + and - as unary and / and * as binary. Where/how are binary + and - handled?






    share|improve this answer

























      up vote
      1
      down vote













      Strange use of currentOperation in calc.c: it is a state variable. Its values should be states that should have been defined with #defines.



      currentOperation = currentOperation*-1;


      This changes the state from the initial state -1 to the "work-in-progress" state 1 and later from state 1 to state -1. Why not use assignment to make it clear? For example:



      #define STATE_0 -1
      #define STATE_1 1
      #define STATE_END 2



      getOp: that is a strange name "get operator/operation" where it calaculates an experssion. Shouldn't calc or so be a better name?



      Also, this function sees + and - as unary and / and * as binary. Where/how are binary + and - handled?






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        Strange use of currentOperation in calc.c: it is a state variable. Its values should be states that should have been defined with #defines.



        currentOperation = currentOperation*-1;


        This changes the state from the initial state -1 to the "work-in-progress" state 1 and later from state 1 to state -1. Why not use assignment to make it clear? For example:



        #define STATE_0 -1
        #define STATE_1 1
        #define STATE_END 2



        getOp: that is a strange name "get operator/operation" where it calaculates an experssion. Shouldn't calc or so be a better name?



        Also, this function sees + and - as unary and / and * as binary. Where/how are binary + and - handled?






        share|improve this answer













        Strange use of currentOperation in calc.c: it is a state variable. Its values should be states that should have been defined with #defines.



        currentOperation = currentOperation*-1;


        This changes the state from the initial state -1 to the "work-in-progress" state 1 and later from state 1 to state -1. Why not use assignment to make it clear? For example:



        #define STATE_0 -1
        #define STATE_1 1
        #define STATE_END 2



        getOp: that is a strange name "get operator/operation" where it calaculates an experssion. Shouldn't calc or so be a better name?



        Also, this function sees + and - as unary and / and * as binary. Where/how are binary + and - handled?







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Feb 6 at 10:11









        Paul Ogilvie

        1194




        1194






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186759%2fadvanced-c-calculator%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Greedy Best First Search implementation in Rust

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            C++11 CLH Lock Implementation