티스토리 뷰

과제

과제3

브로콜리여사 2022. 12. 28. 01:02

나의 답안!

 

//프로그램은 3명에 대한 결과를 출력하면 종료된다. 그전까지는 계~~ 속 입력받는다.

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

int main() {
    int studentcount{ 0 };
    string name;
    int birth = 0;
    bool cough = true;
   double heat = 0.0;
   int menu;
   string condition;



   cout << "1. 신상 정보 입력" << endl;
   cout << "2. 의심 증상이 있는 경우" << endl;
   cout << "3. 코로나 검사를 받은 경우" << endl;
   cout << "4. 결과 출력" << endl << "? ";

/* 4번 메뉴에 studentcount를 두고 한 번 반복할때마다 count값이 1씩 올라가게 한다.
4번 메뉴를 3번 출력하면 while문을 나오면서 프로그램이 종료된다.*/

   while (studentcount < 3) {
     cin >> menu;

    switch (menu) {
    case 1:
       cout << "\n이름과 생년월일을 입력하세요." << endl << "? ";
       cin >> name >> birth;
       break;

   case 2:
      cout << "\n체온과 기침 증상 유무(true/false)를 입력하세요." << endl << "? ";
      cin >> heat >> boolalpha >> cough; //불 타입에 대해 문자로 입력받기
      break;

   case 3:
     cout << "\n신속항원검사 양성: 신속, PCR 양성: PCR, 음성: 음성" << endl << "? ";
     cin >> condition;
     break;

   case 4:
    cout << fixed;
    cout << setprecision(6) << endl;
    cout << name << " " << birth << endl;
/*바로 앞에서 출력을 받지 않은 경우 noname 오늘날자로 출력해야한다. case4를 한 번 하고 난 뒤에는 reset*/

//감염의심자이면서 확진자이면 확진자로 판단한다.
      if (condition == "신속" || condition == "PCR") {
      cout << "확진자입니다. 격리하기 바랍니다.";
      }
     else {
          if (heat > 38 && cough == true) {
             cout << "감염의심자입니다. 검사를 받으세요.";
            }
          else {
            cout << "감염되지 않았습니다.";
            }
      }

    studentcount++;
    name = "noname";
    birth = 220101;
    heat = 37;
    cough = false;
    break;

  default:
//menu에 1-4외의 값이 입력된 경우 
    cout << "잘못된 입력입니다." << endl;
    break;
  }

cout << "\n1. 신상 정보 입력" << endl;
cout << "2. 의심 증상이 있는 경우" << endl;
cout << "3. 코로나 검사를 받은 경우" << endl;
cout << "4. 결과 출력" << endl << "? ";
}
}

 

 

 

>> 오류부분

1) 4번 메뉴를 3번 돌고 나서 1. 신상정보입력, 2. 의심 증상이 있는 경우, 3. 코로나 검사를 받은 경우 4. 결과출력이 한 번 더 출력된다.

2) noname 220101부분

 

모범답안 

//프로그램은 3명에 대한 결과를 출력하면 종료된다. 그전까지는 계~~ 속 입력받는다.

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

int main() {
for(int i = 0; i < 3; ++i){     // 4번 반복한다.
int menu = 0;
string name = "noname";
string birth = "220101";
double heat = 36.5;
bool cough = false;
string condition = "음성";
 // int studentcount{ 0 };
 // string name;
 // int birth = 0;
 // double heat = 0.0;
 // string condition;
 // 변수 선언 및 초기화는 기본이다! 원래 4번 메뉴에서 해줬는데 이걸 처음에 이렇게 초기화하는걸로!

do {
cout << "1. 신상 정보 입력" << endl;
cout << "2. 의심 증상이 있는 경우" << endl;
cout << "3. 코로나 검사를 받은 경우" << endl;
cout << "4. 결과 출력" << endl << "? ";

cin >> menu;

switch (menu) {
case 1:
cout << "\n이름과 생년월일을 입력하세요." << endl << "? ";
cin >> name >> birth;
break;

case 2:
cout << "\n체온과 기침 증상 유무(true/false)를 입력하세요." << endl << "? ";
cin >> heat >> boolalpha >> cough; //bool타입에 대해 true/false로 입력받기
break;

case 3:
cout << "\n신속항원검사 양성: 신속, PCR 양성: PCR, 음성: 음성" << endl << "? ";
cin >> condition;
break;

case 4:
cout <<endl << name << " " << birth << endl;

//감염의심자이면서 확진자이면 확진자로 판단한다.
if (condition == "신속" || condition == "PCR") {
cout << "확진자입니다. 격리하기 바랍니다.";
}
else {
if (heat > 38 && cough) {
cout << "감염의심자입니다. 검사를 받으세요.";
}
else {
cout << "감염되지 않았습니다.";
}
}

default:
//menu에 1-4외의 값이 입력된 경우 
cout << "잘못된 입력입니다." << endl;
}
cout << endl;

} while (menu != 4); 
//do while문 : 무조건 한 번은 실행시킨다 do 쪽의 출력문을 그 다음에 while뒤에 있는 조건을 만족할 때 까지 실행한다.
}
}

 

 

 

 

#include <iostream>
using namespace std;

int main() {
int a, b;
bool sub = false;

do {
cout << "정수의 개수를 양의 정수로 입력해주세요 : ";
cin >> a;
} while (a <= 0);


cout << endl;

for (int i = 0; (i < a) && (!sub); i++) {
cout << "정수를 입력해주세요 : ";
cin >> b;
sub = (b % 7 == 0);
}



if (sub) {
cout << b<< "는 7로 나눌 수 있습니다." << endl;
}
else {
cout <<"7로 나눌 수 있는 숫자가 없습니다." << endl;
}
}

 

 

주사위를 6000000번 굴려 나온 눈의 횟수

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

int getface();

int main() {
unsigned int f1 = 0;
unsigned int f2 = 0;
unsigned int f3 = 0;
unsigned int f4 = 0;
unsigned int f5 = 0;
unsigned int f6 = 0;
int face;
srand(time(0)); //난수를 생성하기 위해서!

for (int i = 0; i <= 6000000; i++) {
face = getface(); //600000번 이지랄 해야됨

switch (face) {
case 1:
f1++;
break;
case 2:
f2++;
break;
case 3:
f3++;
break;
case 4:
f4++;
break;
case 5:
f5++;
break;
case 6:
f6++;
break;
}
}

cout << "Face" << setw(5) << "Frequency" << endl;
cout << setw(4) << "1" << setw(20) << f1 << endl;
cout << setw(4) << "2" << setw(20) << f2 << endl;
cout << setw(4) << "3" << setw(20) << f3 << endl;
cout << setw(4) << "4" << setw(20) << f4 << endl;
cout << setw(4) << "5" << setw(20) << f5 << endl;
cout << setw(4) << "6" << setw(20) << f6 << endl;


}

int getface() {
return 1 + rand() % 6;   // 1-6사이의 난수 생성
}

 

 

 

 

 

#include <iostream>
using namespace std;

class Circle {  //Circle이라는 class 만들기
private:
double radius;
const double PI = 3.14;    //멤버변수정의
public:
double getRadius() const;
double getArea() const;
double getPerimeter() const;
void setRadius(double value);    //멤버함수정의
};

#include <iostream>
using namespace std;

//class정의
class Circle {  //Circle이라는 class 만들기
private: //클래스 외부에서 멤버에 직접 접근 할 수 없음.
double radius;
const double PI = 3.14;    //멤버변수정의
public:
double getRadius() const; // const가 붙은 이유 : 멤버변수의 값 변경불가
double getArea() const;
double getPerimeter() const;
void setRadius(double value);    //멤버함수정의 = 함수의 프로토타입
};

int main() {

cout << "Circle 2" << endl;
Circle circle2;  //Circle 클래스의 이름은 circle2이다!, 객체의 인스턴트화
circle2.setRadius(20); // circle2함수의 반지름이 20이라고 하자. , 멤버접근 연산자
cout << "반지름 : " << circle2.getRadius() << endl; //circle2의 getRadius()함수 출력!
cout << "넓이 : " << circle2.getArea() << endl;  //circle2의 getArea()함수 출력!
cout << "둘레 : " << circle2.getPerimeter() << endl;  //circle2의 getPerimeter()함수 출력!


}

// 함수 정의 Circle class의 getRadius()함수, return값은 radius!
double Circle :: getRadius() const {
return radius;
}
double Circle::getArea() const {
return radius * radius * PI;
}
double Circle::getPerimeter() const {
return 2 * PI * radius;
}

// 매개변수가 있는 Circle class의 setRadius함수 value값을 입력받는다!
void Circle :: setRadius(double value) {
radius = value;
}

 

