summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorRasmus Luha <rasmus.luha@gmail.com>2022-09-21 14:16:01 +0300
committerRasmus Luha <rasmus.luha@gmail.com>2022-09-21 14:16:01 +0300
commitc34cd59be89fb26e63fe959b98afabb4f24ba5bd (patch)
tree60531983cb0bfef27ab91cb3be41ebd40b324c9f /src/main.c
init commit
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..ec1de31
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,45 @@
+#include <stdio.h>
+#include "stack.h"
+
+int main() {
+ int x, y, z;
+
+ while (1) {
+ scanf("%d", &x);
+
+ // 1. Kui x==0 siis print stack.
+ if (x == 0){
+ printf("\nPrindin stacki\n");
+ stackPrint();
+ break;
+ }
+
+ // 2. Kui empty Push.
+ else if (stackIsEmpty())
+ stackPush(x);
+
+ // 3. Kui x pos. ja peeked pos: push(x).
+ // 4. kui x pos. aga peeked neg., y=pop() ja push(x+y).
+ else if (x >= 0){
+ y = stackPeek();
+ if (y >= 0)
+ stackPush(x);
+
+ else{
+ z = stackPop();
+ if (x+z != 0)
+ stackPush(x+z);
+ }
+ }
+
+ // 5. Kui x on neg, siis pop()
+ else if (x < 0) {
+ z = stackPop();
+ if (x+z != 0) {
+ stackPush(x+z);
+ }
+ }
+
+ } // While loop End
+ return 0;
+}