A simple way to create a programm is to use the given text and find the keywords what the computer has to do
and mark anything else as a comment. Voilà you have your first raw modell : a pseudo code!
It is not important to know the exact commands of c++. Write instead a "empty" function which you can fill with the right commands.
The second step is to use the syntax of the language.
/*
Part #1:
Write a program which displays the following menu:
*/
1 2 3 4 5 6
|
DISPLAY(
Geometry Calculator:
1. Calculate the area of a circle
2. Calculate the area of a rectangle
3. Calculate the area of a triangle
4. Quit)
|
/*
If the user enters 1, have the user enter the radius of a circle, then calculate the circle's area.
If the user enters 2, have the user enter the length and width of a rectangle, then calculate its area.
If the user enters 3, have the user enter the length of the triangle's base and its height, and calculate its area.
If the user enters 4, have Part 1 of the program end.
*/
/*
Validations: the user must enter a valid number from the menu, and all other entries must be greater than zero.
*/
1 2 3 4 5
|
ValuateUserInput ()
IF_USERINPUT (1, calcRadius)
IF_USERINPUT (2, calcRectangle)
etc
|
/*
Display the area of the chosen shape after the user selects a choice (areas should be displayed to 3 decimal points).
*/
-----------------------
1 2 3 4 5 6 7 8 9
|
calcRadius()
INPUT ("Radius", radius);
area = pi * radius^2;
PRINT ("Area of circle is: ", area)
calcRectangle ()
INPUT ("Length", length);
area = length^2;
PRINT ("Area of square is: ", area)
|
************************* STEP 2
c++fy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
DISPLAY("Geometry Calculator:");
DISPLAY("1. Calculate the area of a circle");
DISPLAY("2. Calculate the area of a rectangle");
DISPLAY("3. Calculate the area of a triangle");
DISPLAY("4. Quit");
void DISPLAY (char* pszString) {
std::cout << pszString << endl;
}
void INPUT (char* pszCaption, float &flValue) {
DISPLAY (pszCaption);
std::cin >> flValue;
}
|
etc.
Next step is to write layers to the already written and prooved functions
e.g.
1 2 3 4 5 6 7 8 9 10
|
void INPUT (char* pszCaption, float &flValue, bool boCheckAllowedRange, float flRangeFrom, float flRangeTo) {
INPUT (pszCaption, flValue);
if (!boCheckAllowedRange)
return;
if (flValue < flFrom)
return WARNING ("Value is not in range: ", flRangeFrom, flRangeTo);
}
|
etc.
Next step is to shrink the parameter lists and use class or structs
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
|
struct INPUT_DATA {
public:
char* pszCaption;
float flValue;
bool boCheckAllowedRange;
float flRangeFrom;
float flRangeTo;
InputData(
char* _pszCaption,
float* _ptrflValue,
bool _boCheckAllowedRange,
float _flRangeFrom,
float _flRangeTo) {
pszCaption = _pszCaption;
flValue = _flValue;
boCheckAllowedRange = _boCheckAllowedRange;
flRangeFrom = _flRangeFrom;
flRangeTo = _flRangeTo;
}
};
void INPUT (INPUT_DATA &inputData) {
INPUT (inputData.pszCaption, inputData.flValue);
if (!inputData.boCheckAllowedRange)
return;
if (inputData.flValue < inputData.flFrom)
return WARNING ("Value is not in range: ", flRangeFrom, flRangeTo);
}
main:
INPUT_DATA inputData (
"Input radius",
1.0, // default value
true, // check range
0.1,
9.9);
INPUT (inputData);
|
there are more steps, but this is enough for the beginning. ^^
ok, a little trailer:
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
|
class InputCalcOutput_Common {
public:
string strCaption;
void Input (int &v);
void Input (float &v);
void Input (string &v);
void Output (string sCaption, string &v);
virtual CheckRange (void) = 0;
};
class InputCalcOutput_Radius : public InputCalcOutput_Common {
public:
float flValue;
float flMin, flMax;
float flArea, flCircum;
void Input (bool boCheckRange = false) {
Input (flValue);
if (boCheckRange)
CheckRange();
}
void CheckRange () {
if (flValue < flMin || flValue > flMax)
throw exception ("Range invalid!");
}
void Calculate () {
flArea = flValue * ...;
flCircum = flValue * ...;
}
void Output () {
Output("Area: ", flArea);
Output("Circum: ", flCircum);
}
void Execute() {
Input();
Calculate();
Output();
}
};
|