-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsum.c
68 lines (56 loc) · 1.08 KB
/
parsum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"cs50.h"
int main (void)
{
string str = GetString();
int result = 0;
int len = strlen(str);
char op = ' ';
int n = 0;
int i;
for (i = 0; i < len; i++)
{
switch (str[i])
{
case '-':
{
if (op == '-')
result -= n;
else
result += n;
n = 0;
op = '-';
break;
}
case '+':
{
if (op == '-')
result -= n;
else
result += n;
op = '+';
n = 0;
//printf("\nresult = %d n = %d\n", result, n);
break;
}
case ' ':{ break;}
default:
{//The caracter is a numerical
char num[8];
num[0] = str[i];
num[1] = '\n';
n = (n*10 + atoi(num))%10000000;
break;
}
}
}
//The last number
if (op == '-')
result -= n;
else
result += n;
printf("%d\n", result);
return 0;
}