์ƒˆ์†Œ์‹

Languages/C++

[C++] C++๊ธฐ๋ณธ ๋ฌธ๋ฒ• ์ตํžˆ๊ธฐ

  • -
๋ฐ˜์‘ํ˜•

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


 

C++์€ C์–ธ์–ด์™€ ๋งค์šฐ ๋ฌธ๋ฒ•์ด ๋น„์Šทํ•˜๋‹ค.

 

- printf = cout

- scanf = cin 

 

#include <iostream>

//using std::end1; // ์ด๋ ‡๊ฒŒ ํ‘œํ˜„ํ•ด์ฃผ๋ฉด std::end1 ๋ง๊ณ  end1;๋กœ๋งŒ ์จ์ฃผ๋ฉด ๋œ๋‹ค.
using namespace std; //namespace = ์ด๋ฆ„๊ณต๊ฐ„(์ž๋ฐ” ํŒจํ‚ค์ง€๋ž‘ ๋น„์Šทํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋จ) -> ์œ„ ๋‚ด์šฉ๊นŒ์ง€ ํฌํ•จํ•ด์คŒ

namespace kim{
    int a;void show(){}
}
namespace park{
    int b;
}
using namespace kim; //์œ„ ๋‚ด์šฉ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
//๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด ๋†“์€ ๊ฒƒ์„ ์‚ฌ์šฉํ•  ๋•Œ ์“ธ ์ˆ˜๋„ ์žˆ๊ณ , ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•ด์„œ ์จ๋„๋จ

int main()//os์—์„œ ์ฒ˜์Œ ์ฝํ˜€์ง€๋Š” ํ•จ์ˆ˜
{
	
    //์•„๋ž˜๋Š” ์ œ์ผ ๊ธฐ๋ณธ ํ‘œํ˜„
    //std::cout<<"hello"<<std::end1; //์—ญ์Šฌ๋ž˜์‹œ + n ๊ณผ ๊ฐ™๋‹ค : ์ค„๋ฐ”๊ฟˆ
    //str::cout<<"wolrd"<<std::end1;
    //์ด๋ฆ„๊ณต๊ฐ„์— ๊ธฐ์žฌํ•ด์ค€ ์ดํ›„ 
    cout<<"hello"<<end1;
    cout<<"wolrd"<<end1; 
    kim::a=100;
    park::b=300; //์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฌ์šฉํ•ด๋„ ๋จ.
    
    return 0;
}
#include <iostream>
using namespace std;

int main(){
	cout<<"please input num"
	
	int width;
	cin>>width;
	
	cout<<"please input height"
	
	int height;
	cin>>height;
	
	int area = width*height;
	cout<<"Your area "<<area<<"\n";
}

 

์œ„์ฝ”๋“œ๋ฅผ ํ•จ์ˆ˜๋กœ ๋งŒ๋“ ๊ฒƒ

#include<iostream>
//using std::endl;
using namespace std;//cout, endl, cin ๋“ฑ..
int x = 10000;//์ „์—ญ๋ณ€์ˆ˜
int getArea(int width, int height)//์ „์—ญ ํ•จ์ˆ˜
{
	return width*height;
}
void line(); //ํ•จ์ˆ˜์˜ ์›ํ˜•์ด๋ผ๊ณ  ํ•จ -> main๋ณด๋‹ค ์œ„์— ์žˆ์–ด์•ผ ์‹คํ–‰ ๊ฐ€๋Šฅ.
int main()
{
	int width, height;
    line();
    cout<<"๋„ˆ๋น„์™€ ๋†’์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.";
    cin>>width>>height;
    cout<<"์‚ฌ๊ฐํ˜•์˜ ๋ฉด์ ์€?"<<getArea(width,height)<<endl;//returnํ˜• ํ•จ์ˆ˜ ํ˜ธ์ถœ๋ฒ•.
    line(); //void ํ•จ์ˆ˜ ํ˜ธ์ถœ๋ฒ•
}

void line()
{
    for(int i=0;i<50;i++)
    {
    	cout<<"-";
    }
    cout<<endl;
}
    

๊ฒฐ๊ณผ

 

#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;//cout, endl, cin ๋“ฑ..


int main()
{
	
    srand(time(NULL));
    cout<<"์ž„์˜์˜ ์ˆ˜ : " <<rand()%100+1<<endl;//1~100์‚ฌ์ด์˜ ์ž„์˜์˜ ์ˆ˜
    int x[10];
    cout <<"๋ฐฐ์—ด์˜ ํฌํ‚ค :" <<sizeof(x)/sizeof(x[0])<<endl;
    
}

 

๊ฒฐ๊ณผ

 

#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;//cout, endl, cin ๋“ฑ..

void getSort(int arr[], int k);
int main()
{
	srand(time(NULL));
    int x[10];//๋ฐฐ์—ด์˜ ์ž„์˜์˜ ์ˆ˜๋ฅผ ๋„ฃ๊ณ  ์ถœ๋ ฅํ›„ ๊ทธ ํ•ฉ, ํ‰๊ท , ์ตœ๋Œ€ ๊ฐ’ ๊ตฌํ•˜๊ธฐ.
    int z = sizeof(x)/sizeof(x[0]);
    cout <<"๋ฐฐ์—ด์˜ ํฌํ‚ค :" <<z<<endl;
    int sum=0;
	for(int i=0;i<10;i++){
		x[i]=rand()%100+1;
		cout<<x[i]<<" "<<endl;
		sum += x[i];

	}
	cout<<"add : "<<sum<<", age : "<<sum/z;
	int max=x[0];
	for(int i =0;i<10;i++)
	{
		if(x[i]>max){
			max=x[i];
			//max++
		}
		cout<<", max : "<<max<<endl;
		getSort(x,z);
	}
}
void getSort(int arr[], int k)
{
	int x,y,temp;
	for(x=0;x<k-1;x++)
	{
		for(y=x+1; y<k; y++)
		{
			if(arr[x]>arr[y])
			{
				temp=arr[x];
				arr[x]=arr[y];
				arr[y]=temp;
			}
		}
	}
	for(x=0;x<10;x++){
		cout<<arr[x]<<" ";
	}
	cout<<endl;
}

 

๋ฐ˜์‘ํ˜•
Contents

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

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