시스템 프로그래밍 프로젝트 #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

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 · 3 min · Sobamemil

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

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

C++ Programming Ch.9 Exercise 3 Solution

Problem: 다음 추상 클래스 LoopAdder가 있다. 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 class LoopAdder { // 추상 클래스 string name; // 루프의 이름 int x, y, sum; // x에서 y까지의 합은 sum void read(); // x, y 값을 읽어 들이는 함수 void write(); // sum을 출력하는 함수 protected: LoopAdder(string name="") { // 루프의 이름을 받는다. 초깃값은 "" this->name = name; } int getX() { return x; } int getY() { return y; } virtual int calculate() = 0; // 순수 가상 함수. 루프를 돌며 합을 구하는 함수 public: void run(); // 연산을 진행하는 함수 }; void LoopAdder::read() { // x, y 입력 cout << name << ":" << endl; cout << "처음 수에서 두번째 수까지 더한다. 두 수를 입력하세요 >> "; cin >> x >> y; } void LoopAdder::write() { // 결과 sum 출력 cout << x << "에서 " << y << "까지의 합 = " << sum << " 입니다" << endl; } void LoopAdder::run() { read(); // x, y를 읽는다 sum = calculate(); // 루프를 돌면서 계산한다. write(); // 결과 sum을 출력한다. } Write a 다음 main() 함수와 Execution Result처럼 되도록 ForLoopAdder class that inherits from the LoopAdder class. ForLoopAdder 클래스의 calculate() 함수는 for 문을 이용하여 합을 구한다. ...

November 21, 2019 · 3 min · Sobamemil

C++ Programming Ch.9 Exercise 2 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 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 km를 mile(마일)로 변환하는 KmToMile class that inherits from the Converter class. The main() function and execution result are as follows. ...

November 20, 2019 · 2 min · Sobamemil

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