Sei sulla pagina 1di 9

Ministerul Educaiei Republicii Moldova

Universitatea Tehnic a Moldovei


Facultatea Calculatoare, Informatic i
Microelectronic
Filiera anglofon (FAF)

Raport
La lucrarea de laborator la programarea C++ nr 1
Varianta 2

Efectuat de:

Verificat de:

studentul gr. FAF-151 Simionov M.

Doctor Conf. Universitar, Kulev M.

Chiinu 2016

Laboratory work No. 1


Topic:Implementation of Company and Vector abstract data types.
Objectives: Achieving practical skills for implementing and working with
abstract data types, in this work, with structures and arrays-of-structures, in C++
language.

Condition of the problem: Create 2 programs made of 1 file:


Source the file with main() function, in which are placed both
implementation and main function. This file is also called User file.

Work processing:
Methods used in laboratory work:
In this laboratory work we have to implement 2 structures, Vector and Company.
The main goal of this laboratory is to move from C style memory management to C++
style, using new and delete keywords. Also this laboratory introduces the notion of
getters and setters.

Data analyzing:
Main function:
In both main functions we are sequentially testing each function for the given
structure. The main difference between C variant and C++ is that in C++ one we are
using cin and cout I/O methods from iostream instead of printf/scanf/gets/puts
functions from Cs stdio.h library. Almost all variables in the main function arent
allocated dynamically, this was done in order to simplify the implementation and to
concentrate more on functionality of Vector and Company ADTs.

Setters and Getters:


For both Vector and Company data types we have defined a couple of functions
,setters and getters, named set() and get() respectively. Their role is to provide an
interface to set/alter and retrieve information from given ADTs. The idea of
setters/getters comes from OOP, where these are used to provide a safe way to
modify information inside an object. For this laboratory work setter and getter has
each just one parameter, a reference to the ADTs instance. Setter for Company data
type is using a buffer for setting values for each field of structure. Also inside setters,
for both data types, is allocated memory for fields with pointers.

Compare function:
The Company ADT has a special function, called compare. Basically it takes two
instances of Company structure and compares them by date. Date or established
field is the only field which is comparable, because of its format (YYYY/MM/DD).
Note that theres a difference between comparability and equality. Using this function
we can afterwards sort an array of Company items, even pass it as an argument for
qsort function.

Sum and evensum functions:


Vector type has 2 special functions , one that compute the sum of even elements of
its instance, and another that defines vector sum for this ADT.
Sum function takes two references of Vector type and using a simple for loop,
computes the sum and returns the value. Note that it will return a vector with the
length of the smallest vector that was passed inside this function.
Evensum also uses a for loop with a if statement that checks the parity of the element
inside the vector and if it is even, accumulates it, than returning the accumulated
value.

Delete functions:
Both Vector and Company data types have delete functions, because both have
pointer type fields, that require dynamic memory management. Delete functions
provide a more natural interface to delete[] keyword.

Program text in C language:


Company.cpp file:
#include <iostream>
#include <cstring>
using namespace std;
typedef struct {
char *name;
char *type;
char *address;
char *established;
} Company;
void set(Company &com) {
char inputBuffer[256];
cout << "Name: ";
cin.getline(inputBuffer, 256);
com.name = new char[strlen(inputBuffer) + 1];
strcpy(com.name, inputBuffer);
cout << "Type: ";
cin.getline(inputBuffer, 256);
com.type = new char[strlen(inputBuffer) + 1];
strcpy(com.type, inputBuffer);
cout << "Address:[city/street] ";
cin.getline(inputBuffer, 256);
com.address = new char[strlen(inputBuffer) + 1];
strcpy(com.address, inputBuffer);
cout << "Established:[YYYY/MM/DD] ";
cin.getline(inputBuffer, 256);
com.established = new char[strlen(inputBuffer) + 1];
strcpy(com.established, inputBuffer);
}
char** get(Company &com) {
char **stats = new char*[4];
stats[0] = new char[strlen(com.name) + 1];
stats[1] = new char[strlen(com.type) + 1];
stats[2] = new char[strlen(com.address) + 1];
stats[3] = new char[strlen(com.established) + 1];

strcpy(stats[0],
strcpy(stats[1],
strcpy(stats[2],
strcpy(stats[3],

com.name);
com.type);
com.address);
com.established);

return stats;
}
int compare(Company &com1, Company &com2) {
return strcmp(com1.established, com2.established);
}
void free(Company &com) {
if (com.name) { delete[] com.name;} com.name = NULL;
if (com.type) { delete[] com.type;} com.type = NULL;
if (com.address) { delete[] com.address;} com.address = NULL;
if (com.established) { delete[] com.established;}
com.established = NULL;
cout<< "Memory freed successfully" <<endl;
}

