You are on page 1of 19

MINISTERUL EDUCAIEI REPUBLICII MOLDOVA

UNIVERSITATEA TEHNIC A MOLDOVEI


Facultatea Calculatoare, Informatic i Microelectronic
FILIERA ANGLOFON

RAPORT
Lucrarea de laborator nr. 1
la Structuri de date i algoritmi

A efectuat:

st. gr. FAF-141 (l. englez)

Culeva Alexandru

A verificat:

dr., conf. univ.

Mihail Kulev

LABORATORY WORK No. 1


Topic: Processing of structure arrays and usage of files in C language
The work aim: Programming of algorithms for processing of structure
arrays, using functions, indexes, dynamic memory allocation and files in C
language.
Condition of the problem: Create functions providing menu operations.
Variant No. 15:
Structure the UNIVERSITY with a minimum of 5 fields.
Problem Condition.
To create:
1. A file with the extension .h where is described structure of array elements
and prsototype of functions which uses operations on given array.
2. A file with extension .cpp that contains code of fuctions declared in file 1.
3. A user file with the extension .cpp that contains function main for
processing datebase as array of structures.
The course of work:
Abstract data type represents a mathematical model of studied data structure and
operations on the data. Steps for implementation of abstract data type in C language are:
1) Creating a file with the extension (.h) that contains description of properties of
data structure.
2) Prototype of functions for operions on data.
3) Creating a file with extension (.cpp) thant contains the main function. An ADT
can be designed as a mathematical model whom is associated a collection of specific
operators. We can make a parallel with the concept of a procedure. The procedure
generalizes the notion of operator. Instead of being limited to the exclusive use of
defined operators in the programming language(built-in operators) using procedures,
programmer is free to define its own operators, which subsequently to apply on some
operands which need not to belong to the basic types of the language used. An example
of procedure used in this manner is, for instance, routine of multiplication of two
matrices. Procedures encapsulate parts of an algorithm by localization. This means
placing in one section of the program , all relevant instructions.
Example: Using typedef instruction and specificator of structure struc we can define structure and new data type
student:
typedef struct student
{
// definirea elementelor structurii
char *nume;
int nr;
float nm;
} student;
1

Text of the program in C language


