C++ 아주 간단한 문제인데요..
글쓴이: kongman / 작성시간: 화, 2012/07/03 - 6:13오후
#include <iostream> #include <fstream> using namespace std; void Calc(int firstNum, int lastNum, char operatorKind, double &rresult) { if(operatorKind == '+') rresult = firstNum + lastNum; else if(operatorKind == '-') rresult = firstNum - lastNum; else if(operatorKind == '*') rresult = firstNum * lastNum; else if(operatorKind == '/') rresult = firstNum / lastNum; // 왜 소수점이 안뜨지 else if(operatorKind == '%') rresult = firstNum % lastNum; else rresult = false; } void main(void) { int count; int firstNum; int lastNum; char operatorKind; double result; ifstream fin("input.txt"); ofstream fout("output.txt"); fin >> count; for(int i = 1; i <= count; i++) { fin >> firstNum >> operatorKind >> lastNum; Calc(firstNum, lastNum, operatorKind, result); if(result == false) { fout << firstNum << " " << operatorKind << " " << lastNum << " = " << "유효하지 않은 연산자" << endl; cout << firstNum << " " << operatorKind << " " << lastNum << " = " << "유효하지 않은 연산자" << endl; } else { fout << firstNum << " " << operatorKind << " " << lastNum << " = " << result << endl; cout << firstNum << " " << operatorKind << " " << lastNum << " = " << result << endl; } } }
input.txt 내용
6
5 + 8
7 - 5
9 / 7
6 * 8
8 % 6
9 ^ 7
여기서 9 / 7 이부분 소수점이 뜨지를 않습니다.. 도대체 뭐가 문제이며 왜 안뜨는걸까요??
-----------출력결과----------------
5 + 8 = 13
7 - 5 = 2
9 / 7 = 1
6 * 8 = 48
8 % 6 = 2
9 ^ 7 = 유효하지 않은 연산자
Forums:
정수끼리 연산 결과는 정수입니다. 캐스팅을 하세여.
정수끼리 연산 결과는 정수입니다.
캐스팅을 하세여.
댓글 달기