Advertisement
rengetsu

2ND_Algoritmai

Apr 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. //ISf-17/1, Pavel Trostianko. Stack controller (1.0).
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <windows.h>
  7. #include <cstdlib>
  8.  
  9. using namespace std;
  10.  
  11. int amount, stck[1000], add_a, rem_a, exmpl; //how many items are in the stack
  12. bool menu_check = 0;
  13. string pasirinkitefaila; //for ifstream to work with our text files
  14.  
  15. void File() //enter the name of our file
  16. {
  17.     //We specify the path to our text file
  18.     cout << "Please specify the path to text file with your stack.\n";
  19.     cout << "Text: ";
  20.     getline (cin, pasirinkitefaila);
  21.     ifstream fin(pasirinkitefaila.c_str());
  22. }
  23.  
  24. void Checking() //Through this function, we check whether the file is empty
  25. {
  26.     Sleep(1000);
  27.     int length = 0, a; //variable for writing length of file
  28.     cout << "\nChecking...\n\n";
  29.     ifstream fin(pasirinkitefaila.c_str()); //read the text file of the stack
  30.  
  31.     fin.open(pasirinkitefaila.c_str(), ios::binary); //open file to read it
  32.     fin.seekg(0, ios::end);
  33.     length = fin.tellg(); //Characters number
  34.     Sleep(3000);
  35.     if(length == 0) //if empty
  36.     {
  37.         cout<<"Error! File is empty!\n"; //error
  38.         File();
  39.     }
  40.     else //if not
  41.     {
  42.         cout<<"File exist!\n";
  43.     }
  44.     fin.close();
  45. }
  46.  
  47. void StackScan()
  48. {
  49.     ifstream fin(pasirinkitefaila.c_str());
  50.     Sleep(1500);
  51.     cout << "\nReading our stack...\n\n";
  52.     Sleep(3000);
  53.     fin >> amount;
  54.     for(int i=0;i<amount;i++)
  55.     {
  56.         fin >> stck[i];
  57.     }
  58. }
  59.  
  60. void StackShow()
  61. {
  62.     cout << "\n\t\tOur stack right now:\n\n";
  63.     for(int z=0;z<amount;z++)
  64.     {
  65.         if(z==0)
  66.         {
  67.             cout << "Number of elements in our stack: " << stck[z] << endl;
  68.             cout << "Our stack elements : " << endl;
  69.         }
  70.         else
  71.         {
  72.             cout << stck[z] << " ";
  73.         }
  74.  
  75.     }
  76.     cout << "\n";
  77. }
  78.  
  79. void AddItems()
  80. {
  81.     Sleep(1500);
  82.     cout << "\nAdding elements to stack\n\n";
  83.     Sleep(1500);
  84.     cout << "How many elements yo want to add? : ";
  85.     cin >> add_a;
  86.     Sleep(1500);
  87.  
  88.     cout << "\nnow you can add your elements:\n";
  89.     for(int q=0;q<add_a;q++)
  90.     {
  91.         cin >> stck[amount+q];
  92.     }
  93.     amount += add_a;
  94.     Sleep(2000);
  95.     StackShow();
  96. }
  97.  
  98. void RemoveItems()
  99. {
  100.     Sleep(1500);
  101.     cout << "\nRemoving elements from stack\n\n";
  102.     Sleep(1500);
  103.     cout << "How many elements yo want to remove? : ";
  104.     cin >> rem_a;
  105.  
  106.     Sleep(1000);
  107.     cout << "\nRemoving elements...\n";
  108.     Sleep(3000);
  109.  
  110.     for(int l=0;l<rem_a;l++)
  111.     {
  112.         stck[amount+l] = 0;
  113.     }
  114.  
  115.     amount -= rem_a;
  116.     Sleep(2000);
  117.     StackShow();
  118. }
  119.  
  120. void Menu()
  121. {
  122.     Sleep(4000);
  123.     int x;
  124.     if(menu_check == 0)
  125.     {
  126.         cout << "\n\t\tWelcome! This is controller Menu!\n\n";
  127.         menu_check = 1;
  128.     }
  129.     else
  130.     {
  131.         cout << "\n\t\tWelcome back to the menu!\n\n";
  132.     }
  133.     Sleep(1000);
  134.     cout<<"\nPress 0 to add items, press 1 to remove items: ";
  135.     cin >> x;
  136.     if(x == 0)
  137.     {
  138.         AddItems();
  139.     }
  140.     else
  141.     {
  142.         RemoveItems();
  143.     }
  144. }
  145.  
  146. int main()
  147. {
  148.     Sleep(3000);
  149.     cout << "\n\t\tWelcome to Stack controller (1.0)!\n\n";
  150.     Sleep(3000);
  151.     //enter the name of our file
  152.     File();
  153.     //We check if the stack is empty, through the function Checking
  154.     Checking();
  155.     //Our stack and work with it
  156.     StackScan();
  157.     StackShow(); //Showing our stack first time
  158.     //Menu for managing our program
  159.     Menu();
  160.     Menu();
  161.     Menu();
  162.     return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement