C++ Primer 07 프로그래밍 연습
01
#include <iostream>
double cal(double x, double y);
int main()
{
using namespace std;
double x, y;
do
{
cout << "두 수를 입력하세요 (0 입력시 종료) : \n";
cout << "첫번째 수 : ";
cin >> x;
if (x == 0)
break ;
cout << "두번째 수 : ";
cin >> y;
if (y == 0)
break;
cout << "두 수의 조화평균은 " << cal(x, y) << "입니다.\n";
}
while (x != 0 && y != 0);
cout << "프로그램을 종료합니다.\n";
return 0;
}
double cal(double x, double y)
{
return 2.0 * x * y / (x + y);
}
02
#include <iostream>
using namespace std;
int input(int* score);
void output(int* score, int count);
void average(int* score, int count);
int main()
{
int score[10];
int count = input(score);
output(score, count);
average(score, count);
return 0;
}
int input(int* score)
{
int i;
for (i = 0; i < 10; i++)
{
cout << i << "번째 스코어를 입력하세요 (음수 입력시 입력 종료) : ";
cin >> score[i];
if (score[i] < 0)
break;
}
return i;
}
void output(int* score, int count)
{
cout << "획득 스코어 : ";
for (int i = 0; i < count; i++)
cout << score[i] << " ";
cout << endl;
}
void average(int* score, int count)
{
double sum = 0;
for (int i = 0; i < count; i++)
sum += score[i];
cout << "평균 스코어는 " << sum / count << "점 입니다.\n";
}
03
a)
void ouput(box a)
{
cout << "메이커 : " << a.maker << endl;
cout << "높이 : " << a.height << endl;
cout << "넓이 : " << a.width << endl;
cout << "길이 : " << a.length << endl;
cout << "부피 : " << a.volume << endl;
}
b)
void set_volume(box *a)
{
a->volume = a->height * a->length * a->width;
}
c)
#include <iostream>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void output(box a);
void set_volume(box *a);
int main()
{
box a;
cout << "박스 정보를 입력하세요\n";
cout << "박스 메이커 : ";
cin >> a.maker;
cout << "박스 높이 : ";
cin >> a.height;
cout << "박스 가로길이 : ";
cin >> a.width;
cout << "박스 세로길이 : ";
cin >> a.length;
set_volume(&a);
output(a);
return 0;
}
void output(box a)
{
cout << "\n박스의 정보는 다음과 같습니다.\n";
cout << "메이커 : " << a.maker << endl;
cout << "높이 : " << a.height << endl;
cout << "가로 : " << a.width << endl;
cout << "세로 : " << a.length << endl;
cout << "부피 : " << a.volume << endl;
}
void set_volume(box *a)
{
a->volume = a->height * a->length * a->width;
}
04
#include <iostream>
long double probability(unsigned numbers, unsigned picks, unsigned p_min, unsigned p_max);
int main()
{
using namespace std;
double total, choices;
double p_max, p_min;
cout << "전체 수의 개수와 뽑을 수의 개수를 입력하십시오 : \n";
while ((cin >> total >> choices) && choices <= total)
{
cout << "파워볼을 뽑을 범위의 최소값을 입력하십시오 : ";
cin >> p_min;
cout << "파워볼을 뽑을 범위의 최대값을 입력하십시오 : ";
cin >> p_max;
cout << "당신이 이길 확률은 ";
cout << probability(total, choices, p_min, p_max);
cout << "번 중에서 한번입니다.\n";
cout <<"다시 두 수를 입력하십시오. (끝내려면 q를 입력) : ";
}
cout << "프로그램을 종료합니다.\n";
return 0;
}
long double probability(unsigned numbers, unsigned picks, unsigned p_min, unsigned p_max)
{
long double result = 1.0;
long double n;
unsigned p;
for (n = numbers, p = picks; p > 0; n--, p--)
result = result * n / p;
result = result / (p_max - p_min + 1);
return result;
}
05
#include <iostream>
long double factorial(long double x);
int main()
{
using namespace std;
cout << "수를 입력하세요 (음수 입력시 종료) : ";
long double num;
while (cin >> num && num > 0)
{
cout << num << "! = " << factorial(num) << endl;
cout << "수를 입력하세요 (음수 입력시 종료) : ";
}
cout << "프로그램을 종료합니다.\n";
return 0;
}
long double factorial(long double x)
{
if (x == 0)
return 1;
return x * (factorial(x - 1));
}
06
#include <iostream>
using namespace std;
const int SIZE = 5;
int Fill_array(double* arr, int size);
void Show_array(double* arr, int size);
void Reverse_array(double *arr, int size);
int main()
{
double arr[SIZE];
int real_size;
real_size = Fill_array(arr, SIZE);
Show_array(arr, real_size);
Reverse_array(arr, real_size);
Show_array(arr, real_size);
Reverse_array(arr, real_size);
double temp;
temp = arr[0];
arr[0] = arr[real_size - 1];
arr[real_size - 1] = temp;
Show_array(arr, real_size);
return 0;
}
int Fill_array(double* arr, int size)
{
cout << "배열에 저장할 값들을 입력하세요\n";
int i;
for (i = 0; i < size; i++)
{
cout << i + 1 << "번째 값 : ";
if (!(cin >> arr[i]))
break;
}
return i;
}
void Show_array(double* arr, int size)
{
cout << "배열의 값들을 출력합니다.\n";
for (int i = 0; i < size; i++)
cout << i + 1 << "번째 값 : " << arr[i] << endl;
}
void Reverse_array(double *arr, int size)
{
cout << "배열의 값들을 재정렬합니다.\n";
double temp;
for (int i = 0; i < size / 2; i++)
{
temp = arr[i];
arr[i] = arr[size - i - 1];
arr[size - i - 1] = temp;
}
}
07
// arfupt.cpp
#include <iostream>
const int Max = 5;
double* fill_array(double *start, double *end);
void show_array(double* start, double* end);
void revalue(double r, double* start, double* end);
int main()
{
using namespace std;
double properties[Max];
double* pt = fill_array(properties, properties + Max);
show_array(properties, pt);
if (pt != properties)
{
cout << "재평가율을 입력하십시오 : ";
double factor;
while (!(cin >> factor))
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "잘못 입력했습니다, 수치를 입력하세요 : ";
}
revalue(factor, properties, pt);
show_array(properties, pt);
}
cout << "프로그램을 종료합니다.\n";
return 0;
}
double* fill_array(double *start, double *end)
{
using namespace std;
double temp;
double *pt;
int i = 0;
for (pt = start; 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;
i++;
}
return pt;
}
void show_array(double* start, double* end)
{
using namespace std;
double *pt;
int i = 0;
for (pt = start; pt != end; pt++)
{
cout << (i + 1) << "번 부동산 : $";
cout << *pt << endl;
}
}
void revalue(double r, double* start, double* end)
{
double *pt;
for (pt = start; pt != end; pt++)
*pt *= r;
}
08
a)
#include <iostream>
#include <array>
#include <string>
const int Seasons = 4;
const char * Sname[Seasons] = {"Spring", "Summer", "Fail", "Winter"};
void fill(std::array<double, Seasons> *pa);
void show(std::array<double, Seasons> da);
int main()
{
std::array<double, Seasons> expenses;
fill(&expenses);
show(expenses);
return 0;
}
void fill(std::array<double, Seasons> * pa)
{
using namespace std;
for (int i = 0; i < Seasons; i++)
{
cout << Sname[i] << "에 소요되는 비용 : ";
cin >> (*pa)[i];
}
}
void show(std::array<double, Seasons> da)
{
using namespace std;
double total = 0.0;
cout << "\n계졀별 비용\n";
for (int i = 0; i < Seasons; i++)
{
cout << Sname[i] << " : $" << da[i] << endl;
total += da[i];
}
cout << "총 비용 : $" << total << endl;
}
b)
#include <iostream>
#include <array>
#include <string>
const int Seasons = 4;
const char * Sname[Seasons] = {"Spring", "Summer", "Fail", "Winter"};
struct money
{
double expense[Seasons];
};
void fill(money *pa);
void show(money pa);
int main()
{
money pa;
fill(&pa);
show(pa);
return 0;
}
void fill(money *pa)
{
using namespace std;
for (int i = 0; i < Seasons; i++)
{
cout << Sname[i] << "에 소요되는 비용 : ";
cin >> pa->expense[i];
}
}
void show(money pa)
{
using namespace std;
double total = 0.0;
cout << "\n계졀별 비용\n";
for (int i = 0; i < Seasons; i++)
{
cout << Sname[i] << " : $" << pa.expense[i] << endl;
total += pa.expense[i];
}
cout << "총 비용 : $" << total << endl;
}
09
#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);
void display1(student st);
void display2(const student* ps);
void display3(const student pa[], int n);
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 << "프로그램을 종료합니다.\n";
return 0;
}
int getinfo(student pa[], int n)
{
int i;
for (i = 0; i < n; i++)
{
cout << i + 1 << "번째 학생의 정보를 입력합니다.\n";
cout << "이름을 입력하세요 : ";
if (!cin.get(pa[i].fullname, SLEN))
break;
cout << "취미를 입력하세요 : ";
cin >> pa[i].hobby;
cout << "레벨을 입력하세요 : ";
cin >> pa[i].ooplevel;
cin.get();
}
cout << "입력을 종료합니다.\n";
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 << i + 1 << "번째 학생의 정보를 출력합니다.\n";
cout << "학생의 이름 : " << pa[i].fullname << endl;
cout << "학생의 취미 : " << pa[i].hobby << endl;
cout << "학생의 레벨 : " << pa[i].ooplevel << endl;
}
}
10
#include <iostream>
using namespace std;
double calculate(double x, double y, double (*pt)(double, double));
double add(double x, double y);
double sub(double x, double y);
double mul(double x, double y);
double div(double x, double y);
int main()
{
double (*pf[4])(double, double) = {add, sub, mul, div};
double x, y;
cout << "두 수를 입력하세요 : \n";
while (cin >> x >> y)
{
cout << "add : " << calculate(x, y, pf[0]) << endl;
cout << "sub : " << calculate(x, y, pf[1]) << endl;
cout << "mul : " << calculate(x, y, pf[2]) << endl;
cout << "div : " << calculate(x, y, pf[3]) << endl;
cout << "두 수를 입력하세요 : \n";
}
return 0;
}
double calculate(double x, double y, double (*pt)(double, double))
{
return pt(x, y);
}
double add(double x, double y)
{
return x + y;
}
double sub(double x, double y)
{
return x - y;
}
double mul(double x, double y)
{
return x * y;
}
double div(double x, double y)
{
return x / y;
}