Search code examples
carraysdigits

Turning digits in an array to an integer. (and more)


Im new to C and trying to build a program thats uses only:

  • arrays,
  • loops,
  • if-else,
  • basic functions (print, scan)
  • no strings and such!

My requirements:

  • I should get an equation from user in one line. '=' ends the equation.
  • There must be exactly two numbers
  • There must be exactly one operator

Example input:

589*919=

I need to insert the equation into an array. Each digit or operator sits at another place in the array.

Array example:

chars array: | 5 | 8 | 9 | * | 9 | 1 | 9 |

I then need to turn the digits in the array into two integers, and calculate the answer of the equation.

How can I turn the digits in the array into two integers?

My code so far:

#include <stdio.h>
#include <stdlib.h>
#pragma warning (disable: 4996)
#define SIZE 122

void main()
{
    int count = 0;
    int i;
    char input;
    char equation[SIZE];
    char key = 0;
    int oprIndex;

    printf("Insert equation:\n");
    scanf("%c", &input);

    for (i = 0; i<SIZE && input != '='; i++) //fill the array with the equation
    {
        equation[i] = input;
        scanf("%c", &input);
        count++;
    }

    //searching for which operater user inserted
    key = '+';
    for (i = 0; i < count && equation[i] != key; i++);
    if (equation[i] == key)
    {
        printf("key: %c\n", key);
        oprIndex = i;
        printf("index: %d\n", oprIndex);
    }

    key = '-';
    for (i = 0; i < count && equation[i] != key; i++);
    if (equation[i] == key)
    {
        printf("key: %c\n", key);
        oprIndex = i;
        printf("index: %d\n", oprIndex);
    }

    key = '*';
    for (i = 0; i < count && equation[i] != key; i++);
    if (equation[i] == key)
    {
        printf("key: %c\n", key);
        oprIndex = i;
        printf("index: %d\n", oprIndex);
    }

    key = '/';
    for (i = 0; i < count && equation[i] != key; i++);
    if (equation[i] == key)
    {
        printf("key: %c\n", key);
        oprIndex = i;
        printf("index: %d\n", oprIndex);
    }
    //end of searching

    for (i = 0; i < count; i++) //print the equation
    {
        printf("%c", equation[i]);
    }
    printf("=");
    printf("\n");

    system("pause");
}

Solution

  • Just use for statement with + and *.

    If what you don't know is the way to convert character to corresponding number, then use -'0'.

    For example, '5'-'0' will gives you 5, integer value.

    Edit:

    I add some example for you.

    int main(void) // use return value of int, not void
    {
        ... // After you get oprIndex.
    
        // Declare
        // int opA, opB;
        // first.
    
        opA = 0;
        for (i = 0; i < oprIndex; i++)
        {
            opA *= 10;
            opA += equation[i] - '0';
        }
    
        opB = 0;
        for (i = oprIndex + 1; i < count; i++)
        {
            opB *= 10;
            opB += equation[i] - '0';
        }
    
        ... // Do calcuation.
    
        return 0; // Use return 0; at the last line of the main function.
                  // For C++, this is added implicitly,
                  // but in C, implicit adding is not in standard.
    }
    

    However, in your code, there are several semantic bugs
    (for example,

    key = '+';
    for (i = 0; i < count && equation[i] != key; i++);
    if (equation[i] == key)
    

    makes an segmentation fault when your count equals to SIZE and there is no + in the equation.),
    so refactor your logic and code for right execution.