Xom9ik

Lab_1/1 var (IIl semester)

Nov 5th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.09 KB | None | 0 0
  1. //Lab_1.cpp
  2. #include "stdafx.h"
  3. #include <locale>
  4. #include <windows.h>
  5. #include <iostream>
  6. #include "MyDateS.h"
  7. #include "MyDate.h"
  8.  
  9. using namespace std;
  10. /*Структура Дата.Структура должна включать следующие поля : день, месяц, год, список событий на эту дату.
  11. Простейшие функции : увеличение / уменьшение на 1 день,
  12. вычисление количества дней до заданной даты,
  13. вывод текущей даты,
  14. просмотр списка событий,
  15. увеличение и очистка списка событий.*/
  16. int main()
  17. {
  18.     SetConsoleCP(1251);
  19.     SetConsoleOutputCP(1251);
  20.     int option = 0;
  21.     Datefunc::DateStruct *ds;
  22.     // Выделяем динамически память под структуру
  23.     // и присваиваем адрес структуры в указатель
  24.     try
  25.     {
  26.         ds = new Datefunc::DateStruct;
  27.     }
  28.     catch (...)
  29.     {
  30.         cout << "Ошибка: Память не выделена!";
  31.         exit(0);
  32.     };
  33.     // Создаем объект класса
  34.     Date* d;
  35.     // Выделяем динамически память под объект
  36.     d = new Date;
  37.     while (1)
  38.     {
  39.         cout << "1.Ввод даты" << endl;
  40.         cout << "2.Добавление события" << endl;
  41.         cout << "3.Вывод событий для даты" << endl;
  42.         cout << "4.Очистка списка событий" << endl;
  43.         cout << "5.Уменьшение заданной даты на 1 день" << endl;
  44.         cout << "6.Увеличение заданной даты на 1 день" << endl;
  45.         cout << "7.Выход" << endl;
  46.         cin >> option;
  47.         // Работа с классом
  48.         switch (option)
  49.         {
  50.         case 1:
  51.         {
  52.             int day, m, y;
  53.             cout << "Ввод даты" << endl;
  54.             cout << "Введите число: " << endl;
  55.             cin >> day;
  56.             d->SetDay(day);
  57.             cout << "Введите месяц: " << endl;
  58.             cin >> m;
  59.             d->SetMonth(m);
  60.             cout << "Введите год: " << endl;
  61.             cin >> y;
  62.             d->SetYear(y);
  63.             break;
  64.         }
  65.         case 2:
  66.         {          
  67.             int day, m, y;
  68.             string events;
  69.             cout << "К какой дате добавить событие: " << endl;
  70.             cout << "Введите число: " << endl;
  71.             cin >> day;
  72.             cout << "Введите месяц: " << endl;
  73.             cin >> m;
  74.             cout << "Введите год: " << endl;
  75.             cin >> y;
  76.             cout << "Введите событие: " << endl;
  77.             cin.ignore();
  78.             getline(cin, events);
  79.             d->AddEvent(day, m, y, events);
  80.             break;
  81.         }
  82.         case 3:
  83.         {
  84.             int day, m, y;
  85.             cout << "К какой дате вывести события: " << endl;
  86.             cout << "Введите число: " << endl;
  87.             cin >> day;
  88.             cout << "Введите месяц: " << endl;
  89.             cin >> m;
  90.             cout << "Введите год: " << endl;
  91.             cin >> y;
  92.             d->Print(day, m, y);
  93.             break;
  94.         }
  95.         case 4:
  96.         {
  97.             int day, m, y;
  98.             cout << "Для какой даты очистить события: " << endl;
  99.             cout << "Введите число: " << endl;
  100.             cin >> day;
  101.             cout << "Введите месяц: " << endl;
  102.             cin >> m;
  103.             cout << "Введите год: " << endl;
  104.             cin >> y;
  105.             d->Clear(day, m, y);
  106.             break;
  107.         }
  108.         case 5:
  109.         {
  110.             int day, m, y;
  111.             cout << "-1 день: " << endl;
  112.             cout << "Введите число: " << endl;
  113.             cin >> day;
  114.             cout << "Введите месяц: " << endl;
  115.             cin >> m;
  116.             cout << "Введите год: " << endl;
  117.             cin >> y;
  118.             d->Minus(day, m, y);
  119.             break;
  120.         }
  121.         case 6:
  122.         {
  123.             int day, m, y;
  124.             cout << "+1 день: " << endl;
  125.             cout << "Введите число: " << endl;
  126.             cin >> day;
  127.             cout << "Введите месяц: " << endl;
  128.             cin >> m;
  129.             cout << "Введите год: " << endl;
  130.             cin >> y;
  131.             d->Plus(day, m, y);
  132.             break;
  133.         }
  134.         case 7:
  135.         {
  136.             exit(0);
  137.         }
  138.         default:
  139.             cout << "Введите число от 1 до 7" << endl;
  140.         }
  141.         /* Работа со структурой
  142.          Создаем объект класса Datefunc*/
  143.         Datefunc* datefunc;
  144.         // Выделяем динамически память под объект
  145.         datefunc = new Datefunc;
  146.         datefunc->SetDay(ds, d->Day);
  147.         datefunc->SetMonth(ds, d->Month);
  148.         datefunc->SetYear(ds, d->Year);
  149.         datefunc->SetEvent(ds, d->Event);
  150.     }
  151.     return 0;
  152. }
  153. //MyDateS.h
  154. #pragma once
  155. #include "string"
  156. using namespace std;
  157.  
  158. class Datefunc
  159. {
  160. public:
  161.     struct DateStruct
  162.     {
  163.         int Day[100]; //день
  164.         int Month[100]; //месяц
  165.         int Year[100]; //год
  166.         string Event[100]; // массив 100 элементов - события
  167.     };
  168. public:
  169.     void SetDay(struct DateStruct* S, int Day[]);
  170.     void SetMonth(struct DateStruct* S, int Month[]);
  171.     void SetYear(struct DateStruct* S, int Year[]);
  172.     void SetEvent(struct DateStruct* S, string *Event);
  173.     int countDate = 0; // счетчик количества дней
  174. };
  175. //MyDateS.cpp
  176. #include "stdafx.h"
  177. #include "MyDateS.h"
  178. //---------------------------------------------------
  179. void Datefunc::SetDay(struct DateStruct* S, int Day[])
  180. {
  181.     for (int i = 0; i < countDate; i++)
  182.         S->Day[i] = Day[i];
  183. }
  184. void Datefunc::SetMonth(struct DateStruct* S, int Month[])
  185. {
  186.     for (int i = 0; i < countDate; i++)
  187.         S->Month[i] = Month[i];
  188. }
  189. void Datefunc::SetYear(struct DateStruct* S, int Year[])
  190. {
  191.     for (int i = 0; i < countDate; i++)
  192.         S->Year[i] = Year[i];
  193. }
  194. void Datefunc::SetEvent(struct DateStruct* S, string *Event)
  195. {
  196.     for (int i = 0; i<100; i++)
  197.         S->Event[i] = Event[i];
  198. }
  199.  
  200.  
  201. //MyDate.h
  202. #pragma once
  203. #include "string"
  204. using namespace std;
  205.  
  206. class Date
  207. {
  208. public:
  209.     int Day[100]; //день
  210.     int Month[100]; //месяц
  211.     int Year[100]; //год
  212.     string Date_s[100];
  213.     string Event[100]; // массив 100 элементов - события
  214.     int countEvent = 0; // счетчик количества событий
  215.     int countDate = 0; // счетчик количества дней
  216. public:
  217.     void SetDay(int Day);
  218.     void SetMonth(int Month);
  219.     void SetYear(int Year);
  220.     void Plus(int, int, int);// увеличение на 1 день
  221.     void Minus(int, int, int);// уменьшение на 1 день
  222.     void Calculating(int,int,int);// вычисление количества дней до заданной даты
  223.     void Print(int, int, int);// Вывод текущей даты, просмотр списка событий
  224.     void Clear(int, int, int);// Очистка списка событий по дате
  225.     void AddEvent(int, int, int, string); // Добавление события к дате
  226. };
  227. //MyDate.cpp
  228. #include "stdafx.h"
  229. #include <iostream>
  230. #include "MyDate.h"
  231. //---------------------------------------------------
  232. void Date::SetDay(int _Day)
  233. {
  234.     Day[countDate] = _Day;
  235. }
  236. void Date::SetMonth(int _Month)
  237. {
  238.     Month[countDate] = _Month;
  239. }
  240. void Date::SetYear(int _Year)
  241. {
  242.     Year[countDate] = _Year;
  243.     countDate++;
  244. }
  245. void Date::Plus(int d, int m, int y)
  246. {
  247.     for (int i = 0; i < countDate; i++)
  248.         if (Day[i] == d && Month[i] == m && Year[i] == y)
  249.             Day[i] += 1;   
  250. }
  251. void Date::Minus(int d, int m, int y)
  252. {
  253.     for (int i = 0; i < countDate; i++)
  254.         if (Day[i] == d && Month[i] == m && Year[i] == y)
  255.             Day[i] -= 1;
  256. }
  257. void Date::Calculating(int d, int m, int y)
  258. {
  259.  
  260. }
  261. void Date::Print(int d, int m, int y)
  262. {
  263.     cout << "Список событий для " << d << "." << m << "." << y << endl;
  264.     for (int i = 0; i < countDate; i++)
  265.         if (Day[i] == d && Month[i] == m && Year[i] == y)
  266.         {
  267.             for (int j = 0; j < countEvent; j++)
  268.                 cout << "Событие [" << j << "] -> " << Event[j] << endl;
  269.             return;
  270.         }
  271.     cout << "Список пуст" << endl;
  272. }
  273. void Date::Clear(int d, int m, int y)
  274. {
  275.     for (int i = 0; i < countDate; i++)
  276.         if (Day[i] == d && Month[i] == m && Year[i] == y)
  277.             for (int j = 0; j < countEvent; j++)
  278.             {
  279.                 Event[j] = "";
  280.                 countEvent--;
  281.             }
  282. }
  283. void Date::AddEvent(int d, int m, int y, string ev)
  284. {
  285.     for (int i = 0; i < countDate; i++)
  286.         if (Day[i] == d && Month[i] == m && Year[i] == y)
  287.         {
  288.             cout << "Добавлено" << endl;
  289.             Event[countEvent] = ev;
  290.             countEvent++;
  291.         }
  292. }
Advertisement
Add Comment
Please, Sign In to add comment