Sunteți pe pagina 1din 13

Ministerul Educaţiei al Republicii Moldova

Universitatea Tehnică a Moldovei

Departamentul Ingenerie Softwaer si Automatica

REPORT
Laboratory work nr. 1
at Computer Programming

A efectuat:
st. gr. FAF-171(L.engleza) A.Filip

A verificat:
dr., conf.univ. M. Kulev

Chişinău 2018
Laboratory work no.1
Subject: Implementation of abstract data type "Linked List" in C

Objective: Obtaining the practical skills to implement an data structure in C


language.

Task: Develop a program to process a simple list of automotive cartels using at least 5 fields
displaying the on-screen options menu.
Writing 3 files in C language for deploying (2 files) and using (1 file - program with the main
function) of the Data Structure.
1. The header file with the tablou.h extension that describes the layout of the simple linked list
element and the prototypes of the functions that provide for the simple chaining process.
2. The file with the tablou.cpp or tablou.c extension, which contains the codes (implementations) of
all the declared functions in the header file.
3. User's file - the main program program for the simple-to-to-do list, displaying the on-screen
options menu.

The process:

Main notions of theory and used methods:

Structure is another user defined data type available in C that allows to combine data items of
different kinds. To define a structure, you must use the struct statement. The struct statement defines
a new data type, with more than one member. The structure tag is optional and each member
definition is a normal variable definition, such as int i; or float f; or any other valid variable
definition. At the end of the structure's definition, before the final semicolon, you can specify one or
more structure variables but it is optional.

To access any member of a structure, we use the member access operator (.). The member access
operator is coded as a period between the structure variable name and the structure member that we
wish to access. You would use the keyword struct to define variables of structure type.

Description of file contents and program code in C:


File tablou.h - This file describes the data structure and prototypes of the functions that provide the
single-line list processing operations.
Date Structure:
typedef struct car
{char marca[40];
char model[40];
int anul;
char combustibil[40] ;
float capacitatea;
} car;

void inputs(car *S, int n);


void outputs(car *S, int n);
int searchs( car *S, int n, char *key);
int searchs_m( car *S, int n, char *key1);
void swap( car *S, int k1, int k2);
void modifys(car* S, int k);
car* inserts(car *S, int n, int k,car new);
car* change(car *S, int n, int k,car new);
void sorts( car *S, int n);
car* dels(car *S, int n, int k);
car* push_back(car *S, int n,car new);
car* push_front(car *S, int n,car new);

 File tablou.c - that file contains the codes (implementation) of all the declared
functions in the header file tablou.h.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include"tablou.h"
void inputs(car *S, int n)
{ int i;
puts("\n\t introduceti info despre automobile:\n ");
for( i=0; i<n; i++)
{printf("\n car %d :\n", i+1);
printf("marca : ");fflush(stdin); gets(S[i].marca);
printf("model : "); gets(S[i].model);
printf("anul : ");scanf("%d", &S[i].anul);
printf("combustibil : "); fflush(stdin);gets(S[i].combustibil);
printf ("capacitatea : "); scanf("%f", &S[i].capacitatea);
}
return;
}
void outputs(car *S, int n)
{int i;
printf("\n\t info about car:\n");
for(i=0; i<n; i++)
{
printf("*--------------------------------*\n");
printf("automobilul nr. %d \n",i+1);
printf("marca :");puts( S[i].marca);
printf("model :");puts(S[i].model);
printf("anul :%d \n",S[i].anul);
printf("combustibil :");puts(S[i].combustibil);
printf("capacitatea :%.3f litri \n",S[i].capacitatea);

}
return;}

int searchs( car *S, int n, char *key)


{
int i;
for(i=0; i<n; i++)
{
if (strcmp(S[i].marca, key) == 0)
{
printf("model : %s \n",S[i].model);

return ;
}
int searchs_m( car *S, int n, char *key1)
{
int i;
for(i=0; i<n; i++)
{
if (strcmp(S[i].model, key1) == 0)
{
printf("\n\t info despre automobilul nr. %d: ",i+1);
printf("marca :");puts( S[i].marca);
printf("model :");puts(S[i].model);
printf("anul :%d \n",S[i].anul);
printf("combustibil :");puts(S[i].combustibil);
printf("capacitatea :%.3f litri \n",S[i].capacitatea);

}
else{("asa model nu exista!!!");}
}
return ;
}

void swap( car *S, int k1, int k2)


{ car t;
t = S[k1-1];
S[k1-1] =S[k2-1];
S[k2-1] = t;
return;
}

void modifys(car* S, int k)


{int flag;
printf("doriti sa modificati marca (1/0): ");
scanf("%d", &flag);
if (flag == 1)
{printf("marca: "); fflush(stdin); gets(S[k].marca);}
printf("doriti sa modificati modelul (1/0): ");
scanf("%d", &flag);
if (flag == 1)
{printf("model: "); fflush(stdin); gets(S[k].model);}
printf("doriti sa modificati anul (1/0): ");
scanf("%d", &flag);
if (flag == 1)
{printf("anul: "); scanf("%d",&S[k].anul);}
printf("doriti sa modificati tipul de combustibil (1/0): ");
scanf("%d", &flag);
if (flag == 1)
{printf("combustibil : "); scanf("%i", &S[k].combustibil);}
printf("doriti sa modificati capacitatea(1/0):");
scanf("%d", &flag);
if (flag == 1)
{printf("capacitatea: "); scanf("%f", &S[k].capacitatea);}
flag = 0;
return;
}
car* inserts(car *S, int n, int k,car new)
{ int i;
car *p=(car*)realloc(S,(n+1)*sizeof (*p) );
if (p==NULL) {return p;}
for(i=n; i>k; i--)
{ p[i]= p[i-1]; }
p[k]=new;
return p;
}
car* change(car *S, int n, int k,car new)
{ int i;
car *p=(car*)realloc(S,(n+1)*sizeof (*p) );
if (p==NULL) {return p;}
p[k]=new;

return p;
}
void sorts( car *S, int n)
{ int i, k;
car t;
for(i=0; i<n-1; i++)
{
for( k=0; k<n-1-i; k++)
{if (strcmp(S[k].marca, S[k+1].marca)>0)
{
t=S[k];
S[k]=S[k+1];
S[k+1]=t;
}
}
}
return;
}

car* dels(car *S, int n, int k)


{ int i;
car *p;
for(i=k; i<n-1; i++)
{S[i]= S[i+1];}
p= (car *)realloc(S, (n-1)*sizeof(*p) );
if (p==NULL) {return p;}
return p;
}
car* push_back(car *S, int n,car new)
{ int i;
car *p=(car*)realloc(S,(n+1)*sizeof (*p) );
if (p==NULL) {return p;}
p[n]=new;
return p;
}
car* push_front(car *S, int n,car new)
{ int i;
car *p=(car*)realloc(S,(n+1)*sizeof (*p) );
if (p==NULL) {return p;}
for(i=n; i>0; i--)
{ p[n]= p[n-1]; }
p[0]=new;
return p;
}

 File lab1.c - user file - the program main with function for processing the list,
with the on-screen options menu displayed.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include"tablou.h"

int main( )
{
car *S, new, *p;
int nm, n, i , k, k1,k2;
char key[40],key1[40], fname[40];
while( 1 )
{
puts("\n \t \t menu:\n");
puts("\n 1. alocarea dinamica a memoriei ");
puts("\n 2. introducerea datelor ");
puts("\n 3. afisarea datelor");
puts("\n 4. cautarea unui element");
puts("\n 5. modificarea unui element");
puts("\n 6. schimbarea a doua elemente");
puts("\n 7. eliberarea memoriei");
puts("\n 8. inserarea unui nou element");
puts("\n 9. inlocuirea unui element");
puts("\n 10.Stergerea unui element");
puts("\n 11.Sortarea elementelor");
puts("\n 12.Push_back");
puts("\n 13.Push_front");
puts("\n 0. exit\n");
printf("introduceti optiunea: ");
scanf("%d", &nm);

switch(nm)
{
case 1:
{printf("introduceti numarul de automobile: ");
scanf("%d", &n);
S=(car*)malloc(n*sizeof(*S));
if(S==NULL)
{puts("memoria nu a fost alocata ");
return 1;}
break;}
case 2:
{inputs(S, n);
break;}
case 3:
{outputs(S, n);
getch( );
break;}
case 4:
{printf("introduceti numele automobilului:");
fflush(stdin);
gets(key);
if (searchs(S, n, key)!=NULL)
{printf("ce model doriti sa vedeti ? \n");
fflush(stdin);
gets(key1);
searchs_m(S, n, key1);}
else{printf(" automobilul: %s nu a fost gasit ", key);}
break;}
case 5:
{printf("introduceti pozitia pentru modificare: ");
scanf("%d", &k); modifys(S, k-1);
getch( );
break;}
case 6:
{printf("introduceti 2 pozitii pentru schimbare: ");
scanf("%d%d", &k1, &k2);
swap(S, k1, k2);
getch( );
break;}
case 7:
{free(S);
S = NULL;
getch( );
break;}
case 8:
{printf("\n introduceti informatia despre automobil: \n");
printf("marca : "); fflush(stdin);gets(new.marca);
printf("model : "); scanf("%s",&new.model);
printf("anul : "); scanf("%d",&new.anul);
printf("combustibil : ");fflush(stdin); scanf("%s",&new.combustibil);
printf("capacitatea : "); scanf("%f",&new.capacitatea);
printf("introduceti pozitia de inserare: ");
scanf("%d", &k);
p=inserts(S, n, k-1, new); if (p==NULL)
{puts("memory was not reallocated");}
else {S=p;}
n=n+1;
getch( );
break;}

case 9:
{printf("introduceti pozitia automobilului pe care doriti sa il schimbati: ");
scanf("%d", &k);
printf("\nintroduceti informatia despre automobil: \n");
printf("marca : "); fflush(stdin);gets(new.marca);
printf("model : "); scanf("%s",&new.model);
printf("anul : "); scanf("%d",&new.anul);
printf("combustibil : ");fflush(stdin); scanf("%s",&new.combustibil);
printf("capacitatea : "); scanf("%f",&new.capacitatea);
p=inserts(S, n, k-1, new); if (p==NULL)
{puts("memoria nu a fost alocata");}
else {S=p;}
getch( );
break;}
case 10:
{printf("introduce pozitia care urmeaza sa fie stearsa: ");
scanf("%d", &k);
p=dels(S, n, k-1); if (p==NULL)
{puts("memoria nu a fost realocata");}
else { S= p;}
n=n-1;
getch( );
break;}
case 11:
{sorts(S, n);
getch( );
break;}
case 12:
{printf("\n introduceti informatia despre automobil: \n");
printf("marca : "); fflush(stdin);gets(new.marca);
printf("model : "); scanf("%s",&new.model);
printf("anul : "); scanf("%d",&new.anul);
printf("combustibil : ");fflush(stdin); scanf("%s",&new.combustibil);
printf("capacitatea : "); scanf("%f",&new.capacitatea);
p=push_back(S, n, new); if (p==NULL)
{puts("memory was not reallocated");}
else {S=p;}
n++;
getch( );
break;}
case 13:
{printf("\n introduceti informatia despre automobil: \n");
printf("marca : "); fflush(stdin);gets(new.marca);
printf("model : "); scanf("%s",&new.model);
printf("anul : "); scanf("%d",&new.anul);
printf("combustibil : ");fflush(stdin); scanf("%s",&new.combustibil);
printf("capacitatea : "); scanf("%f",&new.capacitatea);
p=push_front(S, n, new); if (p==NULL)
{puts("memory was not reallocated");}
else {S=p;}
n++;
getch( );
break;}
case 0:
{ exit(0);
return 0;}
default :{
puts("\n asa optiune nu exista");
getch( ); break;}
}
}
}

Main MENU:

1 option:
2 option:

3 option:
Concluzie:
In this laboratory work we obtained the skill :how to optimize our program by
repartization of code in file.This help us to obtain a program which can be
easier to modify and understandable.

Bibliografie:

1- Sarcina de lucru pentru „Lucrarea de laborator 1 la SDA”. M.Kulev

S-ar putea să vă placă și