1.

#include <iostream>
using namespace std;
int main() {
int count = 0;
int ch;
while ((ch = cin.get()) != EOF) {
if (ch == 'a') count++;
else if (ch == '\n') break;
}
cout << "a 문자는 총 " << count << "개 입니다." << endl;
}
2.

#include <iostream>
using namespace std;
int main() {
int count = 0;
char ch;
while (true) {
cin.get(ch);
if (ch == EOF) break;
else if (ch == ' ') count++;
else if (ch == '\n') break;
}
cout << "\' \' 문자는 총 " << count << "개 입니다." << endl;
}
3.

#include <iostream>
using namespace std;
int main() {
int ch;
cin.ignore(100, ';');
while ((ch = cin.get()) != EOF) {
cout.put(ch);
if (ch == '\n') {
cin.ignore(100, ';');
}
}
}
4.

#include <iostream>
using namespace std;
int main() {
int ch;
while ((ch = cin.get()) != EOF) {
if (ch == ';') {
cin.ignore(100, '\n');
cout.put('\n');
}
else cout.put(ch);
}
}
5.

#include <iostream>
#include <string>
using namespace std;
int main() {
string cmd;
while (true) {
cout << "종료하려면 exit을 입력하세요 >> ";
getline(cin, cmd);
if (cmd == "exit") {
cout << "프로그램을 종료합니다...." << endl;
break;
}
}
}
6.

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
cout << left;
cout << setw(15) << "Number";
cout << setw(15) << "Square";
cout << setw(15) << "Square Root" << endl;
for (int i = 0; i <= 45; i += 5) {
cout << setw(15) << setfill('_') << i;
cout << setw(15) << setfill('_') << i * i;
cout << setw(15) << setprecision(3) << setfill('_') << sqrt((double)i) << endl;
}
}
7.

#include <iostream>
#include <string>
#include <cmath>
#include <cctype>
#include <iomanip>
using namespace std;
int main() {
cout << "dec" << '\t' << "hexa" << '\t' << "char" << '\t';
cout << "dec" << '\t' << "hexa" << '\t' << "char" << '\t';
cout << "dec" << '\t' << "hexa" << '\t' << "char" << '\t';
cout << "dec" << '\t' << "hexa" << '\t' << "char" << endl;
cout << "---" << '\t' << "---" << "\t" << "---" << "\t";
cout << "---" << '\t' << "---" << "\t" << "---" << '\t';
cout << "---" << '\t' << "---" << "\t" << "---" << '\t';
cout << "---" << '\t' << "---" << "\t" << "---" << endl;
for (int i = 0; i < 128; i++) {
cout << dec << i << '\t' << hex << i << '\t';
cout << (isprint((char)i) ? (char)i : '.') << '\t';
if (i % 4 == 3)
cout << endl;
}
}
8.

#include <iostream>
#include <string>
#include <cmath>
#include <cctype>
#include <iomanip>
using namespace std;
class Circle {
string name;
int radius;
public:
Circle(int radius = 1, string name = "") {
this->radius = radius;
this->name = name;
}
friend istream& operator>> (istream& in, Circle& c);
friend ostream& operator<<(ostream& out, Circle c);
};
istream& operator>> (istream& in, Circle& c) {
cout << "반지름 >> ";
in >> c.radius;
cout << "이름 >> ";
in >> c.name;
return in;
}
ostream& operator<<(ostream& out, Circle c) {
out << "(반지름" << c.radius << "인 " << c.name << ")";
return out;
}
int main() {
Circle d, w;
cin >> d >> w;
cout << d << w << endl;
}
9. 띄어쓰기 포함 -> getline(in, ph.address)

#include <iostream>
#include <string>
#include <cmath>
#include <cctype>
#include <iomanip>
using namespace std;
class Phone {
string name;
string telnum;
string address;
public:
Phone(string name = "", string telnum = "", string address = "") {
this->name = name;
this->telnum = telnum;
this->address = address;
}
friend istream& operator>> (istream& in, Phone& ph);
friend ostream& operator<< (ostream& out, Phone ph);
};
istream& operator>> (istream& in, Phone& ph) {
cout << "이름:";
getline(in, ph.name);
cout << "전화번호:";
getline(in, ph.telnum);
cout << "주소:";
getline(in, ph.address);
return in;
}
ostream& operator<< (ostream& out, Phone ph) {
cout << "(" << ph.name << "," << ph.telnum << "," << ph.address << ")";
return out;
}
int main() {
Phone girl, boy;
cin >> girl >> boy;
cout << girl << endl << boy << endl;
}
10

#include <iostream>
#include <string>
using namespace std;
istream& prompt(istream& in) {
cout << "암호?";
return in;
}
int main() {
string password;
while (true) {
cin >> prompt >> password;
if (password == "C++") {
cout << "login success!!" << endl;
break;
}
else
cout << "login fail. try again!!" << endl;
}
}
11

#include <iostream>
using namespace std;
istream& pos(istream& in) {
cout << "위치는? ";
return in;
}
int main() {
int x, y;
cin >> pos >> x;
cin >> pos >> y;
cout << x << ',' << y << endl;
}'C++' 카테고리의 다른 글
| [명품 C++ Programming] 12장 실습 문제 (0) | 2025.06.16 |
|---|---|
| [명품 C++ Programming] 10장 실습 문제 (0) | 2025.05.30 |
| [명품 C++ Programming] 9장 실습 문제 (0) | 2025.05.24 |
| [명품 C++ Programming] 8장 실습 문제 (0) | 2025.05.24 |
| [명품 C++ Programming] 7장 실습 문제 (0) | 2025.05.24 |