게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
C언어] 괄호 짝맞추기
게시물ID : programmer_13844짧은주소 복사하기
작성자 : 김길환
추천 : 0
조회수 : 4057회
댓글수 : 3개
등록시간 : 2015/10/13 15:30:04
옵션
  • 본인삭제금지
Visual Studio 2015로 괄호를 입력했을 때 괄호의 짝이 맞으면 Yes를 출력하고 괄호의 짝이 맞지 않으면 No를 출력하는 프로그램을 짰는데
처음에 do while 반복문 없이 프로그램을 한번 돌릴 때는 프로그램이 알맞게 돌아가는데 여러번 실행하게 하고 싶어서 do while문을 이용해서 반복시키니까 입력값에 상관 없이 무조건 처음 결과값이 출력돼요 ㅜㅜ 아무리 구글을 뒤지고 이것저것 다 해봤는데 정말 너무 안되서 이렇게 질문 남깁니다...ㄸㄹ
do while 하단에 있는 출력하는 함수는 제가 넣어준 문자열이 제대로 초기화 되는지 확인하기 위해서 넣어놓은 거예요.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_SIZE 100
char stack[MAX_SIZE];
int top = -1;
void stack_full()
{
printf("Stack is full");
}
char stack_empty()
{
return NULL;
}
void push(char x)
{
if (top >= MAX_SIZE - 1)
stack_full();
stack[++top] = x;
}
char pop()
{
if (top == -1)
return stack_empty();
return stack[top--];
}
int main(void)
{
char str[MAX_SIZE], ch;
int i = 0;
int end = 1;
int result = 0;
do {
printf_s("%d", result);
printf("Enter the expression:");
gets(str);
while (str[i] != '\0')
{
if (str[i] == '(')
push('(');
else if (str[i] == ')')
{
ch = pop();
if (ch != '(')
{
result = 0;
goto end;
}
}
i++;
}
if (top == -1)
result = 1;
else
result = 0;
end:
if (result == 1)
printf_s("Yes\n");
else if (result == 0)
printf_s("No\n");
printf_s("%s\t%d", str, result);
_strset_s(str, _countof(str),'\n');
printf_s("%s", str);
top = -1;
result = 0;
} while (end != 0);
system("PAUSE");
return 0;
}
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호