Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* infix notation calculator in C using Dijkstra's shunting-yard algorithm */
- #include <limits.h>
- #include <stdio.h>
- #include <stdlib.h>
- struct Stack {
- int top;
- unsigned capacity;
- int* array;
- };
- struct Stack* createStack(unsigned capacity)
- {
- struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
- stack->capacity = capacity;
- stack->top = -1;
- stack->array = (int*)malloc(stack->capacity * sizeof(int));
- return stack;
- }
- int isFull(struct Stack* stack)
- {
- return stack->top == stack->capacity - 1;
- }
- int isEmpty(struct Stack* stack)
- {
- return stack->top == -1;
- }
- void push(struct Stack* stack, int item)
- {
- if (isFull(stack))
- return;
- stack->array[++stack->top] = item;
- printf("%d pushed to stack\n", item);
- }
- int pop(struct Stack* stack)
- {
- if (isEmpty(stack))
- return INT_MIN;
- return stack->array[stack->top--];
- }
- int peek(struct Stack* stack)
- {
- if (isEmpty(stack))
- return INT_MIN;
- return stack->array[stack->top];
- }
- enum TokenType {
- NUMBER = 0,
- OPERATOR = 1
- };
- struct Token {
- char* raw; // char* raw = char raw[]; is it legal?
- enum TokenType* type;
- };
- struct Token* createToken (char raw[], enum TokenType* type) {
- struct Token* token = (struct Token*)malloc(sizeof(struct Token));
- token->raw = raw;
- token->type = type;
- }
- enum TokenType getType (char raw[]) {
- }
- struct Token* tokenize (char raw[]) {
- struct Token tokens[] = {createToken("42", NUMBER)};
- unsigned int tokensLength = sizeof(tokens) / sizeof(tokens[0]);
- unsigned int tokensSize = sizeof(struct Token) * tokensLength;
- struct Token* ret = malloc(tokensSize);
- for(int i = 0; i < tokensLength; ++i) {
- ret[i] = tokens[i];
- printf("%d\n", tokens[i]);
- }
- return ret;
- }
- // some info on c-like strings:
- // Do not confuse a single-character string literal, e.g. "A" with a character constant, 'A'.
- // The former is actually two characters, because of the terminating null byte stored at the end.
- // The string is basically bounded from the place where it is pointed to (char *ptrChar;), to the null character (\0).
- int main() {
- char raw[64];
- scanf("%64s", raw);
- struct Token *tokens = tokenize(raw);
- printf(tokens->raw);
- //struct Token* tokens[] = tokenize(raw);
- //printf(tokens[0]->raw);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement