Advertisement
Guest User

Игорь, ты? Помнишь меня? Я твой одноклассник. Я узнал тебя п

a guest
Aug 24th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. /* infix notation calculator in C using Dijkstra's shunting-yard algorithm */
  2.  
  3. #include <limits.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct Stack {
  8.     int top;
  9.     unsigned capacity;
  10.     int* array;
  11. };
  12.  
  13. struct Stack* createStack(unsigned capacity)
  14. {
  15.     struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
  16.     stack->capacity = capacity;
  17.     stack->top = -1;
  18.     stack->array = (int*)malloc(stack->capacity * sizeof(int));
  19.     return stack;
  20. }
  21.  
  22. int isFull(struct Stack* stack)
  23. {
  24.     return stack->top == stack->capacity - 1;
  25. }
  26.  
  27. int isEmpty(struct Stack* stack)
  28. {
  29.     return stack->top == -1;
  30. }
  31.  
  32. void push(struct Stack* stack, int item)
  33. {
  34.     if (isFull(stack))
  35.         return;
  36.     stack->array[++stack->top] = item;
  37.     printf("%d pushed to stack\n", item);
  38. }
  39.  
  40. int pop(struct Stack* stack)
  41. {
  42.     if (isEmpty(stack))
  43.         return INT_MIN;
  44.     return stack->array[stack->top--];
  45. }
  46.  
  47. int peek(struct Stack* stack)
  48. {
  49.     if (isEmpty(stack))
  50.         return INT_MIN;
  51.     return stack->array[stack->top];
  52. }
  53.  
  54. enum TokenType {
  55.     NUMBER = 0,
  56.     OPERATOR = 1
  57. };
  58.  
  59. struct Token {
  60.     char* raw;  // char* raw = char raw[]; is it legal?
  61.     enum TokenType* type;
  62. };
  63.  
  64. struct Token* createToken (char raw[], enum TokenType* type) {
  65.     struct Token* token = (struct Token*)malloc(sizeof(struct Token));
  66.     token->raw = raw;
  67.     token->type = type;
  68. }
  69.  
  70. enum TokenType getType (char raw[]) {
  71.    
  72. }
  73.  
  74. struct Token* tokenize (char raw[]) {
  75.     struct Token tokens[] = {createToken("42", NUMBER)};
  76.     unsigned int tokensLength = sizeof(tokens) / sizeof(tokens[0]);
  77.     unsigned int tokensSize = sizeof(struct Token) * tokensLength;
  78.  
  79.     struct Token* ret = malloc(tokensSize);
  80.     for(int i = 0; i < tokensLength; ++i) {
  81.         ret[i] = tokens[i];
  82.         printf("%d\n", tokens[i]);
  83.     }
  84.  
  85.  
  86.     return ret;
  87. }
  88.  
  89.  
  90. // some info on c-like strings:
  91. // Do not confuse a single-character string literal, e.g. "A" with a character constant, 'A'.
  92. // The former is actually two characters, because of the terminating null byte stored at the end.
  93. // The string is basically bounded from the place where it is pointed to (char *ptrChar;), to the null character (\0).
  94. int main() {
  95.     char raw[64];
  96.     scanf("%64s", raw);
  97.     struct Token *tokens = tokenize(raw);
  98.     printf(tokens->raw);
  99.     //struct Token* tokens[] = tokenize(raw);
  100.     //printf(tokens[0]->raw);
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement