1. #include <fstream>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <conio.h>
using namespace std;
//Function reads characters from the user and controls that user
enters integer number
bool read_integer(istream& fin,int &number);
//Function loads array from txt file of special format
void load_from_file(int a[],const char filename[]);
//Function save array to txt file of special format
void save_to_file(int a[],const char filename[]);
//Prints array and the active element
void print_array(int a[]);
//Function prints menu of the program
void print_menu();
//Function will insert an integer before the selected integer and
makes the newly inserted integer active.
void insert(int a[],int num_array);
//Function deletes the active integer.
void delete_active(int a[]);
//Function sorts the list in ascending order
void sort(int a[]);
//Function selects the next integer in the list.
void select(int a[]);
//Function moves the selected integer one position to the right
void move_right(int a[]);
//Function moves the selected integer one position to the left
void move_left(int a[]);
int main()
{
const int MAX_LENGTH = 10; // defines the maximum number
of elements in the third string
int values[MAX_LENGTH+2];
2. char scanCode;
// load information from the file
load_from_file(values,"numbers.txt");
// cycle for user dialog
do
{
// print menu and array
print_menu();
print_array(values);
scanCode = getch(); // read pressed key
if (scanCode == ' ')
scanCode = getch(); // clean ' '
if ((scanCode == 0)||(scanCode == 224)) // for extended control
keys
scanCode = getch();
switch (scanCode)
{
case '1': // insert integer before active ('1' or 'insert')
case 82:
insert(values,MAX_LENGTH);
break;
case '2': // delete active ('2' or 'delete')
case 83:
delete_active(values);
break;
case '3': // sort values (3 or F2)
case 60 :
sort(values);
break;
case '4': // select element ('4' or 'down arrow')
case 80:
select(values);
break;
case '5': // move right ('5' or 'right arrow')
case 77:
move_right(values);
3. break;
case '6': // move left ('6' or 'left arrow')
case 75:
move_left(values);
break;
case '7': // exit the program ('7' or 'F1')
case 59:
// save array to file
save_to_file(values,"numbers.txt");
//exit
cout<<"End of program"<<endl;
return 0;
break;
}
} while ((scanCode!='7')&&(scanCode!=59)); // 59 is 'F1'
code
return 0;
}
void new_line(istream& fin)
{
char symbol;
do
{
fin.get(symbol);
} while (symbol != ' ');
}
bool read_integer(istream& fin,int &number)
{
char symbol;
number = 0; // initialize the number
bool good_int;
good_int = true;
fin.get(symbol);
if (symbol == ' ')
fin.get(symbol); // clean if we have ' '
while (symbol !=' ')
4. {
if (isdigit(symbol)) // correction of the number
number = number*10 + (symbol-'0');
else // user enters forbidden symbol
good_int = false;
fin.get(symbol);
}
return good_int;
}
void load_from_file(int a[],const char filename[])
{
ifstream fin;
fin.open(filename);
if (fin.fail( ))
{
cout << "Input file opening failed. ";
exit(1);
}
fin >> a[0]; // read number of elements in the list
fin >> a[1]; // read index of active element
// read elements of the list
for (int i=0; i<a[0]; i++) // first 2 elements are length and active
element
{ // so elements of 3rd line in
array[2],array[3]...array[MAX_LENGTH+1]
fin >> a[i+2];
}
fin.close(); // close stream
return;
}
void save_to_file(int a[],const char filename[])
{
ofstream fout;
fout.open(filename);
if (fout.fail())
{
5. cout << "Output file opening failed. ";
exit(1);
}
fout << a[0] << endl; // write length of list to file
fout<< a[1] << endl; // write index if active element to file
// write elements of list to file, separated by space character
for (int i = 0; i < a[0]; i++)
fout<<a[i+2]<<' ';
fout<<endl;
fout.close(); //close out file
return;
}
void print_array(int a[])
{
cout<<endl;
// check length of the array
if (a[0] == 0)
cout<<"empty";
for (int i = 1; i <= a[0]; i++)
if (a[1]== i) // number of active element
cout<<"["<<a[i+1]<<"] ";
else
cout<< a[i+1]<< " ";
// array[1] indicates the number of active element in line,
starting from 1
cout<<endl;
}
void print_menu()
{
// clear screen system("cls");
system("cls"); // for(int i=0;i<25;i++) cout<<endl;
cout.setf(ios::left);
cout <<setw(15)<<"1.Insert"<<""Insert" key"<<endl;
cout <<setw(15)<<"2.Delete"<<""Delete" key "<<endl;
cout <<setw(15)<<"3.Sort"<<""F2" key "<<endl;
cout <<setw(15)<<"4.Select"<<""Down Arrow" key
6. "<<endl;
cout <<setw(15)<<"5.Move Right"<<""Right Arrow" key
"<<endl;
cout <<setw(15)<<"6.Move Left"<<""Left Arrow" key
"<<endl;
cout <<setw(15)<<"7.Exit"<<""F1" key"<<endl<<endl;
}
void insert(int a[],int num_array)
{
int value;
bool result;
// checking, that list is full
if (a[0]==num_array)
{
cout<<"List of integers is full. Insertion is not
possible."<<endl;
return;
}
do
{
cout<<"Enter inserted integer : ";
result = read_integer(cin,value); // read integer with control
if (!result)
cout<<"You entered a wrong integer value"<<endl;
} while (!result);
// shift all elements after active one position right
for (int i = a[0]; i >= a[1]; i--)
a[i+2] = a[i+1]; // +2 correction (first two elements are amount
and active element)
// check if array was empty (active was not selected)
if (a[1] == 0)
a[1] = 1;
// insert new element at active position
a[a[1]+1] = value;
// increase the total amount of elements in array
a[0] ++;
7. return;
}
void delete_active(int a[])
{
if (a[0]==0)
{
cout<<"List of integers is empty. Deletion is not
possible."<<endl;
return;
}
// shift active element and all elements after active one position
left
for (int i= a[1]; i < a[0]; i++)
a[i+1] = a[i+2];
// decrease the total amount of elements in array
a[0] --;
// correct the index of active element
if (a[1]>a[0])
a[1] = a[0];
return;
}
void sort(int a[])
{
if (a[0]<=1) // sorting illegal (one, or none elements in the list)
return;
int active = a[ a[1] + 1 ]; // save active integer element
// bubble sort of elements array[2]...array[ array[0] ]
for (int i = a[0] + 1 ; i > 2; i--)
for (int j = 2; j < i; j++)
if (a[j] > a[j+1])
{
int temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
// search new index of saved active integer
8. for (int i = 2; i <= a[0] + 1; i++)
if (a[i] == active)
{
a[1] = i - 1; // write index of active integer
break;
}
return;
}
void select(int a[])
{
if (a[0]<=1) // selection illegal (one, or none elements in the
list)
return;
// if the last integer is selected
if (a[1] == a[0])
a[1] = 1; // select the first item in the array
else
a[1]++; // select next element in the list
return;
}
void move_right(int a[])
{
if (a[1]==a[0]) // move right is illegal (if the last integer is
selected)
return;
int index = a[1]; // index of active element
// swap active element (index in array[1]) with the next element
int tmp = a[index+1];
a[index+1] = a[index+2];
a[index+2] = tmp;
// correct index of active element
a[1]++;
return;
}
void move_left(int a[])
{
9. // if the first integer is selected (array[1] = 1) or there is not
selected element (array[1] = 0)
if (a[1]<=1) // move left is illegal
return;
int index = a[1]; // index of active element
// swap active element (index in array[1]) with the previous)
int tmp = a[index];
a[index] = a[index+1];
a[index+1] = tmp;
// correct index of active element
a[1]--;
return;
}
bool read_integer(istream& fin,int &number)
{
char symbol;
number = 0; // initialize the number
bool good_int;
good_int = true;
fin.get(symbol);
if (symbol == ' ')
fin.get(symbol); // clean if we have ' '
while (symbol !=' ')
{
if (isdigit(symbol)) // correction of the number
number = number*10 + (symbol-'0');
else // user enters forbidden symbol
good_int = false;
fin.get(symbol);
}
return good_int;
}
I can input all positive intergal in this program, but I cannot
input negative intergal. this program require that i just allow to
input integer, and i need to input both negative and positive
integer. what should I do? CAUsers Roder Desktopl Project
10. 2.exe Insert" key "Delete" key F2" key Down Arrow" key 1.
Insert 2. Delete 3. Sort 4. Select 5. Move Right "Right Arrow"
key 6. Move Left 7. Exit "Left Arrow" key "F1" key -8-3 0 1
5 [12] 22 55 66 Enter inserted integer 8 You entered a wrong
integer value Enter inserted integer:
Solution
The below function has an actual problem in reading negative
numbers. The function is designed to check the weather the
entered number is positive or not a number.
It reads each and every character and checks weather it is digit
or not if it's not a digit then function return false to the insert
function.
If you enter 345 as input then reads 3,4 and 5 individually and
checks digit or not. In your case if you enter -8 function reads
'-' and 8 individually. '-' is not a digit hence your function
return false.
Here just modify the function as given below
bool read_integer(istream& fin,int &number)
{
char symbol;
number = 0; // initialize the number
bool good_int;
11. good_int = true;
fin.get(symbol);
if (symbol == ' ')
fin.get(symbol); // clean if we have ' '
int count=0; // count variable to check first char '-' or not
int negNumber=0;
while (symbol !=' ')
{
if(count==0) // checking first character weather enterd '-' or
not
{
if(symbol=='-') // if it's negative number
{
fin.get(symbol); // next char
negNumber=1; // detecting negative number
count++; //incrementing count
}
else //if it's positive normal operation
{
if (isdigit(symbol)) // correction of the number
number = number*10 + (symbol-'0');
else // user enters forbidden symbol
good_int = false;
fin.get(symbol);
count++;
12. }
}
else // for the next chars as usual operation
{
if (isdigit(symbol)) // correction of the number
number = number*10 + (symbol-'0');
else // user enters forbidden symbol
good_int = false;
fin.get(symbol);
count++;
}
}
if(negNumber==1)
number=number-(2*number); //loading negative number using
basic formula ; i.e 10-2*10=-10;
return good_int;
}