Skip to content

Commit

Permalink
Basic calculator
Browse files Browse the repository at this point in the history
Signed-off-by: begeekmyfriend <[email protected]>
  • Loading branch information
begeekmyfriend committed Jan 9, 2018
1 parent b55babc commit a000d94
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 224_basic_calculator/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test calculator.c
68 changes: 68 additions & 0 deletions 224_basic_calculator/calculator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <stdio.h>
#include <stdlib.h>

static int calculator(char *s)
{
int n;
int pos1 = 0;
int pos2 = 0;
int *nums = malloc(1000 * sizeof(int));
char *signs = malloc(1000 * sizeof(char));

nums[pos1++] = 0;
while (*s != '\0') {
switch (*s) {
case '+':
case '-':
case '(':
signs[pos2++] = *s;
break;
case ')':
--pos2;
if (pos1 >= 2 && pos2 > 0 && signs[pos2 - 1] != '(') {
n = nums[--pos1];
int a = nums[--pos1];
if (signs[--pos2] == '+') {
n = a + n;
} else {
n = a - n;
}
}
nums[pos1++] = n;
break;
case ' ':
break;
default:
n = 0;
while(*s >= '0' && *s <= '9') {
n = n * 10 + (*s - '0');
s++;
}
s--;

if (pos1 >= 2 && signs[pos2 - 1] != '(' && signs[pos2 - 1] != '(') {
int a = nums[--pos1];
if (signs[--pos2] == '+') {
n = a + n;
} else {
n = a - n;
}
}
nums[pos1++] = n;
break;
}
s++;
}

return n;
}

int main(int argc, char **argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: ./test string\n");
exit(-1);
}
printf("%d\n", calculator(argv[1]));
return 0;
}
2 changes: 2 additions & 0 deletions 227_basic_calculator_ii/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test calculator.c
75 changes: 75 additions & 0 deletions 227_basic_calculator_ii/calculator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <stdio.h>
#include <stdlib.h>

static int calculator(char *s)
{
int n;
int pos1 = 0;
int pos2 = 0;
int *nums = malloc(100 * sizeof(int));
char *signs = malloc(100 * sizeof(char));

nums[pos1++] = 0;
while (*s != '\0') {
switch (*s) {
case '+':
case '-':
if (pos1 >= 3) {
int b = nums[pos1 - 1];
int a = nums[pos1 - 2];
if (signs[--pos2] == '+') {
nums[pos1 - 2] = a + b;
} else {
nums[pos1 - 2] = a - b;
}
pos1--;
}
case '*':
case '/':
signs[pos2++] = *s;
break;
case ' ':
break;
default:
n = 0;
while(*s >= '0' && *s <= '9') {
n = n * 10 + (*s - '0');
s++;
}
s--;

if (pos1 >= 2 && signs[pos2 - 1] != '+' && signs[pos2 - 1] != '-') {
int a = nums[--pos1];
if (signs[--pos2] == '*') {
n = a * n;
} else {
n = a / n;
}
}
nums[pos1++] = n;
break;
}
s++;
}

while (pos2 > 0) {
n = nums[--pos1];
int a = nums[--pos1];
if (signs[--pos2] == '+') {
n = a + n;
} else {
n = a - n;
}
}
return n;
}

int main(int argc, char **argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: ./test string\n");
exit(-1);
}
printf("%d\n", calculator(argv[1]));
return 0;
}

0 comments on commit a000d94

Please sign in to comment.