C++ 4단원 실습과제 1
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1;
s1 = "hello";
cout<< s1 <<"의 문자열의 길이는 "<< s1.size()<<"입니다.\n";
string s2 = "good-bye";
s1 = s2;
if (s1 == s2)
cout<< "s1과 s2는 같습니다.\n";
else
cout<< "s1과 s2는 다릅니다.\n";
return 0;
}
C++ 4단원 실습과제 2
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector v1;
for(int i = 0; i < 10 ; i++)
v1.push_back(i);
cout<< "원소의 개수 : "<<v1.size()<<"\n";
cout<< "원소 : ";
for(i = 0; i < v1.size(); i++)
cout<< v1[i] <<" ";
cout<< "\n";
vector v2;
char* colors[7] = {"red","green","blue","yellow","white","black","pink"};
for(i = 0; i < 7; i++)
v2.push_back(colors[i]);
cout<< "원소의 개수 : " <<v2.size() <<"\n";
cout<< "원소(역순) : ";
for(i = v2.size()-1; i >= 0; i--)
cout<< v2[i] <<" ";
cout<< "\n";
return 0;
}
C++ 4단원 실습과제 3
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct CUSTOMER
{
string name;
int age;
char sex;
};
int main()
{
vector vec;
char menu;
int i;
while(true)
{
cout << "1. 고객 정보 입력\n";
cout << "2. 고객 정보 출력\n";
cout << "3. 종료\n";
cout << "메뉴 선택 : ";
cin >> menu;
if( menu == '3')
{
break;
}
switch(menu)
{
case '1' :
{
CUSTOMER cust;
cout << "고객 이름 : ";
cin >> cust.name;
cout << "나이 : ";
cin >> cust.age;
cout << "성별(M, F) : ";
cin >> cust.sex;
vec.push_back(cust);
}
break;
case '2' :
cout<< "---------------------------------\n";
cout<< "이름 나이 성별\n";
cout<< "---------------------------------\n";
for(i = 0; i < vec.size(); i++)
{
CUSTOMER &cust = vec[i];
cout.width(20);
cout.setf(ios_base::left);
cout << cust.name;
cout.width(5);
cout << cust.age;
cout << cust.sex << "\n";
}
cout << "---------------------------------\n\n";
break;
default:
cout << "잘못 입력하셨습니다.\n";
continue;
}
}
return 0;
}
C++ 5단원 스택 메인함수 채우기 (Stack Class)
#include <iostream>
using namespace std;
class stack{
int m_size;
int m_top;
int *m_buffer;
public:
void Initialize(int size=10);
void RemoveAll();
bool push(int value);
bool pop(int& value);
};
void stack::Initialize(int size){
m_buffer = new int[size];
m_top = -1;
m_size = size;
}
void stack::RemoveAll(){
m_top = -1;
delete[] m_buffer;
}
bool stack::push(int value){
if(m_top >= m_size-1) return false;
m_buffer[++m_top] = value;
return true;
}
bool stack::pop(int& value){
if(m_top < 0 ) return false;
value = m_buffer[m_top--];
return true;
}
void main(){
stack s1, s2;
int s1p, s2p;
s1.Initialize(), s2.Initialize();
s1.push(1), s2.push(10);
s1.push(2), s2.push(20);
s1.push(3), s2.push(30);
s1.push(4), s2.push(40);
s1.push(5), s2.push(50);
s1.pop(s1p), s2.pop(s2p);
cout << s1p << " " << s2p << endl;
s1.pop(s1p), s2.pop(s2p);
cout << s1p << " " << s2p << endl;
s1.pop(s1p), s2.pop(s2p);
cout << s1p << " " << s2p << endl;
s1.push(6), s2.push(60);
s1.push(7), s2.push(70);
s1.pop(s1p), s2.pop(s2p);
cout << s1p << " " << s2p << endl;
s1.RemoveAll(), s2.RemoveAll();
}
C++ 5단원 protect 멤버에 접근하는 함수 만들기 (getter, setter)
#include <iostream>
using namespace std;
class Point
{
protected :
int m_x,m_y;
public:
void Printf ( );
bool IsEqual ( const Point& p );
bool SetX ( int x ) ;
bool SetY ( int y ) ;
int GetX ( ) ;
int GetY ( ) ;
};
void Point::Printf( )
{
cout<< "(" << m_x << ", " << m_y << ")" ;
}
bool Point::IsEqual ( const Point& p )
{
return (m_x==p.m_x && m_y==p.m_y);
}
int Point::GetX() // GetX를 구현하시오
{
return m_x ;
}
int Point::GetY() // GetY를 구현하시오
{
return m_y ;
}
bool Point::SetX(int x) // SetX를 구현하시오
{
if ( x > 1000 )
{
m_x = 1000 ;
return false ;
}
else if ( x < 0 )
{
m_x = 0 ;
return false ;
}
else
m_x = x ;
return true ;
}
bool Point::SetY(int y) // SetX를 구현하시오
{
if ( y > 1000 )
{
m_y = 1000 ;
return false ;
}
else if ( y < 0 )
{
m_y = 0 ;
return false ;
}
else
m_y = y ;
return true ;
}
int main()
{
Point p1;
if( ! p1.SetX(10) )
cout<<"X 좌표 변경 실패\n";
if( ! p1.SetY(10) )
cout<<"Y 좌표 변경 실패\n";
cout<<"p1=";
p1.Printf();
cout<<"\n";
if( ! p1.SetX(1500) )
cout<<"X 좌표 변경 실패\n";
if( ! p1.SetY(-100) )
cout<<"Y 좌표 변경 실패\n";
cout<<"p1=";
p1.Printf();
cout<<"\n";
return 0;
}
'학습공간 > C++, MFC' 카테고리의 다른 글
[MFC] Modeless Dialog (대화상자), 계산기 프로그램 (0) | 2019.11.27 |
---|---|
[3주차] 생성자와 소멸자, 멤버 함수의 활용, 클래스의 활용 (this) (0) | 2019.11.27 |
[1주차] C++ 시작하기, C 보다 나은 C++ Ⅰ/Ⅱ(new, delete) (0) | 2019.11.27 |