Advertisement
Jgug

l7_v4

Dec 23rd, 2012
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.08 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "stdafx.h"
  3. #include<iostream>
  4. #include<fstream>
  5. #include<conio.h>
  6. #include<string.h>
  7. #include<vector>
  8. #define SIZE 256
  9.  
  10. using namespace std;
  11.  
  12. double sum = 0;
  13. int time = 0;
  14.  
  15. class City
  16. {
  17. protected:
  18.     char* name;
  19.     char* date;
  20.     char* code;
  21.     char* phoneSubs;
  22.     char* phone;
  23. private:
  24.     virtual void printInfo() = 0;
  25. };
  26.  
  27. class Call
  28. {
  29. protected:
  30.     int duration;
  31.     double tariff;
  32. };
  33.  
  34. class Info: public City, Call
  35. {
  36. public:
  37.     Info();
  38.     void set(char*, char*, char*, char*, char*, int, double);
  39.     void run();
  40.     void printInfo();
  41.     void printSumary();
  42.     void filePrintCityInfo();
  43.     void filePrintCitySummary();
  44.     void fileShowInfo();
  45.     void fileDelte();
  46. };
  47.  
  48. Info::Info()
  49. {
  50.     name = new char [SIZE];
  51.     code = new char [SIZE];
  52.     phoneSubs = new char [SIZE];
  53.     phone = new char [SIZE];
  54.     date = new char [SIZE];
  55. };
  56.  
  57. void Info::set(char* name_, char* code_, char* phoneSubs_, char* phone_, char* date_, int duration_, double tariff_)
  58. {
  59.     strcpy(name, name_);
  60.     strcpy(code, code_);
  61.     strcpy(phoneSubs, phoneSubs_);
  62.     strcpy(phone, phone_);
  63.     strcpy(date, date_);
  64.     duration = duration_;
  65.     tariff = tariff_;
  66. };
  67.  
  68. void Info::run()
  69. {
  70.     time += duration;
  71.     sum += (duration * tariff);
  72. };
  73.  
  74. void Info::printInfo()
  75. {
  76.     cout<<"_____City__Info_____"<<endl;
  77.     cout<<"Name: "<<name<<endl;
  78.     cout<<"Code: "<<code<<endl;
  79.     cout<<"Subscriber's phone number: "<<phoneSubs<<endl;
  80.     cout<<"Destination phone number: "<<phone<<endl;
  81.     cout<<"Date of a call: "<<date<<endl;
  82.     cout<<"Duration of a call: "<<duration<<endl;
  83.     cout<<"Tariff of a call: "<<tariff<<endl;
  84.     cout<<"____________________"<<endl;
  85. };
  86.  
  87. void Info::printSumary()
  88. {
  89.     cout<<"_____City__Summary__"<<endl;
  90.     cout<<"Total duration: "<<time<<endl;
  91.     cout<<"Total value: "<<sum<<endl;
  92.     cout<<"____________________"<<endl;
  93. };
  94.  
  95. void Info::filePrintCityInfo()
  96. {
  97.     ofstream file("cityInfo.txt", ios::app);
  98.     file<<name<<" "<<code<<" "<<phoneSubs<<" "<<phone<<" "<<date<<" "<<duration<<" "<<tariff<<endl;
  99.     file.close();
  100. };
  101.  
  102. void Info::filePrintCitySummary()
  103. {
  104.     ofstream file("cityInfo.txt", ios::app);
  105.     file<<endl<<name<<" "<<time<<" "<<sum<<endl;
  106.     file.close();
  107. };
  108.  
  109. void Info::fileShowInfo()
  110. {
  111.     try
  112.     {
  113.         char word[160];
  114.         ifstream fileout("cityInfo.txt", ios::_Nocreate);
  115.         if (fileout.fail())
  116.         {
  117.             throw 4;
  118.         }
  119.         cout<<"Summary city info: "<<endl;
  120.         cout<<"--------------------------------------------"<<endl;
  121.         while ( !fileout.eof() )
  122.         {
  123.             fileout.getline(word,159);
  124.             cout<<word<<endl;
  125.         }
  126.         cout<<endl;
  127.         fileout.close();
  128.     }
  129.     catch (int x)
  130.     {
  131.         cout<<"Requesting file dose not exist or have enother name! ERROR "<<x<<endl;
  132.     }
  133.     system("pause");
  134. };
  135.  
  136. void Info::fileDelte()
  137. {
  138.     try
  139.     {
  140.         ifstream fileout("cityInfo.txt", ios::_Nocreate);
  141.         if (fileout.fail())
  142.         {
  143.             throw 4;
  144.         }
  145.         else
  146.         {
  147.             fileout.close();
  148.             remove("cityInfo.txt");
  149.             cout<<"Requires file deleted"<<endl;
  150.         }
  151.     }
  152.     catch (int x)
  153.     {
  154.         cout<<"Requesting file dose not exist or have enother name! ERROR "<<x<<endl;
  155.     }
  156.     system("pause");
  157. };
  158.  
  159. int menu()
  160. {
  161.     system("cls");
  162.     cout<<endl;
  163.     cout<<"1. Add city & city info"<<endl;
  164.     cout<<"2. Output city info"<<endl;
  165.     cout<<"3. Output city summary"<<endl;
  166.     cout<<"4. Write summary info to file"<<endl;
  167.     cout<<"5. Print info from file"<<endl;
  168.     cout<<"6. Delete info file"<<endl;
  169.     cout<<"0. Exit"<<endl<<endl;;
  170.     int item;
  171.     cin>>item;
  172.     return item;
  173. }
  174.  
  175. int main()
  176. {
  177.     Info one;
  178.     char name_[64];
  179.     char code_[8];
  180.     char phoneSubs_[16];
  181.     char phone_[16];
  182.     char date_[16];
  183.     int duration_;
  184.     double tariff_;
  185.  
  186.     int chose = menu();
  187.  
  188.     while (chose)
  189.     {
  190.         switch (chose)
  191.         {
  192.         case 1:
  193.             cout<<"Input name, code, phoneSubs, phone, date, duration, tariff: "<<endl;
  194.             cin>>name_;
  195.             //-----
  196.             l1: try
  197.             {
  198.                 char codeTry[8];
  199.                 cin>>codeTry;
  200.                 if (strlen(codeTry) != 5)
  201.                 {
  202.                     throw 1;
  203.                 }
  204.                 else (strcpy(code_, codeTry));
  205.             }
  206.             catch (int x) {cout<<"Incorrect phone code! ERROR "<<x<<endl<<"Try again..."<<endl; goto l1;}
  207.             //-----
  208.             l2: try
  209.             {
  210.                 char phoneSubsTry[16];
  211.                 cin>>phoneSubsTry;
  212.                 if (strlen(phoneSubsTry) != 7)
  213.                 {
  214.                     throw 2;
  215.                 }
  216.                 else (strcpy(phoneSubs_, phoneSubsTry));
  217.             }
  218.             catch (int x) {cout<<"Incorrect phoneSubs number! ERROR "<<x<<endl<<"Try again..."<<endl; goto l2;}
  219.             //-----
  220.             l3: try
  221.             {
  222.                 char phoneTry[16];
  223.                 cin>>phoneTry;
  224.                 if (strlen(phoneTry) != 7)
  225.                 {
  226.                     throw 3;
  227.                 }
  228.                 else (strcpy(phone_, phoneTry));
  229.             }
  230.             catch (int x) {cout<<"Incorrect phone number! ERROR "<<x<<endl<<"Try again..."<<endl; goto l3;}
  231.             //-----
  232.             cin>>date_;
  233.             cin>>duration_;
  234.             cin>>tariff_;
  235.             one.set(name_, code_, phoneSubs_, phone_, date_, duration_, tariff_);
  236.             one.run();
  237.             one.filePrintCityInfo();
  238.             system("pause");
  239.             break;
  240.         case 2:
  241.             one.printInfo();
  242.             system("pause");
  243.             break;
  244.         case 3:
  245.             one.printSumary();
  246.             system("pause");
  247.             break;
  248.         case 4:
  249.             one.filePrintCitySummary();
  250.             break;
  251.         case 5:
  252.             one.fileShowInfo();
  253.             break;
  254.         case 6:
  255.             one.fileDelte();
  256.             break;
  257.         case 0:
  258.             return 0;
  259.             break;
  260.         }
  261.         chose = menu();
  262.     }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement