과제 - 재귀함수 계산기 만들기

2025. 9. 24. 19:45·개인 코테 & 스타디 <비공개>/알고리즘 스타디 과제

재귀함수로 인터렉티브 계산기 만들기

//인터렉티브 계산기
#include <iostream>
#include <string>
#include <cstdlib>
#include <stack>


using namespace std;

float calculate(float currentHistory)
{
	char oper;
	float num;

	while (true)
	{
		cout << "연산자 입력(+, -, *, /):";
		cin >> oper;
			
		if (oper == 'p')
		{
			cout << "계산기 종료" << endl;
			exit(0);
		}
		if (oper == '+' || oper == '-' || oper == '*' || oper == '/')
			break; 
		cout << "잘못입력하셨습니다!" << endl;
	}
	cout << "숫자 입력: ";
	cin >> num;

	if (oper == '+')
		currentHistory += num;
	else if (oper == '-')
		currentHistory -= num;
	else if (oper == '*')
		currentHistory *= num;
	else if (oper == '/')
		currentHistory /= num;

	cout << currentHistory << endl;
	return calculate(currentHistory);	
}

float ImmediateTerminationCheck(const string& str)
{
	if (str == "p")
	{
		cout << "계산기 종료" << endl;
		exit(0);
	}

	try
	{
		return stof(str);
	}
	catch (...)
	{
		exit(0);
	}
}

int main()
{
	cout << "p 를 누르면 즉시 종료됩니다 " << endl;
	string firstInput;
	cout << "숫자 입력: ";
	cin >> firstInput;

	float firstNum = ImmediateTerminationCheck(firstInput);
	float result = calculate(firstNum);
	cout << result << endl;
	
}

 

 

'개인 코테 & 스타디 <비공개> > 알고리즘 스타디 과제' 카테고리의 다른 글

농부, 배추, 염소, 늑대 강건너기  (0) 2025.10.01
과제 - 자료구조(Data Struct)  (1) 2025.09.14
'개인 코테 & 스타디 <비공개>/알고리즘 스타디 과제' 카테고리의 다른 글
  • 농부, 배추, 염소, 늑대 강건너기
  • 과제 - 자료구조(Data Struct)
lucodev
lucodev
언리얼 포폴개발 일기
  • lucodev
    루코 개발테이블
    lucodev
  • 전체
    오늘
    어제
    • 분류 전체보기 (213) N
      • Unreal 프로젝트 다이어리 (110) N
        • 첫번째 프로젝트 (73)
        • 두번째 프로젝트 (37) N
      • Unreal 팁 (8)
      • Unreal 디버깅 (8)
      • C++ 프로그래머스 (52)
        • Stack,Queue (7)
        • Hash (4)
        • Heap (2)
        • Sort (5)
        • Exhaustive search (5)
        • Greedy (2)
        • BFS , DFS (7)
        • Graph (2)
        • Dynamic Programming (1)
        • C++ Math (2)
        • 기타 문제 (14)
      • C++ 백준 (4)
      • C++ 팁 (1)
      • 개인 코테 & 스타디 <비공개> (29)
        • 코드 개인보관함 (9)
        • 코딩테스트+@ (11)
        • 알고리즘 스타디 (6)
        • 알고리즘 스타디 과제 (3)
        • 비공개 (0)
  • 인기 글

  • 최근 글

  • 최근 댓글

  • 링크

  • 공지사항

  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 태그

    unreal 인벤토리
    언리얼 프로그래스바
    언리얼 컷씬
    unreal 모션매칭
    언리얼 parkour
    언리얼
    언리얼 시퀀스
    언리얼 인벤토리
    unreal 파쿠르
    언리얼 파쿠르
    unreal inventory
    unreal
    언리얼 ui
    언리얼 behaviortree
    언리얼 모션매칭
    Unreal Parkour
    언리얼 비헤이비어트리
    언리얼 motionmatching
    unreal 시퀀스
    언리얼 behavior tree
  • hELLO· Designed By정상우.v4.10.3
lucodev
과제 - 재귀함수 계산기 만들기
상단으로

티스토리툴바