후위식 계산프로그램은 피연산자를 스택에 push하고 연산자를 만났을 때 pop하여 연산 후 다시 push하는 과정이었지만

중위수식을 후위식으로 변환하는 프로그램은 반대로 연산자를 push하고 피연산자는 그대로 출력합니다.


중위 수식을 후위 수식으로 변환할 떄 중요한 것은 연산자의 우선순위입니다.

괄호와 숫자로 구성된 중위 수식을 후위 수식으로 변환 할 때 연산자의 순위는 다음과 같습니다.


괄호 < +, - 연산자 < *, / 연산자


가장 중요한 것은 현재 처리중인 연산자와 스택에 있는 연산자의 우선순위를 이용한 처리 입니다.


1. 현재 처리 중인 연산자의 우선순위가 스택에 있는 연산자의 우선순위보다 낮을 때 

예를들어) 처리중인 연산자: + or - ,  스택에있는 연산자: * or / 일 때

=> 스택에 있는 연산자를 pop 해서 출력 후 처리 중인 연산자를 push 합니다.


2. 현재 처리 중인 연산자의 우선순위가 스택에 있는 연산자의 우선순위보다 클 때

예를들어) 처리중인 연산자: * or / , 스택에 있는 연산자: +, - 일 때

=> 현재 처리 중인 연산자를 그냥 push합니다.


3. 현재 처리중인 연산자의 우선순위와 스택에 있는 연산자의 우선순위가 같을 때

예를들어) 처리중인 연산자: +, 스택에 있는 연산자: - 일 때

=> 1번 경우와 똑같이 처리한다.

이유는 간단한 예시를 들면 쉽게 알 수 있습니다.

ex) 5-4+3일 때, 5/4*3 일 때 

=> 우선순위를 어떻게 하느냐에 따라 답이 달라집니다.


코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 100
typedef struct
{
    char data[max];
    int top;
}stackType;
 
int check(char* exp);
void init(stackType* s);
void push(stackType* s, char x);
int pop(stackType* s);
int is_full(stackType* s);
int is_empty(stackType* s);
char peek(stackType* s);
void infix_to_postfix(const char* s);
int prec(char op);
int main()
{
    
    infix_to_postfix("(2+3)*4+9");
    return 0;
}
void init(stackType* s)
{
    s->top = -1;
}
int is_full(stackType* s)
{
    return s->top == max - 1;
}
int is_empty(stackType* s)
{
    return s->top == -1;
}
void push(stackType* s, char x)
{
    if (is_full(s))
    {
        fprintf(stderr, "원소가 다 찼습니다.\n");
        return;
    }
    s->data[++(s->top)] = x;
}
char peek(stackType* s)
{
    if (is_empty(s))
    {
        fprintf(stderr, "원소가 없습니다.\n");
        exit(1);
    }
    return s->data[(s->top)];
}
int pop(stackType* s)
{
    if (is_empty(s))
    {
        fprintf(stderr, "원소가 없습니다\n");
        exit(1);
    }
    return s->data[(s->top)--];
}
void infix_to_postfix(const char* s)
{
    char ch,top_op;
    int n = strlen(s);
    stackType st;
    init(&st);
    for (int i = 0; i < n; i++)
    {
        ch = s[i];
        switch (ch)
        {
        case '+'case '-':case '*':case '/':
            while (!is_empty(&st) && (prec(ch) <= prec(peek(&st))))
            {
                printf("%c"pop(&st));
            }
            push(&st, ch);
            break;
        case '(':
            push(&st, ch);
            break;
        case ')':
            top_op = pop(&st);
            while (top_op != '(')
            {
                printf("%c", top_op);
                top_op = pop(&st);
            }
            break;
            //숫자일 떈 그대로 출력
        default:
            printf("%c", ch);
            break;
        }
    }
        //아직 스택에 남아있다면 다 뽑아냄
        while (!is_empty(&st))
            printf("%c"pop(&st));
    
}
int prec(char op)
{
    switch (op)
    {
    case '(':case ')'return 0;
    case '+':case '-'return 1;
    case '*':case '/':return 2;
    }
    return -1;
}
 
cs


결과






주의할 것은 스택에 있는 피연산자를 뽑아낼 때 op1, op2의 순서입니다.


코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 100
typedef struct
{
    char data[max];
    int top;
}stackType;
 
int check(char* exp);
void init(stackType* s);
void push(stackType* s, int x);
int pop(stackType* s);
int is_full(stackType* s);
int is_empty(stackType* s);
int eval(const char* s);
int main()
{
    int result;
    puts("82/3-32*+ 계산하기\n");
    result = eval("82/3-32*+");
    printf("결과값은%d\n", result);
}
void init(stackType* s)
{
    s->top = -1;
}
int is_full(stackType* s)
{
    return s->top == max - 1;
}
int is_empty(stackType* s)
{
    return s->top == -1;
}
void push(stackType* s, int x)
{
    if (is_full(s))
    {
        fprintf(stderr, "원소가 다 찼습니다.\n");
        return;
    }
    s->data[++(s->top)] = x;
}
int pop(stackType* s)
{
    if (is_empty(s))
    {
        fprintf(stderr, "원소가 없습니다\n");
        exit(1);
    }
    return s->data[(s->top)--];
}
int eval(const char* s)
{
    int op1, op2, value;
    int len = strlen(s);
    char ch;
    stackType st;
    init(&st);
    for (int i = 0; i < len; i++)
    {
        ch = s[i];
        if (ch != '+' && ch != '*' && ch != '/' && ch != '-')
        {
            value = ch - '0';
            push(&st, value);
        }
        else
        {
            //op1, op2 순서 조심, op2가 최근에 넣은 피연산자임
            op2 = pop(&st);
            op1 = pop(&st);
            switch (ch)
            {
            case '+': push(&st,op1 + op2); break;
            case '-':push(&st,op1 - op2); break;
            case '*': push(&st,op1 * op2); break;
            case '/': push(&st,op1 / op2); break;
            }
        }
    }
    return pop(&st);
 
}
cs


결과





괄호가 들어간 문장을 입력하고 괄호가 올바르게 표현되었을 때와 틀렸을 때를 표현하는 프로그램 만들기

주의: 띄어쓰기 들어간 문자열의 입력? fgets함수사용하기 (gets함수는 비표준함수라 사용X)



코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 100
typedef struct
{
    char data[max];
    int top;
}stackType;
 
int check(char* exp);
void init(stackType* s);
void push(stackType* s, int x);
int pop(stackType* s);
int is_full(stackType* s);
int is_empty(stackType* s);
int main()
{
    stackType s;
    char exp[20];
    fgets(exp, sizeof(exp), stdin);
    if (check(exp) == 1)
        puts("성공!");
    else
        puts("실패!");
    return 0;
}
void init(stackType* s)
{
    s->top = -1;
}
int is_full(stackType* s)
{
    return s->top == max - 1;
}
int is_empty(stackType* s)
{
    return s->top == -1;
}
void push(stackType* s, int x)
{
    if (is_full(s))
    {
        fprintf(stderr, "원소가 다 찼습니다.\n");
        return;
    }
    s->data[++(s->top)] = x;
}
int pop(stackType* s)
{
    if (is_empty(s))
    {
        fprintf(stderr, "원소가 없습니다\n");
        exit(1);
    }
    return s->data[(s->top)--];
}
int check(char* exp)
{
    stackType s;
    char ch, open_ch;
    int n = strlen(exp);
    init(&s);
    for (int i = 0; i < n; i++)
    {
        ch = exp[i];
        switch (ch)
        {
        case '(':case '[':case'{':
            push(&s, ch);
            break;
        case ')':case']':case'}':
            if (is_empty(&s))return 0;
            else
            {
                open_ch = pop(&s);
                if ((open_ch == '(' && ch != ')'|| (open_ch == '{' && ch != '}'||
                    (open_ch == '[' && ch != ']'))
                    return 0;
                
            }
            break;
        }
    }
    //괄호검사 다했는데 스택에 원소가 남아있다면? => 잘못된 수식
    if (!is_empty(&s))return 0;
    return 1;
}
cs


결과






스택에 1,2,3 삽입 후 모든 원소를 삭제함과 동시에 출력하고 스택이 비었으면 1, 아니면 0 출력하기


top변수와 스택배열을 하나의 구조체로 묶고, 구조체 변수를 이용하여 포인터로 값을 수정하는 방식의 장점

=> 구조체 변수를 여러 개 선언 함으로서 스택을 여러개 구현하기 쉬움



코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<stdio.h>
#include<stdlib.h>
 
#define max 5
typedef struct
{
    int stack[max];
    int top;
}stackType;
void init(stackType* s);
void push(stackType* s, int x);
int pop(stackType* s);
int is_full(stackType* s);
int is_empty(stackType* s);
 
int main()
{
    stackType st;
    //스택 초기화 하기
    init(&st);
    push(&st, 1); push(&st, 2), push(&st, 3);
    printf("%d\n"pop(&st));
    printf("%d\n"pop(&st));
    printf("%d\n"pop(&st));
    printf("스택이 비었는지 ? %d\n", is_empty(&st));
}
void init(stackType* s)
{
    s->top = -1;
}
int is_full(stackType* s)
{
    return s->top == max - 1;
}
int is_empty(stackType* s)
{
    return s->top == -1;
}
void push(stackType* s, int x)
{
    if (is_full(s))
    {
        fprintf(stderr, "원소가 다 찼습니다.\n");
        return;
    }
    s->stack[++(s->top)] = x;
}
int pop(stackType* s)
{
    if (is_empty(s))
    {
        fprintf(stderr, "원소가 없습니다\n");
        exit(1);
    }
    return s->stack[(s->top)--];
}
cs


결과





학생의 이름, 주소, 학번을 하나의 구조체로 묶어서 구조체 스택배열로 3명의 정보 저장 후 모든 학생의 정보 출력 후 

1명을 삭제하고 삭제한 학생의 정보 출력시키기


핵심: 각 구조체 배열스택 내부의 문자열을 어떻게 입력받을까?


strcpy이용하기


1) 입력받지않고 직접 설정 하기

strcpy(stack[i].name,"홍길동");

2) 입력받아서 저장하기

방법1: scanf("%s",stack[i].name);

방법2: char name[10]; scanf("%s",name); strcpy(stack[i].name,name);


주의

2) 방법 사용시

stack[i].name =name;   => 잘못된 코드

배열의 주소값을 임의로  다른 주소로값으로 지정할 수 없음! => strcpy함수 이용하기


코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma warning(disable:4996)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 5
typedef struct
{
    char name[10];
    char add[10];
    int id;
}info;
int top = -1;
info stack[max];
void push(info item);
info pop();
int is_full();
void show();
int main()
{
    info std_info;
    for (int i = 0; i < 3; i++)
    {
        scanf("%s%s%d", std_info.name, std_info.add, &std_info.id);
        push(std_info);
    }
    show();
    std_info = pop();
    printf("삭제한 학생의 이름은%s\n", std_info.name);
    printf("삭제한 학생의 주소는%s\n", std_info.add);
    printf("삭제한 학생의 학번은%d\n", std_info.id);
}
void push(info item)
{
    if (is_full())
    {
        fprintf(stderr, "원소가 다찼어요\n");
        return;
    }
    stack[++top] = item;
}
int is_full()
{
    if (top == max - 1// 찼으면 -1 , 아니면 0반환
        return 1;
    else return 0;
}
int is_empty()
{
    return top == -1//비었으면 1 , 아니면 0 반환
}
info pop()
{
    if (is_empty())
    {
        fprintf(stderr, "원소가 없어요\n");
        exit(1);
    }
    return stack[top--];
}
void show()
{
    if (is_empty())
    {
        fprintf(stderr, "원소가 없어요\n");
        return;
    }
    for (int i = 0; i <= top; i++)
    {
        printf("이름:%s 주소:%s 학번:%d\n"stack[i].name, stack[i].add, stack[i].id);
    }
}
cs


결과






1,2, 3 삽입 후 1, 2 빼고 현재 탑원소(3)출력후 남아있는 원소 제거 후에  없는 원소를 뺐을때 오류 문구 나오게 하기


전역변수로 top변수 이용 시 문제점 

1. 하나 이상의 스택을 구현하려면 여러개의 top변수를 사용해야하나?(top1, top2...)

-> 너무 비효율적임

해결: top변수와 스택배열을 하나의 구조체로 결합시켜서 포인터로 이용하기 


코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//1 2 3 삽입 후 1 2 빼고 현재 탑원소(3)출력후 다 빼고 없는 원소를 뺐을때 오류 문구 나오게
#include<stdio.h>
#include<stdlib.h>
#define max 5
int top = -1;
int stack[max];
void push(int x);
void pop();
void peek();
int is_full();
int main()
{
    push(1); push(2); push(3); pop(); pop(); peek(); pop(); pop();
}
void push(int x)
{
    if (is_full())
    {
        fprintf(stderr, "원소가 다찼어요\n");
        return;
    }
    stack[++top] = x;
}
int is_full()
{
    if (top == max - 1// 찼으면 -1 , 아니면 0반환
        return 1;
    else return 0;
}
int is_empty()
{
    return top == -1//비었으면 1 , 아니면 0 반환
}
void pop()
{
    if (is_empty())
    {
        fprintf(stderr, "원소가 없어요\n");
        return;
    }
    top--;
}
 
void peek()
{
    if (is_empty())
    {
        fprintf(stderr, "원소가 없습니다\n");
        return;
    }
    printf("스택의 top 원소는 %d입니다\n"stack[top]);
}
cs


네트워크 프로그래밍 수업을 위해 비쥬얼 스튜디오에서 교재의 server.c와 client.c 코드를 작성했는데

윈도우가 아닌 리눅스 기반의 OS에서 실행을 해야했습니다.. (비쥬얼스튜디오에서는 헤더부터 인식을 못하더군요..)

저는 VMware와 Ubuntu를 설치했는데 리눅스환경에서 c코드를 컴파일/실행 하려면 GCC컴파일러를 설치해야합니다.

방법은 간단합니다. 일단 프로그램목록에 있는 "터미널"로 들어갑니다.


일단 GCC컴파일러가 깔려있는지 확인하려면 "gcc" 를 입력합니다. 

아래 처럼 뜨면 GCC컴파일러가 이미 깔려있는 상태이므로 설치할 필요가없습니다.

 


만약 

E: Could not get lock /var/lib/dpkg/lock frontend - open (11: Resource temporarily unavailable)

E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?

이런 문구들이 뜬다면 아래의 코드들을 입력합니다.

sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock
dpkg --configure -a

**주의: 띄어쓰기 주의해야합니다. 띄어쓰기가 올바르지않으면 오류가 뜹니다.


이제 아래의 명령어를 입력하고 설치를 진행합니다.

sudo apt-get install gcc


정상작동한다면 설치가 진행되고 설치가 완료되면 다시 명령어 입력 상태로 돌아옵니다.

설치완료 후 정상적으로 설치가 되었다면 gcc를 입력하면  맨위의 그림과같이 결과창이 뜹니다.


이렇게 GCC컴파일러는 설치완료되었고 이제 간단하게 코드를 컴파일/실행하는 법을 알아봅시다.


일반적으로 리눅스 상에서 코드를 컴파일 및 실행하는 순서는 다음과 같습니다.


파일 생성 -> 코드 작성 및 저장 -> 컴파일 실행 -> 실행 


각 단계별로 리눅스 명령어를 이용해 해결해야합니다.

일단 간단한 리눅스 명령어몇개를 알아야하는데 그냥 몇개정도만 알면 될 것 같습니다.


vi : 파일 생성 

ls : 디렉토리의 파일목록 확인

gcc -o "실행파일명" "작성한코드파일명.c" : 실행파일이름의 실행파일로 .c파일을 컴파일 한다.

./"실행파일명": 실행파일을 실행한다.(코드가 출력됩니다.)


일단 아래의 코드를 입력하여 hello.c 이름의 파일을 생성합니다.

" vi hello.c "

입력하면 파일이 생성되고 입력할 수 있는모드로 바뀝니다.


여기서 간단하게 코드를 작성하고나서 ESC를 누르고  " :wq! " 를 입력하시고 엔터를 누르면 코드가 저장됩니다.


이제 아래의 코드를 작성함으로써 test라는 실행파일명으로 hello.c파일을 컴파일 하면됩니다.

gcc -o test hello.c


실행파일생성 및 컴파일과정에 문제가 없다면 명령어 입력 준비상태로 돌아오고, 다른 에러 문구가 떴다면 오타가 난 것이기 때문에 다시

vi hello.c 로 파일에 접속하여 코드에 오타가 없는지 확인합니다.

이제 ls명령어를 입력하여 아래처럼 실행파일과 코드파일이 생성되었는지 확인합니다.


이제 코드를 실행할 차례입니다.

./test 를 입력합니다.

최종적으로 아래와 같은 결과가 출력됩니다.







문제

5명의 학생들 중 입력된 학번, 이름, 성적 중 학생의 성적을 기준으로 내림차순으로 구현하되

해당 학번의 학생 정보 전체가 아래와 같이 정렬되도록 순환함수를 이용해 구현하시오.

 

 

 

풀이

주의) 제 소스코드는 자료구조 교재 끝부분에 배우는 정렬 단원의 "퀵정렬"을 이용한 코드입니다. 학교진도에 맞춰서 공부하신다면 1,2,3,4,5일 때 모두 하나씩 비교해가며 5,4,3,2,1 로 만드는 방법으로 구현해보시는게 좋을 것 같습니다.

교수님께서 주신 기본 코드를 유지하면서 순환을 이용하여 해결할 수 있도록 퀵정렬로 풀었습니다.

추가점수 옵션으로 구조체 배열대신 구조체 포인터 변수로 동적할당받아서 해결하는 조건이 있어서

84행을 85행으로 고쳐서 풀었습니다. 연산이 종료되고 동적할당을 free했습니다!

 

 

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma warning(disable:4996)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#define MAX 5
 
typedef struct student
{
    int student_no; //학번
    char name[40]; //이름
    int score; //성적
}student;
 
//배열 교환
void SWAP(student* arr, int a, int b)
{
    //한 배열이 뭉탱이로 옮겨짐.(정보갱신)
    student temp;
    temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp;
}
// 배열, 하한, 상한)
void SORT(student* arr, int m, int n, int choice)
{
    //선택 옵션이 1이면 성적순 내림차순정렬
        if (choice == 1)
        {
            if (m < n)
            {
                int key = m;
                int i = m + 1;
                int j = n;
                //엇갈리지 않을동안
                while (i <= j)
                {
                    while (i <= n && arr[i].score >= arr[key].score)
                        i++;
                    while (j > m&& arr[j].score <= arr[key].score)
                        j--;
                    //엇갈리면 j와 key값 교체
                    if (i > j)
                        SWAP(arr, j, key);
                    //엇갈리지않으면 i와 j 교체
                    else
                        SWAP(arr, i, j);
                }
                //각각 정렬된 수의 전, 후 에서 똑같이 순환반복
                SORT(arr, m, j - 1,choice);
                SORT(arr, j + 1, n,choice);
            }
        }
        //선택 옵션이 2이면 학번순 내림차순 정렬
        else if (choice == 2)
        {
                if (m < n)
                {
                    int key = m;
                    int i = m + 1;
                    int j = n;
                    //엇갈리지 않을동안
                    while (i <= j)
                    {
                        while (i <= n && arr[i].student_no >= arr[key].student_no)
                            i++;
                        while (j > m&& arr[j].student_no <= arr[key].student_no)
                            j--;
                        //엇갈리면 j와 key값 교체
                        if (i > j)
                            SWAP(arr, j, key);
                        //엇갈리지않으면 i와 j 교체
                        else
                            SWAP(arr, i, j);
                    }
                    //각각 정렬된 수의 전, 후 에서 똑같이 순환반복
                    SORT(arr, m, j - 1, choice);
                    SORT(arr, j + 1, n, choice);
                }
        }
}
int main() {
 
    //student s[MAX];
    student* s = (student*)malloc(sizeof(student) * 5);
    int i = 0;
    //입력받을 옵션변수
    int choice;
    char str[40];
    //stduent information field
    for (i = 0; i < MAX; i++)
    {
        printf("Student Information (학번, 이름, 성적) : ");
        scanf("%d"&s[i].student_no);
        scanf("%s", str);
        strcpy(s[i].name, str);
        scanf("%d"&s[i].score);
    }
    printf("성적순 정렬은 1, 학번순 정렬은 2를입력하세요: ");
    scanf("%d"&choice);
    SORT(s, 0, MAX - 1,choice); //소트(학생 배열,0,num-1)
    for (i = 0; i < MAX; i++)
        printf("%d\t%s\t%d\n", s[i].student_no, s[i].name, s[i].score);
    printf("\n");
    //동적할당 해제
    free(s);
    return 0;
}
cs

 

+ Recent posts