- •Курсовая работа по дисциплине «Информатика и программирование»
- •1.Постановка задачи
- •2.Состав классов
- •2.1.2. Описание класса:
- •2.2.2. Описание класса:
- •2.3.2. Описание класса:
- •2.3.3. Описание методов класса
- •3.Схема взаимодействия классов
- •4. Список функций, не состоящих в классах.
- •5.1.Структура
- •5.2.Особенности организации
- •6. Пример работы программы
- •7.Список использованной литературы
- •8.Приложение
- •Код файла Сlass1.H
- •Код файла Сlass1.Cpp
- •Код файла Сlass2.Cpp
Код файла Сlass1.H
struct turWork{
int id;
char * country;
char ** towns;
int aId;
turWork *next;
};
class Class1{
public:
Class1(): first(0), current(0) {};
void FileSave();
void FileLoad();
void Add(turWork *);
bool Remove(int);
void Show();
char * Return(int);
int Return(char *);
void Company(int);
private:
turWork *first;
turWork *current;
};
Код файла Сlass1.Cpp
#include "stdafx.h"
#include "Class1.h";
#include <iostream>
void Class1::Add(turWork * data){
if(!this -> first) this -> first = data;
else{
this -> current = this -> first;
while(this -> current -> next) this -> current = this -> current -> next ;
this -> current -> next = data;
}
data = NULL;
delete data;
};
void Class1::Show(){
if(!this -> first) std::cout << "List 1 is empty\n";
else{
this -> current = this -> first;
std::cout << "List1:\n";
do{
std:: cout << "Id = " << this -> current -> id << ", country = " << this -> current -> country << ", towns: ";
for(char** i = this -> current -> towns; i[0]; i++) std::cout << i[0] << ", ";
std:: cout << "aId = " << this -> current -> aId << "\n";
this -> current = this -> current -> next;
} while(this -> current);
}
};
bool Class1::Remove(int id){
if(!this -> first) return false;
else if(this -> first -> id == id){
this -> current = this -> first;
this -> first = this -> first -> next;
delete this -> current;
return true;
}else{
this -> current = this -> first;
while(this -> current -> next){
if(this -> current -> next -> id == id){
turWork * temp = this -> current -> next;
this -> current -> next = this -> current -> next -> next;
delete temp;
return true;
}
this -> current = this -> current -> next;
}
}
return false;
};
char * Class1::Return(int id){
this -> current = this -> first;
while(this -> current){
if(this -> current -> id == id) return this -> current -> country;
this -> current = this -> current -> next;
}
return "Not in database";
};
int Class1::Return(char* country){
this -> current = this -> first;
while(this -> current){
if(!strcmp(this -> current -> country,country)) return this -> current -> id;
this -> current = this -> current -> next;
}
return NULL;
};
void Class1::Company(int aId){
std::cout << "Country list: ";
this -> current = this -> first;
while(this -> current){
if(this -> current -> aId == aId) std::cout << this -> current -> country << " ";
this -> current = this -> current -> next;
}
std::cout << std::endl;
}
void Class1::FileSave(){
FILE *f = fopen("class1.txt", "w");
this -> current = this -> first;
while(this -> current){
fprintf(f, "Id = %d, country = %s, towns: ", this -> current -> id, this -> current -> country);
for(char** i = this -> current -> towns; i[0]; i++) fprintf(f ,"%s, ",i[0]);
fprintf(f, "aId = %d\n", this -> current -> aId);
this -> current = this -> current -> next;
}
fclose(f);
}
void Class1::FileLoad(){
FILE *f = fopen("class1.txt", "r");
char * s = new char[201];
this -> current = this -> first;
if(this -> current) while(this -> current -> next) this -> current = this -> current -> next;
while(fgets(s,150,f) != NULL ){
turWork * temp = new turWork;
temp -> next = NULL;
s = strtok(s," ,=:");
s = strtok(0," ,=:");
temp -> id = atoi(s);
s = strtok(0," ,=:");
s = strtok(0," ,=:");
temp -> country = s;
s = strtok(0," ,=:");
s = strtok(0," ,=:");
char ** i = new char*[10];
int j(0);
for(;strcmp(s,"aId") != 0;j++){
i[j] = s;
s = strtok(0," ,=:");
}
i[j] = NULL;
temp -> towns = new char*[j-1];
for(int k = 0; k <= j; k++) temp -> towns[k] = i[k];
delete[] i;
s = strtok(0," ,=:");
temp -> aId = atoi(s);
if(!this -> first){
this -> first = temp;
temp = NULL;
delete temp;
this -> current = this -> first;
}else{
this -> current -> next = temp;
temp = NULL;
delete temp;
this -> current = this -> current -> next;
}
}
fclose(f);
}
Код файла Сlass2.h
struct exWork{
int id;
char * comment;
int exId;
exWork *next;
};
class Class2{
public:
Class2(): first(0), current(0) {};
void FileSave();
void FileLoad();
void Add(exWork *);
bool Remove(int);
void Show();
void exList(int);
char* Return(int);
private:
exWork *first;
exWork *current;
};