열혈c++ 예제풀이-1챕터
사용된 컴파일러는 visual studio입니다.
1-1
문제 : 사용자로부터 총 5개의 정수를 입력받아서, 그 합을 출력하는 프로그램을 작성해보자
코드
#include
using namespace std;
void main()
{
int result=0;
int num;
for (int index = 0; index < 5; index++)
{
cout << (index+1) << "번째 정수 입력 : ";
cin >> num;
result += num;
}
cout << "정수의 합 : " <<result<<endl;
system("pause");
}
실행례
1-2
프로그램 사용자로부터 이름과 전화번호를 문자열의 형태로 입력받아서, 입력받은 데이터를 그대로 출력하는 프로그램을 작성하라
#include
void main()
{
char name[20];
char phone[20];
std::cout <<"이름 입력 : ";
std::cin >> name;
std::cout << "전화번호 입력 : ";
std::cin >> phone;
std::cout << "입력값\n이름 : " << name << std::endl << "전화번호 : " << phone << std::endl;
system("pause");
}
1-3
숫자를 하나 입력받아서 그 숫자에 해당하는 구구단을 출력하는 프로그램을 작성하라
#include
void Func9_9(int num)
{
for (int re = 1; re <= 9; re++)
{
std::cout << num << " x " << re << " = " << (num*re) << std::endl;
}
}
void main()
{
int input;
std::cout << "원하는 구구단의 숫자를 입력하시오 : ";
std::cin >> input;
std::cout << "출력" << std::endl;
Func9_9(input);
system("pause");
}
1-4
//판매원들의 급여 계산프로그램... 기본급여 50만원, 물품 판매 가격의 12% 추가지급
#include
void PayPlus(int sellMoney,int *salary)
{
salary += (sellMoney0.12);
}
void Output(int salary)
{
std::cout << "이번달 급여 : " << salary<<std::endl;
}
void main()
{
int salary = 50;
int sell;
for (int outNum=0;outNum!=(-1);)
{
std::cout << "판매 금액을 만원 단위로 입력(-1 입력시 종료) : ";
std::cin >> outNum;
if (outNum <= 0)
{
Output(salary);
break;
}
else
{
PayPlus(outNum, &salary);
Output(salary);
}
}
std::cout << "프로그램을 종료합니다." << std::endl;
system("pause");
}
2-1
//Swap 함수 오버로딩 구현
#include
void Swap(char *one, char *two)
{
char keep=*one;
*one = *two;
*two = keep;
}
void Swap(int *one, int *two)
{
int keep = *one;
*one = *two;
*two = keep;
}
void Swap(double *one, double *two)
{
double keep=*one;
*one = *two;
*two = keep;
}
void main()
{
int num1 = 20;
int num2 = 30;
Swap(&num1, &num2);
std::cout << num1 << ' ' << num2 << std::endl;
char ch1 = 'A', ch2 = '2';
Swap(&ch1, &ch2);
std::cout << ch1 << ' ' << ch2 << std::endl;
double dbl1 = 1.111;
double dbl2 = 5.555;
Swap(&dbl1, &dbl2);
std::cout << dbl1 << ' ' << dbl2 << std::endl;
system("pause");
}
3-1
제시된 디폴트 생성자 활용 함수를 오버로딩 함수로 변경하라
#include
int BoxVol(int length, int width, int height)
{
return lengthwidthheight;
}
int BoxVol(int length, int hmm)
{
return length*hmm * 1;
}
int BoxVol(int length)
{
return length * 1 * 1;
}
void main()
{
std::cout << "[3.3.3] : " << BoxVol(3, 3, 3) << std::endl <<
"[5.5.D] : " << BoxVol(5.5) << std::endl <<
"[7.D.D] : " << BoxVol(7) << std::endl;
system("pause");
}
4-1
제시된 코드를 두개의 소스파일과 하나의 헤더파일로 분리
헤더파일
namespace OneSpace
{
void SimpleFunc(void);
void PrettyFunc(void);
}
namespace TwoSpace
{
void SimpleFunc(void);
}
함수 소스코드
#include "1_4_header.h"
#include
void OneSpace::SimpleFunc(void)
{
std::cout << "so simple" << std::endl;
}
void OneSpace::PrettyFunc(void)
{
std::cout << "so pretty" << std::endl << "one more-"; SimpleFunc();
}
void TwoSpace::SimpleFunc(void)
{
std::cout << "Two Simple" << std::endl;
}
메인 소스코드
#include "1_4_header.h"
#include
void main()
{
OneSpace::SimpleFunc();
OneSpace::PrettyFunc();
TwoSpace::SimpleFunc();
system("pause");
}
OPP
은행계좌 관리프로그램 기능-계좌개설,입금,출금,전체고객 잔액조회
실수가 많았던 문제입니다.
strcmp함수의 성공 반환값을 1로 착각하기도 하고 루프를 개떡같이 짜놓기도 했고...
#include
#include
using namespace std;
struct Bank {
char ID[50];
char Name[50];
int Money;
};
void OutPut(Bank * account,int index)
{
cout << "ID : " << account[index].ID << endl <<
"소유주 : " << account[index].Name << endl <<
"현재 계좌 금액 :" << account[index].Money << "달러" << endl;
}
void NewAccount(Bank * account,int index)
{
cout << "당신의 이름을 입력하십시오 : ";
cin >> account[index].Name;
cout << "등록할 ID를 입력하십시오 : ";
cin >> account[index].ID;
account[index].Money = 0;
OutPut(account, index);
}
void Deposit(Bank *account,int index)
{
char text[50];
int inMoney;
bool complete = false;
int out=0;
for(;;)
{
cout << "돌아가기(1)"<<endl<<"입금할 계좌 ID : ";
if (out == 1)
break;
cin >> text;
for (int re = 0; re <= index; re++)
{
if (0==strcmp(text, account[re].ID))
{
OutPut(account, re);
cout << "입금 금액 : ";
cin >> inMoney;
account[re].Money+=inMoney;
cout << inMoney << "달러가 입금되었습니다." << endl << "총 금액 : " << account[re].Money << "달러" << endl;
complete = true;
break;
}
}
if (complete == true)
break;
else
cout << "해당 ID의 계좌가 없습니다. 다시 입력해주십시오." << endl;
}
}
void Withdraw(Bank *account,int index)
{
char text[50];
int getMoney;
bool complete = false;
int out = 0;
for (;;)
{
cout << "돌아가기(1)" << endl << "출금할 계좌 ID : ";
if (out == 1)
break;
cin >> text;
for (int re = 0; re <= index; re++)
{
if (0==strcmp(text, account[re].ID))
{
OutPut(account, re);
for(;;)
{
cout << "출금 금액 : ";
cin >> getMoney;
if (getMoney > account[re].Money)
{
cout << "출금 금액이 너무 큽니다.";
continue;
}
else
break;
}
account[re].Money -= getMoney;
cout << getMoney << "달러를 출금했습니다." << endl << "총 금액 : " << account[re].Money << "달러" << endl;
complete = true;
break;
}
}
if (complete)
break;
else
cout << "해당 ID의 계좌가 없습니다. 다시 입력해주십시오." << endl;
}
}
void AllOutput(Bank *account,int index)
{
for (int this_Index = 0;this_Index<=index;this_Index++)
{
cout << "------" << this_Index << "번째 인덱스------" << endl;
OutPut(account, this_Index);
cout << endl;
}
}
int main(void)
{
struct Bank account[50];
int choice=0;
int accountIndex = 0;
bool for_out = false;
for (;for_out!=true;)
{
cout <<endl<< "----Menu----" << endl << "1.계좌 개설" << endl << "2.입금" << endl << "3.출금" <<
endl << "4.전체고객 잔액조회" << endl << "5.프로그램 종료" << endl;
cout << "선택 : ";
cin >> choice;
switch (choice)
{
case 1: NewAccount(account,accountIndex); accountIndex++; break;
case 2: Deposit(account,accountIndex-1); break;
case 3: Withdraw(account, accountIndex-1); break;
case 4: AllOutput(account, accountIndex-1); break;
case 5: cout << "프로그램을 종료합니다." << endl; for_out = true; break;
}
}
system("pause");
return 0;
}