Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Raport
La lucrarea de laborator la programarea C++ nr 1
Varianta 2
Efectuat de:
Verificat de:
Chiinu 2016
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.
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.
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.
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;
}
free(company[i]);
}
return 0;
}
Vector.cpp file:
#include <iostream>
#include <cstring>
using namespace std;
typedef struct {
int count;
int *nums;
} Vector;
}
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)
Bibliography
1. Lectures on C++ Programming of dr., conf. univ. Kulev M. Chi inu: UTM,
2016.
2. http://stackoverflow.com/