/* Programmer: Dylan McDonald Class & Section: CS 54, Section Date: 11 September, 2001 Purpose: Break & score balsa wood bridges for the Stimsonian Institute */ #include #include using namespace std; int main() { bool lightEnough; short joints, userChoice = 0; double sticks; double appliedMass = 0.0, breakScore = 0.0, mass, score; /* ask user for bridge specifications */ cout << "Number of joints: "; cin >> joints; cout << "Number of 10 cm sticks: "; cin >> sticks; /* make sure the specifications make sense */ if (joints < 0) joints = -joints; if (sticks == 0) { cout << "You can't build a bridge without sticks. Maybe you've been " << "eating too much glue." << endl; sticks = 4.2; } else if (sticks < 0) sticks = -sticks; /* calculate the mass of the bridge */ mass = 1.5 * sticks + joints / 2.0; cout << "The mass of the bridge is " << mass << " g." << endl; /* see if the bridge qualified */ if (mass <= 25) lightEnough = true; else lightEnough = false; /* score the bridge */ score = (((sticks + joints) * (sticks + joints)) / (mass * sticks)) * lightEnough; if (score > 0.0) cout << "The bridge clocked in at " << score << " points." << endl; else cout << "Surely this is not a bridge over troubled waters!" << endl; /* while the bridge isn't broken and the user doesn't want to exit... */ while (breakScore < (score / 2.0) && userChoice != 5) { /* ...present the menu of options */ do { cout << "Apply Mass to Bridge: " << endl << "1) 0.5 kg" << endl << "2) 1 kg" << endl << "3) 2 kg" << endl << "4) 5 kg" << endl << "5) Exit" << endl; cin >> userChoice; } while (userChoice < 1 || userChoice > 5); /* ...apply the appropriate mass */ if (userChoice == 1) appliedMass += 0.5; else if (userChoice == 2) appliedMass += 1.0; else if (userChoice == 3) appliedMass += 2.0; else if (userChoice == 4) appliedMass += 5.0; /* ...try and break the bridge */ if (userChoice != 5 && score != 0 && mass != 0) { breakScore = (appliedMass * fabs(mass - joints)) / (mass * score * score); cout << "You have a break score of " << breakScore << endl; } } return 0; }