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
|
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
char usrinpt(42), prusript(42), a(42);
unsigned pntr(0), display[99], timesec[1], delta, maxdelta(3);
unsigned startwith[] {32, 44, 65, 68, 71, 74, 77, 80, 84, 87};
cout << "Enter single digits 1..9, one at a time to compose letters,\n";
cout << "'x' to discard the last one; 'c' to restart; 'q' to quit.\n\n";
timesec[0] = time(0) - maxdelta - 1; // runin cycle
for (;;) // do forever
{
cin >> usrinpt;
timesec[1] = time(0);
delta = timesec[1] - timesec[0];
switch (usrinpt)
{
case 'x':
if (pntr > 0) --pntr;
break;
case 'c':
pntr = 0;
break;
case 'q':
return(0);
default:
switch ((usrinpt == prusript? 0 : 1)+(delta <= maxdelta? 0 : 2))
{
case 0:; // input repeated within delay
{
if (usrinpt > 49 && usrinpt < 58) // 2..9 only, 0 and 1 later
display[pntr] += 1; // replace last entry with next letter
else if (usrinpt == 48) // 0 is for blank only, disregarding delay
display[++pntr] = 32; // increment pointer _before_ asignment
else if (usrinpt == 49) // 1 is for some punctuation
switch (display[pntr])
{
case 44:
display[pntr] = 46;
break;
case 46:
display[pntr] = 58;
break;
case 58:
display[pntr] = 59;
break;
default:
display[pntr] = 44;
} // punctuation
else
cout << "Programmers fault, input not handled: " << usrinpt << "\n";
break;
} // same entry within delay
default: // different input than before and/or (vel) delay expired
pntr += 1; // increment pointer
if (usrinpt > 49 && usrinpt < 58) // 2..9 only, 0 and 1 later
display[pntr] = startwith[usrinpt - 48]; // append new char
else if (usrinpt == 48) // 0 is for blank only
display[pntr] = 32;
else if (usrinpt == 49) // 1 is for punctuation
display[pntr] = 44;
else
cout << "Programmers fault, input not handled: " << usrinpt << "\n";
} // reduced 4 cases
/* default: // should never come here */
} // user input including commands c, q, and x
timesec[0] = timesec[1];
prusript = usrinpt;
cout << "Display: ";
for (uint i = 0; i <= pntr; i++)
{
a = display[i];
cout << a;
}
cout << "\n";
} // do forever
}
|