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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
const int size=5;
void initialize (char text [size], int& current);
void move (int& current, int distance);
void insert (char text [size], int& current, char ch);
void replace (char text [size], int current, char ch);
void print (char text [size], int current);
int main()
{
char array[size];
char ch;
int distance;
int current=0;
char character;
initialize (array, current);
do
{cout << "Enter p (print), m (move), i (insert), r (replace) or q (quit): ";
cin >> character;
cout << endl;
if(character=='p')
print (array, current);
else if (character=='m')
{ cout << "Enter the distance to move: ";
cin >> distance;
move (current, distance);
}
else if (character=='i')
{
cout << "Enter the character to insert: ";
cin >> ch;
cout << endl;
cout << "Character " <<ch<< " inserted at index " << current<< endl;
cout << endl;
insert (array, current, ch);
}
else if (character=='r')
{
cout << "Enter the character to replace: ";
cin >> ch;
cout << endl;
cout << "Character " <<ch<< " placed at index " <<current<< endl;
replace (array, current, ch);
cout << endl;
}
}
while (character!='q');
return EXIT_SUCCESS;
}
void initialize (char text [size], int& current)
{
current=0;
int index;
for (index=0; index<size; index++)
{
text[index]='*';
}
}
void move (int& current, int distance)
{
if (distance+current>size-1)
{
cout << "Error, new position "<<distance<<" is illegal!"<< endl;
cout << endl;
}
else if (distance+current<0)
{
cout <<"Error, new position "<< distance<<" is illegal!"<< endl;
cout << endl;
}
else
{
(current=current+distance);
cout <<"current index changed to: "<< distance<< endl;
cout << endl;
}
}
void replace (char text [size], int current, char ch)
{
text[current]=ch;
}
void insert (char text [size], int& current, char ch)
{
int index;
if (current<size-1)
{
for (index=size-1; index>current; index--);
{
text[index]=ch;
text[current-1]=ch;
current++;
}
}
else
cout << "Error, insertion is not allowed" << endl;
}
void print (char text [size], int current)
{
int i;
cout << "The current array is: " << endl;
for (i=0; i<size; i++)
cout << text[i];
cout << endl;
cout << setw(current+1)<<"^" << endl;
cout << endl;
}
|