pypi1103   6년 전

여기서 여러 케이스 돌려봤는데 정상적으로 되서 올렸더니 자꾸 틀렸다고하네요.. 어디가 틀린걸까요?

#include <stdio.h>
#define ADDR(x) ((((x) << 3) >> 3) & 31)
/* declaring data type */
typedef enum instructions {sta=0,lda,beq,nop,dec,inc,jmp,hlt} inst;
typedef unsigned int DWORD;
typedef unsigned char BYTE;
/* declaring global variables. */
BYTE pc;
BYTE calculator;
BYTE memory[32];
/* declaring function prototype */
BYTE input_byte();
void print_byte(BYTE a);
void sta_inst(int addr);
void lda_inst(int addr);
void beq_inst(int addr);
void nop_inst(int addr);
void dec_inst(int addr);
void inc_inst(int addr);
void jmp_inst(int addr);
void hlt_inst(int addr);
void (*ins_funcs[8])(int) = {&sta_inst,&lda_inst,&beq_inst,
                             &nop_inst,&dec_inst,&inc_inst,
                             &jmp_inst,&hlt_inst};
int main(void)
{
    inst ins;
    BYTE cur_ins=0,addr=0;
    for(int i = 0; i < 32; i++)
        memory[i] = input_byte();
    while(pc < 32){
        addr = ADDR(memory[pc]);
        cur_ins = memory[pc] >> 5;
        pc++;
        ins_funcs[cur_ins](addr);
    }
    print_byte(calculator);
}
BYTE input_byte()
{
    int result = 0;
    for(int i = 7; i >= 0; i--){
        result += ((getchar() - '0') << i);
    }
    getchar();
    return result;
}
void print_byte(BYTE a)
{
    for(int i = 7; i >= 0; i--)
        printf("%d",(a & (1 << i)) >> i);
    printf("\n");
}
void sta_inst(int addr)
{
    memory[addr] = calculator;
}
void lda_inst(int addr)
{
    calculator = memory[addr];
}
void beq_inst(int addr)
{
    if(calculator == 0)
        pc = addr;
}
void nop_inst(int addr)
{
    return;
}
void dec_inst(int addr)
{
    calculator--;
}
void inc_inst(int addr)
{
    if(calculator == 255)
        calculator = 0;
    else
        calculator++;
}
void jmp_inst(int addr)
{
    pc = addr;
}
void hlt_inst(int addr)
{
    pc = 0xff;
}

sgchoi5   6년 전

http://gooddaytocode.blogspot.... 에 가시면 대회 때 사용했던 test input / expected result 를 받을 수 있는 링크가 있습니다.

그 데이터를 입력값으로 해보시고 결과 제대로 나오는지 해보시면 되겠네요..

댓글을 작성하려면 로그인해야 합니다.