Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.58 KB | None | 0 0
  1. #define ll long long
  2.  
  3. #include <stdio.h>
  4. #include <mm_malloc.h>
  5. #include <memory.h>
  6. #include <stdbool.h>
  7. #include <ctype.h>
  8.  
  9. typedef struct {
  10.     int* arrayOfIntegers;
  11.     int size;
  12. } stack;
  13.  
  14.  
  15. stack* createStack(size_t size) {
  16.     stack *newStack = calloc(1, sizeof(stack));
  17.     newStack -> arrayOfIntegers = (int*)calloc(size, sizeof(int));
  18.     newStack -> size = 0;
  19.     return newStack;
  20. }
  21.  
  22. //Add the element to stack                                                                  Time: O(1)
  23. void push(stack* stack, int value) {
  24.  
  25. }
  26.  
  27. //Returns the element on the peek of stack                                                  Time: O(1)
  28. int peek(stack* stack) {
  29.  
  30. }
  31.  
  32. //Delete the peek element from stack                                                        Time: O(1)
  33. void pop(stack* stack) {
  34.  
  35. }
  36.  
  37.  
  38. char* getLine() {
  39.  
  40. }
  41.  
  42. //Splitting expression to "Tokens" which can be numbers or symbols of operations            Time: O(length)
  43. char** splitBySpaces(char* expression, int *quantityOfTokens) {
  44.  
  45. }
  46.  
  47.  
  48. //All of the methods below are calculating an operation for two peek operands in stack      Time: O(1)
  49. void operationAdd(stack* stack) {
  50.  
  51. }
  52.  
  53. void operationSubtract(stack* stack) {
  54.  
  55. }
  56.  
  57. void operationMultiply(stack* stack) {
  58.  
  59. }
  60.  
  61. void operationDivide(stack* stack) {
  62.  
  63. }
  64.  
  65. //Checking a token if it's a number                                                           Time: O(length)
  66. bool isNumber(char* token) {
  67.     for (size_t i = 0; i < strlen(token); i++) {
  68.         if (!isdigit(token[i])) {
  69.             return false;
  70.         }
  71.     }
  72.     return true;
  73. }
  74.  
  75. int main() {
  76.     stack* stackOfNumbers = createStack(1000);
  77.  
  78.     char* expression = (char*)calloc(1001, sizeof(char));
  79.     if (!expression) {
  80.         printf("No memory?!");
  81.         exit(1);
  82.     } else {
  83.         expression = getLine();
  84.     }
  85.  
  86.     int quantityOfTokens = 0;
  87.     char** splittedString = splitBySpaces(expression, &quantityOfTokens);
  88.  
  89.     for (int i = 0; i < quantityOfTokens; i++) {
  90.         if (strcmp(splittedString[i], "+") == 0) {
  91.             operationAdd(stackOfNumbers);
  92.         } else if (strcmp(splittedString[i], "-") == 0) {
  93.             operationSubtract(stackOfNumbers);
  94.         } else if (strcmp(splittedString[i], "*") == 0) {
  95.             operationMultiply(stackOfNumbers);
  96.         } else if (strcmp(splittedString[i], "/") == 0) {
  97.             operationDivide(stackOfNumbers);
  98.         } else if (isNumber(splittedString[i])){
  99.             push(stackOfNumbers, );
  100.         } else {
  101.             printf("bad input");
  102.             exit(1);
  103.         }
  104.     }
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement