Menu
CoddyTech

プロジェクト:カタログ

Coddyの「C#開発者のためのC++」コースのレッスン 45/48。

challenge icon

チャレンジ

簡単

プロジェクトのステップ 3:これまでの API を維持します。Catalog.add は負の冊数と重複するタイトルを拒否します。borrow/give_back は存在しないタイトルを拒否し、available は存在しないタイトルに対して -1 を返します。report はタイトルを title:available の形式で、タイトル順にスペース区切りで一覧表示します。該当するタイトルがない場合は empty を表示します。入力はステージ 3 の後にタイトル/冊数の行が続きます。それぞれを追加して report を出力します。独立したテストでは貸し出しと返却も行います。ステージ 1 と 2 も引き続き動作します。

編集可能なモジュール:Book.cpp、Book.hpp、LoanPolicy.cpp、LoanPolicy.hpp、Catalog.cpp、Catalog.hpp、processor.cpp、processor.hpp。必要な各型には、それぞれ名前付きのヘッダーと実装があります。すべての公開シグネチャを維持してください。ロックされた main.cpp は標準入力を読み取り、API を直接呼び出します。また、指定された箇所では公開 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";}

}

C#開発者のためのC++のすべてのレッスン

自分で練習してみよう: C++オンラインコンパイラ