728x90
1๋ฒ
#include<iostream>
using namespace std;
void print(char* a, int n = 0);
int main()
{
char a[50];
int n,count=0;
for(;;)
{
cout << "๋ฌธ์์ด ์
๋ ฅ : ";
cin.getline(a, 50);
cout << "๋๋ฒ์งธ ๋งค๊ฐ๋ณ์ ์
๋ ฅ: ";
(cin >> n).get();
if (n == 0)
{
print(a);
count++;
}
else
for (int i = 0; i < count; i++)
print(a, n);
}
return 0;
}
void print(char* a, int n)
{
cout << a << endl;
}
2๋ฒ
#include<iostream>
#include<cstring>
using namespace std;
struct CandyBar
{
char Brandname[40];
double weight;
int kcal;
};
void set_CandyBar(CandyBar& a, char* name = "Millennium Munch", double w = 2.85, int k = 350);
void show(CandyBar& a);
int main()
{
CandyBar a;
set_CandyBar(a);
show(a);
}
void set_CandyBar(CandyBar& a, char* name,double w, int k)
{
strcpy_s(a.Brandname, name);
a.weight = w;
a.kcal = k;
}
void show(CandyBar& a)
{
cout << a.Brandname << endl
<< a.weight << endl
<< a.kcal << endl;
}
3๋ฒ
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
void up_alpha(string& a);
int main()
{
string str;
cout << "๋ฌธ์์ด์ ์
๋ ฅํ์์ค (๋๋ด๋ ค๋ฉด q) : ";
getline(cin, str);
while (str != "q")
{
up_alpha(str);
cout << str << endl;
cout << "๋ค์ ๋ฌธ์์ด์ ์
๋ ฅํ์์ค (๋๋ด๋ ค๋ฉด q) :";
getline(cin,str);
}
cout << "๋" << endl;
return 0;
}
void up_alpha(string& a)
{
for (int i = 0; a[i] != '\0'; i++)
{
if (isalpha(a[i]))
a[i]=toupper(a[i]);
}
}
4๋ฒ
#include<iostream>
using namespace std;
#include<cstring>
struct stringy
{
char* str;
int ct;
};
void set(stringy& beany, char* str);
void show(stringy a, int n = 1)
{
for(int i=0;i<n;i++)
cout<<a.str << endl;
}
void show(const char* a, int n = 1)
{
for (int i = 0; i < n; i++)
cout << a << 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, char* str)
{
beany.ct = strlen(str);
beany.str = new char[beany.ct + 1];
strcpy(beany.str, str);
}
5๋ฒ
#include<iostream>
template <class T>
T max(T a[]);
int main()
{
int i_max,a[5] = { 3,4,1,7,4 };
double d_max,b[5] = { 2.4,5.5,4.3,1.3,6.4 };
i_max = max(a);
d_max = max(b);
std::cout << i_max << std::endl
<< d_max << std::endl;
return 0;
}
template <class T>
T max(T a[])
{
T max = a[0];
for (int i = 1; i < 5; i++)
if (max < a[i])
max = a[i];
return max;
}
6๋ฒ
#include<iostream>
#include<cctype>
using namespace std;
template <class T>
T maxn(T a[], int n);
template<> char* maxn(char* a[], int n);
int main()
{
int int_max,a[6] = { 2,5,3,5,1,4 };
double double_max,b[4] = { 1.4,3.5,4.2,3.4 };
char* c[5] = {
"aasdefdasssdda",
"ddsss",
"aaeessddffeeff",
"eeeeerrrrrtttttqqqqq",
"qqqqqwwwwweeeeerrrrr"
};
char* char_max;
int_max = maxn(a, 6);
double_max = maxn(b, 4);
char_max = maxn(c, 5);
cout << int_max << endl
<< double_max << endl
<< char_max << endl;
return 0;
}
template <class T>
T maxn(T a[], int n)
{
T max = a[0];
for (int i = 1; i < n; i++)
if (max < a[i])
max = a[i];
return max;
}
template<> char* maxn(char* a[], int n)
{
char* max = a[0];
for (int i = 1; i < n; i++)
if (strlen(max) < strlen(a[i]))
max = a[i];
return max;
}
7๋ฒ
#include <iostream>
template <typename T>
T ShowArray(T arr[], int n);
template <typename T>
T ShowArray(T* arr[], int n);
struct debts
{
char name[50];
double amount;
};
int main(void)
{
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];
double sum;
for (int i = 0; i < 3; i++)
pd[i] = &mr_E[i].amount;
cout << "Mr. E์ ์ฌ์ฐ ํฉ๊ณ:\n";
sum = ShowArray(things, 6);
cout << sum << endl;
cout << "Mr. E์ ์ฑ๋ฌด ํฉ๊ณ:\n";
sum = ShowArray(pd, 3);
cout << sum << endl;
return 0;
}
template <typename T>
T ShowArray(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 ShowArray(T* arr[], int n)
{
using namespace std;
T sum = 0;
for (int i = 0; i < n; i++)
sum += *arr[i];
return sum;
}
728x90