Created
April 22, 2017 09:05
-
-
Save mindw/161e0111c99bd5657f1bf5231f266024 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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