10x.cpp

来自「《C/C++程序设计导论(第二版)》一书的程序源文件」· C++ 代码 · 共 43 行

CPP
43
字号
// widcost.cpp  Output the cost of a widget given the color and
//     model indexes.
//  ASSUMPTION: file "shirts.txt" contains the 4x7 cost table
#include <iostream>
#include <fstream>
#include <string>using namespace std;const string filename = "shirts.txt";
const int COLORS = 7;
const int MODELS = 4;

// ReadTable() Fill a 2-D cost table from the `filename' file
// ASSUMPTION: file exists.
// IN:  filename is the correct file name string
// OUT:  table is a 2-D array of costs
void ReadTable (float table[ ][COLORS], string filename)
{	ifstream infile (filename, ios::in);
	int color, model;
	for (model=0; model<MODELS; model++)
		for (color=0; color<COLORS; color++)
			infile >> table[model][color];
}

// DoLookUps()  Output a cost for a given model and color
// IN: table is the 2-D array of costs.
void DoLookUps (float table[ ][COLORS])
{	int color, model;
	cout << "enter color and model (Ctrl-C) to end";
	while (cin >> color >> model)
	{	if (color >= COLORS || style >= STYLES)			cout << "invalid color or style...";		else cout << " cost for color: "<<color<<" and model "<<model
			<<" is "<< table[model][color] << endl;
	}
}

void main ()
{	float cost_table[4][7]; 					// color is second index, model is first
	ReadTable (cost_table, filename);					// input the table from disk
	DoLookUps (cost_table);					// perform lookups for user
}

⌨️ 快捷键说明

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