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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
ifndef GRAPHLIB_H
define GRAPHLIB_H
// Graphics library interface
const int NROWS = 20;
const int NCOLS = 30;
--------------------------------------------------------------------
void clearGrid();
void setChar(int r, int c, char ch);
char getChar(int r, int c);
void draw();
endif / GRAPHLIB_H /
include "graphlib.h"
include <iostream>
include <iomanip>
include <string>
include <cstdlib>
include <cctype>
using namespace std;
char grid[NROWS][NCOLS]; bool isGridInitialized = false;
void drawHorizBorder(); void checkPos(int r, int c, string func);
void clearGrid() {
for (int r = 0; r < NROWS; r++)
for (int c = 0; c < NCOLS; c++)
grid[r][c] = ' ';
isGridInitialized = true;
}
void setChar(int r, int c, char ch) {
checkPos(r, c, "setChar");
grid[r-1][c-1] = ch;
}
char getChar(int r, int c) {
checkPos(r, c, "getChar");
return grid[r-1][c-1];
}
void draw() {
// Write header lines of column numbers
cout << " ";
for (int c = 1; c <= NCOLS; c++)
{
int t = c / 10;
if (t == 0)
cout << ' ';
else
cout << t;
}
cout << "\n ";
for (int c = 1; c <= NCOLS; c++)
cout << (c%10);
cout << "\n";
// Draw grid
drawHorizBorder();
for (int r = 1; r <= NROWS; r++)
{
cout << setw(2) << r << '|';
for (int c = 1; c <= NCOLS; c++)
{
char ch = grid[r-1][c-1];
if (isprint(ch))
cout << ch;
else
cout << '?';
}
cout << "|\n";
}
drawHorizBorder();
}
void drawHorizBorder() {
cout << " +";
for (int c = 1; c <= NCOLS; c++)
cout << '-';
cout << "+\n";
}
void checkPos(int r, int c, string func) {
if (!isGridInitialized)
{
cout << func << ": You must first call clearGrid." << endl;
exit(1);
}
if (r < 1 || r > NROWS || c < 1 || c > NCOLS)
{
cout << func << ": invalid position (" << r << "," << c << ")"
<< endl;
exit(1);
}
}
-----------------------------------------------------------------------------------
include "graphlib.h"
include <iostream>
include <string>
using namespace std;
int r, c, l; char ch; string cmd;
void setChar(int r, int c, char ch);
void plotHorizontalLine(int r, int c, int l, char ch);
int main() {
clearGrid();
cout << "Enter command string: ";
getline(cin, cmd);
if (cmd == "")
{
return 0;
}
if (cmd == "h")
plotHorizontalLine(10, 10, 10, '-');
draw();
}
void plotHorizontalLine(int r, int c, int l, char ch)
{
for (int k = 1; k <= l; k++)
{
setChar(r, c+k, ch);
}
}
void plotVerticalLine(int r, int c, int l, char ch)
{
for (int k = 1; k <= l; k++)
{
setChar(r+k, c, ch);
}
}
|