Menu
CoddyTech

프로젝트: 명령 처리기

Coddy의 Rust 개발자를 위한 C++ 코스 레슨. 48개 중 46번째.

challenge icon

챌린지

쉬움

프로젝트 4단계: 이전의 모든 단계를 유지하세요. process를 구현하세요. 새 Catalog를 만들고 다음 명령을 처리합니다. add title copies, borrow title, return title. 잘못된 형식의 명령, 추가 필드, 중복 항목, 존재하지 않는 제목, 불가능한 대여는 거부합니다. 거부된 각 행마다 개수를 1씩 늘립니다. report와 | rejected:COUNT를 반환하며, 세로 막대 양쪽에 공백을 둡니다. 입력은 4단계 다음에 명령을 입력합니다. 빈 명령 묶음은 empty | rejected:0을 반환합니다.

편집 가능한 모듈: Book.cpp, Book.hpp, LoanPolicy.cpp, LoanPolicy.hpp, Catalog.cpp, Catalog.hpp, processor.cpp, processor.hpp. 각 필수 타입에는 이름이 지정된 헤더와 구현 파일이 각각 있습니다. 모든 public 시그니처를 그대로 유지하세요. 잠겨 있는 main.cpp는 stdin을 읽고 API를 직접 호출합니다. 또한 명시된 경우 public API 동작을 독립적으로 검사합니다. API 내부에서 출력하지 말고 반환값을 사용하세요. 정수 입력과 결과는 부호 있는 int 범위에 들어가며, 별도 언급이 없으면 텍스트는 ASCII입니다.

직접 해보기

#include "processor.hpp"
#include <iostream>
int main(){

std::vector<std::string> rows; std::string line; while(std::getline(std::cin,line)) rows.push_back(line);
int stage=std::stoi(rows.at(0));
if(stage>=1){Book b("probe",1);if(b.title()!="probe"||b.available()!=1||!b.borrow()||b.borrow())return 31;b.give_back();if(b.available()!=1)return 32;}
if(stage>=2){LoanPolicy p(7,3);if(p.fee(5)!=0||p.fee(7)!=0||p.fee(9)!=6)return 33;}
if(stage>=3){Catalog c;if(!c.add("a",1)||c.add("a",3)||c.add("b",-1)||!c.borrow("a")||c.borrow("a")||c.borrow("missing")||c.available("missing")!=-1||!c.give_back("a")||c.report()!="a:1")return 34;}
if(stage==1){std::istringstream in(rows.at(1));std::string t;int n,k;in>>t>>n>>k;Book b(t,n);int success=0;for(int i=0;i<k;++i)if(b.borrow())++success;std::cout<<b.title()<<" "<<success<<" "<<b.available()<<"\n";}
else if(stage==2){std::istringstream in(rows.at(1));int d,f,e;in>>d>>f>>e;LoanPolicy p(d,f);std::cout<<p.fee(e)<<"\n";}
else if(stage==3){Catalog c;for(size_t i=1;i<rows.size();++i){std::istringstream in(rows[i]);std::string t;int n;in>>t>>n;c.add(t,n);}std::cout<<c.report()<<"\n";}
else if(stage==4){std::cout<<process(std::vector<std::string>(rows.begin()+1,rows.end()))<<"\n";}

}

Rust 개발자를 위한 C++의 모든 레슨

직접 연습해 보세요: 온라인 C++ 컴파일러