Advertisement
n3zzyswapz

Lab 01

Mar 26th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.26 KB | None | 0 0
  1. // udari.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5.  
  6. #include <iostream> //standard input output file. (input and output ganna programs waladi me library eka thiyenna oone)
  7. #include<iomanip> //this is also a part of the input output library but for massives
  8. #include<fstream> //also like iostream but to read and write text files
  9. #include<string>
  10. #include<sstream>
  11. #include<Windows.h>
  12.  
  13. using namespace std;
  14. const int n = 20;
  15.  
  16. int main()
  17. {
  18.     string fileName;
  19.     string stringElement;
  20.  
  21.     ifstream inputFile;
  22.     stringstream streamstr;
  23.  
  24.     int count = 0;
  25.  
  26.     do
  27.     {
  28.         cout << "Enter the file name: " << endl;
  29.         getline(cin, fileName);//console eke type karapu file name eka "filename" kiyala variable ekakata dagannawa
  30.         inputFile.open(fileName);//ios::in -> allow(разрещать) to read the file
  31.         if (inputFile)
  32.             break; //exit from the loop
  33.         cout << " file: " << fileName << " Unable to Access! " << endl;
  34.     } while (!inputFile.is_open());
  35.     //ws - манипулятор, он пропускает пробельные символы
  36.     //Пропускаем пробельные символы, если они есть
  37.  
  38.     inputFile >> ws;  //Проверяем файл на пустоту
  39.     if (inputFile.eof())
  40.     {
  41.         inputFile.close();
  42.         cout << "The file is empty!" << endl;
  43.         system("pause");
  44.         return 0;
  45.     }
  46.  
  47.     while (!inputFile.eof())
  48.     {
  49.         int x;
  50.         inputFile >> stringElement >> ws; //помещаем элемент stringElement в поток stringstream
  51.         streamstr.str(stringElement); //Извлекаем элемент из потока stringstream в переменную x для проверки на коректность
  52.         streamstr.seekg(0) >> x;
  53.         //Если был установлен failbit то извлекаеммый символ некоректный
  54.         //Если был установлен goodbit и НЕ БЫЛ установлен eofbit то ЗНАЧИТ ЭТО ЧАСТНЫЙ СЛУЧАЙ (когда элемент в потока имеет вид(пример): 3g)
  55.         if (streamstr.fail() || (streamstr.good() && !streamstr.eof()))
  56.         {
  57.             inputFile.close();
  58.             cout << "Invalid Element! '" << stringElement << "'[position in File : " << count + 1 << "]" << endl;
  59.             system("pause");
  60.             return 0;
  61.         }
  62.         count++;
  63.     }
  64.  
  65.     //Выделение памяти
  66.     int*array;
  67.     try
  68.     {
  69.         array = new int[count];
  70.     }
  71.     catch (...)
  72.     {
  73.         inputFile.close();
  74.         cout << "Memory allocation error!" << endl;
  75.         system("pause");
  76.         return 0;
  77.     }
  78.     inputFile.clear();
  79.     inputFile.seekg(0);
  80.     for (int i = 0; i < count; i++)
  81.         inputFile >> array[i];
  82.     inputFile.close(); //Close the file
  83.     //Display the numbers read:
  84.     cout << "The numbers are:" << endl;
  85.     for (int i = 0; i < count; i++)
  86.     {
  87.         cout << array[i] << " "<<endl;
  88.     }
  89.     cout << endl;
  90.     cout << "------------------------------------------------------------------------------------------------------------------------" <<endl;
  91.     cout << "-------------------------------------------------------Result-----------------------------------------------------------" << endl;
  92.     //Task
  93.     int sum = 0, avg = 0;
  94.     for (int i = 0; i < n; i++)
  95.     {
  96.         sum = sum + array[i];
  97.     }
  98.     avg = (int)sum / n;
  99.     int min = array[0];
  100.     int index = 0;
  101.     for (int i = 0; i < n; i++)
  102.     {
  103.         if (array[i] < min)
  104.         {
  105.             min = array[i];
  106.             index = 1;
  107.         }
  108.     }
  109.     for (int i = index+1; i < n; i++)
  110.     {
  111.         array[i] = avg;
  112.     }
  113.     for (int i = 0; i < n; i++)
  114.     {
  115.         cout << (int)array[i] << ' ' << endl;
  116.     }
  117.     delete[]array; //freeing the memory
  118.     system("pause");
  119.     return 0;
  120. }
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. /*
  137.  
  138. int avg = 0, sum = 0;
  139. int min = array[0];
  140. int k = 0;
  141. for (int i = 0; i < sizeof(array); ++i)
  142. {
  143.     sum += array[i];
  144.     if (min < array[i])
  145.     {
  146.         min = array[i];
  147.         k = i;
  148.     }
  149.     array[k + 1] = sum / sizeof(array);
  150.     cout << array[i] << ' ';
  151.     cout << endl;
  152. }
  153.  
  154. */
  155.  
  156.  
  157. /*for (int i = 0; i < count; i++)
  158.     {
  159.         sum = sum + array[i];
  160.  
  161.     }
  162.     avg = sum / count;
  163.     int Lmin = array[1];
  164.     for (int j = 1; j < count; j++)
  165.     {
  166.         if (array[j] < Lmin)
  167.         {
  168.             Lmin = array[j];
  169.  
  170.         }
  171.  
  172.         if (Lmin == array[j])
  173.         {
  174.             for (int k = Lmin; k < count; k++)
  175.             {
  176.                 array[k] = avg;
  177.             }
  178.         }
  179.         for (int m = 0; m < count; m++)
  180.             cout << array[m] << ' ';
  181.         cout << endl;*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement