C언어에서 숫자로 스택에 저장하는것으로 문자로 저장하게 변경

kimscom의 이미지

아래는 숫자를 입력받아 스택에 저장하는 코드입니다.
수업시간에 받은 샘플인데요. 여기에 제가 X[역순]으로 배열에 넣도록 추가했습니다.
그런데 입력을 숫자가 아닌 문자열로 해볼려고 하는데.. 잘 안되네요. data를 int가 아닌 char 배열로 받으려면 어떻게 수정을 해야 하는지 좀 봐주세요.

#include
#define ON 1
#define OFF 0
#define STACK_SIZE 20

void main(){
int data, temp, sw, stack[STACK_SIZE];
int top_pos = -1;
int pop(), top();
char opcode, inp_string[5];
int rev[50];
int c=0, i=0;
void push();

sw=ON;
while(sw) {
printf("\n Insert Opcode\n (S:Push[삽입], P:Pop[삭제], T:Top[상위], R:Print[출력], X:[역순], Q:Quit): ");
scanf("%s", inp_string);
opcode=inp_string[0];
printf("** opcode=%c\n", opcode);

switch(opcode){
case 'S':
printf("\n Enter a number to push : ");
scanf("%d", &data);
printf("Push Data= %d\n", data);
push(stack, data, &top_pos);
break;
case 'P':
data=pop(stack, &top_pos);
printf("Pop Data is %d \n", data);
break;
case 'X':
data=Rev(stack, &top_pos);
rev[c++] = data;
printf("reversed value : ");
for(i=0;i printf("%d ", rev[i]);
break;
case 'T':
data=top(stack, &top_pos);
printf("Top of the stack is %d \n", data);
break;
case 'R':
temp=top_pos;
while(temp>=0)
printf("%3d \n", stack[temp--]);
break;
case 'Q':
sw = OFF;
break;
default:
printf("\n *** Warning: 부적절한 op_code 입니다. \n");
break;
}
}
}

void push(int stack[], int data, int *top_pos){
if(*top_pos == STACK_SIZE-1)
printf("Stack is full\n");
(*top_pos)++;
stack[*top_pos]=data;
}

int pop(int stack[], int *top_pos){
int data;
if(*top_pos < 0)
printf("Stack if empty \n");
data=stack[*top_pos];
(*top_pos)--;
return(data);
}

int Rev(int stack[], int *top_pos){
int data;
if(*top_pos < 0)
printf("Stack if empty \n");
data=stack[*top_pos];
(*top_pos)--;
return(data);
}

int top(int stack[], int *top_pos){
int data;
if(*top_pos < 0)
printf("Stack if empty \n");
data=stack[*top_pos];
return(data);
}

chadr의 이미지

숫자는 고정길이이지만 문자열은 가변길이이므로 2차원배열을 쓰셔야합니다.

또한 문자열의 길이는 어떻게 될지 모르므로 동적할당을 이용해서 하셔야합니다.
그렇지 않는다면 그냥 적당하게 크게 배열을 잡아서 사용하셔도 됩니다.

그리고 스택과 데이터는 문자열이므로 int형이 아니라 char배열로 선언을 하셔야하며
스택에 푸시하는 것도 숫자의 경우에는 대입연산자로 되지만 문자열은 배열이므로
strcpy와 같은 함수를 이용하여 복사를 하셔야합니다.
-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.