Skip to content

Instantly share code, notes, and snippets.

@mindw
Created April 22, 2017 09:05
Show Gist options
  • Select an option

  • Save mindw/161e0111c99bd5657f1bf5231f266024 to your computer and use it in GitHub Desktop.

Select an option

Save mindw/161e0111c99bd5657f1bf5231f266024 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
// your code goes here
double sum = 0;
const char *input = "300 - 400";
// const char *input = "1 + 3 / 5 * 8";
// const char *input = "10 + 34 / 3 + 8";
double second_num = 0;
// read the first num
sum = atof(input);
while(isdigit(*input) || isspace(*input))
{
++input;
}
while(*input)
{
switch(*input)
{
case '/':
{
second_num = atof(++input);
sum /= second_num;
break;
}
case '+':
{
second_num = atof(++input);
sum += second_num;
break;
}
case '-':
{
second_num = atof(++input);
sum -= second_num;
break;
}
case '*':
{
second_num = atof(++input);
sum *= second_num;
break;
}
}
while(isdigit(*input) || isspace(*input))
{
++input;
}
}
printf("%g\n", sum);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment