πŸ’» Dev & CSC++ Programming

C++ Programming Ch.9 Exercise 9 Solution

Problem: λ‹€μŒ κ·Έλ¦Όκ³Ό 같은 상속 ꡬ쑰λ₯Ό κ°–λŠ” 클래슀λ₯Ό μ„€κ³„ν•œλ‹€. λͺ¨λ“  ν”„λ¦°ν„°λŠ” λͺ¨λΈλͺ…(model), μ œμ‘°μ‚¬(manufacturer), 인쇄 맀수(printedCount), 인쇄 쒅이 μž”λŸ‰(availableCount)을 λ‚˜νƒ€λ‚΄λŠ” 정보λ₯Ό κ°€μ§„λ‹€. print(int pages) ν•¨μˆ˜μ™€ show() ν•¨μˆ˜λŠ” 가상 ν•¨μˆ˜λ‘œ κ΅¬ν˜„ν•˜λΌ. print(int pages)λŠ” pages 만큼 ν”„λ¦°νŠΈν•˜λŠ” ν•¨μˆ˜μ΄κ³ , show() ν•¨μˆ˜λŠ” ν˜„μž¬ ν”„λ¦°νŠΈμ˜ λͺ¨λΈ, μ œμ‘°μ‚¬, 인쇄 맀수, 인쇄 쒅이 μž”λŸ‰ 등을 좜λ ₯ν•˜λŠ” ν•¨μˆ˜μ΄λ‹€. μž‰ν¬μ ― ν”„λ¦°ν„°λŠ” μž‰ν¬ μž”λŸ‰(availableInk) 정보λ₯Ό μΆ”κ°€μ μœΌλ‘œ κ°€μ§€λ©°, λ ˆμ΄μ € ν”„λ¦°ν„°λŠ” ν† λ„ˆ μž”λŸ‰(availableToner) 정보λ₯Ό μΆ”κ°€μ μœΌλ‘œ κ°€μ§„λ‹€. μ΄λ“€μ˜ print(int pages) 멀버 ν•¨μˆ˜λŠ” ν”„λ¦°ν„° νƒ€μž…μ— 맞게 κ΅¬ν˜„ν•˜λΌ. 각 클래슀λ₯Ό 섀계 κ΅¬ν˜„ν•˜κ³  λ‹€μŒκ³Ό 같이 μ‹€ν–‰λ˜λ„λ‘ 전체 ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. InkJetPrinter 객체와 LaserPrinter 객체λ₯Ό 각각 ν•˜λ‚˜λ§Œ λ™μ μœΌλ‘œ μƒμ„±ν•˜μ—¬ μ‹œμž‘ν•œλ‹€. ...

November 26, 2019 Β· 3 min Β· Sobamemil
πŸ’» Dev & CSSystem & Embedded

μ‹œμŠ€ν…œ ν”„λ‘œκ·Έλž˜λ° ν”„λ‘œμ νŠΈ #6

Problem: μž…λ ₯ 데이터 : [Command.txt 0.00MB](https://blog.kakaocdn.net/dna/bnbU30/btqBgeimBnY/AAAAAAAAAAAAAAAAAAAAAAPakDbTA4jf0WyG_DmDE76emfyLiazho4x1advtOToN/Command.txt?credential=yqXZFxpELC7KVnFOS48ylbz2pIh7yKj8&expires=1788188399&allow_ip=&allow_referer=&signature=2iAm290NW4CFkRgpHKAQHRqBvxw%3D&attach=1&knm=tfile.txt) Execution Result: Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #include<stdio.h> #include<string.h> #include<stdlib.h> void PrintAssm(char* name_buf, int name_size); struct OPTAB{ char name[8]; int len; } Wordtab[] = { {"LDA", 3}, {"STA", 4}, {"ADD", 5}, {"TIX", 2}, {"CMP", 6} }; int main() { char name_buf[8]; int wordtab_size = sizeof(Wordtab)/12; // Wordtab에 μ •μ˜λœ 단어 갯수, 12 = size of char[8] + int FILE* fp; if((fp = fopen("Command.txt", "r"))==NULL) { printf("file not found...\n"); exit(1); } while(fgets(name_buf, sizeof(name_buf), fp) != NULL) { // fgets() ν•¨μˆ˜λŠ” scanfλ‚˜ gets()μ™€λŠ” 달리 뒀에 κ°œν–‰λ¬Έμžκ°€ λΆ™λŠ”λ‹€ if(name_buf[0] == '\n') // name_buf λ°°μ—΄ 맨 처음이 κ°œν–‰λ¬Έμžμ΄λ©΄ λ©”μ‹œμ§€ 좜λ ₯ 없이 λ¬΄μ‹œ continue; if(name_buf[strlen(name_buf) - 1] == '\n') // name_buf λ°°μ—΄ 맨 끝이 κ°œν–‰λ¬Έμžμ΄λ©΄ name_buf[strlen(name_buf) - 1] = '\0'; // naem_buf λ°°μ—΄ 맨 끝에 뢙은 κ°œν–‰λ¬Έμž 제거 PrintAssm(name_buf, wordtab_size); } fclose(fp); } void PrintAssm(char* name_buf, int wordtab_size){ static int k=1; // 쀄 번호λ₯Ό λ‚˜νƒ€λ‚΄λŠ” λ³€μˆ˜ static int loc=0; // λ‹¨μ–΄μ˜ μ‹œμž‘ μœ„μΉ˜λ₯Ό λ‚˜νƒ€λ‚΄λŠ” λ³€μˆ˜ int i, res=-1; for(i=0; i<wordtab_size; i++){ res = strcmp(name_buf, Wordtab[i].name); // strcmp : 두 λ¬Έμžμ—΄μ΄ κ°™μœΌλ©΄ 0 리턴 if(res==0){ break; } } if(i==wordtab_size){ printf(" Undefined word\n"); // Wordtab λ°°μ—΄ μ•ˆμ— μ •μ˜λ˜μ–΄ μžˆμ§€ μ•ŠλŠ” λͺ…λ Ήμ–΄λ©΄ 좜λ ₯ν•˜κ³  단어 λ¬΄μ‹œ } else{ if(k < 10) printf(" "); printf("%d, %.2X, %s, %.2d\n", k, loc, Wordtab[i].name, Wordtab[i].len); loc += Wordtab[i].len; // λ‹¨μ–΄μ˜ 해당길이λ₯Ό λ”ν•΄μ„œ λ‹€μŒ μ‹œμž‘ μœ„μΉ˜λ₯Ό μ•Œλ €μ€Œ k++; } } Explanation: ...

November 21, 2019 Β· 2 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.9 Exercise 8 Solution

Problem: μ‚¬κ°ν˜•μ— λ‚΄μ ‘ν•˜λŠ” λ„ν˜•μ„ ν‘œν˜„ν•˜κΈ° μœ„ν•œ Shape ν΄λž˜μŠ€κ°€ μžˆλ‹€. 1 2 3 4 5 6 7 8 9 class Shape { protected: string name; // λ„ν˜•μ˜ 이름 int width, height; // λ„ν˜•μ΄ λ‚΄μ ‘ν•˜λŠ” μ‚¬κ°ν˜•μ˜ λ„ˆλΉ„μ™€ 높이 public: Shape(string n="", int w=0, int h=0) { name = n; width = w; height = h; } virtual double getArea() { return 0; } // dummy κ°’ 리턴 string getName() { return name; } // 이름 리턴 }; 문제 7에 μ£Όμ–΄μ§„ Shape 클래슀λ₯Ό 좔상 클래슀둜 λ§Œλ“€κ³  문제 7을 λ‹€μ‹œ μž‘μ„±ν•˜λΌ. ...

November 21, 2019 Β· 2 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.9 Exercise 7 Solution

Problem: μ‚¬κ°ν˜•μ— λ‚΄μ ‘ν•˜λŠ” λ„ν˜•μ„ ν‘œν•œν•˜κΈ° μœ„ν•œ Shape ν΄λž˜μŠ€κ°€ μžˆλ‹€. 1 2 3 4 5 6 7 8 9 class Shape { protected: string name; // λ„ν˜•μ˜ 이름 int width, height; // λ„ν˜•μ΄ λ‚΄μ ‘ν•˜λŠ” μ‚¬κ°ν˜•μ˜ λ„ˆλΉ„μ™€ 높이 public: Shape(string n="", int w=0, int h=0) { name = n; width = w; height = h; } virtual double getArea() { return 0; } // dummy κ°’ 리턴 string getName() { return name; } // 이름 리턴 }; Write a 타원을 ν‘œν˜„ν•˜λŠ” Oval, μ‚¬κ°ν˜•μ„ ν‘œν˜„ν•˜λŠ” Rect, μ‚Όκ°ν˜•μ„ ν‘œν˜„ν•˜λŠ” Triangular class that inherits from the Shape class. main()을 μž‘μ„±ν•˜κ³  μ‹€ν–‰ν•˜λ©΄ λ‹€μŒκ³Ό κ°™λ‹€. ...

November 21, 2019 Β· 2 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.9 Exercise 6 Solution

Problem: λ‹€μŒ AbstractStack은 μ •μˆ˜ μŠ€νƒ ν΄λž˜μŠ€λ‘œμ„œ 좔상 ν΄λž˜μŠ€μ΄λ‹€. 1 2 3 4 5 6 7 class AbstrackStack { public: virtual bool push(int n) = 0; // μŠ€νƒμ— n을 ν‘Έμ‹œν•œλ‹€. μŠ€νƒμ΄ full이면 false 리턴 virtual bool pop(int& n) = 0; // μŠ€νƒμ—μ„œ νŒν•œ μ •μˆ˜λ₯Ό n에 μ €μž₯ν•˜κ³  μŠ€νƒμ΄ empty이면 false 리턴 virtual int size() = 0; }; 이λ₯Ό 상속받아 μ •μˆ˜λ₯Ό ν‘Έμ‹œ, νŒν•˜λŠ” IntStack 클래슀λ₯Ό λ§Œλ“€κ³  μ‚¬μš© 사둀λ₯Ό 보여라. Objective & Hints: ...

November 21, 2019 Β· 2 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.9 Exercise 1 Solution

Problem: The following is an abstract class Converter that converts units. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include using namespace std; class Converter { protected: double ratio; virtual double convert(double src)=0; // srcλ₯Ό λ‹€λ₯Έ λ‹¨μœ„λ‘œ λ³€ν™˜ν•œλ‹€. virtual string getSourceString()=0; // src λ‹¨μœ„ λͺ…μΉ­ virtual string getDestString()=0; // dest λ‹¨μœ„ λͺ…μΉ­ public: Converter(double ratio) { this->ratio = ratio; } void run(){ double src; cout << getSourceString() << "을 " << getDestString() << "둜 λ°”κΏ‰λ‹ˆλ‹€. "; cout << getSourceString() << "을 μž…λ ₯ν•˜μ„Έμš”>> "; cin >> src; cout << "λ³€ν™˜ κ²°κ³Ό : " << convert(src) << getDestString() << endl; } }; Write a λ‹¬λŸ¬λ₯Ό μ›ν™”λ‘œ ν™˜μ‚°ν•˜λŠ” WonToDollar class that inherits from the Converter class. The main() function and execution result are as follows. ...

November 20, 2019 Β· 2 min Β· Sobamemil