Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

CSC112 Spring 2016 Examples

2370 views
1
//implmentation of the shape helper function
2
#include "shape.h"
3
#include "square.h"
4
#include "rectangle.h"
5
#include "tty_functions.h"
6
#include "termmanip.h"
7
#include <iostream>
8
#include <unistd.h>
9
#include <cstdio>
10
11
using namespace std;
12
13
//clear our menu area
14
static void clearMenuArea(int firstLine) {
15
//clear the three lines of the menu area
16
for(int i=firstLine; i<firstLine+3; i++) {
17
cout << cursorPosition(1, i) << clearLine;
18
}
19
cout.flush();
20
}
21
22
shape*
23
getUserShape() {
24
const char *shapeNames[] = { "Square", "Rectangle", "Triangle", "Quit" };
25
int n = sizeof(shapeNames) / sizeof(shapeNames[0]); //number of shapes
26
int i;
27
int x, y;
28
int choice;
29
ttySize screenSize = ttyGetSize(STDOUT_FILENO);
30
shape *result;
31
int firstLine = screenSize.rows - 3;
32
33
//display the menu
34
clearMenuArea(firstLine);
35
x=1;
36
y=firstLine;
37
for(i=0; i<n; i++) {
38
cout << cursorPosition(x, y)
39
<< i+1 << ".) " << shapeNames[i];
40
41
//update y
42
y++; //move down a notch
43
//handle end of the column
44
if(y>firstLine+1) {
45
y = firstLine; //go back up
46
x+= 20; //move over
47
}
48
}
49
50
//get the user choice
51
cout << cursorPosition(1, firstLine+2) << "Choice: ";
52
cin >> choice;
53
54
//perform the choice
55
clearMenuArea(firstLine);
56
switch(choice) {
57
case 1: //Square
58
result = new square;
59
result->getUserParameters(firstLine);
60
break;
61
case 2: //Rectangle
62
result = new rectangle;
63
result->getUserParameters(firstLine);
64
break;
65
case 3: //Triangle
66
result = 0x0; //TODO Implement triangle
67
break;
68
default:
69
result = 0x0; //quit case
70
}
71
72
return result;
73
}
74
75