-
[C++] 함수 - C++ 의 프로그래밍 모듈 (2)Programming/프로그래밍 2020. 8. 7. 00:47
C++ 기초플러스
7장. 연습문제 풀이 6 ~ 10번
이어서 작성합니다. 기능적으로 필요한 부분은 문제 요구사항에 따라 작성을 하였으며
최대한 함수를 구현함으로써 문제를 해결해 나가도록 하였습니다.
6. 다음과 같은 함수를 사용하는 프로그램을 작성하라.
Fill_array()는 double형 값들의 배열 이름과 배열 크기를 매개변수로 취한다.
이 함수는 배열에 저장할 double형 값들을 입력하라는 프롬프트를 표시한다.
배열이 가득 찼거나, 숫자가 아닌 것이 입력되면 입력받기를 멈추고, 그때까지 입력 받은 값의 개수를 리턴한다.
Show_array()는 double형 값들의 배열 이름과 배열 크기를 배개변수로 취하고, 배열의 내용을 출력한다.
Reserve_array()는 double형 값들의 배열 이름과 배열 크기를 매개변수로 취하고, 배열에 저장된 값들의 순서를 뒤집는다.
프로그램은 이 함수들을 사용하여 배열을 채우고, 배열을 출력하고, 배열을 저장 순서를 뒤집고, 배열을 출력하고, 배열을 첫 번째 원소와 마지막 원소를 제외한 모든 원소의 순서를 뒤집고, 배열의 내용을 출력해야 한다.
코드
#include <iostream> #define MAX 9 int Fill_array(double[], int); void Show_array(double[], int); void Reverse_array(double[], int); int main() { double arr[MAX] = {-1, }; int cnt; cnt = Fill_array(arr, MAX); Show_array(arr, cnt); Reverse_array(arr, cnt); Show_array(arr, cnt); Reverse_array(arr + 1, cnt - 2); Show_array(arr, cnt); return 0; } int Fill_array(double arr[], int size) { using namespace std; int i; for(i = 0; i < size; i++) { if(!(cin >> arr[i])) { return i; } } return i; } void Show_array(double arr[], int cnt) { using namespace std; for(int i = 0; i < cnt; i++) { cout << arr[i] << " "; } cout << endl; } void Reverse_array(double arr[], int cnt) { using namespace std; int i, j; double temp; // 순서 바꾸기 로직! for(i = 0, j = cnt - 1; i < j; i++, j--) { temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } }
7번.
Listing 7.7 에서 세 개의 배열 처리 함수가 범위를 나타내는 두 개의 포인터 매개변수를 사용하도록 수정하라.
실제로 입력받은 개수를 리턴하는 대신에, fill_array() 함수는 배열에서 마지막으로 채워진 곳 바로 다음 위치를 지시하는 포인터를 리턴해야 한다. 다른 두 함수는 이 포인터를 두 번째 매개변수로 사용하여 데이터의 끝을 판단해야 한다.
원본 Listing 7.7 코드
#include <iostream> const int Max = 5; int fill_array(double ar[], int limit); void show_array(const double ar[], int n); void revalue(double r, double ar[], int n); int main() { using namespace std; double properties[Max]; int size = fill_array(properties, Max); show_array(properties, size); if (size > 0) { cout << "재평가율을 입력하십시오. : "; double factor; while (!(cin >> factor)) { cin.clear(); while (cin.get() != '\n') continue; cout << "잘못된 입력입니다. 수치를 입력하세요. : "; } revalue(factor, properties, size); show_array(properties, size); } cout << "프로그램을 종료합니다.\n"; cin.get(); cin.get(); return 0; } int fill_array(double ar[], int limit) { using namespace std; double temp; int i; for (i = 0; i < limit; i++) { cout << (i + 1) << "번 부동산의 가격: $"; cin >> temp; if (!cin) { cin.clear(); while (cin.get() != '\n') continue; cout << "입력 불량; 입력 과정을 끝내겠습니다.\n"; break; } else if (temp < 0) break; ar[i] = temp; } return i; } void show_array(const double ar[], int n) { using namespace std; for (int i = 0; i < n; i++) { cout << (i + 1) << "번 부동산: $"; cout << ar[i] << endl; } } void revalue(double r, double ar[], int n) { for (int i = 0; i < n; i++) ar[i] *= r; }
문제의 요구사항 수정 코드
#include <iostream> const int Max = 5; double *fill_array(double *, double *); void show_array(double *, const double *); void revalue(double *, double *); int main() { using namespace std; double properties[Max]; double *end = fill_array(properties, properties + Max); show_array(properties, end); revalue(properties, end); show_array(properties, end); cout << "프로그램을 종료합니다." << endl; return 0; } double * fill_array(double *first, double *end) { using namespace std; double temp; double *pt; int i = 0; for (pt = first; pt < end; pt++) { cout << (i + 1) << "번 부동산의 가격: $"; cin >> temp; if (!cin) { cin.clear(); while (cin.get() != '\n') continue; cout << "입력 불량; 입력 과정을 끝내겠습니다.\n"; break; } else if (temp < 0) break; *pt = temp; } return pt; } void show_array(double *first, const double *end) { using namespace std; double *pt; int i = 0; for (pt = first; pt != end; pt++) { cout << (i + 1) << "번 부동산: $"; cout << *pt << endl; } } void revalue(double *first, double *end) { using namespace std; double *pt; double factor; cin >> factor; for (pt = first; pt < end; pt++) *pt *= factor; }
8번은 걍 .. 예제소스 고치는 문제라 생략하자..ㅠㅠ
9번.
함수를 정의해두었는데 주석문에 서술된 함수들을 작성하여 프로그램을 완성하면 된다.
#include <iostream> using namespace std; const int SLEN = 30; struct student { char fullname[SLEN]; char hobby[SLEN]; int ooplevel; }; int getInfo(student pa[], int n) { int i = 0; for(i = 0; i < n; i++) { cin >> pa[i].fullname; if( strcmp(pa[i].fullname, "") == 0 ) { cout << "중단합니다." << endl; return i; } cin >> pa[i].hobby; cin >> pa[i].ooplevel; } return i; } void display1(student st) { cout << st.fullname << endl; cout << st.hobby << endl; cout << st.ooplevel << endl; } void display2(const student *ps) { cout << ps->fullname << endl; cout << ps->hobby << endl; cout << ps->ooplevel << endl; } void display3(const student pa[], int n) { for(int i = 0; i < n; i++) { cout << pa[i].fullname << endl; cout << pa[i].hobby << endl; cout << pa[i].ooplevel << endl; } } int main() { cout << "학급의 학생 수를 입력하십시오. : "; int class_size; cin >> class_size; while (cin.get() != '\n') continue; student *ptr_stu = new student[class_size]; int entered = getInfo(ptr_stu, class_size); for(int i = 0; i < entered; i++) { display1(ptr_stu[i]); display2(&ptr_stu[i]); } display3(ptr_stu, entered); cout << "프로그램을 종료합니다." << endl; return 0; }
10번.
calculate() 함수를 설계하면 된다.
반응형'Programming > 프로그래밍' 카테고리의 다른 글
[PHP] Composer classmap 사용 (0) 2020.09.05 [C++] 객체와 클래스 연습문제 (2) (0) 2020.08.16 [C++] 객체와 클래스 연습문제 (1) (0) 2020.08.14 [C++] 함수 - C++의 프로그래밍 모듈 (1) (0) 2020.08.06 [PHP] APM 설치 (0) 2019.01.07