OJ4-D. Balancing Symbols

  1. Balancing Symbols
    时间限制1000 ms 内存限制65536 KB
    题目描述
    Balancing symbols: Check if parenthesis ( ), brackets 【】, and braces {} are balanced.输入格式
    An expression includes variables and symbols. The max length is 50.
    输出格式
    1 line.
    If all the (, 0 and {} are balanced, then output 0.
    If () are not balanced, output 1; If 0 are not balanced, output 2;lf {} are not balanced, output 3.
    If more than one symbol are not balanced, output corresponding numbers, sort them in ascending order and divide them by comma.请添加图片描述请添加图片描述
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct stackRecord;
typedef struct stackRecord *Stack;

void push(Stack s, char x);
char popAndtop(Stack s);
void printfKey(int j1, int j2, int j3);

struct stackRecord
{
    int capacity;
    char *array;
    int topofStack;
};

int main()
{
    Stack stack;
    stack = malloc(sizeof(struct stackRecord));
    stack->array = malloc(sizeof(char) * 52);
    stack->capacity = 50;
    stack->topofStack = -1;

    int judge1, judge2, judge3;
    judge1 = judge2 = judge3 = 1;
    // printf("%d %d %d\n", judge1, judge2, judge3);

    char x, xinStack;

    while ((x = getchar()) != '\n' )
    {

        if (x == '(' || x == '[' || x == '{')
        {
            push(stack, x);
            // printf("%c\n", stack->array[stack->topofStack]);
        }
        else if (x == ')' || x == ']' || x == '}')
        {
            xinStack = stack->array[stack->topofStack];

            if ((x == ')' && xinStack == '(') ||
                (x == ']' && xinStack == '[') ||
                (x == '}' && xinStack == '{'))
            {
                stack->topofStack--;
            }
            else
            {
                if (x == ')')
                {
                    judge1 = 0;
                }

                else if (x == ']')
                {
                    judge2 = 0;
                }

                else
                {
                    judge3 = 0;
                }
            }
        }
    }

    while (stack->topofStack != -1)
    {
        x = popAndtop(stack);
        if (x == '(')
            judge1 = 0;
        else if (x == '[')
            judge2 = 0;
        else if (x == '{')
            judge3 = 0;
    }
    printfKey(judge1, judge2, judge3);
    printf("\n");

    return 0;
}

void push(Stack s, char x)
{
    s->topofStack++;
    s->array[s->topofStack] = x;
}

char popAndtop(Stack s)
{
    char temp = s->array[s->topofStack];
    s->topofStack--;
    return temp;
}

void printfKey(int j1, int j2, int j3)
{

    if (j1 != 0 && j2 != 0 && j3 != 0)
    {
        printf("0\n");
    }
    else
    {
        if (j1 == 0)
            printf("1,");
        if (j2 == 0)
            printf("2,");
        if (j3 == 0)
            printf("3,");
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值