728x90
7๋ฒ
#include<iostream>
const int Max = 5;
double * fill_array(double * a, double * b);
void show_array(double* a, double* b);
void revalue(double r, double* a, double* b);
int main()
{
using namespace std;
double properties[Max];
double * b = fill_array(properties, properties + Max);
show_array(properties, b);
if (properties != b)
{
cout << "์ฌํ๊ฐ์จ์ ์
๋ ฅํ์ญ์์ค: ";
double factor;
while (!(cin >> factor))
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "์๋ชป ์
๋ ฅ, ์์น ์
๋ ฅ : ";
}
revalue(factor, properties, b);
show_array(properties, b);
}
cout << "ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.\n";
return 0;
}
double * fill_array(double* a, double* b)
{
using namespace std;
double temp;
int i;
for (i = 0; i < Max; i++)
{
cout << (i + 1) << "๋ฒ ๋ถ๋์ฐ์ ๊ฐ๊ฒฉ: $";
cin >> temp;
if (!cin)
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "์
๋ ฅ ๋ถ๋; ์
๋ ฅ ๊ณผ์ ๋\n";
break;
}
else if (temp < 0)
break;
*(a+i) = temp;
}
return (a + i);
}
void show_array(double* a, double* b)
{
using namespace std;
for (int i = 0; (a + i) != b; i++)
{
cout << (i + 1) << "๋ฒ ๋ถ๋์ฐ: $";
cout << *(a + i) << endl;
}
}
void revalue(double r, double* a, double* b)
{
for (int i = 0; (a + i) != b; i++)
*(a + i) *= r;
}
8-a
#include<iostream>
#include<array>
#include<string>
const int Seasons = 4;
const char * Sname[4] =
{ "Spring","Summer","Fall","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๊ณ์ ๋ณ ๋น์ฉS/n";
for (int i = 0; i < Seasons; i++)
{
cout << Sname[i] << " : $" << da[i] << endl;
total += da[i];
}
cout << "์ด ๋น์ฉ : $" << total << endl;
}
8-b
#include<iostream>
#include<array>
#include<string>
const int Seasons = 4;
const char * Sname[4] =
{ "Spring","Summer","Fall","Winter" };
void fill(struct a * pa);
void show(struct a da);
struct a {
double ex[Seasons];
};
int main()
{
struct a expenses;
fill(&expenses);
show(expenses);
return 0;
}
void fill(struct a* pa)
{
using namespace std;
for (int i = 0; i < Seasons; i++)
{
cout << Sname[i] << "์ ์์๋๋ ๋น์ฉ:";
cin >> pa->ex[i];
}
}
void show(struct a da)
{
using namespace std;
double total = 0.0;
cout << "\n๊ณ์ ๋ณ ๋น์ฉS\n";
for (int i = 0; i < Seasons; i++)
{
cout << Sname[i] << " : $" << da.ex[i] << endl;
total += da.ex[i];
}
cout << "์ด ๋น์ฉ : $" << total << endl;
}
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);
void display1(student st);
void display2(const student* pa);
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 << "ํ์ ์ด๋ฆ : "; cin.getline(pa[i].fullname, SLEN);
if (pa[i].fullname[0] == '\0')
break;
cout << "์ทจ๋ฏธ : "; cin.getline(pa[i].hobby, SLEN);
cout << "ooplevel : "; (cin >> pa[i].ooplevel).get();
}
return i;
}
void display1(student st)
{
cout << st.fullname << " "
<< st.hobby << " "
<< st.ooplevel << endl;
}
void display2(const student* pa)
{
cout << pa->fullname << " "
<< pa->hobby << " "
<< pa->ooplevel << endl;
}
void display3(const student pa[], int n)
{
for (int i = 0; i < n; i++)
{
cout << pa[i].fullname << " "
<< pa[i].hobby << " "
<< pa[i].ooplevel << endl;
}
}
10๋ฒ
#include<iostream>
double calculate(double x, double y, double (*cal)(double, double));
double add(double x, double y);
double min(double x, double y);
using namespace std;
int main()
{
double (*pf[2])(double, double) = { add,min };
for (;;)
{
double x, y;
int n;
cout << "๋ ์ ์
๋ ฅ (์ข
๋ฃํ๋ ค๋ฉด ์๋ฌด ๋ฌธ์๋ ์
๋ ฅ) : ";
if (!(cin >> x >> y))
break;
cout << "๋ง์
: 0 ๋บ์
: 1\n"; cin >> n;
double q = calculate(x, y, *pf[n]); cout << "๊ฒฐ๊ณผ๊ฐ " << q << endl;
}
}
double calculate(double x, double y, double (*cal)(double, double))
{
return (*cal)(x, y);
}
double add(double x, double y)
{
return x + y;
}
double min(double x, double y)
{
return x - y;
}
728x90