/**************************************************************************/ /* Author: Chang */ /* Aim for: CS135 class */ /* Problem: Write a program that will receive a small pizza size and */ /* price and calculate the price per square inch for the small */ /* size and a large size (4'' larger). Using function. */ /**************************************************************************/ //preprocessor directives #include #include // needed for using pow() function using namespace std; // function prototype double Calculate_Price_Per_Square(double, double); void prettyPrintDouble(double x); void prettyPrintString(string s); int main () { double small_pizza_diameter, large_pizza_diameter, pizza_price, large_pizza_price; double small_unit_price, large_unit_price; // Get Inputs cout << "Enter the diameter of a round pizza (inches): "; cin >> small_pizza_diameter; cout << "Enter the price of the pizza ($): "; cin >> pizza_price; cout << "Enter the price of 4'' larger pizza ($): "; cin >> large_pizza_price; // initialize large_pizza_diameter large_pizza_diameter = small_pizza_diameter + 4; // Function call // Calculate the prices per square inch small_unit_price = Calculate_Price_Per_Square(small_pizza_diameter, pizza_price); large_unit_price = Calculate_Price_Per_Square(large_pizza_diameter, large_pizza_price); // Print outputs prettyPrintString( "Diameter"); prettyPrintString( "Price"); prettyPrintString( "Unit Price"); cout << endl; prettyPrintDouble(small_pizza_diameter); prettyPrintDouble(pizza_price); prettyPrintDouble(small_unit_price); cout << endl; prettyPrintDouble(large_pizza_diameter); prettyPrintDouble(large_pizza_price); prettyPrintDouble(large_unit_price); cout << endl; system ("PAUSE"); return 0; } // function body double Calculate_Price_Per_Square(double d, double p) { const double PI = 3.14; double size; double PricePerSquare; // size = PI * (d * d) / 4.0; size = PI * ( pow(d, 2.0) ) / 4.0; PricePerSquare = p / size; return PricePerSquare; } // function body void prettyPrintDouble(double x){ char tmp[21]; sprintf(tmp, "%15.2lf", x); cout << tmp; return; } // function body void prettyPrintString(string s){ char tmp[21]; sprintf(tmp, "%15s", s.c_str()); cout << tmp; return; }