⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 10x.cpp

📁 C/C++程序设计导论(第二版)》程序源文件
💻 CPP
字号:
// 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -