01
#include <iostream>
void show(const char * str, int i = 0);
int main()
{
using namespace std;
show("abc");
show("abc");
show("abc");
show("abc");
show("abcde", 1);
return 0;
}
void show(const char * str, int num)
{
static int count = 0;
count++;
if (num == 0)
std::cout << str << std::endl;
else
{
for(int i = count; i > 0; i--)
std::cout << str << std::endl;
}
}
02
#include <iostream>
#include <cstring>
struct CandyBar
{
char name[30];
double weight;
int cal;
};
void set_candy(CandyBar & candy, const char * str = "Millennium Munch", double w = 2.85, int c = 350);
void Show(const CandyBar & candy);
int main()
{
using namespace std;
CandyBar a, b;
set_candy(a, "aaaaa", 100, 50);
Show(a);
set_candy(b);
Show(b);
return 0;
}
void set_candy(CandyBar & candy, const char * str, double w, int c)
{
strcpy(candy.name, str);
candy.weight = w;
candy.cal = c;
}
void Show(const CandyBar & candy)
{
using namespace std;
cout << "name : " << candy.name << endl;
cout << "weight : " << candy.weight << endl;
cout << "cal : " << candy.cal << endl;
}
03
#include <iostream>
#include <string>
using namespace std;
string change_upper(string & str);
int main()
{
string str;
cout << "문자열을 입력하시오 (끝내려면 q) : ";
getline(cin, str);
while (str != "q")
{
str = change_upper(str);
cout << str << endl;
cout << "다음 문자열을 입력하시오 (끝내려면 q) : ";
getline(cin, str);
}
cout << "끝.\n";
return 0;
}
string change_upper(string &str)
{
for (int i = 0; str[i]; i++)
str[i] = toupper(str[i]);
return str;
}
04
#include <iostream>
#include <cstring>
using namespace std;
struct stringy
{
char * str;
int ct;
};
void set(stringy & beany, const char testing[]);
void show(const stringy & beany, int count = 1);
void show(const char testing[], int count = 1);
void show(const stringy & beany, int count)
{
for (int i = count; i > 0; i--)
cout << beany.str << endl;
}
void show(const char testing[], int count)
{
for (int i = count; i > 0; i--)
cout << testing << endl;
}
int main()
{
stringy beany;
char testing[] = "Reality isn't what it used to be.";
set(beany, testing);
show(beany);
show(beany, 2);
testing[0] = 'D';
testing[1] = 'u';
show(testing);
show(testing, 3);
show("Done!");
return 0;
}
void set(stringy & beany, const char testing[])
{
char * new_str = new char;
beany.str = new_str;
strcpy(new_str, testing);
beany.ct = strlen(new_str);
}
05
#include <iostream>
template <class T>
T max5(const T arr[5])
{
T max;
max = arr[0];
for (int i = 1; i < 5; i++)
{
if (max < arr[i])
max = arr[i];
}
return max;
}
int main()
{
using namespace std;
int arr1[5] = {1, 5, 3, 10, 2};
double arr2[5] = {1.1, 2.14, 5.42, 3.22, 4.83};
cout << max5(arr1) << endl;
cout << max5(arr2) << endl;
return 0;
}
06
#include <iostream>
#include <cstring>
template <class T>
T maxn(T arr[], int n);
template <>
const char * maxn(const char * arr[], int n);
int main()
{
using namespace std;
int arr1[6] = {1, 5, 3, 10, 2, 99};
double arr2[4] = {1.1, 2.14, 5.42, 3.22};
const char * arr3[5] = {"a", "bb", "ccc", "dddd", "eeeee"};
cout << maxn(arr1, 6) << endl;
cout << maxn(arr2, 4) << endl;
cout << maxn(arr3, 5) << endl;
return 0;
}
template <class T>
T maxn(T arr[], int n)
{
T max;
max = arr[0];
for (int i = 1; i < n; i++)
{
if (max < arr[i])
max = arr[i];
}
return max;
}
template <>
const char * maxn(const char * arr[], int n)
{
const char * max = arr[0];
for (int i = 1; i < n; i++)
{
if (strlen(max) < strlen(arr[i]))
max = arr[i];
}
return max;
}
07
#include <iostream>
template <typename T>
T SumArray(T arr[], int n);
template <typename T>
T SumArray(T * arr[], int n);
struct debts
{
char name[50];
double amount;
};
int main()
{
using namespace std;
int things[6] = {13, 31, 103, 301, 310, 130};
struct debts mr_E[3] =
{
{"Ima Wolfe", 2400.0},
{"Ura Foxe", 1300.0},
{"Iby Stout", 1800.0}
};
double * pd[3];
for (int i = 0; i < 3; i++)
pd[i] = &mr_E[i].amount;
cout << "Mr.E의 재산의 합 : ";
cout << SumArray(things, 6) << endl;
cout << "Mr.E의 채무의 합 : ";
cout << SumArray(pd, 3) << endl;
return 0;
}
template <typename T>
T SumArray(T arr[], int n)
{
using namespace std;
T sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
template <typename T>
T SumArray(T * arr[], int n)
{
using namespace std;
T sum = 0;
for (int i = 0; i < n; i++)
sum += *arr[i];
return sum;
}
댓글