1.
0부터 127까지 아스키코드와 해당 문자를 다음과 같이 출력하는 프로그램을 작성하라. 화면에 출력가능하지 않는 아스키코드는 ‘.’ 으로 출력하라
#include <iostream>
#include <cctype> // isprint()
#include <iomanip> // setw()
using namespace std;
// void showDec(int d) { // 10진수 출력
// cout << setw(10) << dec << d;
// }
// void showHexa(int h) { // 16진수 출력
// cout << setw(10) << hex << h;
// }
// void showChar(int c) {
// int i = 0;
// if ( (i = isprint(c)) != 0) // 출력 가능한 문자인지 확인
// cout << setw(10) << (char)c;
// else // 출력 가능한 문자이면 "." 출력
// cout << setw(10) << ".";
// }
// void print() {
// for (int i = 0; i < 4; i++) {
// cout << setw(10) << "dec";
// cout << setw(10) << "hexa";
// cout << setw(10) << "char";
// }
// cout << endl;
// for (int i = 0; i < 4; i++) {
// cout << setw(10) << "___";
// cout << setw(10) << "____";
// cout << setw(10) << "____";
// }
// cout << endl;
// f
// }
int main() {
for (int i = 0; i < 4; i++)
cout << setw(4) << "dec" << setw(5) << "hex" << setw(5) << "char";
cout << endl;
for (int i = 0; i < 4; i++)
cout << setw(4) << "___" << setw(5) << "____" << setw(5) << "____";
cout << endl;
for (int i = 0; i < 128; i++) {
cout << setw(4) << dec << i << setw(5) << hex << i << setw(5) << (isprint(i) ? (char)i : '.');
if(i%4 == 3) cout << endl;
}
}
C++
복사
2.
Phone 클래스의 객체를 입출력하는 아래 코드와 실행결과를 참조하여 <<. >> 연산자를 작성하고, Phone 클래스를 수정하는 등 프로그램을 완성하시오.
#include <iostream>
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 ostream& operator << (ostream& os, Phone p);
// friend istream& operator >> (istream& is, Phone& p);
// };
// ostream& operator << (ostream& os, Phone p) {
// os << "(" << p.name << "," << p.telnum << "," << p.address << ")";
// return os;
// }
// istream& operator >> (istream& ins, Phone& p) {
// cout << "이름:";
// ins >> p.name;
// cout << "전화번호:";
// ins >> p.telnum;
// cout << "주소:";
// ins >> p.address;
// return ins;
// }
// int main() {
// Phone girl, boy;
// cin >> girl >> boy;
// cout << girl << endl << boy << endl;
// }
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 ostream& operator << (ostream& outs, Phone phone);
friend istream& operator >> (istream& ins, Phone& phone);
};
ostream& operator << (ostream& outs, Phone phone) {
outs << "(" << phone.name << "," << phone.telnum << "," << phone.address << ")";
return outs;
}
istream& operator >> (istream& ins, Phone& phone) {
cout << "이름:";
getline(ins, phone.name);
cout << "전화번호:";
getline(ins, phone.telnum);
cout << "주소:";
getline(ins, phone.address);
return ins;
}
int main() {
Phone girl, boy;
cin >> girl >> boy;
cout << girl << endl << boy << endl;
}
C++
복사