题解 | #简单计算器#

简单计算器

https://www.nowcoder.com/practice/5759c29a28cb4361bc3605979d5a6130

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <malloc.h>
#include <memory.h>

#define MAX 200

struct sNode {
    void* data;
    struct sNode* next;
};
typedef struct LStack {
    struct sNode top;
    int size;
} LStack, *LinkStack;
LinkStack init_LinkStack() {
    struct LStack* myStack = (struct LStack*) malloc(sizeof(struct LStack));
    if (!myStack) {
        return NULL;
    }
    myStack->size = 0;
    myStack->top.next = NULL;
    return myStack;
};

void push_LinkStack(LinkStack stack, void* data) {
    if (!stack) {
        return;
    }
    struct sNode* newNode = (struct sNode*) malloc(sizeof(struct sNode));
    if (!newNode) {
        return;
    }
    newNode->data = malloc(sizeof(data));
    memcpy(newNode->data, data, sizeof(data));
    newNode->next = stack->top.next;
    stack->top.next = newNode;
    stack->size++;
};

void pop_LinkStack(LinkStack stack) {
    if (!stack || stack->size == 0) {
        return;
    }
    struct sNode* topNode = stack->top.next;
    stack->top.next = topNode->next;
    stack->size--;
    free(topNode);
}

void* get_and_pop_LinkStack(LinkStack stack) {
    if (!stack || stack->size == 0) {
        return NULL;
    }
    void* data = stack->top.next->data;
    void* result = malloc(sizeof(data));
    memcpy(result, data, sizeof(data));
    pop_LinkStack(stack);
    return result;
};

void destroy_LinkStack(LinkStack stack) {
    if (!stack) {
        return;
    }
    while (stack->size != 0) {
        pop_LinkStack(stack);
    }
};

int Priority(char c) {                      //优先级顺序 #,$,+-,*/
    if (c == '#') {
        return 0;
    } else if (c == '$') {
        return 1;
    } else if (c == '+' || c == '-') {
        return 2;
    } else {
        return 3;
    }
}

double Calculate(double x, double y, char op) {
    double result = 0;
    if (op == '+') {
        result = x + y;
    } else if (op == '-') {
        result = x - y;
    } else if (op == '*') {
        result = x * y;
    } else if (op == '/') {
        result = x / y;
    }
    return result;
}

int main() {
    char input[MAX];
    char end[] = "0$";
    struct LStack* operation = (LStack*) init_LinkStack();
    struct LStack* number = (LStack*) init_LinkStack();
    char jing = '#';
    push_LinkStack(operation, &jing);

    fgets(input, MAX, stdin);

    input[strlen(input) - 1] = '$';
    input[strlen(input)] = '\0';

    char convert[MAX];
    double f;
    int j = 0;
    while (strcmp(input, end) != 0) {
        int i = 0;
        while (i < strlen(input)) {
            if ('0' <= input[i] && input[i] <= '9') {
                while (input[i] != ' ' && input[i] != '$') {
                    convert[j] = input[i];
                    i++;
                    j++;
                }
                convert[j] = '\0';
                f = strtod(convert, NULL);
                push_LinkStack(number, &f);
                j = 0;
            } else if (input[i] == ' ') {
                i++;
            } else {
                int priority = Priority(input[i]);
                int topPri = Priority(*(char*) operation->top.next->data);
                if (priority > topPri) {
                    push_LinkStack(operation, &input[i]);
                    i++;
                } else {
                    double a = *(double*) get_and_pop_LinkStack(number);
                    double b = *(double*) get_and_pop_LinkStack(number);
                    char ope = *(char*) get_and_pop_LinkStack(operation);
                    double result = Calculate(b, a, ope);
                    push_LinkStack(number, &result);
                }
            }
        }
        /*        char out;
                while (1) {
                    if (operation->size != 0) {
                        out = *(char *) get_and_pop_LinkStack(operation);
                        printf("%c ", out);
                    } else {
                        break;
                    }
                }
                printf("\n");
                double out1;
                while (1) {
                    if (number->size != 0) {
                        out1 = *(double *) get_and_pop_LinkStack(number);
                        printf("%0.1lf ", out1);
                    } else {
                        break;
                    }
                }*/
        printf("%0.2lf\n", *(double*)number->top.next->data);
        destroy_LinkStack(operation);
        destroy_LinkStack(number);
        break;
        push_LinkStack(operation, &jing);
        fgets(input, MAX, stdin);
        input[strlen(input) - 1] = '$';
        input[strlen(input)] = '\0';
    }
    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-27 10:28
点赞 评论 收藏
分享
11-07 13:31
怀化学院 Java
勇敢牛牛不怕难:又疯一个
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务