Chap 1 Example Program include iostream system header

  • Slides: 37
Download presentation
Chap. 1: Example Program #include <iostream> // system header file using namespace std; //

Chap. 1: Example Program #include <iostream> // system header file using namespace std; // 변수의 name space int main ( ) { cout << “Hello, World!n”; // 출력 return 0; } Note: cout << 3 + 4;

Errors n n Compile errors (Syntax errors) n C++ source program 을 기계어로 번역하는

Errors n n Compile errors (Syntax errors) n C++ source program 을 기계어로 번역하는 과정에서 발생하는 error들 n cot << “Hello, Worldn”; n cout << “Hello, World”; Logic errors, Run-time errors n 기계어 program 수행 중 발생 n A = B / 0; n 각 종류의 debugger 사용 가능

실행 프로그램의 생성 C++ source program 작성 : file n Compiler 실행 : g++,

실행 프로그램의 생성 C++ source program 작성 : file n Compiler 실행 : g++, visual studio builder n n n *. obj, *. o 등 OS의 규정에 따른 이름의 file 생성됨 n Object code(file)라 함 Compiler 와 연동되어 Linker 수행 (자동) n 여러 개의 object file을 하나의 executable file 로 n a. out, a. exe 등, 기타 원하는 이름

Chap 2. Elementary Data Types n n Integer n integer a, b, c; n

Chap 2. Elementary Data Types n n Integer n integer a, b, c; n a = 345; floating point number (real) n double x, y; n float z; n x = 3. 5; n y = 3. 0 * 5. 5 E-03 / 2. 5;

Example #include <iostream> int main( ) { int pennies = 8; // initialization int

Example #include <iostream> int main( ) { int pennies = 8; // initialization int dimes = 4; int quarters = 3; double total = pennies *. 01 + dimes *. 1 + quarters *. 25; // in dollars count << “Total = “ << total << “ in dollars n”; return 0; }

C++ components n Reserved words : predefined in C++ compiler n n Variables :

C++ components n Reserved words : predefined in C++ compiler n n Variables : Symbolic address (symbolic location name) n n int, main, include… int a, b, tax; Constants in source program n a = 3; x = 3. 0; int a = 3; // compile time init n int a; a = 3; // run-time assignment n

C++ components n Function (main) n n n C++ Statement n Declaration : int

C++ components n Function (main) n n n C++ Statement n Declaration : int a; int b = 3; n Assignment : a = 3; a = b; n IO statement : cout << “a = “ << a << endl; n Expression : a = 3 * 5 / b; n … C++ Preprocessor n n int main() { … return 0; } #include <iostream> String : “Total value is = “

Scale & Precession int (= long) : - 2 31 ~ 232 -1 n

Scale & Precession int (= long) : - 2 31 ~ 232 -1 n float : 소숫점 이하 7자리 정도 n double : 15자리 정도 n roundoff error, truncation error n double original_price = 3 E 14; double discount_price = original_price – 0. 05; double discount = original_price – discount_price; n discount? (0. 05 is right, output is around 0. 0625)

Number System n an. an-1. an-2. . . a 1. a 0. a-1. a-2

Number System n an. an-1. an-2. . . a 1. a 0. a-1. a-2 …. a-m in radix r = anrn+an-1 rn-1+…+ a 1 r 1+a 0 r 0+a-1 r-1+a-2 r-2+…+a-mr-m , where 0 <= an < r n ex) n 11. 012 = 21 + 20 + 2 -2 = 2 + 1 + 0. 25 = 3. 2510 n 22. 13 = 3 + 1/3 = 4. 33333… 10

정수(Integer)의 표현 일정 크기의 기억 공간 (1 -4 byte)에 0과 음수 및 양의 정수

정수(Integer)의 표현 일정 크기의 기억 공간 (1 -4 byte)에 0과 음수 및 양의 정수 를 이진수로 표현 n Fixed point representation (고정 소수점 표현 방식) n n Signed magnitude 표현방식 (1 byte integer : -127 ~ +127) Signed 1’s complement 표현방식 ( 1 byte integer : -127 ~ +127) Signed 2’s complement 표현방식 ( 1 byte integer : -128 ~ +127, n bit integer : -2 n ~ +2 n-1) : 1’s compement +1, addition, substraction 오차 없는 수의 표현 및 계산 n 수의 절대 크기의 제한 n 2의 보수 표현에서의 덧셈 및 뺄셈 (overflow, carry) n

정수(Integer)의 표현 n Signed Magnitude n (8 bit example : 28 (256)개의 수 표현

정수(Integer)의 표현 n Signed Magnitude n (8 bit example : 28 (256)개의 수 표현 가능) n +3 : 00000011 , n MSB = sign bit, 0 : positive, 1 : negative n 2 개의 zero : 0000, 10000000 n 8 bit : -127 (1111) ~ +127 (01111111) n 255개의 수 표현 -3 : 10000011

정수(Integer)의 표현 n 1’s complement (보수) n 3 : 00000011 , -3 : 11111100

정수(Integer)의 표현 n 1’s complement (보수) n 3 : 00000011 , -3 : 11111100 n MSB = sign bit, 0 : positive, 1 : negative n 2개의 zero : 0000, 1111 n 8 bit : -127 (1111) ~ +127 (01111111) n 255개의 수 표현 n 3 + 2 = 00000011 + 00000010 = 00000101 n 3 – 2 = 3 + (-2) = n 00000011 + 11111101 = 0000 + (1) = 00000001 n 빼기는 음수와의 더한 합에 1을 추가로 더한다. n 더하기 회로만 있으면 빼기도 할 수 있다.

정수(Integer)의 표현 n 2’s complement (보수) (대부분의 CPU가 사용) n 음수의 표현시 1의 보수에

정수(Integer)의 표현 n 2’s complement (보수) (대부분의 CPU가 사용) n 음수의 표현시 1의 보수에 1을 더하여 표현한다. n 3 : 00000011 , n MSB = sign bit, 0 : positive, 1 : negative n 1개의 zero : 0000, 1111 + 1 = 0000 n 8 bit : -128 (1111) ~ +127 (01111111) n 256개의 수 모두 표현 n 3 + 2 = 00000011 + 00000010 = 00000101 n 3 – 2 = 3 + (-2) = n 00000011 + 11111110 = 00000001 n 더하기 회로만 있으면 빼기도 할 수 있다. n n bit system 의 수 표현 범위 : 2 n-1 ~ 2 n-1 -1 -3 : 11111100 + 1 = 11111101

실수 (real number, floating point number)의 표현 일정기억공간 (보통 4 byte (32 bits))에 다음과

실수 (real number, floating point number)의 표현 일정기억공간 (보통 4 byte (32 bits))에 다음과 같 이 표현되는 실수의 필요 정보를 수록 n + 0. a-1 a-2. . . a-m x r + n in radix r (normalized) n n a-1 a-2. . . a-m : mantissa (fraction), n n : exponent (integer) n r : base(radix)

문자 (character)의 표현 n n n ASCII (American Stabdard Code for Ingermation Interchange) n

문자 (character)의 표현 n n n ASCII (American Stabdard Code for Ingermation Interchange) n 영문자, 숫자, 특수문자 (alpha-numeric and special charaters) 의 표 현을 위한 7 bit code, 저장시 보통 8 bit 사용 n 7 bit or 8 bit transmission n string : sequence of characters + 0 한글코드 (한자) n n byte 형 : 7 bit code : 한글자모 할당 n 2 byte 형 : 완성형, 조합형, 유니코드 기타 n Packed decimal n 통신 또는 digitization 을 위한 여러 coding 방법이 있음

Input and Output #include <iostream> int main() { int pennies, nickels, dimes, quarters; double

Input and Output #include <iostream> int main() { int pennies, nickels, dimes, quarters; double total; cout << “How many pennies do you have? “; cin >> pennies; : total = pennies *. o 1 …. . ; cout << “Total is = “ << total << “n”; return 0; }

지정문 (Assignment Statement) variable(변수) = expression(수식); n total = count * 0. 05 +

지정문 (Assignment Statement) variable(변수) = expression(수식); n total = count * 0. 05 + total; n count++; count--; n 변수와 수식의 자료형은 같아야 한다. n n int i; double x; n x = i; i = x; // auto-conversion but warning, n // turnc. (roundoff) error occured n i = “abc”; // error

Casting (자료형 변환) n int n; double y; n = static_cast<int>(y + 0. 5);

Casting (자료형 변환) n int n; double y; n = static_cast<int>(y + 0. 5); // C++ n = (int)(y+0. 5); // C y = (double)n + 0. 05;

상수 (Constants) int main() { int bottles, cans; double total; const double BOTTLE_VOLUME =

상수 (Constants) int main() { int bottles, cans; double total; const double BOTTLE_VOLUME = 2. 0; const double CAN_VOLUME = 0. 355; cout << “How many bottles do you have? “; cin >> bottles; count << “How many cans do you have? “; cin >> cans; total = bottles * BOTTLE_VOLUME + cans * CAN_VOLUME; count >> total; return 0; } n n readability : 읽기 쉽고 프로그램내에 여러 번 나오는 경우, 값의 변화가 있을 때 한곳 만 수정하면 되는 큰 장점이 있다.

Enumerated Data Type enum Weekday { MON, TUE, WED, THU, DRI, SAT, SUN}; n

Enumerated Data Type enum Weekday { MON, TUE, WED, THU, DRI, SAT, SUN}; n Weekday는 새로운 자료형 n 실질적으로는 integer n Weekday homework_due_day; homework_due_day = FRI;

Mathematical Expression n n Operator Precedence n ( ) ++ (postfix) -- (postfix) n

Mathematical Expression n n Operator Precedence n ( ) ++ (postfix) -- (postfix) n + (unary) - (unary) ++(prefix) -- (prefix) n * / % n + - n = += -= *= /=. . Ex. n x = a + b * 3 * c; // a + (b*3)*c n x = (- a + b++) * 3; 애매한 표현은 괄호를 사용할 것 n a+b; // a, b : operands n

Mathematical Expression n n n Inc. & Dec. operators (prefix and postfix) n a

Mathematical Expression n n n Inc. & Dec. operators (prefix and postfix) n a = ++b; // b = b + 1; a = b; n a = b++; // a = b; b = b+1; a = b = c = 0; // a = ( b = (c = 0)); a = (b = 2) + (c = 3); a += b; // a = a + b; b = a%5; // 5로 나눈 나머지 (modulo op. ) a %= b; // a = a%b;

Mathematical Expression n Integer division n a = 5 / 3; // a= 1;

Mathematical Expression n Integer division n a = 5 / 3; // a= 1; truncated n a = 5. 0 /3; // 5. 0/3. 0 is converted into integer n 두 operand 중 하나가 실수이면 일단 계산은 실수로 함.

Example int main() { int pennies, nickels, dimes, quarters, value; int dollars, cents; cin

Example int main() { int pennies, nickels, dimes, quarters, value; int dollars, cents; cin >> pennies >> nickels >> dimes >> quarters; value = pennies + 5 * nickels + 10 * dimes + 25 * quarters; // in cents dollars = value / 100; cents = value % 100; cout << dollars << “ dollar “ << cents << “cents” << endl; return 0; }

함수의 사용 함수 호출 function_name (arg 1, arg 2, arg 3, …, argn); n

함수의 사용 함수 호출 function_name (arg 1, arg 2, arg 3, …, argn); n 예: double x; x = sin(1. 2); n Library : 자주 사용되는 함수들을 미리 프로그램 하여 종류별로 그룹화 하여 저장 n n n Math. Library : g++ myprog. cpp –lm n 참고 : text pp. 71 Graphic library

문자 (Character) n n n 영문자, 특수문자, 수문자 등 ASCII code 사용 (한글 코드는

문자 (Character) n n n 영문자, 특수문자, 수문자 등 ASCII code 사용 (한글 코드는 별도) 7 bit (최대 128 자) 저장은 1 byte에 통신시는 통신에 필요한 bit 추가하여 10 bit 정도 char c; // char. variavle in C++ n c = ‘a’; // ‘a’ means ASCII code value of small a (int) n c = ‘n’ // new line character n c = ‘ ‘; // blank character

문자 (Character) character는 1 byte integer 로도 사용 가능. n c = ‘A’; //

문자 (Character) character는 1 byte integer 로도 사용 가능. n c = ‘A’; // same as c = 65; n c += 32; // c becomes small ‘a’ n cin, cout은 그대로 사용 n

문자열 (String) #include string name = “John”; cout << name; name = “jgkim”; cout

문자열 (String) #include string name = “John”; cout << name; name = “jgkim”; cout << name; cout << “Enter your name: “; cin >> name; // Kim Jung 입력시 Kim 만 입력 getline (cin, name); // Kim Jung -> name으로

문자열 관련 함수 getline(cin, s) n string name = “Dept. of CSE. ”; name.

문자열 관련 함수 getline(cin, s) n string name = “Dept. of CSE. ”; name. length(); // name의 길이 14 name. substr(9, 3); // 9번째에서부터 3개의 문자열 // CSE (0에서 시작) n 접합 string fname = “Harry”; string lname = “Hacker”; string name = fname + “ “ + lname; n

예제 프로그램 #include <iostream> #include <string> int main() { cout << “Enter your full

예제 프로그램 #include <iostream> #include <string> int main() { cout << “Enter your full name (First Middle Last): “; string first, middle, last; cin >> first >> middle >> last; string initials = first. substr(0, 1) + middle. substr(0, 1) + last. substr(0, 1); cout << “Your initials are “ << initials << endl; return 0; } n Text의 편집출력은 각자 공부 할 것