์ƒˆ์†Œ์‹

Languages/C++

[C++ ] ๊ฐ์ฒด ํฌ์ธํ„ฐ | ํฌ์ธํ„ฐ ๊ฐ์ฒด | ๊ฐ์ฒด ๋ฐฐ์—ด ์˜ˆ์ œ

  • -
๋ฐ˜์‘ํ˜•

http://tmmse.xyz/2018/09/06/c-temp/


์ด๋ฒˆ ์‹ค์Šต์€ ๋‹ค์Œ ์‹ค์Šต์„ ์œ„ํ•œ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ๋ช‡๊ฐ€์ง€ ์˜ˆ์ œ๋ฅผ ์‹ค์Šตํ•ด๋ณด๋ ค ํ•œ๋‹ค.

#include<iostream>
using namespace std;
/*
int main(){
	cout <<"์ž…๋ ฅํ•  ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜๋Š”?";
	int n;
	cin >> n; //์ •์ˆ˜์˜ ๊ฐœ์ˆ˜ ์ž…๋ ฅ
	if(n <= 0) return 0;
	int *p = new int[n]; // n๊ฐœ์˜ ์ •์ˆ˜๋ฐฐ์—ด ๋™์  ํ• ๋‹น
	if(!p){
		cout<<"๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.";
		return 0;	
	} 
	
	for(int i=0; i<n; i++){
		cout <<i+1 <<"๋ฒˆ์งธ ์ •์ˆ˜"; //ํ”„๋กฌํ”„ํŠธ ์ถœ๋ ฅ 
		cin >> p[i];
	}
	
	int sum = 0;
	for(int i = 0; i<n; i++)
		sum += p[i];
	cout<<"ํ‰๊ท  = "<<sum/n <<endl;
	
	delete [] p; //๋ฐฐ์—ด ๋ฉ”๋ชจ๋ฆฌ ๋ฐ˜ํ™˜ 
}
*/

class Circle{
	int radius;
	string name;
public:
	Circle();
	~Circle(){
	}
	void setRadius(string n ,int r){name = n; radius = r;}
	double getArea(){return 3.14*radius*radius;}
	double getArea2(){return 3.14*(radius+radius);}
	string getname(){return name;}
	
}; 

Circle::Circle(){
	radius = 1;
	name ="";
	//cout<<"์ƒ์„ฑ์ž ์‹คํ–‰ radius = "<<radius << endl;
}
/*
Circle::Circle(int r){
	radius = 1;
	cout<<"์ƒ์„ฑ์ž ์‹คํ–‰ radius = "<<radius << endl;
}*/
int main(){
	cout<<"์ƒ์„ฑํ•˜๊ณ ์ž ํ•˜๋Š” ์›์˜ ๊ฐœ์ˆ˜?";
	int n, radius;
	string name;
	cin >>n; // ์›์˜ ๊ฐœ์ˆ˜ ์ž…๋ ฅ

	Circle *pArray = new Circle [n]; // n๊ฐœ์˜ Cricle ๋ฐฐ์—ด ์ƒ์„ฑ
	for(int i =0; i<n;i++){
		cout <<"์›"<<i+1<<"์˜ ์ด๋ฆ„๊ณผ ๋ฐ˜์ง€๋ฆ„์€ ?"; // ํ”„๋กฌํ”„ํŠธ ์ถœ๋ ฅ 
		cin >> name;cin >> radius;  //๋ฐ˜์ง€๋ฆ„ ์ž…๋ ฅ
		pArray[i].setRadius(name,radius); //๊ฐ Circle ๊ฐ์ฒด๋ฅผ ๋ฐ˜์ง€๋ฆ„์œผ๋กœ ์ดˆ๊ธฐํ™”	 
	} 
	cout<<"\n";
	int count =0; //์นด์šดํŠธ ๋ณ€์ˆ˜
	Circle *p = pArray;
	for(int i=0; i<n; i++){
		cout << p->getname()<<"์˜ ๋ฉด์ ๊ณผ ๋‘˜๋ ˆ๋Š” ["<<p->getArea() << ", "; //์›์˜ ๋ฉด์  ์ถœ๋ ฅ
		cout << ""<<p->getArea2() << "] \n"; //์›์˜ ๋ฉด์  ์ถœ๋ ฅ
		if(p->getArea()>=100&& p ->getArea() <=200)
			count++;
		p++;	 
	} 
	cout <<endl <<"๋ฉด์ ์ด 100์—์„œ 200์‚ฌ์ด์ธ ์›์˜ ๊ฐœ์ˆ˜๋Š” " <<count <<endl;
	
	cout << "\n๊ฒ€์ƒ‰ํ•˜๊ณ ์ž ํ•˜๋Š” ์›์˜ ์ด๋ฆ„์€? ";
	
	cin >> name;
	Circle *p2 = pArray;
	for(int i=0; i<n; i++){
		if(p2->getname()==name){
			cout << name<<"์˜ ๋ฉด์ ๊ณผ ๋‘˜๋ ˆ๋Š” ["<<p2->getArea() << ", "; //์›์˜ ๋ฉด์  ์ถœ๋ ฅ
			cout << ""<<p2->getArea2() << "] \n"; //์›์˜ ๋ฉด์  ์ถœ๋ ฅ		
		} 
		p2++ ;
	} 
	delete [] pArray; // ๊ฐ์ฒด ๋ฐฐ์—ด ์†Œ๋ฉธ 
	 
}
 


#include<iostream>
#include<string>

using namespace std;
int t = 100;
int main()
{
	string a= "We are";
	a.append(" the");
	a.append(" world");
	cout << a <<", "<<a.length()<<endl;
	string name;
	cout<<"์ด๋ฆ„์€ ? ";
	getline(cin, name);//cin>>name;
	cout<<a.substr(7,3)<<endl;
	cout<<a.find("e",2)<<endl;
	return 0; 

}
#include<iostream>
using namespace std;
/*
int main(){
	cout <<"์ž…๋ ฅํ•  ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜๋Š”?";
	int n;
	cin >> n; //์ •์ˆ˜์˜ ๊ฐœ์ˆ˜ ์ž…๋ ฅ
	if(n <= 0) return 0;
	int *p = new int[n]; // n๊ฐœ์˜ ์ •์ˆ˜๋ฐฐ์—ด ๋™์  ํ• ๋‹น
	if(!p){
		cout<<"๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.";
		return 0;	
	} 
	
	for(int i=0; i<n; i++){
		cout <<i+1 <<"๋ฒˆ์งธ ์ •์ˆ˜"; //ํ”„๋กฌํ”„ํŠธ ์ถœ๋ ฅ 
		cin >> p[i];
	}
	
	int sum = 0;
	for(int i = 0; i<n; i++)
		sum += p[i];
	cout<<"ํ‰๊ท  = "<<sum/n <<endl;
	
	delete [] p; //๋ฐฐ์—ด ๋ฉ”๋ชจ๋ฆฌ ๋ฐ˜ํ™˜ 
}
*/

class Circle{
	int radius;
public:
	Circle();
	~Circle(){
	}
	void setRadius(int r){radius = r;}
	double getArea(){return 3.14*radius*radius;}
	double getArea2(){return 3.14*(radius+radius);}
	
}; 

Circle::Circle(){
	radius = 1;
	//cout<<"์ƒ์„ฑ์ž ์‹คํ–‰ radius = "<<radius << endl;
}
/*
Circle::Circle(int r){
	radius = 1;
	cout<<"์ƒ์„ฑ์ž ์‹คํ–‰ radius = "<<radius << endl;
}*/
int main(){
	cout<<"์ƒ์„ฑํ•˜๊ณ ์ž ํ•˜๋Š” ์›์˜ ๊ฐœ์ˆ˜?";
	int n, radius;
	cin >>n; // ์›์˜ ๊ฐœ์ˆ˜ ์ž…๋ ฅ

	Circle *pArray = new Circle [n]; // n๊ฐœ์˜ Cricle ๋ฐฐ์—ด ์ƒ์„ฑ
	for(int i =0; i<n;i++){
		cout <<"์›"<<i+1<<":"; // ํ”„๋กฌํ”„ํŠธ ์ถœ๋ ฅ 
		cin >> radius; //๋ฐ˜์ง€๋ฆ„ ์ž…๋ ฅ
		pArray[i].setRadius(radius); //๊ฐ Circle ๊ฐ์ฒด๋ฅผ ๋ฐ˜์ง€๋ฆ„์œผ๋กœ ์ดˆ๊ธฐํ™”	 
	} 
	
	int count =0; //์นด์šดํŠธ ๋ณ€์ˆ˜
	Circle *p = pArray;
	for(int i=0; i<n; i++){
		cout << "["<<p->getArea() << ", "; //์›์˜ ๋ฉด์  ์ถœ๋ ฅ
		cout << ""<<p->getArea2() << "] "; //์›์˜ ๋ฉด์  ์ถœ๋ ฅ
		if(p->getArea()>=100&& p ->getArea() <=200)
			count++;
		p++;	 
	} 
	cout <<endl <<"๋ฉด์ ์ด 100์—์„œ 200์‚ฌ์ด์ธ ์›์˜ ๊ฐœ์ˆ˜๋Š” " <<count <<endl;
	delete [] pArray; // ๊ฐ์ฒด ๋ฐฐ์—ด ์†Œ๋ฉธ 
	 
}


