Branch of Free International University of Moldova Informatics. Part 2 Practical exercises for topic16 Set of Operations on an Array of Structures. Menu of Operations
b. Program for database processing (all operations)
-1- -2- #include<stdio.h> // text of function for searching an element of #include<conio.h> // array by name of student #include<stdlib.h> int searchs( STUDENT *S, int n, char *key) #include<string.h> { // definition of structure data type STUDENT int i; typedef struct student for(i=0; i<n; i++) {char name[40]; { int year; if (strcmp(S[i].name, key) = = 0) int clas; { return i; } float average; } } STUDENT; return -1; //text of function for input an array of students } // from keyboard // text of function for swapping two elements void inputs(STUDENT *S, int n) void swap( STUDENT *S, int k1, int k2) { int i; { STUDENT t; puts(“\n\t enter info about students:\n “); t = S[k1]; for( i=0; i<n; i++) S[k1] =S[k2]; {printf(“\n student %d :\n”, i+1); S[k2] = t; printf(”name: ”); fflush(stdin); return; gets(S[i].name); } printf(“year: “); scanf(“%d”, &S[i].year); // text of function for sorting an array of students printf(“class: “); scanf(“%d”, &S[i].clas); // by names printf(“average: “); scanf(“%f”, &S[i].average); void sorts( STUDENT *S, int n) } { int i, k; return; STUDENT t; } for(i=0; i<n-1; i++) //text of function for output an array of students { // on the screen for( k=0; k<n-1-i; k++) void outputs(STUDENT *S, int n) {if (strcmp(S[k].name, S[k+1].name)>0) {int i; { puts(“\n\t info about students:\n”); t=S[k]; for(i=0; i<n; i++) S[k]=S[k+1]; { S[k+1]=t; printf(“%d. name: %s year: %d class: %d } average: %.2f\n ”, i+1, S[i].name, S[i].year, } S[i].clas, S[i].average); } } return; return;} } -3- -4- // text of function for modifying info of a student // text of student for inserting new student element void modifys(STUDENT* S, int k) STUDENT* inserts(STUDENT *S, int *n, int k, {int flag; STUDENT new) . printf(“do you want to modify the name (1/0): “); { int i; scanf(“%d”, &flag); STUDENT *p; if (flag = = 1) p= (STUDENT*)realloc(S, (*n+1)*sizeof(*p) ); {printf(“name: “); fflush(stdin); if (p= =NULL) {return p;} gets(S[k].name);} for(i=*n; i>k; i--) printf(“do you want to modify the year (1/0): “); { p[i]= p[i-1]; } scanf(“%d”, &flag); p[k]=new; if (flag = = 1) *n= *n+1; {printf(”year: ”); scanf(“%d”,&S[k].year);} return p; printf(“do you want to modify the class(1/0): “); } scanf(“%d”, &flag); // text of function for deleting a student element if (flag = = 1) STUDENT* dels(STUDENT *S, int *n, int k) {printf(”class: “); scanf(“%d”, &S[k].clas);} { int i; printf(“do you want to modify the average(1/0):“); STUDENT *p; scanf(“%d”, &flag); for(i=k; i<*n-1; i++) if (flag = = 1) {S[i]= S[i+1];} {printf(”average: “); scanf(“%f”, p= (STUDENT *)realloc(S, (*n-1)*sizeof(*p) ); &S[k].average);} if (p= =NULL) {return p;} return; *n= *n-1; } return p; // text of function for writing info in file } int writes(STUDENT *S, int n, char * fname) // text of main( ) function { int i; int main( ) FILE *fp; { // body of main( ) function fp=fopen( fname, “w”); if(fp==NULL){return 0;} STUDENT *S, new, *p; for(i=0; i<0; i++) int nm, n, i , k, k1,k2; {fprintf(fp, “%s %d %d %.2f\n”, S[i].name, char key[40], fname[40]; S[i].year, S[i].clas, S[i].average); } while( 1 ) // infinite while loop return 1;} { clrscr( ); // text of function for reading info from file puts(“\n \t \t menu:\n”); int reads(STUDENT *S, int n, char * fname) puts(“\n 1. dynamic memory allocation ”); { int i; puts(“\n 2. input an array from keyboard”); FILE *fp; puts(“\n 3. output an array on the screen”); fp=fopen( fname, “r”); if(fp==NULL){return 0;} puts(“\n 4. searching an element”); for(i=0; i<0; i++) puts(“\n 5. modifying an element”); {fscanf(fp, “%s%d%d%f”, S[i].name, puts(“\n 6. swapping two elements”); &S[i].year, &S[i].clas, &S[i].average); } puts(“\n 7. sorting elements”); return 1;} puts(“\n 8. writing elements in file”); // text of function for appending new student element puts(“\n 9. reading elements from file”); STUDENT* appends(STUDENT *S, int *n, puts(“\n10. freeing memory”); STUDENT new) puts(“\n11. appending an element”); { int i; puts(“\n12. inserting an element”); STUDENT *p; puts(“\n13. deleting an element”); p= (STUDENT *)realloc(S, (*n+1)*sizeof(*p) ); puts(“\n 0. exit\n“); if (p= =NULL) {return p;} printf(“enter number of option: ”); p[*n] = new; scanf(“%d”, &nm); *n= *n+1; return p; 2 -5- -6- switch(nm) // switch statement case 10: {case 1: free(S); S = NULL; printf(“enter number of students: “); getch( ); break; scanf(“%d”, &n); case 11: printf(“enter info of appending S= (STUDENT*)malloc(n*sizeof(*S)); student:”); if(S==NULL) printf(”name: ”); fflush(stdin); {puts(“memory was not allocated “); gets(new.name); return 1;} printf(“year: “); scanf(“%d”, &new.year); getch( ); break; printf(“class: “); scanf(“%d”, &new.clas); case 2: inputs(S, n); getch( ); break; printf(“average: “); scanf(“%f”, &new.average); case 3: outputs(S, n); getch( ); break; p=appends(S, n, new); if (p==NULL) case 4: {puts(“memory was not reallocated”);} else printf(“enter name of student for searching:“); { S=p; } fflush(stdin); gets(key); getch( ); break; k=searchs(S, n, key); case 12: if(k>=0) printf(“enter info of inserting student:”); {puts(“\n\t info about student: “); printf(”name: ”); fflush(stdin); printf(“%d. name: %s year: %d class: %d gets(new.name); average: %.2f\n ”, k+1, S[k].name, S[k].year, printf(“year: “); scanf(“%d”, &new.year); S[k].clas, S[k].average); } printf(“class: “); scanf(“%d”, &new.clas); else printf(“average: “); scanf(“%f”, &new.average); {printf(“ student: %s was not found “, key);} printf(“enter position for insertion: “); getch( ); break; scanf(“%d”, &k); case 5: p=inserts(S, n, k, new); if (p==NULL) printf(“enter position for modifying: “); {puts(“memory was not reallocated”);} scanf(“%d”, &k); modifys(S, k); else {S=p;} getch( ); break; getch( ); break; case 6: case 13: printf(“enter two positions for swapping: “); printf(“enter position for delition: “); scanf(“%d%d”, &k1, &k2); scanf(“%d”, &k); swap(S, k1, k2); p=dels(S, n, k); if (p==NULL) getch( ); break; {puts(“memory was not reallocated”);} case 7: sorts(S, n); getch( ); break; else { S= p;} case 8: getch( ); break; printf(“enter name of file: “); fflush(stdin); case 0: gets(fname); if (writes(S, n, fname)== 0) free(S); S = NULL; {puts(“file was not opend”); return 0; getch( ); break; default : case 9: puts(“\n wrong number of option”); printf(“enter name of file: “); fflush(stdin); getch( ); break; gets(fname); if (reads(S, n, fname)==0) } // end of switch statement puts(“file was not opend”); } // end of while loop getch( ); break; } // end of program and body of main( ) function
Lecturer: Mihail Kulev, 17.07.08, Sudan, Jonglei State, Bor town.