#include #include #include #include #define NAME 40 #define CLASS 10 #define SUBJ 5 #define NUM_REC 100 struct ST { char name[NAME]; char exam[CLASS]; int marks[SUBJ]; } ; //end of structure void showrec(ST *, int ); void addrec(ST *, int *); void edit(ST *, int); char lower(char ); int getchoice(char *); int load(ST *, char *); void save(ST *, char *, int); void find(ST *, int ); void show1(ST , char *); int total(ST ); void main(int argc, char *argv[]) { ST s[NUM_REC]; int cha,j=0; if(!argv[1]) { printf("\nUSAGE: drive:\...> MDB filename.mdb \n"); exit(0); } do { clrscr(); printf("%s : Is this a N: New File or " "U: File to Update? ",argv[1]); cha=getche(); cha=lower(cha); } while(cha!='n' && cha!='u') ; if(cha=='u') { j=load(s, argv[1]); if(j==-1) { puts("\nBAD FILENAME!! IT DOES NOT EXIST!!"); exit(1); } } else if(cha=='n') while(cha!=27) { addrec(s, &j); if(j>NUM_REC-1) break; //to break in case of XS rec. printf("\nPress ESC to stop, any other key to continue..."); cha=getche(); } showchoices: cha=getchoice(argv[1]); if(cha==0) { printf("\nSave File? [yn] "); if(lower(getche())=='y') save(s, argv[1], j); } else if(cha==1) { save(s, argv[1], j); goto showchoices; } else if(cha==2) { showrec(s,j); goto showchoices; } else if(cha==3) { find(s,j); goto showchoices; } else if(cha==4) { addrec(s, &j); goto showchoices; } else if(cha==5) { edit(s, j); goto showchoices; } else { putch(7); goto showchoices; } { // credits puts("\n\nThanks for using this program. "); puts("This program is (c) Hrishikeh M. 1998."); puts("Please feel free to MODIFY it in any way you like :)"); } // end of credits } // end of main() int getchoice(char *filename) { clrscr(); printf("File %s is active... ",filename); puts("\n"); puts("0: SAVE & EXIT"); puts("1: SAVE"); puts("2: LIST RECORDS"); puts("3: FIND "); puts("4: ADD RECORD"); puts("5: EDIT RECORD"); int ch; ch=getche(); ch-=0x30; // 30h is ascii 0 return ch; } // end of getchoice() int load(ST *s, char *filename) { int i=0; FILE*stream=fopen(filename, "rt"); if(!stream) { putch(7); return -1; //ERROR condition } for(i=0; iname); fgetc(stream); fscanf(stream,"%[^\n]",(s+i)->exam); fgetc(stream); for(int j=0; jmarks[j]=atoi(buff); } if(feof(stream)) { fclose(stream); break; } while(fgetc(stream)!='\n') ; } //i+=1; return i; } void save(ST *s, char *filename, int limit) { char ch; printf("\n\nCurrent filename is: %s. Change? [yn]",filename); ch=getche(); if(ch=='y' || ch=='Y') { printf("Enter New Filename: "); scanf("%12s",s); } FILE*save=fopen(filename,"wt"); for(int i=0; iname,(s+i)->exam); for(int j=0; jmarks[j]); } fprintf(save,"\n"); } fclose(save); } void addrec(ST *s, int *ip) { int it=*ip; puts("\n\n"); //3 line scroll (puts appends \n) fflush(stdin); printf("Enter Student's NAME: "); gets((s+it)->name); fflush(stdin); printf("Enter %s's Exam Number: ",(s+it)->name); gets((s+it)->exam); fflush(stdin); for(int i=0;i64 && ch<91) ch^=0x20; return ch; } void showhead() { clrscr(); gotoxy(59,1); printf("S U B J E C T S"); gotoxy(1,2); printf("REC# "); printf("%-30s","Student's Name"); printf("%-15s","Exam Number"); for(int j=0; jname); printf("%-15s",(s+i)->exam); for(int j=0; jmarks[j]); } printf("%-5d",total(*(s+i)) ); } getch(); } void find(ST *s, int limit) { int ch; do { puts("\nEnter the type of search desired:"); puts("N: Search for NAME of student "); puts("S: Subject-wise topper"); puts("O: Overall Topper"); ch=getche(); ch=lower(ch); } while(ch!='n' && ch!='s' && ch!='o'); if(ch=='n') { char *buff, *tofind; fflush(stdin); buff=(char*)malloc(NAME); // char is NOT sys. tofind=(char*)malloc(NAME); // dep. IS 1 byte. printf("Enter the name to Find: "); gets(tofind); int sl=strlen(tofind); for(int i=0;iname); buff+=sl; *buff='\0'; buff-=sl; if(!stricmp(buff, tofind)) { show1(*(s+i) , "Person found. Records are..." ); return; } } puts("Reqd. person NOT FOUND!! "); getch(); return; } else if(ch=='s') { do { printf("\nEnter the Subject No. (1..5): "); ch=getche(); ch-=0x30; } while(ch<0 && ch>5) ; ch-=1; int max=0; int nmax=1; for(int i=0;imarks[ch]>(s+i)->marks[max] ) { max=i; nmax=1; } else if( (s+i)->marks[ch]==max ) { nmax+=1; } else { continue; } } char *temp; temp=(char*)malloc(50); //char is if(nmax>1) // ** ALWAYS ** 1byte { char *t2; t2=(char*)malloc(5); //5 bytes itoa(nmax, t2, 10); temp="The first topper of "; strcat(temp,t2); strcat(temp, " is:"); } else temp="The Subject topper is: "; show1( *(s+max), temp); return; } else if(ch=='o') { int max=0, mp=0, nmax=0; for(int i=0;i total(*(s+max)) ) { max=total(*(s+i) ); mp=i; nmax=0; } if(total(*(s+i)) == max) { nmax+=1; } } char *temp; temp=(char*)malloc(50); if(nmax>0) { char t2[5]; itoa(nmax, t2, 10); temp="There are "; strcat(temp,t2); strcat(temp," Toppers, first of which is:"); } else temp="The overall topper is: "; show1( *(s+mp), temp); return; } } void show1(ST st, char *textstring) { puts("\n\n"); puts(textstring); printf("\nName : %s",st.name); printf("\nExam No. : %s",st.exam); puts("\nSubject Marks Are... "); for(int i=0;i