[ 2주차 - 1 ]
#include <stdio.h>
//c++은 이 두 줄을 반드시 써줘야함
#include <iostream> //iostream은 c++에서 입출력 관련 함수가 들어있는 파일
using namespace std; //키워드 -> 표준 namespace std를 사용하겠다.
int main() { // entry point
cout << "Hello world"; //c++ 출력함수 : cout<<
return 0;
}
* namespace : 여러 가지 프로그램 원소들이 저장될 수 있는 선언적 공간을 생성
-> std에는 c++를 실행하는데 필요한 유용한 정보를 가지고 있다.
* #include : 프로그램에 필요하거나 유용한 정보를 포함
* return 0 : 운영체제한테 정상적으로 종료됐음을 알림
* block : 하나의 단위로 취급하는 프로그램 문장들의 집합
#include <iostream>
using namespace std;
int main() {
int gallons, liters;
cout << "Enter number of gallons : ";
cin >> gallons; //cin을 사용해서 입력을 받는다.
liters = gallons * 4;
cout << "Liters : " << liters;
return 0;
}
cout << → 출력 * 출력하고자 하는 값이 더 있을 경우 <<를 사이사이에 써주면됨 ( 자바의 + 같은 기능 )
cin >> → 입력
프로토타입
#include <iostream>
using namespace std;
void myfunc(); //myfunc의 프로토타입
int main() {
cout << "In main()\n";
myfunc(); // myfunc()를 호출
cout << "Back in main()\n";
return 0;
}
void myfunc() {
cout << "Inside myfunc()\n";
}
함수와 정보를 미리 선언해줘야 프로그램이 위에서 부터 코드를 컴파일할 때 에러가 나지 않는다.
미리 선언해주지 않으면 아래에 선언돼있는 함수를 컴파일러는 알 수 없다.
+ 프로토타입 대신 함수를 main() 위에서 선언해줘도 된다. 하지만 main() 함수를 먼저 선언하는게 좋은 코드이다.
* void로 선언된 함수는 값을 반환하지 않으므로 return문이 없다.
#include <iostream>
using namespace std;
int mul(int x, int y); //mul의 프로토타입
int main() {
int answer;
answer = mul(10, 11);
cout << "The answer is " << answer;
return 0;
}
int mul(int x, int y) {
return x * y;
}
조건문
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter First number : ";
cin >> a;
cout << "Enter Second number : ";
cin >> b;
if (a < b) {
cout << "First number is less than second";
}
else {
cout << "Frist number is more than second";
}
return 0;
}
반복문
#include <iostream>
using namespace std;
int main() {
int count;
for (int count = 1; count <= 10; count++) {
cout << count << " ";
}
return 0;
}
소수 출력하기
#include <iostream>
using namespace std;
int main() {
int num;
int count = 0;
cout << "Enter one number : ";
cin >> num;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
count++;
}
}
if (count == 2) {
cout << "소수입니다.";
}
else {
cout << "소수가 아닙니다.";
}
return 0;
}
[ 2주자-2 : 자료형 ]
변수의 선언
int i; // 4 byte
char ch; // 1 byte
float f; // 4 byte
double d; // 8 byte
지역변수 : 함수내에서 선언되는 변수 → 함수 호출들 사이에서 자신의 값을 유지하지 않음
매개변수 : 매개변수의 선언은 함수 이름 다음에 오며 괄호안에 있다.
int func1( int first, int last, char ch ){
...
}
전역변수 : 프로그램이 끝날 때까지 값을 유지. 전역변수는 메모리를 공유하기 때문에 모든 함수의 외부에서 선언
어떤 함수에서도 접근이 가능하다.
#include <iostream>
using namespace std;
void func1();
void func2();
int count; //전역변수
int main() {
int i; //지역변수
for (i = 0; i < 10; i++) {
count = i * 2;
func1();
}
return 0;
}
void func1() {
cout << "전역변수 count : " << count;
cout << "\n";
func2();
}
void func2() {
int count; // 지역변수
for (count = 0; count < 3; count++) {
cout << ".";
}
}
형 수정자
기본 자료형의 의미를 수정하여 그 자료형이 다양한 상황의 요구에 좀더 정확히 맞도록 하기 위해 사용
char, double, int 자료형 앞에 수정자(modifier)를 붙일 수 있음
signed // 부호가 있는 정수
unsigned // 부호가 없는 정수
long
short // ex) short int : 2 byte
리터럴
리터럴 : 프로그램에 의해 변경될 수 없는 고정된 값 → '상수'라고함
문자열 리터럴 : 큰 따옴표 안에 쓴 문자들의 집합 * 문자열 자료형 : char[]
ex) "a" 는 뒤에 널문자를 포함해서 총 2byte임
'a'는 문자형이다.
< 사용자의 명시적인 형선언 >
c++는 정수를 int, 소수를 double을 기본적으로 사용하는데 floar 형을 쓰고 싶은경우 접미사로 F를 붙여줘야한다.
연산자 ( operator )
#include <iostream>
using namespace std;
bool x_or(bool a, bool b);
int main() {
bool p, q;
cout << "Enter 0 or 1 : ";
cin >> p;
cout << "Enter 0 or 1 : ";
cin >> q;
cout << "P AND Q : " << (p&&q) << "\n";
cout << "P OR Q : " << (p||q) << "\n";
cout << "P XOR Q : " << x_or(p,q) << "\n";
return 0;
}
bool x_or(bool a, bool b) {
return (a || b) && !(a && b);
// 두 비트가 다를 때 1, 같으면 0
}
형변환
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 20; ++i) {
cout << i << "/2 is : " << (float)i / 2 << "\n";
}
return 0;
}
형변환할 변수 앞에 (자료형)을 써줘야한다.
1~10까지 tab 간격만큼 띄우면서 출력하기
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= i; j++) {
cout << j << "\t";
}
cout << "\n";
}
return 0;
}
정수 n의 모든 약수와 약수들의 갯수 구하기
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number : ";
cin >> n;
int count = 0;
cout << "약수 : ";
for (int i = 1; i <= n; i++) {
if ((n % i) == 0) {
count++;
cout << i << "\t";
}
}
cout << "\n약수의 개수 : " << count;
return 0;
}
'2CHAECHAE 학교생활 > 객체지향프로그래밍(C++)' 카테고리의 다른 글
[ 객체지향프로그래밍(C++) 5주차 ① ] (0) | 2022.04.01 |
---|---|
[ 객체지향프로그래밍(C++) 4주차 ② ] (0) | 2022.03.28 |
[ 객체지향프로그래밍(C++) 4주차 ① ] (0) | 2022.03.24 |
[ 객체지향프로그래밍(C++) 3주차 ② ] (0) | 2022.03.21 |
[ 객체지향프로그래밍(C++) 3주차 ① ] (0) | 2022.03.21 |