Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- #include <string>
- #include <sstream>
- #include <cmath>
- #include <stdio.h>
- #include <locale.h>
- #include <conio.h>
- using namespace std;
- int isexisting(string filename) {
- int result;
- ifstream fin;
- double check;
- fin.open(filename.c_str());
- if( fin.is_open() ) {
- if(fin >> check && fin.eof()) {
- result = 1;
- }
- else {
- if(fin.eof()) {
- result = 0;
- }
- if(!fin.eof()) {
- result = 1;
- }
- }
- }
- else {
- result = 2;
- }
- return result;
- }
- int numberofelement(string filename, double thenumber) {
- int number, nstrings, i, maxi;
- double test;
- ifstream fin;
- double element;
- string s;
- fin.open(filename.c_str());
- i = 1;
- maxi = 0;
- while(!fin.eof()) {
- fin >> element;
- if(element == thenumber) {
- maxi = i;
- }
- i++;
- }
- if(i == 2) {
- if(thenumber == 0) {
- if(element == 0 && i == 2) {
- maxi = 1;
- }
- else {
- nstrings = 0;
- while(!fin.eof()) {
- getline(cin, s);
- nstrings = nstrings + 1;
- }
- if(nstrings >= 1) {
- maxi = maxi - (nstrings);
- }
- else {
- cout << "";
- }
- }
- }
- if(!(thenumber == 0)) {
- if(element == thenumber) {
- maxi = 1;
- }
- else {
- maxi = 0;
- }
- }
- }
- return maxi;
- }
- int main() {
- string filename;
- double thenumber;
- ofstream fout;
- setlocale(LC_ALL, "Russian");
- cout << "Введите имя или путь файла: ";
- cin >> filename;
- isexisting(filename);
- if(isexisting(filename) == 0) {
- cout << "Файл пуст." << endl;
- fout.open("output.txt");
- fout << "Файл пуст.";
- fout.close();
- }
- if(isexisting(filename) == 1) {
- cout << "Введите элемент (число): ";
- cin >> thenumber;
- cout << endl;
- numberofelement(filename, thenumber);
- if(numberofelement(filename, thenumber) == 0) {
- cout << "Элемента, который вы ввели в данной последовательности нет." << endl;
- fout.open("output.txt");
- fout << "Элемента, который вы ввели в данной последовательности нет.";
- fout.close();
- }
- else {
- cout << "Номер последнего элемента равного " << thenumber << " - " << numberofelement(filename, thenumber) << "." << endl;
- fout.open("output.txt");
- fout << "Номер последнего элемента равного " << thenumber << " - " << numberofelement(filename, thenumber) << ".";
- fout.close();
- }
- }
- if(isexisting(filename) == 2) {
- cout << "Неверно указано имя или путь файла." << endl;
- fout.open("output.txt");
- fout << "Неверно указано имя или путь файла.";
- fout.close();
- }
- cout << endl << "Нажмите любую клавишу чтобы выйти.";
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement