티스토리 뷰

학교복습용/객체1

예제에서 배울 점

브로콜리여사 2022. 12. 21. 22:57

표현과 상태

1. 소수점 아래 n번째 자리까지 표현하기 (조정자)

#include <iomanip>

 

cout << fixed;

cout.precision(n);

 

2.

(1) 초를 시간, 분 ,초로 바꾸기 (연산자)

초 : time

>> 시간 : time / 3600, 분 : (time/ 3600) / 60, 초 :  (time/ 3600) % 60

원리

 

(2) 세자리 양의 정수에서 각 자리 숫자를 추출하는 프로그램 작성

 

(1)과 원리동일

 

3. 부동소수점 분리

 

      소수부분 구하는 방법

 


조건문

반복횟수가 정해지지 않은 반복문의 경우 sentinel value를 이용하여 멈추도록 프로그램을 작성할 수 있다.

 

1.

(-1이 입력될때 까지 입력받고, 입력받은 숫자들의 평균을 소수점 둘째 자리 까지 출력)

#include <iostream>
#include <iomanip>   // setprecision을 입력받기 위해
using namespace std;

int main() {
int score, sum = 0;
unsigned int sn = 0;


cout << "Enter grade or -1 to quit :";
cin >> score;

 

// -1(감시값)이 입력될 때 까지 계속 입력받기

while (score != -1) { 
sum = sum + score;
sn = sn + 1;

cout << "Enter grade or -1 to quit :";
cin >> score;
}

cout << "\nTotal of the "<<sn<<" grades entered is " << sum;

// 소수점 2번째 자리까지 출력
cout << setprecision(2) << fixed; 
cout << "\nClass average is " << static_cast<double>(sum/sn) << endl;
}

 

 

2.

//10명 학생을 처리하는 반복문, 반복문 안에 if문을 두어서 pass인지 fail인지 확인, pass및 fail한 학생을 세는 counter 필요,
//반복문 종료 후 pass학생이 8명 이상인지 확인 

 

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
int score;
int pass = 0;
int fail = 0;
unsigned int snumber = 1;

// 10번 반복한다.
while (snumber <= 10) {;

snumber = snumber + 1;

cout << "Enter result (1 = pass, 2 = fail) : ";
cin >> score;

if (score == 1) {
pass++;
}
else {
fail++;
}
}

cout << "\nPassed: "<<pass;
cout << "\nFailed: " << fail;

if (pass > 8) {
cout << "\n훌륭한 반 이군요 ^^";
}

}

 

 

 

'학교복습용 > 객체1' 카테고리의 다른 글

(4) 클래스 ( CLASS )  (0) 2023.01.04
(3) 함수(function)  (0) 2022.12.29
참고블로그  (0) 2022.12.23
(2) 제어문 ( 반복문 / 조건문 )  (0) 2022.12.22
(1) 입출력연산자  (0) 2022.12.21
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2026/01   »
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
글 보관함