728x90
1๋ฒ
#include<iostream>
#include<cctype>
int main()
{
using namespace std;
char ch;
do
{
cout << "๋ฌธ์ ์
๋ ฅ : ";
cin >> ch;
if (islower(ch))
ch = toupper(ch);
else if (isupper(ch))
ch = tolower(ch);
cout << ch << endl;
} while (ch != '@');
return 0;
}
2๋ฒ
#include<iostream>
#include<array>
int main()
{
using namespace std;
array<double, 10> donation; //double donation[10];
double sum = 0.0;
double aver;
int count = 0;
for (int i = 0; i < 10; i++)
{
cout << "๊ธฐ๋ถ๊ธ ์
๋ ฅ #" << i + 1 << " :";
if (!(cin >> donation[i]))
exit(EXIT_FAILURE);
sum += donation[i];
}
aver = sum / 10;
for (int i = 0; i < 10; i++)
{
if (donation[i] > aver)
count++;
}
cout << "๊ธฐ๋ถ๊ธ๋ค์ ํ๊ท : " << aver << endl
<< "ํ๊ท ๋ณด๋ค ํฐ ๊ธฐ๋ถ๊ธ์ ์ : " << count << endl;
return 0;
}
array ํ ํ๋ฆฟ ํด๋์ค(C++11)
array ๊ฐ์ฒด๋ ์์ ์ ์ฅ ๋์ ์ ๊ณ ์ ๋ ํฌ๊ธฐ์ ๊ณ ์ ๋ฉ๋ชจ๋ฆฌ ๋์ ์ ์ฌ์ฉํ์ฌ ๋ด์ฌ ๋ฐฐ์ด์ด ์ง๋ ๊ฒ๊ณผ ๋์ผํ ์์ค์ ํจ์จ์ฑ์ ์ง๋
array<typeName,n_elem> arr;
typeName์ n_elem ์์๋ฅผ ํตํด์ array ๊ฐ์ฒด arr๋ฅผ ์์ฑํจ.
n_elem์ ๋ณ์๊ฐ ๋ ์ ์๋ค.
3๋ฒ
#include<iostream>
int main()
{
using namespace std;
cout << "๋ค์ ์ ํ ์ฌํญ ์ค์์ ํ๋๋ฅผ ์ ํํ์ญ์์ค. (๋๋ด๋ ค๋ฉด q)\n"
<< "c) camera p) pianist\n"
<< "t) tree g) game\n";
char ch;
cin >> ch;
while (ch != 'q')
{
switch (ch)
{
case 'c': cout << "์นด๋ฉ๋ผ ์ ํ\n";
break;
case 'p':cout << "ํผ์๋์คํธ ์ ํ\n";
break;
case 't':cout << "๋๋ฌด ์ ํ\n";
break;
case 'g':cout << "๊ฒ์ ์ ํ\n";
break;
default: cout << "c, p, t, g ์ค์์ ํ๋ ์ ํ. (๋๋ด๋ ค๋ฉด q): ";
cin >> ch;
continue;
}
break;
}
return 0;
}
switch ๊ตฌ๋ฌธ
switch ๊ตฌ๋ฌธ์ ์ฌ์ฉํ๋ ๊ธฐ๋ณธ ํ์
switch (integer-expresson)
{
case label1:statement(s)
case label2:statement(s)
...
default : statements(s)
}
4๋ฒ
#include<iostream>
const int strsize = 20;
struct bop {
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference; //0 = fullname,1=title,2=bopname
};
int main()
{
using namespace std;
char ch;
bop list[5] = {
{"Wimp Macho","aaa","bbb",0},
{"Raki Rhodes","Junior Programmer","ccc",1},
{"Celia Laiter","ddd","MIPS",2},
{"Hoopy Hipman","Analyst Trainee","eee",1},
{"Pat Hand","fff","LOOPY",2}
};
cout << "Benevolent Order of Programmers\n"
<< "a. ์ค๋ช
์ผ๋ก ์ด๋ b. ์งํจ์ผ๋ก ์ด๋\n"
<< "c. BOP ์์ด๋๋ก ์ด๋ d. ํ์์ด ์ง์ ํ ๊ฒ์ผ๋ก ์ด๋\n"
<< "q. ์ข
๋ฃ\n";
do
{
cout << "์ํ๋ ๊ฒ์ ์ ํํ์ญ์์ค : ";
cin >> ch;
switch (ch)
{
case 'a':
for (int i = 0; i < 5; i++)
cout << list[i].fullname << endl;
break;
case 'b':
for (int i = 0; i < 5; i++)
cout << list[i].title << endl;
break;
case 'c':
for (int i = 0; i < 5; i++)
cout << list[i].bopname << endl;
break;
case 'd':
for (int i = 0; i < 5; i++)
{
if(list[i].preference==0)
cout << list[i].fullname << endl;
else if(list[i].preference == 1)
cout << list[i].title << endl;
else
cout << list[i].bopname << endl;
}
break;
case 'q':
break;
}
} while (ch != 'q');
cout << "ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค." << endl;
return 0;
}
5๋ฒ
#include<iostream>
int main()
{
using namespace std;
int tvarp;
int tax=0;
cout << "์๋ ์
๋ ฅ : ";
while (cin >> tvarp && tvarp>0)
{
if (tvarp <= 5000)
tax = 0;
else if (tvarp > 5000 && tvarp <= 10000)
tax = (tvarp - 5000) * 0.1;
else if (tvarp > 10000 && tvarp <= 20000)
tax = (tvarp - 10000) * 0.15 +1000;
else if (tvarp >= 35000)
tax = (tvarp - 35000) * 0.2 + 4000;
cout << "๋ด์ผํ ์๋์ธ : " << tax << endl;
cout << "์๋ ์
๋ ฅ : ";
}
return 0;
}
728x90