#define NUM 5
int main(int argn, char **argv) {
Company company[NUM];
char* name, *address, *type, *established;
for (int i = 0; i < NUM; i++) {
cout<< "Setting values for "<< i + 1<<" company: " <<endl;
set(company[i]);
}
for (int i = 0; i < NUM; i++) {
cout<< "Getting values for "<< i + 1 <<" companies" <<endl;
cout<< "Name: " << get(company[i])[0] <<endl;
cout<< "Type: " << get(company[i])[1] <<endl;
cout<< "Address: " << get(company[i])[2] <<endl;
cout<< "Established: " << get(company[i])[3] <<endl;
}

for (int i = 0; i < NUM; i++) {

free(company[i]);
}
return 0;
}

Vector.cpp file:
#include <iostream>
#include <cstring>
using namespace std;
typedef struct {
int count;
int *nums;
} Vector;

void set(Vector& vec, int count) {


vec.nums = new int[count];
vec.count = count;
for (int i = 0; i < count; i++) {
cout<<"Set value "<< i + 1 <<": ";
cin>>vec.nums[i];
}
}
int* get(Vector& vec) {
int *arr = new int[vec.count];
for (int i = 0; i < vec.count; i++) {
arr[i] = vec.nums[i];
}
return arr;
}
Vector sum(Vector& vec1, Vector& vec2) {
int count = vec1.count - vec2.count > 0 ? vec2.count :
vec1.count;
Vector arr;
arr.nums = new int[count];
arr.count = count;
for (int i = 0; i < count; i++) {
arr.nums[i] = vec1.nums[i] + vec2.nums[i];
}
return arr;

}
void del(Vector& vec) {
if (vec.nums) { delete[] vec.nums;}
vec.nums = NULL;
}
int evensum(Vector& vec) {
int acc = 0;
for (int i = 0; i < vec.count; i++) {
if (vec.nums[i] % 2 == 0) {
acc += vec.nums[i];
}
}
return acc;
}
int main(int argc, char **argv) {
Vector test1, test2;
cout<< "Set 1st Vector: " <<endl;
set(test1, 4);
cout<< "Set 2nd Vector: " <<endl;
set(test2, 5);
cout<< "The sum of previously defined vectors is: "<<
sum(test1, test2).nums[0] << ", " <<
sum(test1, test2).nums[1] << ", " <<
sum(test1, test2).nums[2] << ", " <<
sum(test1, test1).nums[3] << endl;
cout<< "The sum of even numbers of first vector is: ";
cout<<evensum(test1)<<endl;
cout<< "The values stored in first vector" <<endl;
cout<< get(test1)[0] <<", ";
cout<< get(test1)[1] <<", ";
cout<< get(test1)[2] <<", ";
cout<< get(test1)[3] <<endl;
cout<< "Freeing both vectors" <<endl;
del(test1);
del(test2);
if (!test1.nums && !test2.nums) {cout<<"No vectors in
memory.\n";}
return 0;
}

Vector
(top)
and
Company (bottom) program output (first vector dimension 4, second vector dimension 5, size of
array of Company structures is 5)

Analysis of results and conclusions:


1. Verification shows that the results are correct and the program works
correctly.
2. Using functions, we can divide problem in sub problems, making
the problem more understandable and improving scalability.
3. Using structures as elements of a one dimensional array, we can
create a kind of database, with a great functionality.
4. Structures are a way to make programming more abstract, this way
spending time thinking more about interface rather than
implementation.

Bibliography
1. Lectures on C++ Programming of dr., conf. univ. Kulev M. Chi inu: UTM,
2016.
2. http://stackoverflow.com/

Potrebbero piacerti anche