Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <sstream>
  6. #include <cmath>
  7. #include <stdio.h>
  8. #include <locale.h>
  9. #include <conio.h>
  10.  
  11. using namespace std;
  12.  
  13. int isexisting(string filename) {
  14. int result;
  15. ifstream fin;
  16. double check;
  17.  
  18. fin.open(filename.c_str());
  19. if( fin.is_open() ) {
  20. if(fin >> check && fin.eof()) {
  21. result = 1;
  22. }
  23. else {
  24. if(fin.eof()) {
  25. result = 0;
  26. }
  27. if(!fin.eof()) {
  28. result = 1;
  29. }
  30. }
  31. }
  32. else {
  33. result = 2;
  34. }
  35.  
  36.  
  37. return result;
  38. }
  39.  
  40. int numberofelement(string filename, double thenumber) {
  41. int number, nstrings, i, maxi;
  42. double test;
  43. ifstream fin;
  44. double element;
  45. string s;
  46.  
  47. fin.open(filename.c_str());
  48. i = 1;
  49. maxi = 0;
  50. while(!fin.eof()) {
  51. fin >> element;
  52. if(element == thenumber) {
  53. maxi = i;
  54. }
  55. i++;
  56. }
  57. if(i == 2) {
  58. if(thenumber == 0) {
  59. if(element == 0 && i == 2) {
  60. maxi = 1;
  61. }
  62. else {
  63. nstrings = 0;
  64. while(!fin.eof()) {
  65. getline(cin, s);
  66. nstrings = nstrings + 1;
  67. }
  68. if(nstrings >= 1) {
  69. maxi = maxi - (nstrings);
  70. }
  71. else {
  72. cout << "";
  73. }
  74. }
  75. }
  76. if(!(thenumber == 0)) {
  77. if(element == thenumber) {
  78. maxi = 1;
  79. }
  80. else {
  81. maxi = 0;
  82. }
  83. }
  84. }
  85.  
  86. return maxi;
  87. }
  88.  
  89. int main() {
  90. string filename;
  91. double thenumber;
  92. ofstream fout;
  93.  
  94. setlocale(LC_ALL, "Russian");
  95. cout << "Введите имя или путь файла: ";
  96. cin >> filename;
  97. isexisting(filename);
  98. if(isexisting(filename) == 0) {
  99. cout << "Файл пуст." << endl;
  100. fout.open("output.txt");
  101. fout << "Файл пуст.";
  102. fout.close();
  103. }
  104. if(isexisting(filename) == 1) {
  105. cout << "Введите элемент (число): ";
  106. cin >> thenumber;
  107. cout << endl;
  108. numberofelement(filename, thenumber);
  109. if(numberofelement(filename, thenumber) == 0) {
  110. cout << "Элемента, который вы ввели в данной последовательности нет." << endl;
  111. fout.open("output.txt");
  112. fout << "Элемента, который вы ввели в данной последовательности нет.";
  113. fout.close();
  114. }
  115. else {
  116. cout << "Номер последнего элемента равного " << thenumber << " - " << numberofelement(filename, thenumber) << "." << endl;
  117. fout.open("output.txt");
  118. fout << "Номер последнего элемента равного " << thenumber << " - " << numberofelement(filename, thenumber) << ".";
  119. fout.close();
  120. }
  121. }
  122. if(isexisting(filename) == 2) {
  123. cout << "Неверно указано имя или путь файла." << endl;
  124. fout.open("output.txt");
  125. fout << "Неверно указано имя или путь файла.";
  126. fout.close();
  127. }
  128.  
  129. cout << endl << "Нажмите любую клавишу чтобы выйти.";
  130.  
  131. _getch();
  132. return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement