CSC112 Spring 2016 Examples
//implmentation of the shape helper function1#include "shape.h"2#include "square.h"3#include "rectangle.h"4#include "tty_functions.h"5#include "termmanip.h"6#include <iostream>7#include <unistd.h>8#include <cstdio>910using namespace std;1112//clear our menu area13static void clearMenuArea(int firstLine) {14//clear the three lines of the menu area15for(int i=firstLine; i<firstLine+3; i++) {16cout << cursorPosition(1, i) << clearLine;17}18cout.flush();19}2021shape*22getUserShape() {23const char *shapeNames[] = { "Square", "Rectangle", "Triangle", "Quit" };24int n = sizeof(shapeNames) / sizeof(shapeNames[0]); //number of shapes25int i;26int x, y;27int choice;28ttySize screenSize = ttyGetSize(STDOUT_FILENO);29shape *result;30int firstLine = screenSize.rows - 3;3132//display the menu33clearMenuArea(firstLine);34x=1;35y=firstLine;36for(i=0; i<n; i++) {37cout << cursorPosition(x, y)38<< i+1 << ".) " << shapeNames[i];3940//update y41y++; //move down a notch42//handle end of the column43if(y>firstLine+1) {44y = firstLine; //go back up45x+= 20; //move over46}47}4849//get the user choice50cout << cursorPosition(1, firstLine+2) << "Choice: ";51cin >> choice;5253//perform the choice54clearMenuArea(firstLine);55switch(choice) {56case 1: //Square57result = new square;58result->getUserParameters(firstLine);59break;60case 2: //Rectangle61result = new rectangle;62result->getUserParameters(firstLine);63break;64case 3: //Triangle65result = 0x0; //TODO Implement triangle66break;67default:68result = 0x0; //quit case69}7071return result;72}737475