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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#include <fstream>
#include <iostream>
using namespace std;
fstream F;
class Array {
public:
void setV() { cout << "Enter value ::: "; cin >> value; }
void setVA(int value) { this->value = value; }
const int getV() const { return this->value; }
Array(int value = NULL) : value(value) {}
Array(const Array& Obj) : value(Obj.value) {}
~Array(){}
friend ostream &operator<<(ostream&out, Array&Obj);
friend void display(const Array X[], int n);
friend void create(Array X[], int n);
friend void load(const Array X[], int n);
friend void save (const Array X[], int n);
protected:
int value;
int max(const Array X[], int n)
{
int max = X[0].getV();
for (int i = 1; i < n; i++)
if (max < X[i].getV())
max = X[i].getV();
return max;
}
int min(const Array X[], int n)
{
int min = X[0].getV();
for (int i = 1; i < n; i++)
if (min > X[i].getV())
min = X[i].getV();
return min;
}
int sum(const Array X[], int n)
{
int sum = NULL;
for (int i = 0; i < n; ++i)
sum += X[i].getV();
return sum;
}
public:
void menu(Array A[], int n)
{
system("cls");
int option;
do {
cout << " \n\n\t\t ******* MENU FILE MANAGEMENT *******\n";
cout << " \t\t * *\n ";
cout << " \t\t * 1. Create array of objects *\n";
cout << " \t\t * 2. Display array of objects *\n";
cout << " \t\t * 3. Save file *\n";
cout << " \t\t * 4. Load file *\n";
cout << " \t\t * 5. Sum of array *\n";
cout << " \t\t * 6. Maximum value *\n";
cout << " \t\t * 7. Minimum value *\n";
cout << " \t\t * 0. Exit *\n";
cout << " \t\t * *\n";
cout << " \t\t ************************************\n";
cout << " \n\n\t\t\t Enter option number: ";
cin >> option;
switch (option)
{
case 0: exit(0);
case 1: system("cls"); create(A, n); system("pause"); break;
case 2: system("cls"); display(A, n); system("pause"); break;
case 3: system("cls"); save(A, n); system("pause"); break;
case 4: system("cls"); load(A, n); system("pause"); break;
case 5: system("cls"); cout << "The sum of values is " << sum(A, n) << '\n'; break;
case 6: system("cls"); cout << "The maximum value is " << max(A, n) << '\n'; break;
case 7: system("cls"); cout << "The minimum value is " << min(A, n) << '\n'; break;
default: cout << "Invalid option\n";
}
} while (option != 0);
}
};
ostream& operator<<(ostream& out, const Array& Obj)
{
return out << "Array =" << Obj.getV();
}
istream& operator<<(istream& in, Array& Obj)
{
cout<< "Enter Array =";
return in >> Obj.value;
}
void display(const Array X[], int n)
{
for (int i = 0; i < n; i++)
cout << "\nA " << "[" << i << "]" << X[i].getV();
}
void create(Array X[], int n)
{
for (int i = 0; i < n; i++)
X[i].setV();
}
void save(const Array X[], int n){
F.open("deep.txt", ios::out );
if( F.fail() ) cerr<<" Error Fstream FILE "<<endl;
else cout<<" OK Fstream FILE "<<endl;
for (int i = 0; i < n; i++)
F <<' '<< X[i].getV();
F.close();
}
void load(const Array X[], int n)
{
F.open("deep.txt", ios::in );
if( F.fail() ) cerr<<" Error Fstream FILE "<<endl;
else cout<<" OK Fstream FILE "<<endl;
for (int i = 0; i < n; i++)
{
int val[10];
F >> val;
X[i].setVA(val); }
F.close();
}
F.close();
}
int main()
{
int N;
cout << "Enter number of the elements ::: "; cin >> N;
Array A[10],menu;
menu.menu(A, N);
}
| |