Documente Academic
Documente Profesional
Documente Cultură
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
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:
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.
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;}
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 ;
}
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;
}
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: