main.cpp

来自「ssd5 op6的答案」· C++ 代码 · 共 72 行

CPP
72
字号
// main.cpp -- driver program for "Sum of Its Parts"

#pragma warning(disable:4786)

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

#include "parts.h"

void load_definitions(char* filename) {

	ifstream inf(filename);
	string part, subpart;
	
	int quantity;
	
	while ( inf.good() ) {
		inf >> part >> quantity >> subpart;
		if ( !inf.good() ) return;
		add_part(part,quantity,subpart);
	};
}

void whatis_query(string const &x) {

	Part* xp = partContainer.lookup(x);
	cout << endl;
	xp->describe();
}

void howmany_query(string const &x, string const &y) {

	Part* xp = partContainer.lookup(x);
	Part* yp = partContainer.lookup(y);
	cout << endl << y << " has " << yp->count_howmany(xp) << " " << x << endl;
}

void process_queries(char* filename) {

	ifstream inf(filename);
	string query, x, y;
	
	while ( inf.good() ) {
		
		inf >> query >> x;
		
		if ( query == "howmany" )
			inf >> y;
    
		if ( !inf.good() ) return;
    
		if ( query == "howmany" )
			howmany_query(x,y);
		else if ( query == "whatis" )
			whatis_query(x);
		else {
			cerr << "*** Illegal query: " << query << endl;
			return;
		};
	};
}

int main(void) {

	load_definitions("definitions.txt");
	process_queries("queries.txt");
	return EXIT_SUCCESS;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?