/* * File: main.cpp * Author: Ivan * * Created on January 7, 2014, 2:30 PM */#include <iostream>#include <fstream>using namespace std;int cuttingOfCake(int nmen) { int result = 0; if ((nmen == 0) || (nmen == 1)) { result = 0; } else if (nmen%2 == 0) { result = nmen / 2; } else { result = nmen; } return result;}int main(int argc, char** argv) { string inFileName = "input.txt"; ifstream in; in.open(inFileName.c_str()); if (!in.is_open()) { cerr << "Error: could not open the file " << inFileName.c_str() << endl; return 1; } string outFileName = "output.txt"; ofstream out; out.open(outFileName.c_str()); if (!out.is_open()) { cerr << "Error: could not open the file " << outFileName.c_str() << endl; in.close(); return 1; } int nmen; if (in >> nmen) { int result = cuttingOfCake(nmen); out << result << endl; } else { cerr << "Error: incorrect data in the file " << inFileName.c_str() << endl; in.close(); out.close(); return 1; } in.close(); out.close(); return 0;}
/* * File: newtestclass.cpp * Author: Ivan * * Created on Jan 7, 2014, 2:53:05 PM */#include "newtestclass.h"CPPUNIT_TEST_SUITE_REGISTRATION(newtestclass);newtestclass::newtestclass() {}newtestclass::~newtestclass() {}void newtestclass::setUp() {}void newtestclass::tearDown() {}int cuttingOfCake(int nmen);void newtestclass::testCuttingOfCake_001() { int nmen = 2; int actual = cuttingOfCake(nmen); int expected = 1; CPPUNIT_ASSERT_EQUAL(expected, actual);}void newtestclass::testCuttingOfCake_002() { int nmen = 3; int actual = cuttingOfCake(nmen); int expected = 3; CPPUNIT_ASSERT_EQUAL(expected, actual);}void newtestclass::testCuttingOfCake_003() { int nmen = 4; int actual = cuttingOfCake(nmen); int expected = 2; CPPUNIT_ASSERT_EQUAL(expected, actual);}void newtestclass::testCuttingOfCake_004() { int nmen = 5; int actual = cuttingOfCake(nmen); int expected = 5; CPPUNIT_ASSERT_EQUAL(expected, actual);}void newtestclass::testCuttingOfCake_005() { int nmen = 6; int actual = cuttingOfCake(nmen); int expected = 3; CPPUNIT_ASSERT_EQUAL(expected, actual);}void newtestclass::testCuttingOfCake_006() { int nmen = 7; int actual = cuttingOfCake(nmen); int expected = 7; CPPUNIT_ASSERT_EQUAL(expected, actual);}void newtestclass::testCuttingOfCake_007() { int nmen = 8; int actual = cuttingOfCake(nmen); int expected = 4; CPPUNIT_ASSERT_EQUAL(expected, actual);}void newtestclass::testCuttingOfCake_008() { int nmen = 0; int actual = cuttingOfCake(nmen); int expected = 0; CPPUNIT_ASSERT_EQUAL(expected, actual);}void newtestclass::testCuttingOfCake_009() { int nmen = 1; int actual = cuttingOfCake(nmen); int expected = 0; CPPUNIT_ASSERT_EQUAL(expected, actual);}
vector<int> arr; int value; string input; stringstream stream; while (in >> input) { stream << input; if (stream >> value) { cout << value << endl; } else { cerr << "Error: incorrect data in the file " << inFileName.c_str() << endl; in.close(); out.close(); return 1; } }
#include <vector>#include <string>#include <iostream>#include <fstream>#include <sstream>using namespace std;int sumOfMaxAndMin(const vector<int>& arr) { int sum = 0; return sum;}int main(int argc, char** argv) { string inFileName = "input.txt"; ifstream in; in.open(inFileName.c_str()); if (!in.is_open()) { cerr << "Error: could not open the file " << inFileName.c_str() << endl; return 1; } string outFileName = "output.txt"; ofstream out; out.open(outFileName.c_str()); if (!out.is_open()) { cerr << "Error: could not open the file " << outFileName.c_str() << endl; in.close(); return 1; } vector<int> arr; int value; string input; stringstream stream; while (in >> input) { stream << input; if (stream >> value) { cout << value << endl; } else { cerr << "Error: incorrect data in the file " << inFileName.c_str() << endl; in.close(); out.close(); return 1; } } int sum = sumOfMaxAndMin(arr); out << sum << endl; in.close(); out.close(); return 0;}
C++ (Qt)while (in >> val) arr.push_back(val);
vector<int> arr; int value; string input; stringstream stream; while (in >> input) { stream << input; if (stream >> value) { cout << value << endl; arr.push_back(value); } else { cerr << "Error: incorrect data in the file " << inFileName.c_str() << endl; in.close(); out.close(); return 1; } }
stream.clear();
vector<int> arr; int value; string input; while (in >> input) { if (stringstream(input) >> value) { cout << value << endl; arr.push_back(value); } else { cerr << "Error: incorrect data in the file " << inFileName.c_str() << endl; in.close(); out.close(); return 1; } }
C++ (Qt)if (!in.eof()) // печать ошибки
/* * File: main.cpp * Author: Ivan * * Created on January 7, 2014, 10:33 AM */#include <vector>#include <string>#include <iostream>#include <fstream>#include <sstream>using namespace std;int sumOfMaxAndMin(const vector<int>& arr) { int sum = 0; return sum;}int main(int argc, char** argv) { string inFileName = "input.txt"; ifstream in; in.open(inFileName.c_str()); if (!in.is_open()) { cerr << "Error: could not open the file " << inFileName.c_str() << endl; return 1; } string outFileName = "output.txt"; ofstream out; out.open(outFileName.c_str()); if (!out.is_open()) { cerr << "Error: could not open the file " << outFileName.c_str() << endl; in.close(); return 1; } vector<int> arr; int value; string input; while (in >> input) { if (stringstream(input) >> value) { arr.push_back(value); } else { cerr << "Error: incorrect data in the file " << inFileName.c_str() << endl; in.close(); out.close(); return 1; } } int sum = sumOfMaxAndMin(arr); out << sum << endl; in.close(); out.close(); return 0;}