Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include<Windows.h>
  4. #include<string>
  5. #include <fstream>
  6. #pragma warning(disable:4996)
  7. #include <stdio.h>
  8. #include <cctype>
  9.  
  10.  
  11. #define MAX_LINE 512
  12.  
  13. using namespace std;
  14.  
  15. int countChars(char letter);
  16.  
  17. int sumofletters()
  18. {
  19. int freq[128]; // frequencies of letters
  20. ifstream inFile; // input file
  21. char ch;
  22.  
  23. inFile.open("test.txt");
  24. if (!inFile)
  25. {
  26. cout << "The input file could not be opened." << endl;
  27. return 1;
  28. }
  29.  
  30. // initialize frequency counts to zero for each possible letter
  31. for (int k = 0; k < 128; k++)
  32. {
  33. freq[k] = 0;
  34. }
  35.  
  36. // Read the file, keeping track of frequency with which each letter occurs
  37. ch = inFile.get();
  38. while (ch != EOF)
  39. {
  40. ch = toupper(ch);
  41. freq[ch]++;
  42. ch = inFile.get();
  43. }
  44. // Print the output table
  45.  
  46. int sum = 0;
  47. for (char ch = 'A'; ch <= 'Z'; ch++)
  48. {
  49.  
  50. sum = freq[ch] + sum;
  51. }
  52. cout << "Suma liter: " << sum << endl;
  53. return 0;
  54. }
  55.  
  56. int download()
  57. {
  58. char url[MAX_LINE] = "ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KLAX.TXT";
  59. char destination[MAX_LINE] = "test.txt";
  60. char buffer[MAX_LINE];
  61.  
  62. HRESULT dl;
  63.  
  64. typedef HRESULT(WINAPI* URLDownloadToFileA_t)(LPUNKNOWN pCaller, LPCSTR szURL, LPCSTR szFileName, DWORD dwReserved, void* lpfnCB);
  65. URLDownloadToFileA_t xURLDownloadToFileA;
  66. xURLDownloadToFileA = (URLDownloadToFileA_t)GetProcAddress(LoadLibraryA("urlmon"), "URLDownloadToFileA");
  67.  
  68. dl = xURLDownloadToFileA(NULL, url, destination, 0, NULL);
  69.  
  70. sprintf_s(buffer, "Downloading File From: %s, To: %s\n", url, destination);
  71.  
  72. if (dl == S_OK)
  73. {
  74. sprintf_s(buffer, "File Successfully Downloaded To: %s\n", destination);
  75. printf(buffer);
  76. }
  77. else if (dl == E_OUTOFMEMORY)
  78. {
  79. sprintf_s(buffer, "Failed To Download File Reason: Insufficient Memory\n");
  80. printf(buffer);
  81. return 0;
  82. }
  83. else
  84. {
  85. sprintf_s(buffer, "Failed To Download File Reason: Unknown\n");
  86. printf(buffer);
  87. return 0;
  88. }
  89. }
  90.  
  91. int countWords() {
  92. string line;
  93. int words = 0;
  94. string word;
  95. ifstream myfile("test.txt");
  96. while (myfile >> word) {
  97. ++words;
  98. }
  99. cout << words << "\n";
  100.  
  101. return 0;
  102. }
  103.  
  104.  
  105. class RedirectStdOutput {
  106. public:
  107. RedirectStdOutput(std::ofstream& file)
  108. : _psbuf{ file.rdbuf() }, _backup{ std::cout.rdbuf() }
  109. {
  110. std::cout.rdbuf(_psbuf);
  111. }
  112.  
  113. ~RedirectStdOutput() {
  114. std::cout.rdbuf(_backup);
  115. }
  116.  
  117. private:
  118. std::streambuf* _psbuf;
  119. std::streambuf* _backup;
  120. };
  121.  
  122.  
  123. int letterslist()
  124. {
  125. char letter;
  126.  
  127. cout << "Letter\t\tHow many times" << endl;
  128.  
  129. for (letter = 'a'; letter <= 'z'; letter++)
  130. {
  131.  
  132. cout << letter << "\t\t" << countChars(letter) << " times" << endl;
  133. }
  134. for (letter = 'A'; letter <= 'Z'; letter++)
  135. {
  136. cout << letter << "\t\t" << countChars(letter) << " times" << endl;
  137. }
  138.  
  139.  
  140.  
  141.  
  142. return EXIT_SUCCESS;
  143. }
  144.  
  145. int zdania() {
  146. const int n = 4;
  147. int countx = 0;
  148. char chars[n] = { '.', ',', '?', '!' };
  149.  
  150. ifstream myfile("test.txt");
  151. char c;
  152. while (!myfile.eof()) {
  153. myfile >> c;
  154. for (int i = 0; i < n; i++) {
  155. if (c == chars[i]) {
  156. countx++;
  157. }
  158. }
  159. }
  160. myfile.close();
  161. cout << "Ilosc znakow interpunkcyjnych" << countx - 1 << endl;
  162. return 0;
  163. }
  164.  
  165.  
  166. int znaki() {
  167. const int n = 4;
  168. int count = 0;
  169. char chars[n] = { '.', ',', '?', '!' };
  170. ifstream myfile("test.txt");
  171. char c;
  172. while (!myfile.eof()) {
  173. myfile >> c;
  174. for (int i = 0; i < n; i++) {
  175. if (c == chars[i]) {
  176. count++;
  177. }
  178. }
  179. }
  180. myfile.close();
  181. cout << "Ilosc zdan:" << count - 1 << endl;
  182. return 0;
  183. }
  184.  
  185. int countChars(char letter)
  186. {
  187. {
  188. ifstream stream;
  189. char character;
  190. int count = 0;
  191.  
  192. stream.open("test.txt");
  193. stream.get(character);
  194.  
  195. while (!stream.fail())
  196. {
  197. if (character == letter)
  198. count++;
  199. stream.get(character);
  200. }
  201. stream.close();
  202.  
  203. return count;
  204. }
  205. }
  206.  
  207. int stats1()
  208. {
  209.  
  210. freopen("statystyki.txt", "w", stdout);
  211. sumofletters();
  212. zdania();
  213. znaki();
  214.  
  215.  
  216. return 0;
  217.  
  218.  
  219. }
  220.  
  221. int main(int argc, char* argv[])
  222. {
  223. int choice;
  224.  
  225. do {
  226. cout << "Menu\n";
  227. cout << "Dokonaj wyboru\n";
  228. cout << "1 - Pobierz plik z internetu\n";
  229. cout << "2 - Zlicz liczbe liter w pliku\n";
  230. cout << "3 - Zlicz liczbe wyrazow w pliku\n";
  231. cout << "4 - Zlicz liczbe znakow interpunkcyjnych w pliku\n";
  232. cout << "5 - Zlicz liczbe zdan pliku\n";
  233. cout << "6 - Wygeneruj raport o uzyciu liter (A-Z)\n";
  234. cout << "7 - Zapisz statystyki z punktow 2-5 do pliku statystyki.txt\n";
  235. cout << "8 - Wyjscie z programu\n";
  236. cout << "Wybor: ";
  237. cin >> choice;
  238.  
  239. switch (choice) {
  240. case 1:
  241.  
  242. download();
  243. break;
  244. case 2:
  245. sumofletters();
  246. break;
  247. case 3:
  248. countWords();
  249. break;
  250. case 4:
  251. znaki();
  252. break;
  253.  
  254. case 5:
  255. zdania();
  256. break;
  257. case 6:
  258. letterslist();
  259. break;
  260. case 7:
  261. stats1();
  262. break;
  263. case 8:
  264. cout << "Goodbye!";
  265. return 0;
  266. default:
  267. cout << "Menu\n";
  268. cout << "Dokonaj wyboru\n";
  269. cout << "1 - Pobierz plik z internetu\n";
  270. cout << "2 - Zlicz liczbe liter w pliku\n";
  271. cout << "3 - Zlicz liczbe wyrazow w pliku\n";
  272. cout << "4 - Zlicz liczbe znakow interpunkcyjnych w pliku\n";
  273. cout << "5 - Zlicz liczbe zdan pliku\n";
  274. cout << "6 - Wygeneruj raport o uzyciu liter (A-Z)\n";
  275. cout << "7 - Zapisz statystyki z punktow 2-5 do pliku statystyki.txt\n";
  276. cout << "8 - Wyjscie z programu\n";
  277. cout << "Wybór: ";
  278. cin >> choice;
  279. }
  280. } while (choice != 8);
  281.  
  282. return EXIT_SUCCESS;
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement