File tree Expand file tree Collapse file tree 4 files changed +147
-0
lines changed Expand file tree Collapse file tree 4 files changed +147
-0
lines changed Original file line number Diff line number Diff line change 1+ all :
2+ gcc -O2 -o test calculator.c
Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+ #include <stdlib.h>
3+
4+ static int calculator (char * s )
5+ {
6+ int n ;
7+ int pos1 = 0 ;
8+ int pos2 = 0 ;
9+ int * nums = malloc (1000 * sizeof (int ));
10+ char * signs = malloc (1000 * sizeof (char ));
11+
12+ nums [pos1 ++ ] = 0 ;
13+ while (* s != '\0' ) {
14+ switch (* s ) {
15+ case '+' :
16+ case '-' :
17+ case '(' :
18+ signs [pos2 ++ ] = * s ;
19+ break ;
20+ case ')' :
21+ -- pos2 ;
22+ if (pos1 >= 2 && pos2 > 0 && signs [pos2 - 1 ] != '(' ) {
23+ n = nums [-- pos1 ];
24+ int a = nums [-- pos1 ];
25+ if (signs [-- pos2 ] == '+' ) {
26+ n = a + n ;
27+ } else {
28+ n = a - n ;
29+ }
30+ }
31+ nums [pos1 ++ ] = n ;
32+ break ;
33+ case ' ' :
34+ break ;
35+ default :
36+ n = 0 ;
37+ while (* s >= '0' && * s <= '9' ) {
38+ n = n * 10 + (* s - '0' );
39+ s ++ ;
40+ }
41+ s -- ;
42+
43+ if (pos1 >= 2 && signs [pos2 - 1 ] != '(' && signs [pos2 - 1 ] != '(' ) {
44+ int a = nums [-- pos1 ];
45+ if (signs [-- pos2 ] == '+' ) {
46+ n = a + n ;
47+ } else {
48+ n = a - n ;
49+ }
50+ }
51+ nums [pos1 ++ ] = n ;
52+ break ;
53+ }
54+ s ++ ;
55+ }
56+
57+ return n ;
58+ }
59+
60+ int main (int argc , char * * argv )
61+ {
62+ if (argc != 2 ) {
63+ fprintf (stderr , "Usage: ./test string\n" );
64+ exit (-1 );
65+ }
66+ printf ("%d\n" , calculator (argv [1 ]));
67+ return 0 ;
68+ }
Original file line number Diff line number Diff line change 1+ all :
2+ gcc -O2 -o test calculator.c
Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+ #include <stdlib.h>
3+
4+ static int calculator (char * s )
5+ {
6+ int n ;
7+ int pos1 = 0 ;
8+ int pos2 = 0 ;
9+ int * nums = malloc (100 * sizeof (int ));
10+ char * signs = malloc (100 * sizeof (char ));
11+
12+ nums [pos1 ++ ] = 0 ;
13+ while (* s != '\0' ) {
14+ switch (* s ) {
15+ case '+' :
16+ case '-' :
17+ if (pos1 >= 3 ) {
18+ int b = nums [pos1 - 1 ];
19+ int a = nums [pos1 - 2 ];
20+ if (signs [-- pos2 ] == '+' ) {
21+ nums [pos1 - 2 ] = a + b ;
22+ } else {
23+ nums [pos1 - 2 ] = a - b ;
24+ }
25+ pos1 -- ;
26+ }
27+ case '*' :
28+ case '/' :
29+ signs [pos2 ++ ] = * s ;
30+ break ;
31+ case ' ' :
32+ break ;
33+ default :
34+ n = 0 ;
35+ while (* s >= '0' && * s <= '9' ) {
36+ n = n * 10 + (* s - '0' );
37+ s ++ ;
38+ }
39+ s -- ;
40+
41+ if (pos1 >= 2 && signs [pos2 - 1 ] != '+' && signs [pos2 - 1 ] != '-' ) {
42+ int a = nums [-- pos1 ];
43+ if (signs [-- pos2 ] == '*' ) {
44+ n = a * n ;
45+ } else {
46+ n = a / n ;
47+ }
48+ }
49+ nums [pos1 ++ ] = n ;
50+ break ;
51+ }
52+ s ++ ;
53+ }
54+
55+ while (pos2 > 0 ) {
56+ n = nums [-- pos1 ];
57+ int a = nums [-- pos1 ];
58+ if (signs [-- pos2 ] == '+' ) {
59+ n = a + n ;
60+ } else {
61+ n = a - n ;
62+ }
63+ }
64+ return n ;
65+ }
66+
67+ int main (int argc , char * * argv )
68+ {
69+ if (argc != 2 ) {
70+ fprintf (stderr , "Usage: ./test string\n" );
71+ exit (-1 );
72+ }
73+ printf ("%d\n" , calculator (argv [1 ]));
74+ return 0 ;
75+ }
You can’t perform that action at this time.
0 commit comments