함수의 응용
계산기 (함수 사용, 헤더파일 사용)
출력예)
계산기
10*4
40
string.h
strcmp(): 문자열 비교
strcmp(값, 값)
두 값이 같으면 0 리턴
char oper[3] = {' ', };
if( !strcmp(oper, "+") ){
같을 때 실행할 문장
}
#include <stdio.h>
#include <string.h>
#include"calculator.h"
void calc(int, char[3], int);
void main() {
int num1 = 0; int num2 = 0;
char oper[3] = { ' ', };
printf("계산기\n");
scanf_s("%d%c%d", &num1, oper, sizeof(oper), &num2);
//printf("%d%s%d\n", num1, oper, num2);
calc(num1, oper, num2);
}
#pragma once
void calc(int num1, char oper[3], int num2) {
if (!strcmp(oper, "+")) {
printf("%d\n", num1 + num2);
}
else if (!strcmp(oper, "-")) {
printf("%d\n", num1 - num2);
}
else if (!strcmp(oper, "*")) {
printf("%d\n", num1 * num2);
}
else if (!strcmp(oper, "/")) {
if (num2 != 0) {
printf("%.2lf\n", (double)num1 / num2);
}
else {
printf("0으로 나눌 수 없습니다.\n");
}
}
else {
printf("잘못입력하셨습니다.\n");
}
}
'C언어 > 예습' 카테고리의 다른 글
[C언어 예습]DAY14_포인터(2) 정적 배열, 동적 배열, 이중 포인터 (0) | 2020.02.05 |
---|---|
[C언어 예습]DAY13_포인터(1) (0) | 2020.02.05 |
[C언어 예습]DAY11_함수의 선언과 사용 (0) | 2020.02.04 |
[C언어 예습]DAY10_2차원 배열, 실생활 예제(아파트 월세, 동 월세 구하는 프로그램) (0) | 2020.01.30 |
[C언어 예습]DAY9_배열 실생활 예제(호별 월세 구하는 프로그램), 2중 for문 (0) | 2020.01.30 |