set, get함수 사용하기

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

//class정의
class Student {  //Student이라는 class 만들기
private: //클래스 외부에서 멤버에 직접 접근 할 수 없음.
int _id;    /*멤버변수정의*/
public:
string _name; // 멤버변수를 private에만 넣는 것은 아니다.

// 멤버 함수 정의
void setID(int id) {    // 매개변수는 있는데 출력값은 없는 함수, 여기는 왜 const가 없을까?
if (id > 0) {
_id = id;
}

int getID() const { // 출력하는 함수
return _id;
}
};

int main() {
Student s1; //s1이라는 이름을 가진 Student클래스 생성!

s1.setID(-6); //s1함수의 setID라는 함수에 -6대입!
s1._name = "dlwlrma"; //s1함수의 라는 변수에 "dlwlrma"대입!

cout << s1.getID() << endl; //s1함수를 이용해 출력!
cout << s1._name << endl;

}

 

#include <iostream>
using namespace std;

//class정의
class Tmp{  //Student이라는 class 만들기
private: //클래스 외부에서 멤버에 직접 접근 할 수 없음.
int _a;    /*멤버변수정의*/
public:

void setaa(int a) {    // 매개변수는 있는데 출력값은 없는 함수, 여기는 왜 const가 없을까?
if (a > 0) {
_a = a;
}

int getaa() const { // 출력하는 함수
return _a;
}
};

int main() {
Tmp a; //s1이라는 이름을 가진 Student클래스 생성!

a.setaa(7); //s1함수의 setID라는 함수에 -6대입!

cout << a.getaa() << endl; //s1함수를 이용해 출력!


}

 

 

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

//class정의
class Student{  //Student이라는 class 만들기
private: //클래스 외부에서 멤버에 직접 접근 할 수 없음.
int _id;    /*멤버변수정의*/
string _name;
public:
Student();  //기본생성자
Student(int id, string name); //매개변수 생성자
Student(const Student& origin); //복사생성자
};

int main() {
Student s1;
Student s2(1, "pby"); //매개변수 생성자에 매개변수 넣기
Student s3{ s2 }; //복사생성자에 복사할거 넣기, 소괄호랑 대괄호랑 차이가 없나?
}


//함수 선언
Student::Student() {    //Student클래스의 Student()함수 
_id = 0;            //기본값
_name = "noname";
cout << "default constructor: " << _id << " " << _name << endl;
}

Student::Student(int id, string name) {
_id = id;       //매개변수
_name = name;
cout << "parameter constructor: " << _id << " " << _name << endl;
}

Student::Student(const Student& origin) {
_id = origin._id;         // 복사생성자
_name = origin._name;     // 왜 origin. _를쓰냐 기본, 매개변수 생성자랑 다르게?
cout << "copy constructor: " << _id << " " << _name << endl;
}

 

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

//class정의
class Student{  //Student이라는 class 만들기
private: //클래스 외부에서 멤버에 직접 접근 할 수 없음.
int _id;    /*멤버변수정의*/
string _name;
public:
Student();  //기본생성자
Student(int id, string name); //매개변수 생성자
Student(const Student& origin); //복사생성자
~Student () {
cout << "destructor" << _id << " " << _name << endl;
}
};

int main() {
Student s1;
Student s2(1, "pby"); //매개변수 생성자에 매개변수 넣기
Student s3(2, "wook");
//복사생성자에 복사할거 넣기
Student s4(s3);



}

//Student class의 public함수에 있는거!

Student::Student() {    //Student클래스의 Student()함수 
_id = 0;            //기본값
_name = "noname";
cout << "default constructor: " << _id << " " << _name << endl;
}

Student::Student(int id, string name) {
_id = id;       //매개변수
_name = name;
cout << "parameter constructor: " << _id << " " << _name << endl;
}

Student::Student(const Student& origin) {
_id = origin._id;       //왜 복사생성자는 이케 나타냄??
_name = origin._name;
cout << "parameter constructor: " << _id << " " << _name << endl;
}



 

 

'과제' 카테고리의 다른 글

과제6 (파일 나누기)  (0) 2023.01.11
과제4 (함수)  (0) 2023.01.11
과제5 (클래스)  (0) 2023.01.10
배열 예제  (0) 2023.01.03
공지사항
최근에 올라온 글
최근에 달린 댓글
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
글 보관함