C++

[C++] 명품 C++ 프로그래밍 실습문제 13장 오픈챌린지/openchallenge

Hs’s Coding vlog 2023. 1. 29. 21:58

 

[C++] 명품 C++ 프로그래밍 실습문제 6장 오픈챌린지/openchallenge

//CPU.cpp

#include <iostream>
#include <cctype>
#include "CPU.h"
#include "HardWareException.h"
#include <string>
using namespace std;

void CPU::fetch(){
    blank[0] = -1;
    blank[1] = -1;
    HardwareException h("fetch");
    getline(cin,instruction);
    if(instruction.empty()){
        h.setMsg("명령 라인 없음");
        throw h;
    }

   
}

void CPU::decode(){
    HardwareException h("decode");
    int j = 0;
    for(int i = 0 ;i<instruction.size();i++){
        if(instruction[i]==' ')
            blank[j++] = i;
    }
    cmd = instruction.substr(0,blank[0]);
    string tmp1, tmp2;

    tmp1 = instruction.substr(blank[0]+1,blank[1]-(blank[0]+1));
    tmp2 = instruction.substr(blank[1]+1,instruction.size()-blank[1]-1);
    if(isalpha(tmp1[0]) != 0 || isalpha(tmp2[0]) != 0){
    }

    else{
        op1 = stoi(tmp1);
        op2 = stoi(tmp2);
    }
   
   
    if(blank[0] == -1 || blank[1] == -1){
        h.setMsg("피연산자 예외");
        throw h;
    }
    else if(isalpha(tmp1[0]) != 0 || isalpha(tmp2[0]) != 0){
        h.setMsg("피연산자 타입 예외");
        throw h;
    }
    else if(cmd == "ADD" || cmd == "SUB" || cmd == "MUL" || cmd == "DIV"){
    }
    else{h.setMsg("invalid 명령어 예외");
        throw h;}
}

void CPU::execute(){
    HardwareException h("execute");
    if(cmd=="DIV" && op2 == 0){
        h.setMsg("0으로 나누는 예외");
        throw h;
    }

    if(cmd=="ADD"){
        cout << "\t\tADD " << op1 << " " << op2 << ":\t\t" << op1+op2 << endl;
    }
    else if(cmd=="SUB"){
        cout << "\t\tSUB " << op1 << " " << op2 << ":\t\t" << op1-op2 << endl;
    }
    else if(cmd=="DIV"){
        cout << "\t\tDIV " << op1 << " " << op2 << ":\t\t" << op1/op2 << endl;
    }
    else if(cmd=="MUL"){
        cout << "\t\tMUL " << op1 << " " << op2 << ":\t\t" << op1*op2 << endl;
    }
}

void CPU::run(){
    while(1){
        try{
            fetch();
            decode();
            execute();
        }
        catch(HardwareException a){
            cout << a.getStep() << " 스텝, " << a.getMsg() << endl;
        }
    }
}

//CPU.h

#ifndef CPU_H
#define CPU_H
#include <string>
#include "HardWareException.h"
using namespace std;

class CPU{
    string instruction;
    int op1, op2;
    string cmd;
    int blank[2];
    public:
    void fetch();
    void decode();
    void execute();
    void run();
};


#endif

//HardWareException.cpp
#include <iostream>
#include "HardWareException.h"
#include <string>
using namespace std;

HardwareException::HardwareException(string step,string msg){
    this->step = step;
    this->msg = msg;
}

string HardwareException::getStep(){
    return step;
}

string HardwareException::getMsg(){
    return msg;
}

void HardwareException::setMsg(string msg){
    this->msg = msg;
}

void HardwareException::what(){
    cout << step;
}

//HardWareException.h
#ifndef HARDWARE_H
#define HARDWARE_H
#include <string>
using namespace std;
class HardwareException{
    string step;
    string msg;
    public:
    HardwareException(string step="", string ="");
    string getStep();
    string getMsg();
    void setMsg(string msg);
    void what();
};

#endif

//main.cpp
#include <iostream>
#include "CPU.h"
#include "HardWareException.h"
using namespace std;

int main(){
    CPU a;
    a.run();
}