干墙计划帮助 [英] Drywall program help
问题描述
我正在尝试使用函数编写程序。我遇到了两个功能问题。有问题的是calculate_drywall。对于长度和宽度,以英尺为单位传递两个值,并返回房间的各种尺寸的干墙板。这些纸张分别为4x 8,10,12,14,16。我使用带参考参数的空白。我遇到问题的第二个功能是计算通过建筑物总页数的材料,并返回所需的螺钉和泥浆量。我感到困惑的是,干墙功能会返回每个房间的床单,然后是建筑物总数中的材料。我应该在主要做什么吗?任何帮助,将不胜感激。
这是我目前所拥有的
I am trying to write a program using functions. I am having trouble with two of the functions. one in question is calculate_drywall. Which is passed two values in feet for length and width and returns the various size drywall sheets for the room. The sheets come in 4x 8, 10, 12, 14, 16. I am using a void with reference parameters. The second function I am having trouble with is calculate material which is passed the total number of sheets for the building and returns the amount of screws and mud required. Where I am confused is the drywall function returns the sheets for each room then in materials it is taking in the building total. Should I do something in main? Any help would be appreciated.
Here is what I have so far
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<math.h>
using namespace std;
void get_data(double& val1, double& val2, bool& error, ifstream&);
double convert_to_feet(double val);
void calculate_drywall(double& tot_8ft, double& tot_10ft, double& tot_12ft, double& tot_14ft, double& tot_16ft, double& perimeter, double& in_feet1, double& in_feet2);
void calculate_material(double& tot_8ft, double& tot_10ft, double& tot_12ft, double& tot_14ft, double& tot_16ft, double& mud, double& screws, double& sqr_ft);
void print_output(double room_num, double in_feet1, double in_feet2, bool error, ofstream&, double perimeter, double tot_16ft, double sqr_ft);
int main()
{
double val1, val2, room_num = 0, in_feet1, in_feet2, perimeter = 0;
double tot_8ft, tot_10ft, tot_12ft, tot_14ft, tot_16ft = 0;
double mud, screws, sqr_ft = 0;
bool error;
string proj_title;
ifstream infile;
ofstream outfile;
infile.open("proj_info.txt");
outfile.open("supplies.txt");
getline(infile, proj_title);
get_data(val1, val2, error, infile);
while (infile)
{
room_num++;
in_feet1 = convert_to_feet(val1);
in_feet2 = convert_to_feet(val2);
calculate_drywall(tot_8ft, tot_10ft, tot_12ft, tot_14ft, tot_16ft, in_feet1, in_feet2, perimeter);
get_data(val1, val2, error, infile);
}
calculate_material(tot_16ft, tot_14ft, tot_12ft, tot_10ft, tot_8ft, mud, screws, sqr_ft);
}
void get_data(double& val1, double& val2, bool& error, ifstream& in)
{
in >> val1 >> val2;
if (val1 <= 0 || val2 <= 0)
error = 0;
else
error = 1;
}
double convert_to_feet(double val)
{
double in_feet;
in_feet = val * 3.28;
return in_feet;
}
void calculate_drywall(double& tot_8ft, double& tot_10ft,
double& tot_12ft, double& tot_14ft,
double& tot_16ft, double& in_feet1,
double& in_feet2, double& perimeter)
{
perimeter = 2 * (in_feet1 + in_feet2);
if (perimeter >= 16)
tot_16ft = (perimeter / 16) * 2;
else
if (perimeter <= 16 && perimeter >= 14)
tot_14ft = (perimeter / 14) * 2;
else
if (perimeter <= 14 && perimeter >= 12)
tot_12ft = (perimeter / 12) * 2;
else
if (perimeter <= 12 && perimeter >= 10)
tot_10ft = (perimeter / 10) * 2;
else
tot_8ft = (perimeter / 8) * 2;
}
void calculate_material(double& tot_8ft, double& tot_10ft, double& tot_12ft, double& tot_14ft, double& tot_16ft, double& mud, double& screws, double& sqr_ft)
{
sqr_ft = (tot_16ft * (16 * 4)) + (tot_14ft * (14 * 4)) + (tot_12ft * (12 * 4)) + (tot_10ft * (10 * 4)) + (tot_8ft * (8 * 4));
}
void print_output(double room_num, double in_feet1, double in_feet2, bool error, ofstream& out, double perimeter, double tot_16ft, double sqr_ft)
{
out << ceil (tot_16ft) << sqr_ft << endl;
}
推荐答案
如果你有多个房间,你需要在主要功能中处理这个问题。 br />
您需要在while循环中为每个房间添加干墙板的总和。
If you have more than one room, you need to take care of this in the main function.
You need to add the sums of the drywall sheets for each room in the while loop.
double tmp_8ft, tmp_10ft, tmp_12ft, tmp_14ft, tmp_16ft;
while (infile)
{
room_num++;
in_feet1 = convert_to_feet(val1);
in_feet2 = convert_to_feet(val2);
tmp_8ft = 0.0;
tmp_10ft = 0.0;
tmp_12ft = 0.0;
tmp_14ft = 0.0;
tmp_16ft = 0.0;
calculate_drywall(tmp_8ft, tmp_10ft, tmp_12ft, tmp_14ft, tmp_16ft, in_feet1, in_feet2, perimeter);
tot_8ft += tmp_8ft;
tot_10ft += tmp_10ft;
tot_12ft += tmp_12ft;
tot_14ft += tmp_14ft;
tot_16ft += tmp_16ft;
get_data(val1, val2, error, infile);
}
现在你可以将房子的总价值传递给函数 calculate_material
。
你可能会意识到变量 in_feet_1
和 in_feet_2
可能有更多描述性名称。喜欢 in_feet_width
和 in_feet_height
。
[更新]
刚才意识到这可以在calculate_drywall中更轻松地完成。
(昨天一定是吃了一个愚蠢的药丸)
只需更新受影响的参数,主函数中的变量也会更新。
然后将主函数保留为原始函数。
Now you can pass the total values for the house into the function calculate_material
.
You might realize the the variable in_feet_1
and in_feet_2
could have more descriptive names. like in_feet_width
and in_feet_height
.
[UPDATE]
Just realized that this can be done much easier inside the calculate_drywall.
(Must have been eating a stupid pill yesterday)
Just update the parameter that is affected, and the variable in the main function will be updated as well.
Then keep the main function as the original.
void calculate_drywall(double& tot_8ft, double& tot_10ft,
double& tot_12ft, double& tot_14ft,
double& tot_16ft, double& in_feet1,
double& in_feet2, double& perimeter)
{
perimeter = 2 * (in_feet1 + in_feet2);
if (perimeter >= 16)
tot_16ft += (perimeter / 16) * 2;
else
if (perimeter <= 16 && perimeter >= 14)
tot_14ft += (perimeter / 14) * 2;
else
if (perimeter <= 14 && perimeter >= 12)
tot_12ft += (perimeter / 12) * 2;
else
if (perimeter <= 12 && perimeter >= 10)
tot_10ft += (perimeter / 10) * 2;
else
tot_8ft += (perimeter / 8) * 2;
}
这篇关于干墙计划帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!