Advertisement
Guest User

siema

a guest
May 24th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream.h>
  4.  
  5. using namespace std;
  6.  
  7. int odczyt_z_pliku()
  8. {
  9. ifstream plik;
  10. plik.open("liczby.txt");
  11.  
  12. int x, ile=0;
  13.  
  14. while(!plik.eof())
  15. {
  16. plik>>x;
  17. ile++;
  18. }
  19.  
  20. plik.close();
  21. return ile;
  22. }
  23.  
  24.  
  25.  
  26. int nieparzyste_cyfry()
  27. {
  28. ifstream plik;
  29. plik.open("liczby.txt");
  30.  
  31. int x, ile=0;
  32.  
  33. while(!plik.eof())
  34. {
  35. plik>>x;
  36. if(x%2!=0)
  37. ile++;
  38.  
  39. }
  40. return ile;
  41. }
  42.  
  43. void MIN_MAX()
  44. {
  45. ifstream plik;
  46. plik.open("liczby.txt");
  47.  
  48. int x, min, max;
  49. plik>>min;
  50. max=min;
  51. while(!plik.eof())
  52. {
  53. plik>>x;
  54. if(x<min)
  55. min=x;
  56. if(x>max)
  57. max=x;
  58. }
  59. cout<<"Max liczba to "<<max<<endl;
  60. cout<<"Min liczba to "<<min<<endl;
  61.  
  62. }
  63.  
  64. int suma_siema(int liczba)
  65. {
  66. int suma=0;
  67. while(liczba>0)
  68. {
  69. suma+=liczba%10;
  70. liczba=liczba/10;
  71. }
  72. return suma;
  73. }
  74.  
  75.  
  76.  
  77. int najwiekszasuma()
  78. {
  79. ifstream plik;
  80. plik.open("liczby.txt");
  81.  
  82. int liczba=0, suma=0,x;
  83.  
  84. while(!plik.eof())
  85. {
  86. plik>>x;
  87. if(suma<suma_siema(x))
  88. {
  89. suma=suma_siema(x);
  90. liczba=x;
  91. }
  92.  
  93. }
  94. cout<<"liczba"<<liczba<<endl;
  95. cout<<"jej suma "<<suma<<endl;
  96.  
  97. plik.close();
  98. }
  99. int ileslow()
  100. {
  101. ifstream plik;
  102. plik.open("liczby.txt");
  103. string x;
  104. int ile;
  105.  
  106. while(!plik.eof())
  107. {
  108. plik>>x;
  109. ile++;
  110. }
  111. plik.close();
  112. return ile;
  113. }
  114. void slowaA()
  115. {
  116. ifstream plik;
  117. plik.open("slowa.txt");
  118. string x;
  119.  
  120. while(!plik.eof())
  121. {
  122. plik>>x;
  123. if(x[0]=='A')
  124. cout<<x<<endl;
  125. }
  126. plik.close();
  127. }
  128.  
  129. void najnaj()
  130. {
  131. ifstream plik;
  132. plik.open("slowa.txt");
  133. string x[1000];
  134. int min, max,i=0;
  135. plik>>x[0];
  136. min=x[0].size();
  137. max=min;
  138. while(!plik.eof())
  139. {
  140. i++;
  141. plik>>x[i];
  142. if(x[i].size()>max)
  143. max=x[i].size();
  144. if(x[i].size()<min)
  145. min=x[i].size();
  146. }
  147. cout<<"min:"<<min<<endl;
  148. cout<<"max:"<<max<<endl;
  149.  
  150. for(i=0; i<1000; i++)
  151. {
  152. if(x[i].size()==max)
  153. cout<<x[i]<<" "<<endl;
  154.  
  155. if(x[i].size()==min)
  156. cout<<x[i]<<" "<<endl;
  157.  
  158. }
  159.  
  160.  
  161.  
  162.  
  163. plik.close();
  164. }
  165.  
  166. int main(int argc, char *argv[])
  167. {
  168. cout<<"W pliku jest "<<odczyt_z_pliku()<<" liczb"<<endl;
  169. cout<<"Jest "<<nieparzyste_cyfry()<<" liczb nieparzystych"<<endl;
  170. MIN_MAX();
  171. najwiekszasuma();
  172. cout<<ileslow()<<endl;
  173. cout<<"slowa na A: ";slowaA();
  174. najnaj();
  175. system("PAUSE");
  176. return EXIT_SUCCESS;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement