C Programming Course Project - Calculator using Stack

2025. 1. 9. 15:16·PROJECTS/C 언어
728x90

1. Title

Calculator using Stack structure

 

2. Project Summary

1) Implementing a stack data structure using C structs
2) Creating a function to convert infix notation to postfix notation
3) Evaluating the postfix expression to calculate the final result

 

3. Date

Nov, 15, 2024

 

4. Category

"C Programming", "Data Structure"

 

5. Key Concepts


 

6. Code Review

 

typedef struct {
	int top;
	int items[MAX];
} Stack;


void init(Stack* stack) 
{
	stack->top = -1;
}

int isEmpty(Stack* stack) 
{
	return stack->top == -1;
}

int isFull(Stack* stack) 
{
	return stack->top == MAX - 1;	
}


void push(Stack* stack, int value) 
{
	if (isFull(stack)) 
	{
		printf("Stack overflow\n");
		exit(1);
	}
	stack->items[++(stack->top)] = value;
}

int pop(Stack* stack) 
{
	if (isEmpty(stack)) 
	{
		printf("Stack underflow\n");
		exit(1);
	}
	return stack->items[(stack->top)--];
}

int peek(Stack* stack) 
{
	return stack->items[stack->top];
}

This code defines a Stack structure and implements its basic operations (init, isEmpty, isFull, push, and pop) in C.
It uses an array to store elements and a 'top' variable to keep track of the stack's current position, with error handling for overflow and underflow conditions.

 

 

int precedence(char op) 
{
	switch (op) 
	{
	case '+': return 1;
	case '-': return 1;
	case '*': return 2;
	case '/': return 2;
	default: return 0;
	}
}

int isOperator(char ch) 
{
	return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}

int isDigit(char ch) 
{
	return ch >= '0' && ch <= '9';
}​

This code defines helper functions for expression parsing: precedence() determines the priority of operators, while isOperator() and isDigit() check if a character is an operator or a digit, respectively.

 

 

void infixToPostfix(char* infix, char* postfix)
{
    Stack stack;
    init(&stack);
    int j = 0;
    
    for (int i = 0; infix[i] != '\0'; i++) 
    {
        char ch = infix[i];

        if (isDigit(ch)) 
        {
            while (isDigit(infix[i]))
            {
                postfix[j++] = infix[i++];
            }
            postfix[j++] = ' ';
            i--;
        }

        else if (ch == '(')
        {
            push(&stack, ch);
        }

        else if (ch == ')') 
        {
            while (!isEmpty(&stack) && peek(&stack) != '(')
                postfix[j++] = pop(&stack);
                
            pop(&stack);
        }

        else if (isOperator(ch)) 
        {
            while (!isEmpty(&stack) && precedence(peek(&stack)) >= precedence(ch))
                postfix[j++] = pop(&stack);

            push(&stack, ch);
        }
    }

    while (!isEmpty(&stack)) 
    {
        postfix[j++] = pop(&stack);
    }
    postfix[j] = '\0';
}

This function converts an infix expression to a postfix expression using a stack.

 

 

int evaluatePostfix(char* postfix) {
    Stack stack;
    init(&stack);
    int i = 0;
    
    while (postfix[i] != '\0') {
        if (isDigit(postfix[i])) {
            int num_buffer = 0;
            while (isDigit(postfix[i])) {
                num_buffer = num_buffer * 10 + (postfix[i] - '0');
                i++;
            }
            push(&stack, num_buffer);
        }
        else if (isOperator(postfix[i])) {
            int val2 = pop(&stack);
            int val1 = pop(&stack);
            switch (postfix[i]) {
                case '+': push(&stack, val1 + val2); break;
                case '-': push(&stack, val1 - val2); break;
                case '*': push(&stack, val1 * val2); break;
                case '/': push(&stack, val1 / val2); break;
            }
            i++;
        }
        else {
            i++;
        }
    }
    return pop(&stack);
}

This function evaluates a postfix expression by iterating through each character, pushing numbers onto a stack, and performing calculations when operators are encountered.

 

 

7. Testing and Debugging

* Testing Tools: Visual Studio 2022

C_Project_01.c
0.01MB
장태영_하민지_허재범_자료구조.pdf
1.58MB

728x90

'PROJECTS > C 언어' 카테고리의 다른 글

C Programming Course Project - Reservation Management System  (1) 2025.01.09
'PROJECTS/C 언어' 카테고리의 다른 글
  • C Programming Course Project - Reservation Management System
Dinoj
Dinoj
  • Dinoj
    AlOG
    Dinoj
  • 전체
    오늘
    어제
    • 분류 전체보기 (208)
      • Spice (2)
      • Python (19)
        • Pandas (0)
        • COMPUTER VISION (18)
        • Pytorch (1)
      • PCB 이론 (18)
        • PI (6)
        • SI (12)
      • 회로 이론 (44)
        • 기타 학습 (20)
        • UVM (Universal Verification.. (12)
        • AI HARDWARE (12)
      • PROJECTS (29)
        • AI 가속기 (10)
        • 영상 처리 (3)
        • UVM (Universal Verification.. (2)
        • CPU 설계 (5)
        • CMOS VLSI (2)
        • Verilog (2)
        • Firmware (2)
        • C 언어 (2)
        • 기타 프로젝트 (1)
      • Linux (20)
        • Embedded Linux (Rpi) (7)
        • Petalinux (7)
        • Linux 기초 (6)
      • AMBA BUS (16)
        • AXI BUS (5)
        • APB BUS (2)
        • Vitis (8)
      • AI SOC COURSE (53)
        • 영상 처리 (5)
        • SYSTEM VERILOG (CPU 설계) (20)
        • VERILOG 기초 (5)
        • CMOS VLSI (7)
        • FIRMWARE (9)
        • C PROGRAMMING (1)
        • Python (Keras) (6)
      • 코딩 지식 (5)
        • SYSTEM VERILOG (3)
        • TCL (2)
      • TISTORY (1)
  • 블로그 메뉴

    • 홈
    • 글쓰기
    • 관리
    • Info
  • 인기 글

  • 최근 댓글

  • 최근 글

  • 250x250
  • hELLO· Designed By정상우.v4.10.3
Dinoj
C Programming Course Project - Calculator using Stack
상단으로

티스토리툴바