728x90
1๋ฒ
//golf.cpp
#include"golf.h"
#include<string>
#include<iostream>
void setgolf(golf& g, const char* name, int hc)
{
strcpy(g.fullname, name);
g.handicap = hc;
}
int setgolf(golf& g)
{
using std::cout;
cout << "์ด๋ฆ ์
๋ ฅ : ";
std::cin.getline(g.fullname, 40);
if (g.fullname[0] == '\0')
{
cout << "์ด๋ฆ์ด ์
๋ ฅ๋์ง ์์์ต๋๋ค.\n";
return 0;
}
cout << "ํธ๋์บก ์
๋ ฅ : ";
(std::cin >> g.handicap).get();
return 1;
}
void handicap(golf& g, int hc)
{
g.handicap = hc;
}
void showgolf(const golf& g)
{
using std::cout;
using std::endl;
cout << "์ด๋ฆ : " << g.fullname << endl
<< "ํธ๋์บก : " << g.handicap << endl;
}
//golf.h
const int Len = 40;
struct golf
{
char fullname[Len];
int handicap;
};
void setgolf(golf& g, const char* name, int hc);
int setgolf(golf& g);
void handicap(golf& g, int hc);
void showgolf(const golf& g);
//main.cpp
#include<iostream>
#include"golf.h"
int main()
{
using std::cout;
golf ann;
golf andy;
setgolf(ann, "Ann Birdfree", 24);
showgolf(ann);
cout << "ann์ ํธ๋์บก ์์ \n";
handicap(ann, 30);
showgolf(ann);
cout << "andy์ ์ ๋ณด ์
๋ ฅ\n";
if(setgolf(andy))
showgolf(andy);
cout << "\n\ngolf ๊ตฌ์กฐ์ฒด์ ๋ฐฐ์ด์ ์
๋ ฅ์ ์๊ตฌ\n\n";
golf ar[5];
int i;
for (i = 0; i < 5; i++)
{
if (setgolf(ar[i]) == 0)
break;
}
cout << "\n\ngolf ๊ตฌ์กฐ์ฒด์ ๋ฐฐ์ด์ถ๋ ฅ\n\n";
for (int j = 0; j < i; j++)
showgolf(ar[j]);
}
2๋ฒ
// static.cpp -- ์ ์ ์ง์ญ ๋ณ์ ์ฌ์ฉํ๊ธฐ
#include <iostream>
#include<string>
// ์์
using namespace std;
// ํจ์ ์ํ
void strcount(string str);
int main()
{
using namespace std;
string input;
char next;
cout << "์๋ฌธ์ผ๋ก ํ ํ์ ์
๋ ฅํ์ญ์์ค:\n";
getline(cin, input);
while (input!="")
{
strcount(input);
cout << "๋ค์ ํ์ ์
๋ ฅํ์ญ์์ค(๋๋ด๋ ค๋ฉด ๋น ํ์ ์
๋ ฅ):\n";
getline(cin, input);
}
cout << "ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.\n";
return 0;
}
void strcount(string str)
{
using namespace std;
static int total = 0; // ์ ์ ์ง์ญ ๋ณ์
int count = 0; // ์๋ ์ง์ญ ๋ณ์
cout << "\"" << str << "\"์๋ ";
while (str[count++]) // ๋ฌธ์์ด์ ๋๊น์ง ์นด์ดํธํ๋ค
;
count -= 1;
total += count;
cout << count << "๊ฐ์ ๋ฌธ์๊ฐ ์์ต๋๋ค.\n";
cout << "์ง๊ธ๊น์ง ์ด " << total << "๊ฐ์ ๋ฌธ์๋ฅผ ์
๋ ฅํ์
จ์ต๋๋ค.\n";
}
3๋ฒ
#include<iostream>
#include<string>
#include<new>
struct chaff
{
char dross[20];
int slag;
};
char buffer1[500];
void show(struct chaff& a);
int main()
{
using std::cout;
using std::endl;
chaff *a = new (buffer1) chaff[2];
cout << "๋ฒํผ์ ์ ์ ๋ฐฐ์ด ์ฌ์ฉ" << endl;
if (a == NULL) return -1;
strcpy(a[0].dross, "abcdef");
a[0].slag = 3;
strcpy(a[1].dross, "qwerty");
a[1].slag = 20;
for (int i = 0; i < 2; i++)
show(a[i]);
cout << "\n๋ณดํต์ new๋ฅผ ์ฌ์ฉํ์ฌ ๋ฒํผ ๋์
" << endl;
char* buffer2 = new char[500];
chaff* a2 = new (buffer2) chaff[2];
if (a2 == NULL) return -1;
strcpy(a2[0].dross, "twoaaa");
a2[0].slag = 9;
strcpy(a2[1].dross, "lkjhg");
a2[1].slag = -3;
for (int i = 0; i < 2; i++)
show(a2[i]);
delete[]buffer2;
return 0;
}
void show(struct chaff& a)
{
using std::cout;
using std::endl;
cout << a.dross << endl
<< a.slag << endl;
}
4๋ฒ
//sale.h
namespace SALES
{
const int QUARTERS = 4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
void setSales(Sales& s, const double ar[], int n);
void setSales(Sales& s);
void showSales(const Sales& s);
}
//sale.cpp
#include<iostream>
#include"sale.h"
using namespace std;
namespace SALES
{
void setSales(Sales& s, const double ar[], int n)
{
if (n < 4)
{
for (int i = n; i < 4; i++)
s.sales[i] = 0;
}
else n = 4;
for (int i = 0; i < n; i++)
s.sales[i] = ar[i];
s.max = s.min = s.average = s.sales[0];
for (int i = 1; i < n; i++)
{
if (s.sales[i] > s.max) s.max = s.sales[i];
if (s.sales[i] < s.min) s.min = s.sales[i];
s.average += s.sales[i];
}
s.average /= n;
}
void setSales(Sales& s)
{
for (int i = 0; i < 4; i++)
{
cout << i + 1 << "๋ถ๊ธฐ๋ณ ํ๋งค์ก : ";
cin >> s.sales[i];
}
s.max = s.min = s.average = s.sales[0];
for (int i = 1; i < 4; i++)
{
if (s.sales[i] > s.max) s.max = s.sales[i];
if (s.sales[i] < s.min) s.min = s.sales[i];
s.average += s.sales[i];
}
s.average /= 4;
}
void showSales(const Sales& s)
{
for (int i = 0; i < 4; i++)
cout << i + 1 << "๋ถ๊ธฐ ํ๋งค์ก : " << s.sales[i] << endl;
cout << "์ต๋๊ฐ : " << s.max << endl
<< "์ต์๊ฐ : " << s.min << endl
<< "ํ๊ท ๊ฐ : " << s.average << endl;
}
}
//main.cpp
#include<iostream>
#include"sale.h"
int main()
{
using namespace std;
using namespace SALES;
Sales a1, a2, a3;
double test1[2] = { 3.1,9.7 };
double test2[4] = { 1.4,2.5,3.2,6.5 };
setSales(a1, test1, 2);
setSales(a2, test2, 4);
showSales(a1); showSales(a2);
cout << endl << endl;
setSales(a3);
showSales(a3);
}
728x90