Search
Duplicate
🔢

5장. 실습문제

생성일
2022/04/11 00:57
태그
C++

5장. 실습문제

1.
다음 main() 함수와 실행 결과를 참고하여 half() 함수를 작성하라
#include <iostream> using namespace std; void half(double &n) { n /= 2; } int main() { double n = 20; half(n); cout << n << endl; }
C++
복사
2.
다음과 같이 선언된 정수를 저장하는 스택 클래스 MyIntStack을 구현하라. 단, MyIntStack 스택에 저장할 수 있는 정수의 최대 개수는 10개이다.
#include <iostream> using namespace std; class MyIntStack { int p[10]; int tos; // 스택의 꼭대기를 가리키는 인덱스 public: MyIntStack(); bool push(int n); // 정수 n 푸시, 꽉 차 있으면 false, 아니면 true 리턴 bool pop(int &n); // 팝하여 n에 저장, 스택이 비어있으면 false, 아니면 true 리턴 }; MyIntStack::MyIntStack() { tos = -1; } bool MyIntStack::push(int n) { if (tos == 9) return false; else { ++tos; p[tos] = n; return true; } } bool MyIntStack::pop(int &n) { if (tos == (-1)) return false; else { n = p[tos]; --tos; return true; } } int main() { MyIntStack a; for (int i = 0; i < 11; i++) { if (a.push(i)) cout << i << ' '; else cout << endl << i+1 << " 번째 stack full" << endl; } int n; for (int i = 0; i < 11; i++) { if(a.pop(n)) cout << n << ' '; else cout << endl << i+1 << " 번째 stack empty"; } cout << endl; }
C++
복사
3.
다음은 학과를 나타내는 Dept 클래스와 이를 활용하는 main() 함수를 보여준다
#include <iostream> using namespace std; class Dept { int size; // scores 배열의 크기 int *scores; // 동적 할당 받을 정수 배열의 주소 public: Dept(int size) { // 생성자 this->size = size; scores = new int[size]; } // Dept(Dept &dept); // 복사 생성자 ~Dept(); // 소멸자 int getSize() { return size; } void read(); // size 만큼 키보드에서 정수를 읽어 scores 배열에 저장 bool isOver60(int index); // index 의 학생의 성적이 60보다 크면 true 리턴 }; int countPass (Dept &dept) { // dept 학과에 60점 이상으로 통과하는 학생의 수 리턴 int count = 0; for (int i = 0; i < dept.getSize(); i++) { if (dept.isOver60(i)) count++; } return count; } // Dept::Dept(Dept &dept) { // this->size = dept.size; // this->scores = new int[dept.size]; // for (int i = 0; i < dept.size; ++i) // this->scores[i] = dept.scores[i]; // } Dept::~Dept() { delete[] scores; } void Dept::read() { cout << size << "개 점수 입력>> "; for (int i = 0; i < size; i++) { int n; cin >> n; scores[i] = n; } } bool Dept::isOver60(int index) { if (scores[index] > 60) return true; else return false; } int main() { Dept com(10); // 총 10명이 있는 학과 com com.read(); // 총 10명의 학생들의 성적을 키보드로부터 읽어 scores 배열에 저장 int n = countPass(com); // com 학과에 60점 이상으로 통과한 학생의 수를 리턴 cout << "60점 이상은 " << n << "명"; }
C++
복사
1.
swap() 함수 - 참조에 의한 호출
#include <iostream> using namespace std; class Circle { int num; public: Circle(); Circle(int num) { this->num = num; } void setNum(int num) { this->num = num; } int getNum() { return num; } }; void swap(Circle &a, Circle &b) { int swap; swap = a.getNum(); a.setNum(b.getNum()); b.setNum(swap); } int main() { Circle a(5), b(10); cout << a.getNum() << " " << b.getNum() << endl; swap(a, b); cout << a.getNum() << " " << b.getNum(); }
C++
복사
3.
문자열 이어붙이기 - string 클래스와 참조 사용 연습
#include <iostream> #include <string> using namespace std; void combine(string t1, string t2, string& t3) { t3 = t1 + " " + t2; } int main() { string text1("I love you"), text2("very much"); string text3; combine(text1, text2, text3); cout << text3; }
C++
복사
4.
bigger - 참조에 의한 호출 연습
#include <iostream> using namespace std; bool bigger(int a, int b, int& big) { if (a == b) return true; else { if (a > b) big = a; else big = b; return false; } } int main() { int a, b, big; cout << "두개의 정수를 입력하세요>> "; cin >> a >> b; if (!bigger(a, b, big)) cout << "큰 수는 : " << big; else cout << "두 정수가 같습니다."; }
C++
복사
6.
참조에 의한 호출과 참조를 리턴하는 함수 작성
#include <iostream> #include <string> using namespace std; char& find(char a[], char c, bool& success) { int len = sizeof(a); for (int i = 0; i < len; i++) { if (a[i] == c) { success = true; return a[i]; } } } int main() { char s[] = "Mike"; bool b = false; char& loc = find(s, 'M', b); if (b == false) { cout << "M을 발견할 숙 없다." << endl; return 0; } loc = 'm'; cout << s << endl; }
C++
복사