Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- lake.txt ფაილი შეიცავს ინფორმაციას ტბების შესახებ: სახელი, ადგილმდებარეობა, ფართობი, მაქს. სიღრმე.
- ტბების რაოდენობა არ აღემატება 45-ს. დაწერეთ პროგრამა, რომელიც sorted.txt ფაილში დაწერს სიღრმის კლებადობით
- დალაგებულ მონაცემებს.
- ამოცანის ამოსახსნელა: შექმენით Lake კლასი. შემოიღეთ თქვენი აზრით საჭირო ფუნქციები. კლასის ობიექტისთვის გადატვირთეთ
- >> და << ოპერატორები. მონაცემების ფაილიდან წასაკითხად და ფაილში ჩასაწერად გამოიყენეთ გლობალური ფუნქციები.
- main-ში გააკეთეთ განაცხადი ტბები დინამიკურ მასივზე (გამოიყენეთ new ოპერატორი). ჩაწერეთ მასში მონაცემები lake.txt
- ფაილიდან, დაალაგეთ მასივი მოთხოვნის შესაბამისად სტანდარტული ბიბლიოთეკის ალგორითმით. დალაგებული მასივი
- დაბეჭდეთ sorted.txt ფაილში და ბოლოს გაათავისუფლეთ დაკავებული დინამიკური მეხსიერება
- */
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <fstream>
- using namespace std;
- class Lake{
- private:
- string name, location;
- double area, depth;
- public:
- Lake();
- ~Lake(){}
- double getDepth(){ return depth; }
- friend istream& operator>> (istream&, Lake&);
- friend ostream& operator<<(ostream&, Lake&);
- };
- Lake::Lake(){
- name = ""; location = "";
- area = 0; depth = 0;
- }
- istream& operator>> (istream& in, Lake& lake){
- return in >> lake.name >> lake.location >> lake.area >> lake.depth;
- }
- ostream& operator<<(ostream& out, Lake& lake) {
- return out << lake.name << " " << lake.location << " " << lake.area << " " << lake.depth << endl;
- }
- int fillDate(Lake * lakes){
- ifstream ifs("lake.txt");
- int count(0);
- while (!ifs.eof()){
- ifs >> lakes[count];
- count++;
- }
- return count;
- }
- void writeToFile(Lake * lakes, int size){
- ofstream ofs("sorted.txt");
- for (int i = 0; i < size; i++){
- ofs << lakes[i];
- }
- }
- bool compare(Lake& lake1, Lake& lake2){
- return lake1.getDepth() > lake2.getDepth();
- }
- int main(){
- Lake* lakes;
- lakes = new Lake[45];
- int size = fillDate(lakes);
- sort(lakes, lakes + size, compare);
- writeToFile(lakes, size);
- delete[] lakes;
- lakes = NULL;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement