본문 바로가기

언어/C++

5단원 실습문제 (참조, 복사생성자 )

//6번
#include <iostream>
#include <cstring>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

class MyIntStack {
	int *p;
	int size;
	int tos;
public : 
	MyIntStack();
	MyIntStack(int size){ tos=-1; p = new int[size]; this->size = size; }
	MyIntStack(MyIntStack& s);
	~MyIntStack(){ delete [] p; }
	bool push(int n);
	bool pop(int &n);
};

bool MyIntStack::push(int n) {
	tos++;
	if( tos == 10 ) 
		return false;
	else {
		p[tos] = n;
		return true;
	}
}
bool MyIntStack::pop(int &n) {
	if( tos == -1 )
		return false;
	else {
		n = p[tos];
		return true;
	}
	tos--;
}
MyIntStack::MyIntStack(MyIntStack& s){ 
	this->p = new int[s.size];
	this->size = s.size;
	this->tos = s.tos;
	for(int i=0; i<=s.tos; i++)
		this->p[i] = s.p[i];
}

int main(int argc, char** argv) {
		
MyIntStack a(10);
	a.push(10);
	a.push(20);
	MyIntStack b = a;
	b.push(30);

	int n;
	a.pop(n);
	cout << "스택 a에서 팝한 값 " << n << endl;
	b.pop(n);
	cout << "스택 b에서 팝한 값 " << n << endl;
	return 0;
}
//5번


#include <iostream>
#include <cstring>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

class MyIntStack {
	int p[10];
	int tos;
public : 
	MyIntStack(){ tos = 0; }
	bool push(int n);
	bool pop(int &n);
};

bool MyIntStack::push(int n) {
	if(tos==10)
		return false;
	else{
		p[tos]=n;
		tos++;
		return true;
	}
}

bool MyIntStack::pop(int &n) {
	tos--;
	if(tos==-1){
		return false;
	}
	else{
		n=p[tos];
		return true;
	}
}

int main(int argc, char** argv) {
		
	MyIntStack a;
	for(int i=0; i<11; i++){
		if(a.push(i)) cout<< i << ' ';
		else  cout << endl << i+1 << "번째 stack full" << endl;
	}	
	int n;
	for(int i=0; i<11; i++){
		if(a.pop(n)) cout << n << ' ';
		else cout << endl << i+1 << "번째 stack empty ";
	}
		cout<<endl;
	return 0;
}
//4번 - 질문

#include <iostream>
#include <cstring>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

char& find(char a[], char c, bool& success);

char& find(char a[], char c, bool& success) {

	for(int i=0; i< sizeof(a); i++)
		if( a[i] == c ) { 
			success = true;
			return a[i]; 
		}
		return *a;
}

int main(int argc, char** argv) {
		
	char s[] = "Mike";
	bool b = false;
	char& loc = find(s, 'M', b);
	if( b == false) {
		cout << "M을 발견할 수 없다" << endl;
		return 0;
	}
	loc = 'm'; // 'M 위치에 m을 기록  
	cout << s << endl; // mike가 출려고딤  

	return 0;
}
//3번 

#include <iostream>
#include <cstring>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

class Circle{

	int radius;
	public:
		Circle(int r){radius=r;}
		int getRadius(){
			return radius;
		}
		void setRadius(int r){
			radius=r;
		}
		void show(){
			cout << "반지름이 "<< radius << "인 원 "<<endl;
		}
};

void increaseBy(Circle &a, Circle b) {
	int r = a.getRadius() + b.getRadius();
	a.setRadius(r);
}

int main(int argc, char** argv) {
		
	Circle x(10), y(5);
	increaseBy(x, y);
	x.show();
	
	return 0;
}
// 2번

#include <iostream>
#include <cstring>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

bool bigger(int a, int b, int& big);

bool bigger(int a, int b, int& big) {
	if( a == b )
		return true;
	else {
		if( a>=b) big = a;
		else big = b;
		return false;
	}
}

int main(int argc, char** argv) {
		
	int n=5, nn=9, big;

	if( bigger(n, nn, big) )
		cout << "두 수는 같습니다." << endl;
	else
		cout << "더 큰 수는 " << big << "입니다" << endl;
		
		
	return 0;
}
// 1번 

#include <iostream>
#include <cstring>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

class Circle;
void swap(Circle &c,Circle &cc);

class Circle{
	int r;
public:
	Circle(int r=1) {
		this->r=r;
	}
	void setRadius(int r){
		this->r=r;
	}
	int getRadius(){
		return r;
	}
};

void swap(Circle &c, Circle &cc){
	
	int n;
	n=c.getRadius();
	c.setRadius(cc.getRadius());
	cc.setRadius(n);
	
}





int main(int argc, char** argv) {
		
	Circle a;
	Circle b(10);

	cout << "a 반지름 : " << a.getRadius() << endl;
	cout << "b 반지름 : " << b.getRadius() << endl;
	cout << "swap 후" << endl;
	swap(a, b);
	cout << "a 반지름 : " << a.getRadius() << endl;
	cout << "b 반지름 : " << b.getRadius() << endl;
	 
	return 0;
}

'언어 > C++' 카테고리의 다른 글

겜블링 게임 만들기 !  (0) 2019.11.20
4단원 예제문제  (1) 2019.11.14
복사생성자  (0) 2019.11.12
함수와 참조  (0) 2019.11.12
히스토그램  (0) 2019.11.11