#include <iostream>
#include <string>

using namespace std;
class Test{
private:
	int a; double b;
public:
	Test(){a = 1; b=1.2;}
	Test(int a){this->a = a; b= 7.8;}
	//Test(int c,double d){a = c; b=d;}
	Test(int a,double b){
	this->a=a; this->b=b;
	}
	
	
	~Test(){}
	void show();
};

void Test ::show()
{
	cout<<a<<","<<b<<endl;
}


int main()
{
	Test t;  
	t.show();
	Test *tp = &t;  
	tp ->show();
	
	Test s [] ={Test(3,4.5), Test(5), Test()};
	s[0].show(); //3 4.5
	
	s[1].show(); //5, 7.8
	s[2].show();// 1,1.2
	
	cout<<"Test ํฌ์ธํ„ฐ๋กœ ์ฐ๊ธฐ"<<endl;
	Test *sp = s;
	for(int i =0; i<3.; i ++){
		sp -> show();
		sp ++;
	
	}
	
	cout<<"----------------------"<<endl;
	for(int i=0; i<3; i++)
	{
		s[i].show();
	}

	
	int a = 10;
	int *p = &a;
	cout<<p<<endl<<*p<<endl;
	int b[] = {2,7,4,5,6,5,4};
	int *bp = b;
	cout << *bp <<endl;
	cout<<*bp<<","<<*(bp+2)<<","<<bp[3]<<endl; 
	return 0;
}

๋ฉ”๋ชจ๋ฆฌ
1. ์ฝ”๋“œ์˜์—ญ:์ฝ”๋“œ
2. ๋ฐ์ดํ„ฐ ์˜์—ญ : ์ „์—ญ๋ณ€์ˆ˜ , static๋ณ€์ˆ˜(ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘์‹œ ํ• ๋‹น, ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ์‹œ ํ•ด์ œ)
3. ํž™ (heap) ์˜์—ญ : ๋™์ ํ• ๋‹น์˜์—ญ(new ๋กœ ํ• ๋‹น, delete๋กœ ํ•ด์ œ)
4. ์Šคํƒ (stack)์˜์—ญ : ๋งค๊ฐœ๋ณ€์ˆ˜, ์ง€์—ญ๋ณ€์ˆ˜(ํ•ด๋‹นํ•จ์ˆ˜ ์‹คํŒจ์‹œ ํ• ๋‹น, ํ•จ์ˆ˜ ์ข…๋ฃŒ์‹œ ํ•ด์ œ)

* ๋ณ€์ˆ˜, ํด๋ž˜์Šค ๋ฐฐ์—ด ๋“ฑ์€  stack์˜์—ญ(์ •์ ํ• ๋‹น, ์ปดํŒŒ์ผ์‹œ์— ํ• ๋‹น)
*๋™์ ํ• ๋‹น์€ ๋Ÿฐ (์‹คํ–‰)์‹œ์— ํ• ๋‹นํ•˜๊ณ  ์ง์ ‘ ์ ‘๊ทผ ๋ถˆ๊ฐ€.
๊ทธ๋Ÿฌ๋ฏ€๋กœ ์Šคํƒ์˜์—ญ์— ํฌ์ธํ„ฐ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋†“๊ณ  ์ ‘๊ทผ. 

๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.