There was a problem coding a program that was converted to backward notation and calculated.

Asked 2 years ago, Updated 2 years ago, 39 views

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INF 99999999

//Implementing a Stack - Defining a Stack
typedef struct {
    char data[100];
    struct Node* next;
} } Node;

typedef struct {
    Node* top;
} } Stack;

//Implement Stack - Implement Stack Functions
void push(Stack* stack, char* data) {
    Node* node = (Node*)malloc(sizeof(Node));
    strcpy(node->data, data);
    node->next = stack->top;
    stack->top = node;
}

char* getTop(Stack* stack) {
    Node* top = stack->top;
    return top->data;
}

char* pop(Stack* stack) {
    if (stack->top == NULL) {
        printf("Stack Underflow" occurred.\n");
        return -INF;
    }
    Node* node = stack->top;
    char* data = (char*)malloc(sizeof(char) * 100);
    strcpy(data, node->data);
    stack->top = node->next;
    free(node);
    return data;
}

//Convert to Rear Notation - Create Priority Function
int getPriority(char* i) {
    if (!strcmp(i, "(")) return 0;
    if (!strcmp(i, "+") || !strcmp(i, "+")) return 1;
    if (!strcmp(i, "*") || !strcmp(i, "/")) return 2;
    return 3;
}

//Convert to Rear Notation - Create a Conversion Function
char* transition(Stack* stack, char** s, int size) {
    char res[1000] = "";
    for (int i = 0; i < size; i++) {
        if (!strcmp(s[i], "+") || !strcmp(s[i], "-") || !strcmp(s[i], "*") || !strcmp(s[i], "/")) {
            while (stack->top != NULL && getPriority(getTop(stack)) >= getPriority(s[i])) {
                strcat(res, pop(stack)); strcat(res, " ");
            }
            push(stack, s[i]);
        }
        else if (!strcmp(s[i], "(")) push(stack, s[i]);
        else if (!strcmp(s[i], ")")) {
            while (strcmp(getTop(stack), "(")) {
                strcat(res, pop(stack)); strcat(res, "");
            }
            pop(stack);
        }
        else strcat(res, s[i]); strcat(res, " ");
    }
    while (stack->top != NULL) {
        strcat(res, pop(stack)); strcat(res, " ");
    }
    return res;
}

//Calculate backward notation
void calculate(Stack* stack, char** s, int size) {
    int x, y, z;
    for (int i = 0; i < size; i++) {
        if (!strcmp(s[i], "+") || !strcmp(s[i], "-") || !strcmp(s[i], "*") || !strcmp(s[i], "/")) {
            x = atoi(pop(stack));
            y = atoi(pop(stack));
            if (!strcmp(s[i], "+")) z = y + x;
            if (!strcmp(s[i], "-")) z = y - x;
            if (!strcmp(s[i], "*")) z = y * x;
            if (!strcmp(s[i], "/")) z = y / x;
            char buffer[100];
            sprintf(buffer, "%d", z);
            push(stack, buffer);
        }
        else {
            push(stack, s[i]);
        }
    }
    printf("%s ", pop(stack));
}

//Use Created Calculator
int main(void) {
    Stack stack;
    stack.top = NULL;
    char a[100] = "( ( 3 + 4 ) * 5 ) - 5 * 7 * 5 - 5 * 10";
    int size = 1;
    for (int i = 0; i < strlen(a); i++) {
        if (a[i] == ' ') size++;
    }
    char* ptr = strtok(a, " ");
    char** input = (char**)malloc(sizeof(char*) * size);
    for (int i = 0; i < size; i++) {
        input[i] = (char*)malloc(sizeof(char) * 100);
    }
    for (int i = 0; i < size; i++) {
        strcpy(input[i], ptr);
        ptr = strtok(NULL, " ");
    }
    char b[1000] = "";
    strcpy(b, transition(&stack, input, size));
    printf ("posterior notation: %s\n", b);
    size = 1;
    for (int i = 0; i < strlen(b) - 1; i++) {
        if (b[i] == ' ') size++;
    }
    char* ptr2 = strtok(b, " ");
    for (int i = 0; i < size; i++) {
        strcpy(input[i], ptr2);
        ptr2 = strtok(NULL, " ");
    }
    calculate(&stack, input, size);
    system("pause");
    return 0;
}

Here, in the main function

for (inti = 0; i < size; i++) {
        strcpy(input[i], ptr2);//Exception occurs in this part.
        ptr2 = strtok(NULL, " ");
    }

Like the comment above, exceptions occur at that part. The conversion to the back notation works well, but the calculation keeps getting errorsI'd appreciate your help!

pointer c

2022-09-20 19:45

1 Answers

I will write down where there is a visible error first.

typedef struct {
    char data[100];
    struct Node* next;
} } Node;

This is wrong in the first of all.

typedef struct node{
    char data[100];
    struct node* next;
} } Node;

You have to fix it like this.

Also,

char* pop(Stack* stack) {
    if (stack->top == NULL) {
        printf("Stack Underflow" occurred.\n");
        return -INF;
    }

It's wrong here, too.

char* pop(Stack* stack) {
    if (stack->top == NULL) {
        printf("Stack Underflow" occurred.\n");
        exit(-INF);
    }

It needs to be fixed this way.

Also,

char* transition(Stack* stack, char** s, int size) {
    char res[1000] = "";

It's wrong here, too.

char* transition(Stack* stack, char** s, int size) {
    char* res = (char*)malloc(1000 * sizeof(char));

It needs to be fixed this way.

First of all, there are three errors. I think you need to revise these first, and look at them one by one again. Good luck.


2022-09-20 19:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.