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
    评论
core-oj.jar是一个用于在线评测系统的核心文件。在线评测系统是为了方便对编程作业进行自动化评测而开发的系统。它主要提供了编程语言的编译及执行环境,能够接收用户提交的代码,并执行测试用例进行输出结果的比对。 core-oj.jar是这个在线评测系统的核心库文件,它包含了评测系统运行所需的各种功能模块。 首先,core-oj.jar包含了编程语言的编译器。它能够将用户提交的代码进行编译,生成可执行文件或者字节码,以便后续的运行和评测。 其次,core-oj.jar提供了丰富的执行环境。它能够创建并管理多个运行环境,每个环境可以运行一个用户提交的代码。这些环境可以在独立的进程中运行,避免了代码之间的相互影响。 此外,core-oj.jar还提供了用于输入输出重定向的功能。它能够将用户代码中的输入输出连接到指定的输入输出流,以便比对用户代码的输出和预期输出是否一致。 最后,core-oj.jar还提供了一些用于评测和判题的辅助函数。它们可以根据定义的评测规则,对用户代码的输出结果进行比对,并给出相应的评测结果。 总而言之,core-oj.jar是一个在线评测系统中的核心文件,通过它提供的各种功能,可以实现对编程作业的自动化评测。它起到了连接用户提交的代码和评判系统之间的桥梁作用,提供了安全、高效和准确的评测环境,帮助学生更好地完成编程任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值