-1

How do I make it that when I divide by 2 numbers(10/2/2) it will give me the right solution. Also how do I make it find the remainder when one number is divided by another.

How would I begin with being able to divide by 2 or more numbers? With if-else statements.

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

void main()
{
    char operation = 0;
    double num1=0.0 ;
    double num2=0.0 ;
    printf("Enter the calculation\n");
    scanf("%f %c %f", &num1, &operation, &num2);

    switch (operation) {
    case '+':
        printf("= %f\n", num1 + num2);
        break;
    case '-':
        printf("= %f\n", num1 - num2);
        break;
    case '*':
        printf("= %f\n", num1*num2);
        break;
    case '/': 
        printf("= %f\n", num1 /(double)num2);
        break;
    case '%':
        printf("= %f\n", num1 % (double)num2);
        break;
    default:
        printf("Error!\n");
        break;
    }
    system("pause");
}
TheEWL
  • 19
  • 6

3 Answers3

1

You can get the remainder with the % operator

  case '%':
        printf("= %d\n", (int)num1 %(int)num2);
        break;

As for dividing the number multiple times, you will need to improve your input parsing, create a loop to do multiple operations and save the results (to possibly num1).

vincentleest
  • 835
  • 1
  • 7
  • 18
  • 3
    There is also the floating point remainder function `fmod` from `math.h`. – fuz Apr 21 '15 at 20:43
  • How would I do the dividing the number multiple times part? With if else statements – TheEWL Apr 21 '15 at 21:05
  • 1
    With only if-else statements, you cannot divide a number arbitrarily many times. You will need a loop somewhere. – vincentleest Apr 21 '15 at 21:08
  • C does not have a modulo operator. `%` is the remainder operator. Similar, but different math operations. – chux - Reinstate Monica Apr 22 '15 at 01:32
  • @chux The OP is asking for the remainder. And I believe i have the correct name for it according to wiki http://en.wikipedia.org/wiki/Modulo_operation – vincentleest Apr 22 '15 at 01:34
  • 1
    The modulo operator has various meanings in math/science/computing. C does not define "modulo operator". C does define the `%` operator with "The result of the `/` operator is the quotient from the division of the first operand by the second; the result of the `%` operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.". §6.5.5 5 – chux - Reinstate Monica Apr 22 '15 at 01:59
1

To get the remainder that you're looking for, you have to cast both sides to int:

    printf("= %d\n", (int)num1 % (int)num2);

To parse complex expressions, you need something completely different. Right now your code only does a single operation, and so can ignore operator precidence. But when you chain operations, you need to pay attention to that. Like 1 - 2 * 3, your code should do the 2*3 and then add in the 1. The easiest way to do this is a recursive decent parser: http://en.wikipedia.org/wiki/Recursive_descent_parser

Here are the basic/simple rule concepts:

line       -> expression <EOF>
expression -> factor + expression 
              factor - expression
              factor
factor     -> term * factor
              term / factor
              term % factor
              term
term       -> ( expression )
              <number>

Then the code for each of these rules these looks vaguely like this:

double read_factor(...) {
    double first = read_term(...);
    double second = 0.0;
    switch(peek_next_symbol()) {
    case '*': 
        consume_symbol();
        second = read_term();
        return first * second;
    case '/': 
        consume_symbol();
        second = read_term();
        if (second == 0)
            throw std::logic_error("div/0");
        return first / second;
    case '%': 
        consume_symbol();
        second = read_term();
        if (second == 0)
            throw std::logic_error("mod/0");
        return (double)((int)first % (int)second);
    default:
        return first;
    }
}

And then term looks vaguely like this:

double read_term() {
    double first = 0.0;
    switch(peek_next_symbol()) {
    case '(':
        consume_symbol();
        first = read_expression();
        if (peek_next_symbol() != ')')
            throw std::logic_error("missing )");
        consume_symbol();
    default:
        return read_number();
    }
}

And each of these functions I reference are all relatively complicated, and there's plenty of state being passed around. Now that I've briefly explained the theory, here's a tiny working sample: http://ideone.com/wVqRw

Mooing Duck
  • 56,371
  • 16
  • 89
  • 146
0

Remainder and modulo are similar though often subtly different.

In C, % is the remainder after division. In C, modulo is not defined for integers.

fmod() exists for FP numbers and it is also "floating-point remainder of x/y."

To get a floating point "remainder operator", code could use

case '%':
        {
        printf("= %f\n", fmod(num1, num2));
        break;
        }

In mathematics, the result of the modulo operation is often defined as the remainder of Euclidean division. If code wanted to do a Euclidean division to get the modulo:

case 'M':
        {
        double quot = floor(num1/num2);
        double rem = num1 - quot * num2;
        printf("= %f\n", rem);
        break;
        }

Note: Suggest using "%g" or "%e" for printing.

chux - Reinstate Monica
  • 113,725
  • 11
  • 107
  • 213