#include <stdio.h> #include <math.h> #include <conio.h> #include <iostream.h> #include <string.h> #include <stdlib.h> main(void) { struct record { char name[15]; double price; int numb, clockrate, clock; }; record *base,*tmpbase; FILE *fptr; char fpath[256]=""; int ret=0,nor=0,i=0,ch,nr; clrscr(); printf("Will Enter way to database:\n"); scanf("%s",&fpath); fptr=fopen(fpath,"rb+"); if (fptr==0) fptr=fopen(fpath,"wb+"); while (fptr==0) { printf("\nThe Mistake! Repeat attempt!\n"); getch(); fflush(stdin); clrscr(); printf("Will Enter way to database:\n"); scanf("%s",&fpath); fptr=fopen(fpath,"wb+"); } tmpbase=new record[1]; while (!feof(fptr)) { ret=fread(&tmpbase[0],sizeof(record),1,fptr); if (ret!=1) break; nor++; } delete []tmpbase; base=new record[nor]; fseek(fptr,0,0); i=0; for (i=0;i<nor;i++) fread(&base[i],sizeof(record),1,fptr); fclose(fptr); menu: clrscr(); printf(" Menu\n\n 1. Look the database\n 2. Add new record\n 3. Insert record\n 4. Save the database\n 5. Find clockrate\n 0. Exit\n\nChoice: "); ch=getch(); switch(ch) { case '1': goto ch1; case '2': goto ch2; case '3': goto ch3; case '4': goto ch4; case '5': goto ch5; case '0': goto exit; default: goto menu; } ch1: clrscr(); if (nor!=0) { printf(" # Name Clockrate Price Amount on storehouse \n"); for (i=0;i<nor;i++) printf("%3d %7s %8d %17.2lf %11d\n",i+1,base[i].name,base[i].clockrate,base[i].price,base[i].numb); printf("\nPress any key..."); } else printf("Database does not contain record..."); getch(); goto menu; ch2: clrscr(); printf(" Menu of the accompaniment new record\n\n"); nor++; tmpbase=new record[nor]; for (i=0;i<nor-1;i++) tmpbase[i]=base[i]; delete []base; base=new record[nor]; for (i=0;i<nor;i++) base[i]=tmpbase[i]; delete []tmpbase; printf("Name: "); scanf("%15s",&base[nor-1].name); fflush(stdin); printf("Clockrate: "); ret=scanf("%d",&base[nor-1].clockrate); while (ret!=1) { printf("\nThe Misentry! Repeat attempt\n\n"); getch(); fflush(stdin); printf("Amount on storehouse: "); ret=scanf("%d",&base[nor-1].clockrate); } printf("Price: "); ret=scanf("%lf",&base[nor-1].price); while (ret!=1) { printf("\nThe Misentry! Repeat attempt\n\n"); getch(); fflush(stdin); printf("Price: "); ret=scanf("%lf",&base[nor-1].price); } printf("Amount on storehouse: "); ret=scanf("%d",&base[nor-1].numb); while (ret!=1) { printf("\nThe Misentry! Repeat attempt\n\n"); getch(); fflush(stdin); printf("Amount on storehouse: "); ret=scanf("%d",&base[nor-1].numb); } printf("\nPress any key..."); getch(); goto menu; ch3: clrscr(); printf(" Menu of the insertion new record\n\nWill Enter number a record: "); ret=scanf("%d",&nr); while ((nr<1)||(nr>nor+1)||(ret=0)) { printf("\nThe Misentry! Repeat attempt\n\n"); getch(); fflush(stdin); printf("Will Enter number a record: "); ret=scanf("%d",&nr); } nor++; tmpbase=new record[nor]; for (i=0;i<nor-1;i++) tmpbase[i]=base[i]; delete []base; base=new record[nor]; for (i=0;i<nr-1;i++) base[i]=tmpbase[i]; for (i=nr-1;i<nor-1;i++) base[i+1]=tmpbase[i]; delete []tmpbase; printf("Name: "); scanf("%15s",&base[nr-1].name); fflush(stdin); printf("Clockrate: "); ret=scanf("%d",&base[nr-1].clockrate); while (ret!=1) { printf("\nThe Misentry! Repeat attempt\n\n"); getch(); fflush(stdin); printf("Amount on storehouse: "); ret=scanf("%d",&base[nr-1].clockrate); } printf("Price: "); ret=scanf("%lf",&base[nr-1].price); while (ret!=1) { printf("\nThe Misentry! Repeat attempt\n\n"); getch(); fflush(stdin); printf("Price: "); ret=scanf("%lf",&base[nr-1].price); } printf("Amount on storehouse: "); ret=scanf("%d",&base[nr-1].numb); while (ret!=1) { printf("\nThe Misentry! Repeat attempt\n\n"); getch(); fflush(stdin); printf("Amount on storehouse: "); ret=scanf("%d",&base[nr-1].numb); } printf("\nPress any key..."); getch(); goto menu; ch4: clrscr(); printf("Will Enter way for conservation database:\n"); scanf("%s",&fpath); fptr=fopen(fpath,"wb+"); while (fptr==0) { printf("\nThe Mistake! Repeat attempt!\n"); getch(); fflush(stdin); clrscr(); printf("Will Enter way for conservation database:\n"); scanf("%s",&fpath); fptr=fopen(fpath,"wb+"); } for (i=0;i<nor;i++) fwrite(&base[i],sizeof(record),1,fptr); fclose(fptr); printf("\nPress any key..."); getch(); goto menu; ch5: clrscr(); printf("\nPress any key..."); getch(); goto menu; exit: delete []base; return 0; } |