//header file(univer.h)
//univer.h
typedef struct{
char name[10];
int nr_std;
char rector[25];
int facs;
int rank;
} univer;
void menu();
void output(univer* universities, int n, int cat);//cat=1 if you want
categories to be outputted,0 if not(0 is for outputting just universities
void search(univer* universities, int num);
univer* struct_array(FILE* data, int* num);
univer* swap(univer* university, int num);
univer* add(univer* universities, int* num);
void write_struct(FILE* data, univer* universities, int n);
univer* deletel(univer* universities, int *n);
univer* modify(univer* universities, int n);
/**Sorting functions*/
univer* sort(univer* universities, int n);
int* ssort(char** arr, int n); // returns array with numbers of order of
elements
int* isort(int* arr, int n);
int recursort(char** arr, int i, int j);//recursive function iterating over
each letter in comparing strings if they are equal, j is letter number

//univer.cpp
//univer.cpp
#include "univer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void menu()
{
printf("-----------UNIVERSITIES DATABASE-----------");
printf("\n\n1. Search universities.");
printf("\n\n2. Print the list of all universities.");
printf("\n\n3. Modify information about university.");
printf("\n\n4. Swap two universities.");
printf("\n\n5. Sort universities.");
printf("\n\n6. Add university.");
printf("\n\n7. Delete university.");
printf("\n\n\nESC Exit.");
}
void output(univer* universities, int n, int cat)
{
char c;
int i;
if(cat==1){
system("cls");

printf("
NAME
| STUDENTS NUMBER |
RECTOR NAME
|
FACULTIES | RANK |\n");}
printf("\n
-------------------------------------------------------------------------\n
");
for(i=0;i<n;i++)
{
printf("
%10s|",universities[i].name);
printf("%17d|",universities[i].nr_std);
printf("%25s|",universities[i].rector);
printf("%11d|",universities[i].facs);
printf("%6d",universities[i].rank);
printf(" \n
-------------------------------------------------------------------------\n
");
}
if(cat==1){
printf("\n\n1. Back.");
printf("\n\nESC Exit.");
get_char:
switch(getch())
{
case 49: break;
case 27: exit(2);
default:goto get_char; break;
}}
}
void search(univer* universities, int num)
{
int i;
char name[10];
system("cls");
printf("
SEARCH UNIVERSITY BY NAME");
printf("\nPlease enter the name:");
fflush(stdin);
gets(name);
for(i=0;i<num;i++)
{
if(strcmp(universities[i].name,name)==0)
{
printf("\n\n");
printf("
-------------------------------------------------------------------------\n
");
printf("%10s|%17d|%25s|%11d|
%6d|",universities[i].name,universities[i].nr_std,universities[i].rector,un
iversities[i].facs,universities[i].rank);
printf("\n
-------------------------------------------------------------------------\n
");
break;
}
else{
if(i==num-1) printf("\n\nUniversity with given name not
found!");
}
}
printf("\n\n1. Search another.");

printf("\n2. Back.");
printf("\n\nESC Exit.");
get_char:
switch(getch())
{
case 49: search(universities, num);
break;
case 50: break;
case 27: exit(3);
default: goto get_char;
}

}
univer* struct_array(FILE* data, int* num)
{
char uni[50];
char* str;
str = (char*)malloc(sizeof(char)*25);
univer* universities = (univer*)malloc(sizeof(univer)*20);
int n = 0, i = 0;
while(!feof(data))
{
n = 0;
fgets(uni,50,data);
str=strtok(uni,"-");
strcpy(universities[i].name,str);
while(str!=NULL)
{
n++;
str=strtok(NULL,"-");
if(n==1)
{
universities[i].nr_std = atoi(str);
}
if(n==2)
{
strcpy(universities[i].rector,str);
}
if(n==3)
{
universities[i].facs = atoi(str);
}
if(n==4)
{
universities[i].rank = atoi(str);
}
}
i++;
}
*num=i;
fseek(data,0,SEEK_SET);
return universities;
}
univer* swap(univer* university, int num)
{
char name1[10], name2[10];
int i, j;
univer uni;
univer* universities = (univer*)malloc(sizeof(univer)*num);

universities = university;
again:
system("cls");
printf("Enter the name of the first university to be swapped:");
gets(name1);
for(i=0;i<num;i++)
{
if(strcmp(universities[i].name,name1)==0)
{
break;
}
}
if(i==num)
{
puts("\nUniversity not found.");
puts("1. Try another.");
puts("2. Back.");
puts("\nESC Exit.");
switch(getch())
{
case 49: goto again;
break;
case 50: free(university); return universities;
break;
case 27: exit(4);
}
}
printf("Enter the name of the university to be swapped with:");
gets(name2);
for(j=0;j<num;j++)
{
if(strcmp(universities[j].name,name2)==0)
{
break;
}
}
if(j==num)
{
puts("\nUniversity not found.");
puts("1. Try another.");
puts("2. Back.");
puts("\nESC Exit.");
loop:
switch(getch())
{
case 49: goto again;
break;
case 50: free(university); return universities;
break;
case 27: exit(4);
default: goto loop;
}
}
/*************Basically the swapping itself********/
uni = universities[i];
universities[i]=universities[j];

universities[j] = uni;
printf("\n\nUniversities swapping successful!");
printf("\n\n1. Swap again.");
printf("\n2. Back.");
printf("\n\nESC Exit.");
loopy:
switch(getch())
{
case 49: return swap(universities,num);
case 50: return universities;
case 27: exit(4);
default: goto loopy;
}

}
univer* add(univer* universities, int* num)
{
fflush(stdin);
int i;
int n = *num;
char* name;
name = (char*)malloc(sizeof(char)*10);
char* rect;
rect= (char*)malloc(sizeof(char)*20);
univer uni;
univer* university;
university = (univer*)malloc(sizeof(univer)*(n+1));
for(i=0;i<n;i++)
{
university[i]=universities[i];
}
try_again:
system("cls");
printf("
ADD UNIVERSITY");
printf("\n\nEnter the name(max 10 characters):");
gets(name);
for(i=0;i<n;i++)
{
if(strcmp(name,universities[i].name)==0)
{
printf("\nUniversity already exists!");
puts("\n1. Add another.");
puts("2. Back.");
puts("\nESC Exit.");
loop:
switch(getch())
{
case 49: goto try_again;
break;
case 50: return universities;
break;
case 27: exit(4);
default: goto loop;
}
}
}
strcpy(uni.name,name);
n+=1;
printf("Enter the number of students:");

scanf("%d",&uni.nr_std);
printf("Enter the name of rector:");
fflush(stdin);
gets(rect);
strcpy(uni.rector, rect);
printf("Enter the number of faculties:");
scanf("%d",&uni.facs);
printf("Enter national rank:");
scanf("%d",&uni.rank);
university[n-1] = uni;
printf("\nUniversity successfully added.");
printf("\n\n1. Add another.");
printf("\n2. Back.");
printf("\n\nESC Exit.");
*num = n;
loopy:
switch(getch())
{
case 49: return add(university,num);
case 50: return university;
case 27: exit(5);
default: goto loopy;
}

}
void write_struct(FILE* data, univer* universities, int n)
{
int i;
data = fopen("database.txt","w");
for(i=0;i<n;i++)
{
fprintf(data,"%s-",universities[i].name);
fprintf(data,"%d-",universities[i].nr_std);
fprintf(data,"%s-",universities[i].rector);
fprintf(data,"%d-",universities[i].facs);
fprintf(data,"%d",universities[i].rank);
if(i!=n-1)
{
fprintf(data,"\n");
}
}
return 6;
}
univer* deletel(univer* universities, int *n)
{
char name1[10];
int num = *n;
int i, j;
univer* uni;
uni = (univer*)malloc(sizeof(univer)*num);
again:
j = 0;
system("cls");
printf("Enter the name of the university to be deleted:");
gets(name1);
for(i=0;i<num;i++)
{
if(strcmp(universities[i].name,name1)==0)
{

}
else
{

continue;

uni[j] = universities[i];
j++;
}
}
*n = j;
if(j==num)
{
puts("\nUniversity not found.");
puts("1. Try another.");
puts("2. Back.");
puts("\nESC Exit.");
switch(getch())
{
case 49: goto again;
break;
case 50: free(uni); return universities;
break;
case 27: exit(4);
}
}
del:
puts("University successfully deleted.");
puts("1. Try another.");
puts("2. Back.");
puts("\nESC Exit.");
switch(getch())
{
case 49: goto del;
break;
case 50: return uni;
break;
case 27: exit(4);
}
return uni;
}
univer* sort(univer* universities, int n)
{
univer* uni;
uni = (univer*)malloc(sizeof(univer)*n);
int i, j, k, index = 0;//index = variable for controlling currently
chosen category of sorting(1 from cat array)
int cat[5] = {1,0,0,0,0};
int keyc;
int lengths[5] = {10,17,25,11,6};
int* sorter;
sorter = (int*)malloc(sizeof(int)*n);
char** names;
names = (char**)malloc(sizeof(char*)*n);
for(i=0;i<n;i++)
{
names[i] = (char*)malloc(sizeof(char)*30);
uni[i] = universities[i];
}

int* nums;
nums = (int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
strcpy(names[i],universities[i].name);
}
sorter = ssort(names, n);
for(i=0;i<n;i++)
{
uni[i]=universities[sorter[i]];
}
while(1)
{
system("cls");
printf("Choose the category to sort the database:\n\n ");
printf("\n ");
/**Output categories on the screen*/
for(j=0;j<5;j++)
{
for(k=0;k<lengths[j]+2;k++)
{
printf("%c",(cat[j]==0)?177:178);
}
}
for(i=0;i<5;i++)
{
switch(i)
{
case 0:printf("\n %c%10s%c",(cat[i]==0)?177:178,"NAME",
(cat[i]==0)?177:178);
break;
case 1:printf("%c%17s%c",(cat[i]==0)?177:178,"STUDENT NUMBER",
(cat[i]==0)?177:178);
break;
case 2:printf("%c%25s%c",(cat[i]==0)?177:178,"RECTOR NAME",
(cat[i]==0)?177:178);
break;
case 3:printf("%c%11s%c",(cat[i]==0)?177:178,"FACULTIES",
(cat[i]==0)?177:178);
break;
case 4:printf("%c%6s%c\n ",(cat[i]==0)?177:178,"RANK",
(cat[i]==0)?177:178);
break;
}
}
for(j=0;j<5;j++)
{
for(k=0;k<lengths[j]+2;k++)
{
printf("%c",(cat[j]==0)?177:178);
}
}
output(uni, n, 0);
printf("\n\n\nUse <- and -> to navigate through
categories.\n\nSPACE Stop scrolling.");
/**Check for the pressed key to be arrowleft or arrowright*/
keyc = getch();
if(keyc==224)

keyc = getch();
switch(keyc)
{
case 77://go right
if(index==4)
{
continue;
}
else{
index++;
cat[index] = 1;
cat[index-1] = 0;
}
break;
case 75://go left
if(index==0)
{
continue;
}
else{
index--;
cat[index] = 1;
cat[index+1] = 0;
}
break;
}

}
else if(keyc==32)
{
del:
puts("\n1. Save sorted universities.");
puts("2. Back.");
puts("\nESC Exit.");
switch(getch())
{
case 49: return uni;
break;
case 50: return universities;
break;
case 27: exit(4);
}
}
/** Sorting itself based on category*/
switch(index)
{
case 0:
for(i=0;i<n;i++)
{
strcpy(names[i],universities[i].name);
}
sorter = ssort(names, n);
for(i=0;i<n;i++)
{
uni[i]=universities[sorter[i]];
}
break;
case 1:

10

for(i=0;i<n;i++)
{
nums[i] = universities[i].nr_std;
}
sorter = isort(nums, n);
for(i=0;i<n;i++)
{
uni[i]=universities[sorter[i]];
}
break;
case 2:
for(i=0;i<n;i++)
{
strcpy(names[i],universities[i].rector);
}
sorter = ssort(names, n);
for(i=0;i<n;i++)
{
uni[i]=universities[sorter[i]];
}
break;
case 3:
for(i=0;i<n;i++)
{
nums[i] = universities[i].facs;
}
sorter = isort(nums, n);
for(i=0;i<n;i++)
{
uni[i]=universities[sorter[i]];
}
break;
case 4:
for(i=0;i<n;i++)
{
nums[i] = universities[i].rank;
}
sorter = isort(nums, n);
for(i=0;i<n;i++)
{
uni[i]=universities[sorter[i]];
}
break;

}
}
return uni;

}
int* ssort(char** arr, int n)
{
int i, j, ch;
int* out;
out = (int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
out[i]=i;
}
char word[50];
for(j=n-1;j>0;j--){

11

for(i=0;i<j;i++)
{
if(recursort(arr,i,0))
{
strcpy(word,arr[i]);
strcpy(arr[i], arr[i+1]);
strcpy(arr[i+1], word);
/*-------------------*/
ch = out[i];
out[i] = out[i+1];
out[i+1] = ch;
}
}

}
return out;

int* isort(int* arr, int n)


{
int i, j, ch;
int* out;
out = (int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
out[i]=i;
}
for(j=n-1;j>0;j--){
for(i=0;i<j;i++)
{
if(arr[i]>arr[i+1])
{
ch = arr[i];
arr[i] = arr[i+1];
arr[i+1] = ch;
/*-------------------*/
ch = out[i];
out[i] = out[i+1];
out[i+1] = ch;
}
}
}
return out;
}
int recursort(char** arr, int i, int j)
{
if(arr[i][j]>arr[i+1][j]) return 1;
else if(arr[i][j]==arr[i+1][j] && j<(strlen(arr[i])-1) &&
j<(strlen(arr[i])-1)) return recursort(arr,i,j+1);
else return 0;
}
univer* modify(univer* universities, int n)
{
int i, j, k, num = 0;
char* name;
name = (char*)malloc(sizeof(char)*10);
int index = 0;
int lengths[5] = {10,17,25,11,6};
int keyc;

12

modify_another:
system("cls");
printf("Enter the name of the university to be modified:");
fflush(stdin);
gets(name);
for(i=0;i<n;i++)
{
if(strcmp(universities[i].name,name)==0) break;
}
if(i==n)
{
puts("\nUniversity not found!");
printf("\n\n1. Modify another.");
printf("\n2. Back.");
printf("\n\nESC Exit.");
get_char:
switch(getch())
{
case 49: modify(universities, n);
break;
case 50: return universities;
case 27: exit(8);
default: goto get_char;
}
}
while(1)
{
system("cls");
printf(" ");
for(j=0;j<5;j++)
{
for(k=0;k<lengths[j]+2;k++)
{
printf("%c",(j==index)?178:177);
}
}
for(j=0;j<5;j++)
{
switch(j)
{
case 0:printf("\n %c%10s%c",(j==index)?
178:177,universities[i].name,(j==index)?178:177);
break;
case 1:printf("%c%17d%c",(j==index)?
178:177,universities[i].nr_std,(j==index)?178:177);
break;
case 2:printf("%c%25s%c",(j==index)?
178:177,universities[i].rector,(j==index)?178:177);
break;
case 3:printf("%c%11d%c",(j==index)?
178:177,universities[i].facs,(j==index)?178:177);
break;
case 4:printf("%c%6d%c\n ",(j==index)?
178:177,universities[i].rank,(j==index)?178:177);
break;
}
}
for(j=0;j<5;j++)

13

for(k=0;k<lengths[j]+2;k++)
{
printf("%c",(j==index)?178:177);
}

/**Check for the pressed key to be arrowleft or arrowright*/


keyc = getch();
if(keyc==224)
{
keyc = getch();
switch(keyc)
{
case 77://go right
if(index==4)
{
continue;
}
else{
index++;
}
break;
case 75://go left
if(index==0)
{
continue;
}
else{
index--;
}
break;
}
}
else if(keyc==32)
{
printf("\n\n");
switch(index)
{
case 0:
printf("Enter a new name for %s:
",universities[i].name);
fflush(stdin);
gets(name);
if(name!=NULL)
{
printf("University %s renamed to
%s",universities[i].name, name);
strcpy(universities[i].name,name);
}
else{puts("University name can't be blank.");}
break;
case 1:
printf("Enter the new number of students for %s:
",universities[i].name);
scanf("%d",&num);
if(num>0)
{

14

printf("University %s has now %d


students",universities[i].name, num);
universities[i].nr_std = num;
}
else{puts("University can't have 0 students.");}
break;
case 2:
printf("Enter a new rector for %s:
",universities[i].name);
fflush(stdin);
gets(name);
if(name!=NULL)
{
printf("University %s's rector is now
%s",universities[i].name, name);
strcpy(universities[i].rector,name);
}
else{puts("Rector name can't be blank.");}
break;
case 3:
printf("Enter the new number of faculties for %s:
",universities[i].name);
scanf("%d",&num);
if(num>0)
{
printf("University %s has now %d
students",universities[i].name, num);
universities[i].facs = num;
}
else{puts("University can't have 0 faculties.");}
break;
case 4:
printf("Enter the new rank for %s:
",universities[i].name);
scanf("%d",&num);
if(num>0)
{
printf("University %s is now on %d
place",universities[i].name, num);
universities[i].rank = num;
}
else{puts("University can't be on 0th or less
place.");}
break;
}
printf("\n\n1. Modify another");
printf("\n2. Back");
printf("\n\nESC Exit.");
get_ch:
switch(getch())
{
case 49: goto modify_another;
break;
case 50: return universities;
case 27: exit(8);
default: goto get_ch;
}
}

15

//main.cpp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "univer.cpp"
int main()
{
FILE* data;
int num = 0;
int i = 0;
data = fopen("database.txt","rwb");
univer* universities = (univer*)malloc(sizeof(univer)*20);
universities = struct_array(data, &num);
while(1)
{
system("cls");
menu();
switch(getch())
{
case 49: fflush(stdin); search(universities,num);
break;
case 50: fflush(stdin); output(universities,num,1);
break;
case 51: universities = modify(universities, num);
break;
case 52: fflush(stdin); universities = swap(universities,num);
break;
case 53: fflush(stdin); universities = sort(universities, num);
break;
case 54: fflush(stdin); universities = add(universities,&num);
break;
case 55: fflush(stdin); universities = deletel(universities,
&num);
break;
case 27: fflush(stdin); fclose(data);
write_struct(data,universities,num); return(1);
break;
}
}
fclose(data);
return 0;
}

Results:
16

Output all

Sort:
By rector name:

By rank:

Bibliography
17

http://cprogramminglanguage.net/

http://devcentral.iftech.com/learning/tutorials/c-cpp/c
http://programmersclub.ru/01/
http://itassistant.org/programare/

18