본문 바로가기

Programming/C++ 3

[C++] 01-1. C++ 기반의 데이터 입출력

반응형
더보기

CHAPTER 01 - C언어 기반의 C++ 1

01-1 PRINTF와 SCANF를 대신하는 입출력 방식
01-2 함수 오버로딩(FUNCTION OVERLOADING)
01-3 매개변수의 디폴트 값(DEFAULT VALUE)
01-4 인라인(INLINE) 함수
01-5 이름공간(NAMESPACE)에 대한 소개
01-6 OOP 단계별 프로젝트 01단계
01 프로그래밍 문제의 답안

 

문제 1-1 C++기반의 데이터 입출력

#include <iostream>
using namespace std;

int main(void)
{
	int answer;
	int all=0;

	for (int i = 1; i < 6; i++)
	{
		cout << i << "번째 정수 입력 : ";
		cin >> answer;
		all += answer;
	}

	cout <<"합계 :" <<all;
	
	return 0;
}
#include <iostream>
using namespace std;

int main_1_1_2(void)
{
	char name[100];
	char phone[100];

	cout << "이름을 입력하세요 : ";
	cin >> name;
	
	cout << "전화번호를 입력하세요 : ";
	cin >> phone;

	cout <<"이름     : "<< name<<endl;
	cout <<"전화번호 : "<< phone;

	return 0;
}
#include <iostream>
using namespace std;

int main(void)
{
	int number;

	cout << "몇 단을 출력할까요?";
	cin >> number;

	for (int i=1 ; i < 10; i++)
	{
			cout<<number<<"x"<<i<<"="<<number* i<<endl;
		
	}
	return 0;
}
#include <iostream>
using namespace std;

int cal(int item)
{
	int salary = 50;

	return salary + item * 0.12;
}

int main(void)
{
	int item;

	while (1)
	{
		cout << "판매 금액을 만원 단위로 입력(-1 to end) :";
		cin >> item;
		if (item == -1)
			break;

		cout << "이번달 봉급 :" << cal((int)item) << "만원" << endl;

	}

	return 0;
}

 

 

문제 1-2 함수 오버로딩

#include <iostream>
using namespace std;

void swap(int num1, int num2)
{
	int tmp=0;
	tmp = num1;
	num1 = num2;
	num2 = tmp;
	cout << num1 << ' ' << num2 << endl;
}
void swap(char ch1, char ch2)
{
	char tmp=0;
	tmp = ch1;
	ch1 = ch2;
	ch2 = tmp;
	cout << ch1 << ' ' << ch2 << endl;
}
void swap(double dbl1, double dbl2)
{
	double tmp=0;
	tmp = dbl1;
	dbl1 = dbl2;
	dbl2 = tmp;
	cout <<dbl1 << ' ' << dbl2 << endl;
}
int main(void)
{
	int num1 = 20, num2 = 30;
	swap(num1, num2);

	char ch1 = 'A', ch2 = 'Z';
	swap(ch1, ch2);

	double dbl1 = 1.111, dbl2 = 5.555;
	swap(dbl1, dbl2);

	return 0;
}

 

 

문제 1-3 매개변수의 디폴트 값

#include<iostream>

int BoxVolume(int length, int width)
{
	return length * width * 1;
}
int BoxVolume(int length)
{
	return length * 1 * 1;
}
int BoxVolume(int length, int width, int height)
{
	return length * width * height;
}

int main()
{
	std::cout << "[3, 3, 3] : " << BoxVolume(3, 3, 3) << std::endl;
	std::cout << "[5, 5, D] : " << BoxVolume(5, 5) << std::endl;
	std::cout << "[7, D, D] : " << BoxVolume(7) << std::endl;
	//	std::cout<<"[D, D, D] : "<<BoxVolume()<<std::endl;  
	return 0;
}

 

 

문제 1-4 파일의 분할

//SimpleFunc.h

namespace BestComImpl
{
	void SimpleFunc(void);
}

namespace ProgComImpl
{
	void SimpleFunc(void);
}
//SimpleFunc.cpp

#include <iostream>
#include "SimpleFunc.h"

void BestComImpl::SimpleFunc(void)
{
	std::cout<<"BestCom이 정의한 함수"<<std::endl;
}	

void ProgComImpl::SimpleFunc(void)
{
	std::cout<<"ProgCom이 정의한 함수"<<std::endl;
}
//SimpleMain.cpp

#include "SimpleFunc.h"

int main(void)
{
	BestComImpl::SimpleFunc();
	ProgComImpl::SimpleFunc();
	return 0;
}

 

반응형