vakho

[OOP] საკ. 2 – ბილ.2

Jun 15th, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.22 KB | None | 0 0
  1. /*
  2. 1.
  3.     დაწერეთ პროგრამის ფრაგმენტი, რომელიც numbers.txt ფაილში ჩაწერილ 1205 მთელ რიცხვს განათავსებს
  4.     დინამიკურ მეხსიერებაში (გამოიყენეთ new ოპერატორი), შემდეგ ფაილის ბოლოსწინა რიცხვზე 3 –ჯერ მეტ
  5.     რიცხვებს დაბეჭდავს 10 სვეტად. ჩათვალეთ, რომ ასეთი რიცხვები ფაილში არის. (3 ქულა)
  6. */
  7. #include <iostream>
  8. #include <fstream>
  9.  
  10. using namespace std;
  11.  
  12. #define size 1205
  13.  
  14. int main()
  15. {
  16.     system("COLOR F0");
  17.  
  18.     int *ptr = new int[size];
  19.  
  20.     ifstream ifs("numbers.txt");
  21.     for (int i = 0; i < size; i++)
  22.         ifs >> ptr[i];
  23.     ifs.close();
  24.  
  25.     for (int i = 0, int count = 0; i < size; i++)
  26.     {
  27.         if ( (ptr[size - 1] * 3) < ptr[i] )
  28.         {
  29.             cout << setw(5) << left << ptr[i];
  30.             count++;
  31.         };
  32.         if (count % 10 == 0)
  33.             cout << endl;
  34.     };
  35.  
  36.     delete [] ptr;
  37.     ptr = NULL;
  38.     system("PAUSE");
  39.     return (0);
  40. };
  41.  
  42. /*
  43. 2.
  44.     cities.txt ფაილში ჩაწერილია სხვადასხვა ქალაქების მონაცემები – თითოეულის სახელი, დაკავებული
  45.     ტერიტორიის ფართობი (კვ.კმ), მოსახლეობის რაოდენობა (მლნ.), ქუჩების რაოდენობა. ქალაქების
  46.     რაოდენობა არ აღემატება 900–ს. დაწერეთ პროგრამა, რომელიც density.out ფაილში ჩაწერს ყველა
  47.     იმ ქალაქის მონაცემებს, რომელთა მოსახლეობის სიმჭიდროვე არ აღემატება 1000 ად/კმ^2 –ზე.
  48.  
  49.     ამოცანის ამოსახსნელად შექმენით კლასი City, რომელშიც გაითვალისწირეთ თქვენი აზრით საჭირო
  50.     კლასის ფუნქციები. კლასის ობიექტისთვის გადათვირთეთ >> და << ოპერატორები. მონაცემების
  51.     ფაილიდან წასაკითხად და ფაილში ჩასაწერად შექმენით გლობალური ფუნქციები. მოთხოვნილი
  52.     თვისების მქონე ქალაქის დასადგენად ასევე შექმენით ფუნქცია. ინფორმაცია cities.txt ფაილიდან
  53.     განათავსეთ დინამიკურ მეხსიერებაში. (6 ქულა)
  54. */
  55. #include <iostream>
  56. #include <fstream>
  57. #include <string>
  58.  
  59. #define size 900
  60.  
  61. using namespace std;
  62.  
  63. class City
  64. {
  65. private:
  66.     string name;
  67.     int area, population, streets;
  68. public:
  69.     City(){};
  70.     ~City(){};
  71.     friend ostream& operator << (ostream &cout, City &a)
  72.     {
  73.         cout << a.name << a.area << a.population << a.streets;
  74.         return cout;
  75.     };
  76.     friend istream& operator >> (istream &cin, City &a)
  77.     {
  78.         cin >> a.name >> a.area >> a.population >> a.streets;
  79.         return cin;
  80.     };
  81.     friend bool Dencity(City &a)
  82.     {
  83.         return (( a.population / a.area ) <= 1000);
  84.     };
  85. };
  86.  
  87. void WriteInFile(City *A, ofstream &ofs)
  88. {
  89.     for (int i = 0; i < size; i++)
  90.         if ( Dencity(A[i]) )
  91.             ofs << A[i];
  92. };
  93.  
  94. int main()
  95. {
  96.     system("COLOR F0");
  97.  
  98.     City *ptr = new City[size];
  99.  
  100.     ifstream ifs("cities.txt");
  101.     for (int i = 0; i < size; i++)
  102.         ifs >> ptr[i];
  103.     ifs.close();
  104.  
  105.     ofstream ofs("density.out");
  106.     WriteInFile(ptr, ofs);
  107.     ofs.close();
  108.  
  109.     delete [] ptr;
  110.     ptr = NULL;
  111.     system("PAUSE");
  112.     return (0);
  113. };
  114.  
  115. /*
  116. 3.
  117.     შემქენით კლასი book მონაცემებით – წიგნის დასახელება, ავტორის გვარი, წიგნის ღირებულება,
  118.     წიგნის გამოცემის წელი. კლასში შემოიღეთ ფუნქციები – პარამეტრებიანი კონსტრუქტორი, დესტრუქტორი
  119.     და საჭიროების მიხედვით კლასის სხვა ფუნქციები. book კლასის საფუძველზე შექმენით კლასი library_card
  120.     საკუთარი მონაცემებით – კატალოგის ნომერი (მაგალითად 192039) და წიგნის გაცემის რაოდენობა.
  121.     library_card კლასში იქონიეთ ფუნქციები – პარამეტრებიანი კონსტრუქტორი, დესტრუქტორი და საჭიროების
  122.     მიხედვით კლასის სხვა ფუნქციები.
  123.  
  124.     main -ში გააკეთეთ განაცხადი ორივე კლასის თითო–თითო ობიექტზე. დაბეჭდეთ წიგნის მონაცემები და
  125.     ბიბლიოთეკის ბარათის ინფორმაცია. შემდეგ შეცვალეთ წიგნის ღირებულება და კვლავ დაბეჭდეთ
  126.     ბიბლიოთეკის ბარათის ინფორმაცია. (6 ქულა)
  127. */
  128. #include <iostream>
  129. #include <iomanip>
  130. #include <string>
  131.  
  132. using namespace std;
  133.  
  134. class book
  135. {
  136. protected:
  137.     string name, author_surname;
  138.     double price;
  139.     int release_year;
  140. public:
  141.     book(string n = "noname", string as = "unknown", double p = 0.0, int ry = 0)
  142.     :name(n), author_surname(as), price(p), release_year(ry){};
  143.     ~book(){};
  144.     void SetPrice(double pr = 0.0)
  145.     {
  146.         price = pr;
  147.     };
  148.     void printInfo()
  149.     {
  150.         cout << setw(5) << left << this->name << "\t" << this->author_surname << "\t" << this->price << "\t" << this->release_year << endl;
  151.     };
  152.  
  153. };
  154.  
  155. class library_card : public book
  156. {
  157. private:
  158.     int catalogue_number, give_aways;
  159. public:
  160.     library_card(string n = "noname", string as = "unknown", double p = 0.0, int ry = 0, int cn = 0, int ga = 0)
  161.     :book(n, as, p, ry), catalogue_number(cn), give_aways(ga){};
  162.     ~library_card(){};
  163.     void printInfo()
  164.     {
  165.         cout << setw(5) << left << this->name << "\t" << this->author_surname << "\t" << this->price << "\t" << this->release_year << "\t" << this->catalogue_number << "\t" << this->give_aways << endl;
  166.     };
  167. };
  168.  
  169. int main()
  170. {
  171.     system("COLOR F0");
  172.  
  173.     book BK("CPP", "Straustrup", 100.99, 2012);
  174.     library_card LC("CPP", "Straustrup", 100.99, 2012, 123456, 12);
  175.     BK.printInfo();
  176.     LC.printInfo();
  177.  
  178.     LC.SetPrice(17.5);
  179.     LC.printInfo();
  180.  
  181.     system("PAUSE");
  182.     return (0);
  183. };
Advertisement
Add Comment
Please, Sign In to add comment