C++ 프로그래밍 (1학년 2학기)

231214 C++프로그래밍 15주차

Z2AE 2023. 12. 14. 15:43

1. 

#include <iostream>
using std::cout;
using std::endl;
int main(void)
{
	int x = 10;
	int& rx = x;
	cout << x << " " << rx << endl;
	rx = rx + 10;
	cout << x << " " << rx << endl; //참조자(rx)에 변화를 주면 그 타켓(x)도 변함
	x = x + 10;
	cout << x << " " << rx << endl; //타켓(x)에 변화를 주면 그 참조자(rx)도 변함
	return 0;
}

2. 참조자(reference) : C++에서 제일 중요한 문법

3. call by address vs. call by reference

 

4. 형식 설정 멤버함수 :  출력 폭을 지정하거나 깔끔하게 출력하기 위해 사용, 빈칸을 채우는 멤버함수도 존재

#include <iostream>
using namespace std;
int main(){
	cout << "디폴트\n";
	cout.width(10);
	cout << -50 << endl;
	cout << "[ * fill ]\n";
	cout.fill('*');
	cout.width(10);
	cout << -50 << endl;
	cout.width(10);
	cout << 100.25 << endl;
	cout.width(10);
	cout << "HanSH"<<endl;
	cout.fill(' ');
	cout.precision(6); //소수점을 제외한 전체 자리수
	cout<< 12.34567<<endl;
	cout << fixed; //소수점 이하의 자리수만 다루게 함
	cout.precision(3);
	cout << 12.34567 << endl;
	return 0;
}

결과

5. 

#include <iostream>
using namespace std;
int main()
{
	int num = 10;
	cout << "10진수: " << num << endl;
	cout << "16진수: " << hex << num << endl;
	cout << "8진수: " << oct << num << endl;
	return 0;
}

결과

6. setfill과 setw 예시

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    cout << "abcdefg\n";
    cout << 12345 << endl;
    cout << 123.45 << endl;
    cout << "10칸\n";
    cout << setfill('*');
    cout << setw(10) << "abcdefg" << endl;
    cout << setw(10) << 12345 << endl;
    cout << setw(10) << 123.45 << endl;
    //-----------
    cout << "디폴트\n";
    cout.width(10);
    cout << -50 << endl;
    cout << "[ * fill ]\n";
    cout.fill('*');
    cout.width(10);
    cout << -50 << endl;
    cout.width(10);
    cout << 100.25 << endl;
    cout.width(10);
    cout << "HanSH" << endl;
    cout.fill(' ');
    cout.precision(6);  //소수점을 제외한 전체 자리수 
    cout << 12.34567 << endl;
    cout << fixed;  //소수점 이하의 자리수만 다루게 함 
    cout.precision(3);
    cout << 12.34567 << endl;

    return 0;
}

7. 매개변수를 갖는 조절자를 사용하는 경우에는 헤더파일 를 include해야 함

8. 파일로 출력하기 (기록을 남기기 위함)

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	ofstream hout("test1.txt"); // 출력파일 스트림 객체 hout 선언
	if (!hout) {
		cout << "출력할 파일을 열 수 없습니다.";
		return 1;
	}
	hout << "LSY\n";
	hout << 100 << endl << 23.5 << endl;
	hout.close(); //파일 종결
	return 0;
}

 

 

9. 파일로부터 입력받기

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	ifstream hin("test.txt"); // 입력파일 스트림 객체 hin 선언
	if (!hin) {
		cout << "입력할 파일을 열 수 없습니다.";
		return 1;
	}
	char str[50];
	int i, j;
	hin >> str >> i >> j;
	cout << str << " " << i << " " << j << endl;
	hin.close(); // 파일 종결
	return 0;
}

 

10. 파일 입출력을 하는 이유

입력 : 키보드나 마우스 , 출력 : 모니터나 프린터

입출력이 너무 많아요.

계속 값을 저장해놓고 싶어요.

DB를 쓰기도 해요.

11. 파일의 개방/종결 형식

12. 파일에 데이터를 저장하는 방법 - 2

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	ofstream xxx("test2.txt"); // 출력파일 스트림 객체 hout 선언
	if (!xxx) {
		cout << "출력할 파일을 열 수 없습니다.";
		return 1;
	}
	xxx << "LSY\n";
	xxx << 100 << endl << 23.5 << endl;
	xxx.close(); //파일 종결
	return 0;
}

13. "test2.txt"에 저장된 내용을 읽어들여 공백을 "*"로 채워 화면에 출력

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	char ch;

	ifstream hin("test2.txt");
	if (!hin) {
		cout << "입력할 화일을 열 수 없음";
		return 1;
	}
	hin.unsetf(ios::skipws);//공백을 무시하지 말아라

	while (!hin.eof()) {
		hin >> ch;
		if (ch == ' ') ch = '*';
		cout << ch;
	}
	hin.close();
	return 0;
}

14. 파일의 내용에서 i가 들어간 부분을 j로 바꿔서 출력하는 소스

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	char ch;

	ifstream hin("test2.txt");
	if (!hin) {
		cout << "입력할 화일을 열 수 없음";
		return 1;
	}
	hin.unsetf(ios::skipws);//공백을 무시하지 말아라

	while (!hin.eof()) {
		hin >> ch;
		if (ch == 'i') ch = 'j';
		cout << ch;
	}
	hin.close();
	return 0;
}

 

여기서 7점정도 나옴!!!!!

 

참고 자료 : 한성현 교수님 수업자료