1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
//prototypes
int getTreeNo();
int getSpeciesCode(int[], string[]);
float getDbh();
int getTotHt();
double calcTotVol(int, int[], double[], double[], float, int);
void displayTreeData(ofstream&, int, int, int[], string[], double, int, double);
int main() {
int treeNo, speciesCode, totalHt, noTrees;
float dbh;
double totalVol, avgTotVol;
int Species[6] = { 11,12,13,21,22,23 };
string speciesDesc[6] = { "Balsam Fir ","Black Spruce ","Red Pine ","Trembling Ash ","White Birch ","Other Hardwoods" };
double b0[6] = { 1.3686, 0.000, 2.1892, .8528, 1.7478, .7668 };
double b1[6] = { .002182, .002468, .002052, .002251, .002142, .002164 };
noTrees = 0;//initialized to 0
avgTotVol = 0.0;//set volume to 0
ofstream out("report.dat");//output is to be stored on report.dat external data file.
cout << setiosflags(ios::fixed) << setprecision(3);
out << setiosflags(ios::fixed) << setprecision(3);
do /*major while control loop that allows the user to enter data
tree by tree until a tree number 999 is entered*/
{
treeNo = getTreeNo();
if (treeNo != 999)
{
speciesCode = getSpeciesCode(Species, speciesDesc);
dbh = getDbh();
totalHt = getTotHt();
totalVol = calcTotVol(speciesCode, Species, b0, b1, dbh, totalHt);
noTrees++;//increment tree count
avgTotVol += totalVol;//add current tree's totalVol to avgTotVol
displayTreeData(out, speciesCode, treeNo, Species, speciesDesc, dbh, totalHt, totalVol);
}
} while (treeNo != 999);
cout << "\n\nTotal trees measured = " << noTrees
<< "\nAverage total volume = ";
out << "\n\nTotal trees measured = " << noTrees
<< "\nAverage total volume = ";
if (noTrees < 1)
{
cout << 0;
out << 0;
}
else
{
cout << avgTotVol / noTrees;
out << avgTotVol / noTrees;
}
out.close();
return 0;
}
/*
getTreeNo
Input tree number, input must be type int
and > 0 and less than 150 or = 999.
A value of 999 terminates input.
*/
int getTreeNo()
{
int input;
do //Input is to be a do/while loop.
{
cout << "Enter tree number(between 1-199) or 999 to exit: ";
cin >> input;
if ((input < 1 || input > 199) && input != 999)
{
cout << "Invalid number entered.\n";
}
}
while ((input < 1 || input > 199) && input != 999);
return input; //Function is return by value.
}
/*
getSpeciesCode
Species code is type int and
must be valid and found in the species table.
*/
int getSpeciesCode(int Table[], string Name[])
{
int speciesCode;
int found = 0; //initialized to 0
do //Input is a do/while loop.
{
//display Code & description
cout << "\nspeciesCode\t\t\tDescription\n";
for (int i = 0;i<6;i++)
{
114 cout << Table[i] << " " << Name[i] << endl;
}
//ask for input
cout << "\nEnter Species Code: ";
cin >> speciesCode;
//search Table to see if valid speciesCode entered
for (int c = 0;c<6;c++)
{
if (speciesCode == Table[c])
{//if found,
found = 1;
break; //exit for loop
}
}
if (found != 1)
{
cout << "Invalid species code entered.\n";
}
}
while (found != 1);
return speciesCode;//Function is return by value.
}
/*
getDbh
Values are type float and must be greater than
or equal to 5.0 and no greater than 50.6 inches.
*/
float getDbh()
{
double inches;
do //Input is a do/while loop.
{
cout << "Enter the number of inches(between 5 and 50.6): ";
cin >> inches;
if (inches < 5 || inches > 50.6)
{
cout << "Invalid number entered.\n";
}
}
while (inches <5 || inches > 50.6);
return inches;//Function is return by value.
}
/*
getTotHt
Values are type int and can range from 22 to 180.
Total height is always measured as the closest even integer.
*/
int getTotHt()
{
int Ht;//local alias
do //Input is a do/while loop.
{
cout << "Enter the closest even total height(between 22 - 180): ";
cin >> Ht;
if ((Ht < 22 || Ht > 180) && Ht % 2 != 0)
{
cout << "Invalid number entered Note that it must be a even number.\n";
}
}
while ((Ht < 22 || Ht > 180) && Ht % 2 != 0);
return Ht;//Function is return by value.
}
/*
calcTotVol
Calculate total volume (type double) using supplied formula.
totalVol = b0 + (b1 * (dbh*dbh) * totalHt)
Output is 3 decimal places to the right of the decimal.
*/
double calcTotVol(int code, int table[], double b0[], double b1[], float dbh, int totalHt) {
int index;
double totalVol;
//find index
for (int i = 0;i<6;i++)
{
if (code == table[i])
{
index = i;
break;
}
}
//calculate totalVol = b0 + (b1 * (dbh*dbh) * totalHt)
totalVol = b0[index] + (b1[index] * dbh * dbh * totalHt);
return totalVol;//Return by value function.
}
/*
*/
void displayTreeData(ofstream& out, int speciesCode, int treeNo, int table[], string Name[], double dbh, int totalHt, double totalVol) {
int index;
//find index
for (int i = 0; i < 6; i++)
{
/*224*/ if (speciesCode == table[i])
{
/*226*/ index = i;
break;
}
}
cout << "\nTree No. Species Description DBH Total Ht. Total Vol.\n" << endl;
/*232*/ cout << treeNo << " " << Name[index] << " " << setprecision(1) << dbh << " " << totalHt << " " << setprecision(3) << totalVol << endl;
out << "\nTree No. Species Description DBH Total Ht. Total Vol.\n" << endl;
/*234*/ out << treeNo << " " << Name[index] << " " << setprecision(1) << dbh << " " << totalHt << " " << setprecision(3) << totalVol << endl;
return;
}
|