Alena95Wais
02-06-2015, 15:29
Программа вроде бы и работает, но вылетает.
//заголовочный файл main.h для класса Book
#include "stdafx.h"
#include <string>
#include <vector>
using namespace std;
class Book
{
public:
Book();
Book(int head, std::vector<int> V);
~Book();
string getName_book();
int getNumber_book();
int getCount_head();
string get_title();
vector<int> getMyVector();
void setMyVector(int count);
vector<int> selectCountHead(int count);
void toStringg();
//вывод на экран всего списка
static void showList(Book mas[]);
//вывод на экран одного экземпляра
static void showOneObject(Book mas[], int n);
//Вывод на экран всех экземпляров с количеством страниц больше выбранного
static void showList(Book mas[], int pages);
//Logger log;
static int number_object;
private:
int number_book;
string name_book;
int count_head;
int count_paeges_for_head;
int count_pages_book;
vector<int> myVector;
int calculateCountPagesInBook(vector<int> V);
};
23:18:29
// BookCpp.cpp: определяет точку входа для консольного приложения.
//
#include "stdafx.h"
#include "main.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <assert.h>
using namespace std;
int Book::number_object = 0;
Book::Book(int head, vector<int> V) {
srand(time(NULL));
number_object++; //Статический счётчик количества экземпляров
number_book += number_object; //Номер по порядку
name_book = get_title(); //Название книги
count_head = head; //Количество глав
myVector = selectCountHead(head); //вектор
count_pages_book = calculateCountPagesInBook(V); //Количество страниц в книге
//Logger::getLogger();
}
void Book::toStringg() {
cout « "Количества экземпляров = " + number_object « endl;
cout « "Номер по порядку = " + number_book « endl;
cout « "Название книги = " + name_book « endl;
cout « "Количество глав = " + count_head « endl;
cout « "Количество страниц в книге = " +count_pages_book « endl;
}
Book::Book() {
}
Book::~Book() {
number_object--;
}
int Book::getNumber_book() {
return number_book;
}
string Book::get_title() {
string xstr = "";
ifstream xfile("myfile.txt", ios::binary);
vector<string> tokens;
//узнаем размер файла, и выделяем память в строке
xfile.seekg(0, ios_base::end);
xstr.resize(xfile.tellg());
xfile.seekg(0, ios_base::beg);
//копируем данные
xfile.read((char*)xstr.data(), xstr.size());
string::size_type lastPos = xstr.find_first_not_of(" ", 0);
string::size_type pos = xstr.find_first_of(" ", lastPos);
while (string::npos != pos || string::npos != lastPos)
{
tokens.push_back(xstr.substr(lastPos, pos - lastPos));
lastPos = xstr.find_first_not_of(" ", pos);
pos = xstr.find_first_of(" ", lastPos);
}
xstr = "";
srand(time(NULL));
for (int i = 0; i < 3; i++)
{
xstr += tokens[rand() % tokens.size()] + " ";
}
srand(time(NULL));
return xstr;
}
string Book::getName_book() {
return name_book;
}
int Book::getCount_head() {
return count_head;
}
int Book::calculateCountPagesInBook(vector<int> V) {
int result = 0;
for (int i = 0; i < V.size(); i++)
result += V[i];
return result;
}
vector<int> Book::selectCountHead(int count) {
for (int i = 0; i < myVector.size(); i++)
myVector[i] = 50 + rand() % 51;
return myVector;
}
vector<int> Book::getMyVector() {
return myVector;
}
void Book::setMyVector(int count) {
}
//вывод всего списка
void Book::showList(Book mas[]) {
for (int i = 0; i < 20; i++)
{
cout « mas[i].number_object « endl;
cout « mas[i].number_book « endl;
cout « mas[i].name_book « endl;
cout « mas[i].count_head « endl;
cout « mas[i].count_pages_book « endl;
}
}
//вывод на экран одного экземпляра
void Book::showOneObject(Book mas[], int n) {
mas[n].toStringg();
}
//Вывод на экран всех экземпляров с количеством страниц больше выбранного
void Book::showList(Book mas[], int pages) {
for (int i = 0; i < 20; i++)
{
if (mas[i].count_pages_book > pages) {
//cout « mas[i].number_object « endl;
cout « mas[i].number_book « endl;
cout « mas[i].name_book « endl;
cout « mas[i].count_head « endl;
cout « mas[i].count_pages_book « endl;
}
}
}
void method() {
Book mas[20];
for (int i = 0; i < 20; i++)
{
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int n = 0;
setlocale(LC_ALL, "rus");
Book b;
Book c;
string s = b.get_title();
srand(time(NULL));
string sss = c.get_title();
cout « s « endl;
cout « sss « endl;
std::vector<int> myV(20);
Book mas[20];
string ss = "";
int temp;
for (int i = 0; i < 20; i++)
{
srand(time(NULL));
for (int j = 0; j < myV.size(); j++)
{
srand(time(NULL));
myV[i] = 50 + rand() % 51;
}
temp = 5 + rand() % 6;
mas[i] = Book(temp, myV);
srand(time(NULL));
}
int op;
//Программа
for (;;)
{
n = 0;
cout « "1 - вывод на экран всего списка" « endl;
cout « "2 -
вывод на экран одного экземпляра" « endl;
cout « "3 - вывод на экран всех экземпляров" « endl;
cout « "4 - выйти из программы" « endl;
cin » op;
switch (op)
{
case 1: {
Book::showList(mas);
}; break;
case 2: {
if (n < 20 && n > -1) {
cout « "Введите номер экземпляра" « endl;
cin » n;
}
else {
cout « "Вы ввели недопустимое значение" « endl;
continue;
}
Book::showOneObject(mas, n);
}; break;
case 3: {
if(n < 100 && n > -1) {
cout « "Введите количество страниц " « endl;
cin » n;
}
else {
cout « "Вы ввели недопустимое значение" « endl;
continue;
}
Book::showList(mas, n);
} ; break;
case 4: return 0; break;
default: cout « "Повторите ввод операции" « endl;
break;
}
}
system("pause");
return 0;
}
//заголовочный файл logger.h логер программы
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Logger{
public:
Logger();
static Logger getLogger();
void logging(int number);
void logging(int number, string input_data, string output_data);
void logging(int number, string input_data, string output_data, string method_name);
void logging(int number, string input_data, string output_data, string method_name, bool is_true);
};
//logger.cpp реализация класса Loger
#include "stdafx.h"
#include "main.h"
#include "logger.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <assert.h>
#include <time.h>
using namespace std;
//Logger::Logger() {
//
//}
//Logger Logger::getLogger() {
// Logger log;
// return log;
//}
void Logger::logging(int number) {
string s = "";
time_t some;
struct tm * timeinfo;
time(&some);
// timeinfo = localtime(&some);
// string my_time = asctime_s(timeinfo);
ofstream my_log_file("log.txt", ios::app);
//s += "Время события " + my_time + " ";
s += "Номер обьекта " + number;
my_log_file « s;
}
void Logger::logging(int number, string input_data, string output_data) {
string s = "";
Logger::logging(number);
ofstream my_log_file("log.txt", ios::app);
s += "Входные данные " + input_data + " ";
s += "Выходные данные " + output_data + " ";
}
void Logger::logging(int number, string input_data, string output_data, string method_name) {
string s;
Logger::logging(number, input_data, output_data);
ofstream my_log_file("log.txt", ios::app);
s += "Имя метода " + method_name;
}
void Logger::logging(int number, string input_data, string output_data, string method_name, bool is_true) {
string s = "";
Logger::logging(number, input_data, output_data, method_name);
if (is_true)
{
s += "Успешное событие";
}
else
{
s += "Неуспешное событие";
}
}
//заголовочный файл main.h для класса Book
#include "stdafx.h"
#include <string>
#include <vector>
using namespace std;
class Book
{
public:
Book();
Book(int head, std::vector<int> V);
~Book();
string getName_book();
int getNumber_book();
int getCount_head();
string get_title();
vector<int> getMyVector();
void setMyVector(int count);
vector<int> selectCountHead(int count);
void toStringg();
//вывод на экран всего списка
static void showList(Book mas[]);
//вывод на экран одного экземпляра
static void showOneObject(Book mas[], int n);
//Вывод на экран всех экземпляров с количеством страниц больше выбранного
static void showList(Book mas[], int pages);
//Logger log;
static int number_object;
private:
int number_book;
string name_book;
int count_head;
int count_paeges_for_head;
int count_pages_book;
vector<int> myVector;
int calculateCountPagesInBook(vector<int> V);
};
23:18:29
// BookCpp.cpp: определяет точку входа для консольного приложения.
//
#include "stdafx.h"
#include "main.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <assert.h>
using namespace std;
int Book::number_object = 0;
Book::Book(int head, vector<int> V) {
srand(time(NULL));
number_object++; //Статический счётчик количества экземпляров
number_book += number_object; //Номер по порядку
name_book = get_title(); //Название книги
count_head = head; //Количество глав
myVector = selectCountHead(head); //вектор
count_pages_book = calculateCountPagesInBook(V); //Количество страниц в книге
//Logger::getLogger();
}
void Book::toStringg() {
cout « "Количества экземпляров = " + number_object « endl;
cout « "Номер по порядку = " + number_book « endl;
cout « "Название книги = " + name_book « endl;
cout « "Количество глав = " + count_head « endl;
cout « "Количество страниц в книге = " +count_pages_book « endl;
}
Book::Book() {
}
Book::~Book() {
number_object--;
}
int Book::getNumber_book() {
return number_book;
}
string Book::get_title() {
string xstr = "";
ifstream xfile("myfile.txt", ios::binary);
vector<string> tokens;
//узнаем размер файла, и выделяем память в строке
xfile.seekg(0, ios_base::end);
xstr.resize(xfile.tellg());
xfile.seekg(0, ios_base::beg);
//копируем данные
xfile.read((char*)xstr.data(), xstr.size());
string::size_type lastPos = xstr.find_first_not_of(" ", 0);
string::size_type pos = xstr.find_first_of(" ", lastPos);
while (string::npos != pos || string::npos != lastPos)
{
tokens.push_back(xstr.substr(lastPos, pos - lastPos));
lastPos = xstr.find_first_not_of(" ", pos);
pos = xstr.find_first_of(" ", lastPos);
}
xstr = "";
srand(time(NULL));
for (int i = 0; i < 3; i++)
{
xstr += tokens[rand() % tokens.size()] + " ";
}
srand(time(NULL));
return xstr;
}
string Book::getName_book() {
return name_book;
}
int Book::getCount_head() {
return count_head;
}
int Book::calculateCountPagesInBook(vector<int> V) {
int result = 0;
for (int i = 0; i < V.size(); i++)
result += V[i];
return result;
}
vector<int> Book::selectCountHead(int count) {
for (int i = 0; i < myVector.size(); i++)
myVector[i] = 50 + rand() % 51;
return myVector;
}
vector<int> Book::getMyVector() {
return myVector;
}
void Book::setMyVector(int count) {
}
//вывод всего списка
void Book::showList(Book mas[]) {
for (int i = 0; i < 20; i++)
{
cout « mas[i].number_object « endl;
cout « mas[i].number_book « endl;
cout « mas[i].name_book « endl;
cout « mas[i].count_head « endl;
cout « mas[i].count_pages_book « endl;
}
}
//вывод на экран одного экземпляра
void Book::showOneObject(Book mas[], int n) {
mas[n].toStringg();
}
//Вывод на экран всех экземпляров с количеством страниц больше выбранного
void Book::showList(Book mas[], int pages) {
for (int i = 0; i < 20; i++)
{
if (mas[i].count_pages_book > pages) {
//cout « mas[i].number_object « endl;
cout « mas[i].number_book « endl;
cout « mas[i].name_book « endl;
cout « mas[i].count_head « endl;
cout « mas[i].count_pages_book « endl;
}
}
}
void method() {
Book mas[20];
for (int i = 0; i < 20; i++)
{
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int n = 0;
setlocale(LC_ALL, "rus");
Book b;
Book c;
string s = b.get_title();
srand(time(NULL));
string sss = c.get_title();
cout « s « endl;
cout « sss « endl;
std::vector<int> myV(20);
Book mas[20];
string ss = "";
int temp;
for (int i = 0; i < 20; i++)
{
srand(time(NULL));
for (int j = 0; j < myV.size(); j++)
{
srand(time(NULL));
myV[i] = 50 + rand() % 51;
}
temp = 5 + rand() % 6;
mas[i] = Book(temp, myV);
srand(time(NULL));
}
int op;
//Программа
for (;;)
{
n = 0;
cout « "1 - вывод на экран всего списка" « endl;
cout « "2 -
вывод на экран одного экземпляра" « endl;
cout « "3 - вывод на экран всех экземпляров" « endl;
cout « "4 - выйти из программы" « endl;
cin » op;
switch (op)
{
case 1: {
Book::showList(mas);
}; break;
case 2: {
if (n < 20 && n > -1) {
cout « "Введите номер экземпляра" « endl;
cin » n;
}
else {
cout « "Вы ввели недопустимое значение" « endl;
continue;
}
Book::showOneObject(mas, n);
}; break;
case 3: {
if(n < 100 && n > -1) {
cout « "Введите количество страниц " « endl;
cin » n;
}
else {
cout « "Вы ввели недопустимое значение" « endl;
continue;
}
Book::showList(mas, n);
} ; break;
case 4: return 0; break;
default: cout « "Повторите ввод операции" « endl;
break;
}
}
system("pause");
return 0;
}
//заголовочный файл logger.h логер программы
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Logger{
public:
Logger();
static Logger getLogger();
void logging(int number);
void logging(int number, string input_data, string output_data);
void logging(int number, string input_data, string output_data, string method_name);
void logging(int number, string input_data, string output_data, string method_name, bool is_true);
};
//logger.cpp реализация класса Loger
#include "stdafx.h"
#include "main.h"
#include "logger.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <assert.h>
#include <time.h>
using namespace std;
//Logger::Logger() {
//
//}
//Logger Logger::getLogger() {
// Logger log;
// return log;
//}
void Logger::logging(int number) {
string s = "";
time_t some;
struct tm * timeinfo;
time(&some);
// timeinfo = localtime(&some);
// string my_time = asctime_s(timeinfo);
ofstream my_log_file("log.txt", ios::app);
//s += "Время события " + my_time + " ";
s += "Номер обьекта " + number;
my_log_file « s;
}
void Logger::logging(int number, string input_data, string output_data) {
string s = "";
Logger::logging(number);
ofstream my_log_file("log.txt", ios::app);
s += "Входные данные " + input_data + " ";
s += "Выходные данные " + output_data + " ";
}
void Logger::logging(int number, string input_data, string output_data, string method_name) {
string s;
Logger::logging(number, input_data, output_data);
ofstream my_log_file("log.txt", ios::app);
s += "Имя метода " + method_name;
}
void Logger::logging(int number, string input_data, string output_data, string method_name, bool is_true) {
string s = "";
Logger::logging(number, input_data, output_data, method_name);
if (is_true)
{
s += "Успешное событие";
}
else
{
s += "Неуспешное событие";
}
}