c언어 stack관련하여 질문드립니다.

oeo0750의 이미지

c언어에서 stack을 이용하여 괄호의 쌍이 맞는지 검사하는 함수를 구성해 보았습니다.
현재

main.c: In function ‘parser’:
main.c:21:4: error: unknown type name ‘stack_ptr’
stack_ptr top = NULL;
^
main.c:21:20: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
stack_ptr top = NULL;
^
main.c:28:5: warning: implicit declaration of function ‘push’ [-Wimplicit-function-declaration]
push(&top, expr[i]);
^
main.c:31:19: warning: implicit declaration of function ‘IS_EMPTY’ [-Wimplicit-function-declaration]
case ']': if (IS_EMPTY(top))
^
main.c:33:21: warning: implicit declaration of function ‘pop’ [-Wimplicit-function-declaration]
ch = pop(&top);
^

이런 오류들이 발생하고 있는데, 스택 개념에 대한 이해가 부족한 건지
error: unknown type name ‘stack_ptr’여기서 선언을 어떻게 해야할지 잘 모르겠습니다.
스택을 사용하려면 괄호를 배열로 받으면 안되는 건가요?

도움 주시면 보면서 공부하고 싶습니다. 감사합니다.

 
 
#include <stdio.h>
#include <stdbool.h>
#include <string.h> 
 
int main(){   
   int parser();
   char arr1[20] = "()()(())" ; 
   bool result = parser(arr1);
   if(result==true) {
   printf("괄호의 쌍이 맞습니다."); 
   }
   else {
   printf("괄호의 쌍이 맞지 않습니다.");
   }
 }
 
int parser(char expr[]){
   int i;
   char ch;
   stack_ptr top = NULL;
 
   for (i = 0; i < strlen(expr); i++)
       switch (expr[i]) {
	   case '[': 
	   case  '{': 
	   case '(': 
	   push(&top, expr[i]);
	           break;
 
	   case ']': if (IS_EMPTY(top))
	               return false;
	              ch = pop(&top);
				  if (ch != '[') return false;
				  break;
		case '}': if (IS_EMPTY(top))
	               return false;
	              ch = pop(&top);
				  if (ch != '{') return false;
				  break;	
		case ')': if (IS_EMPTY(top))
	               return false;
	              ch = pop(&top);
				  if (ch != '(') return false;
	   }
    if (IS_EMPTY(top))
        return true;
    return false;
}
세벌의 이미지

stack_ptr 구조체에 무엇을 넣을 건가 생각해 보셔요.

스택개념 뿐 아니라 C 언어도 공부하셔야 될 거 같아요.

typedef struct {
...
...
...
} stack_ptr;

{ 와 } 사이에 들어갈 부분은 님께서 뭘 하고자 하느냐에 따라 달라지겠죠.

hui1601의 이미지

소스코드를 고치셔도 "())("입력하면 "괄호의 쌍이 맞습니다."나오실 겁니다.아마도...

2019년 기준 중2

bushi의 이미지

빈 곳 메워오는, 흔한 학기말 숙제일 뿐인 것 같구요.
제대로 메우기만 했다면 그런 식으로는 동작할 리가 없는 코드로 보입니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.