1 / 11

제 2 장 UNIX 에서의 C++ 프로그래밍

제 2 장 UNIX 에서의 C++ 프로그래밍. C++ Programming in UNIX. UNIX & C++ Programming Environment. UNIX: 1970 년대 이후 가장 대표적인 OS ( 운영체제 : Operating System) C.f., MS-Windows Editor vi – VI sual editor $ vi C++_filename Compiler g++/gcc – GNU C++/C compiler $ g++ C++_filename Make

Télécharger la présentation

제 2 장 UNIX 에서의 C++ 프로그래밍

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 제 2 장UNIX에서의 C++ 프로그래밍 C++ Programming in UNIX

  2. UNIX & C++ Programming Environment • UNIX: 1970년대 이후 가장 대표적인 OS (운영체제: Operating System) • C.f., MS-Windows • Editor • vi – VIsual editor $ vi C++_filename • Compiler • g++/gcc – GNU C++/C compiler $ g++ C++_filename • Make • Make utility for maintaining groups of programs $ make [Makefile] Programmer Editor TXT(C++) Program Compiler/Linker EXE Program Execution Ch2. C++ Programming in Unix

  3. vi (Visual) 에디터 • 명령대기상태 • “문자입력상태”에서 [Esc] 키에 의해 상태 변화 • 에디터의 종료 • 파일의 편집 명령 • 삽입/삭제/치환/복사 • 화면 이동 • 커서 이동 • … • 문자입력상태 • “명령대기상태”에서 입력 명령에 의해 상태 변화 • i/a/o/… – insert/append/open/… [i/a/o/…] $vi filename 명령 대기 문자 입력 :wq [Esc] Ch2. C++ Programming in Unix

  4. H J K L 편집 • 명령대기상태에서 다음 명령에 의해 입력상태로 변경 • i (insert) • a (append) • o/O (open) • 커서 이동 • h/j/k/l • 화면 이동 • [Ctrl]+u – scroll up half screen • [Ctrl]+d – scroll down half screen i i A i B C A B C a a A B a C Ch2. C++ Programming in Unix

  5. Makefile • 여러 프로그램 파일을 관리하는 유틸리티 • 구조 target: dependencies [TAB]command • target이 dependencies보다 오래된 경우 command 실행 test : main.o read.o write.o gcc -o test main.o read.o write.o main.o : io.h main.c gcc -c main.c read.o : io.h read.c gcc -c read.c write.o: io.h write.c gcc -c write.c Ch2. C++ Programming in Unix

  6. 매크로의 사용 • 특정 이름의 매크로(macro) 정의 OBJECTS = main.o read.o write.o test : $(OBJECTS) gcc -o test $(OBJECTS) main.o : io.h main.c gcc -c main.c read.o : io.h read.c gcc -c read.c write.o: io.h write.c gcc -c write.c Ch2. C++ Programming in Unix

  7. 매크로 의미 $* $@ $< 확장자 없는 목표 파일 현재의 목표 파일 현재의 목표 파일보다 최근에 갱신된 파일 이름 매크로와 확장자 규칙 • .c 파일 (C 프로그램 파일)로부터 .o 파일 (object 파일)을 자동적으로 생성 • .SUFFIXES = .c .o • 내부 매크로 OBJECTS = main.o read.o write.o SRCS = main.c read.c write.c CC = gcc CFLAGS = -g TARGET = test $(TARGET) : $(OBJECTS) $(CC) -o $(TARGET) $(OBJECTS) clean : rm -rf $(OBJECTS) $(TARGET) core main.o : io.h main.c read.o : io.h read.c write.o: io.h write.c .c.o: $(CC) $(CFLAGS) -c $< -o $@ Ch2. C++ Programming in Unix

  8. 실습 2-1 • vi를 사용하여 다음과 동일한 텍스트 파일을 작성하라. +--------------+ | io.h | +-------+------+ | +----------------+----------------+ | | | +-------+------+ +-------+------+ +-------+------+ | main.c | | read.c | | write.c | +-------+------+ +-------+------+ +-------+------+ | | | +-------+------+ +-------+------+ +-------+------+ | main.o | | read.o | | write.o | +-------+------+ +-------+------+ +-------+------+ | | | +----------------+----------------+ | +-------+------+ | test | +--------------+ Ch2. C++ Programming in Unix

  9. C++ 컴파일러 • C++ 컴파일러 확인 $ which g++ /usr/bin/g++ • 테스트 컴파일 $ vi hello.cpp $ more hello.cpp $ g++ hello.cpp $ a.out #include <iostream> int main() { std::cout << "Welcome to C++!\n"; return 0; } Ch2. C++ Programming in Unix

  10. 테스트 프로그래밍 • 수입 파일(income.in)에서 수입 금액을 읽어 세금 파일(tax.out)을 출력하는 프로그램(income.cpp)을 작성한다. • income.in 2214 10500 31010 • tax.out Income = 2214 greenbacks Tax = 664 greenbacks Income = 10500 greenbacks Tax = 6300 greenbacks Income = 31010 greenbacks Tax = 18606 greenbacks • cutoff = 6000 • rate1 = 0.3 • rate2 = 0.6 • 2214 * 0.3 = 664 • 10500 * 0.6 = 6300 • 31010 * 0.6 = 18606 Ch2. C++ Programming in Unix

  11. income.cpp while ( infile >> income ) { if ( income < cutoff ) tax = int (rate1 * income); else tax = int (rate2 * income); outfile << "Income = " << income << " greenbacks\n" << "Tax = " << tax << " greenbacks" << endl; } infile.close(); outfile.close(); return 0; } #include <fstream> using namespace std; const int cutoff = 6000; const float rate1 = 0.3; const float rate2 = 0.6; int main() { ifstream infile; ofstream outfile; int income, tax; infile.open( "income.in" ); outfile.open( "tax.out" ); Ch2. C++ Programming in Unix

More Related