C++

C++ 프로그래밍 1주차 정리

mJay8194 2022. 9. 19. 05:48

1. 온라인 컴파일러 사용해보기(https://www.onlinegdb.com/online_c++_compiler)

#include <iostream> //표준 헤더 파일 include

using namespace std;

int main()

{

cout<<"Hello World";

return 0;

}

 

2. C C++ 소스

#include <iostream>

#include <stdio.h>

using namespace std;

int main()

{

cout<<"Hello World\n"; //C++

printf("hello world2"); //C

return 0;

}

 

3. C++, Java, C# 비교

#include <iostream>

using namespace std;

int main()

{

cout<<"Hello World";

return 0;

}//C++

 

public class Main

{

             public static void main(String[] args) {

                           System.out.println("Hello World");

             }

}//Java

 

#include <iostream>

using namespace std;

int main()

{

cout<<"Hello World";

return 0;

}//C#

 

4. 오름차순으로 4명 정렬하기

4.1. 소스

#include <stdio.h>// 전처리기, 5

#define SIZE 4 // 전처리기, 5

typedef struct { // 구조체, 12

             char name[10]; .//문자형 배열 생성

             double w; //실수형 변수 생성

}WEIGHT;

void swap(WEIGHT*, WEIGHT*); // 함수 선언, 8

int main(void)

{

             WEIGHT man[SIZE] = { {"한개발",57.5}, // 배열, 10

             {"엄청군",125.6},

             {"갈비양",35.7},

             {"신명재",130.7}

             };

             int i, j; // 변수, 3, 기억 클래스,9

             for (i =0; i <3; i++) { // 제어문, 7

                           for (j = i +1; j <4; j++) { // 연산자, 6

                                        if (man[i].w > man[j].w) {

                                                     swap(&man[i], &man[j]);

                                        } // call by reference, 11

                           }

             }

             printf(" 이름 \t체중\n");// 표준 라이브러리 함수, 4

             for (i =0; i <4; i++) {

                           printf(" %s %5.1f\n", man[i].name, man[i].w);

             }

             return 0;

} //main()함수 끝

void swap(WEIGHT* mani, WEIGHT* manj)// 포인터, 11

{ //함수 정의

             WEIGHT temp;

             temp =*mani;

             *mani =*manj;

             *manj = temp;

}

 

 4.2. 결과

 

5. CC++ 비교

5.1. C

#include <stdio.h>

int main()

{

               printf("hello world");

               return 0;

}

 

5.2. C++

#include <iostream>

using namespace std;

int main()

{

cout <<"Hello World"<<endl;

return 0;

}

 

#include <iostream>

int main()

{

cout<<"Hello World";

return 0;

}

 

출처 : https://www.youtube.com/channel/UCM8wseo6DkA-D7yGlCrcrwA, C++ 프로그래밍 강의 - 한성현 교수님