Advertisement
Guest User

pętelka

a guest
Mar 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <vector>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. vector<string> v;
  9.  
  10.  
  11. string int2string(int a)
  12. {
  13. stringstream ss;
  14. ss << a;
  15. string wynik = ss.str();
  16. return wynik;
  17. }
  18.  
  19.  
  20. int oct2dec(string s)
  21. {
  22. int n = s.length();
  23. int w = 0;
  24. for (int i = 0; i < n; i++)
  25. {
  26. w = w*8 + s[i] - '0';
  27. }
  28. return w;
  29. }
  30.  
  31.  
  32. bool rosnie(string s)
  33. {
  34. int n = s.length();
  35. for (int i=1; i<n; i++)
  36. {
  37. if (s[i] < s[i-1]) return false;
  38. }
  39. return true;
  40. }
  41.  
  42. bool mniejsze(string a, string b)
  43. {
  44. if (a.size() == b.size())
  45. {
  46. int i = 0;
  47. while(a[i]==b[i])i++;
  48. return a[i] < b[i];
  49. }
  50. else return a.size() < b.size();
  51. }
  52.  
  53. int main()
  54. {
  55. ifstream plik("dane.txt");
  56. ofstream wyniki("wyniki.txt");
  57. int l1 = 0, l2 = 0, l3 = 0;
  58. string o;
  59. for (int i=0; i<5000; i++)
  60. {
  61. plik >> o;
  62. // a
  63. if (o[0] == o[o.size()-1]) l1++;
  64. // b
  65. int dec = oct2dec(o);
  66. string d = int2string(dec);
  67. if (d[0] == d[d.size()-1]) l2++;
  68. // c
  69. if (rosnie(o)) {v.push_back(o); l3++;}
  70. }
  71. plik.close();
  72. wyniki << "a) " << l1 << endl;
  73. wyniki << "b) " << l2 << endl;
  74. wyniki << "c) " << l3 << endl;
  75. sort (v.begin(), v.end(), mniejsze);
  76. wyniki << "c) " << v.front() << endl; // v[0]
  77. wyniki << "c) " << v.back() << endl; // v[v.size()-1]
  78. cout << "Wyniki zapisano w pliku wyniki.txt" << endl;
  79. system("Pause");
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement