!!!! I got stucked with this !!!!

D

dreamcatcher

#include <io.h>

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <conio.h>

#include <ctype.h>

#include <string.h>



#define GENRAND(n) (rand()%n)



#define UNOCCUPIED -1 // to test whether the machine occupied

#define ENTER 1

#define EXIT 0



#define DATABASE "database.dat" // the database file name



const char *mainMenu[]={

/*

"+================================================-
=======+",

"| 1. Log on to system.
|",

"| 2. Log out from system.
|",

"| 3. Record inquire
|",

"| 4. Data statistics
|",

"| 5. Credit
|",

"| 6. This Menu
|",

"| 7. Help
|",

"| 8. Exit
|",

"+==================================================-
=====+\n",

NULL

};



const char *credit[]={

"+======================================================+",

"| CREDIT
|",

"+=====================================================-
=+",

"| THE FOLLOWING STUDENTS FROM
|",

"| FACULTY OF COMPUTER SCIENCE AND TECHNOLOGY CLASS 024
|",

"| WHO HAVE MADE THIS SYSTEM POSSIBLE.
|\n",

"+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-
~+",



NULL

};



const double payPerHour=1.00; // payment rate on an hourly basis

const unsigned totalMachine=100; // total machines

int statistics[totalMachine];



typedef struct tagRECORD { // the management record
structure

char name[8]; // student name

char studentID[9]; // student ID

char logonTime[25]; // log on time

char logoutTime[25]; // log out time

int machineID; // machine ID

clock_t logon; // log on clock

clock_t logout; // log out clock

}RECORD;



void clrscr(void) { }; //I don¡¯t know how to clear the screen under
Visual C++



void onInitialization(void) {

int i;

srand((unsigned)time(NULL)); // seed



for(i=0;i<totalMachine;++i)

statistics=UNOCCUPIED; // initialize the array

}



int assignID(void) { // randomly distribute the available machine ID

int i,r; // to each student log on

int id=0;

FILE *fp;

RECORD record;



fp=fopen(DATABASE,"rb");

if(fp==NULL) {

errorMessage("assignID()");

}



for(i=0;!feof(fp) && i<totalMachine;++i) {

fread(&record,sizeof(record),1,fp);

statistics=record.machineID;

}



for(r=0;r<i && statistics[r]!=-1;++r) {

id=GENRAND(totalMachine);

while(statistics[r]==id)

id=GENRAND(totalMachine);

}



fclose(fp);



return 0;

}



void availableComputers(void) {

int i;

FILE *fp;

RECORD record;



if(isFileExist(DATABASE)==true) {

fp=fopen(DATABASE,"rb");

if(fp==NULL) {

errorMessage("availableComputer()");

}



for(i=0;!feof(fp) && i<=totalMachine;++i) {

fread(&record,sizeof(record),1,fp);

statistics=record.machineID;

if(statistics==UNOCCUPIED)

printf("unoccupied machine
#%d\n",statistics);

}

fclose(fp);

}

else {

errorMessage("database doesn't exist");

}



}



FILE *lookupStudent(char *sid) {

int flag=0,result;

FILE *fp;

RECORD record;





fp=fopen(DATABASE,"rb");

if(fp==NULL) {

errorMessage("lookupStudent()");

}

while(!feof(fp)) {

fread(&record,sizeof(record),1,fp);

result=strcmp(record.studentID,sid); // if the
student ID exist

if(result==0) {

flag=1;

break;

}

else {

flag=0;

continue;

}

}



return ((flag==1)?fp:NULL); //return the specific record
in the data

// to which the FILE *fp points



}



/* BUG!! BUG !!

bool isBlankLine(char *line) {

char *tmp=line;

bool blank=true;



while(*tmp++!='\0') {

if((*tmp!=' ') || (*tmp!='\n')) {

blank=false;

break;

}

}

return blank;

}

*/





void logStudentInfo(RECORD *record) {



validateName(record->name); // gets student name

validateStudentID(record->studentID); // gets student ID



record->machineID=assignID(); // gets machine ID

strcpy(record->logonTime,currentTime());// record log on time

record->logon=clock(); // clock the time

}



void storeStudentInfo(RECORD *record) {

FILE *fp;



if(isFileExist(DATABASE)==true) { // if database
file exist

fp=fopen(DATABASE,"ab"); // open in APPEND mode

if(fp==NULL) {

errorMessage("storeStudentInfo()");

}

}

else {

fp=fopen(DATABASE,"w+b"); // if doesn't
exist, CREATE it

if(fp==NULL) {

errorMessage("storeStudentInfo()");

}

}



fwrite(record,sizeof(*record),1,fp); // write to the database



fclose(fp);

}



double wholeCost(RECORD *record) {

double duration;

duration=(double)((record->logout)-(record-
>logout))/CLOCKS_PER_SEC; // time elapsed

return payPerHour*duration; // money should pay

}



void showRecordContent(RECORD *record) {



printf("Student name:\t%-8s\n",record->name);

printf("Student ID :\t%-9s\n",record->studentID);

printf("Machine ID :\t%-3d\n",record->machineID);

printf("Log on time :\t%-25s",record->logonTime);

printf("Log out time:\t%-25s",record->logoutTime);



}



void logoutSystem(char *sid) {

char *timeIt;

FILE *fp;

RECORD record;

double totalPay;



fp=lookupStudent(sid);

if(fp==NULL) {

errorMessage("no such record exist");

}

if(!feof(fp))

fread(&record,sizeof(record),1,fp);



strcpy(record.studentID,sid);

timeIt=currentTime();

strcpy(record.logoutTime,timeIt);

record.logout=clock();

totalPay=wholeCost(&record);



printf("you are logging out system....\n");

showRecordContent(&record);

printf("Payment:\t%-lf\n",totalPay);



fclose(fp);

}



void inquireRecord(char *sid) {

FILE *fptr;

RECORD record;

double totalPay;

FILE *fp;



fp=fopen(DATABASE,"rb");

if(fp==NULL) {

errorMessage("assignID()");

}

fptr=lookupStudent(sid);

if(fptr==NULL) {

printf("no such record.\n");

exit(1);

}

if(!feof(fptr))

fread(&record,sizeof(record),1,fptr);

if(record.logoutTime==NULL) { //compare with there input student ID

fprintf(stderr,"the student hasn't log out yet\n");

}

totalPay=wholeCost(&record);



showRecordContent(&record);

printf("Payment:\t%-lf\n",totalPay);



fclose(fp);

}



void dataStatistics(void) { // balance for the day

FILE *fp;

RECORD record;

double individualPayment; // individual balance

double paySum=0; // balance



fp=fopen(DATABASE,"rb");

if(fp==NULL) {

errorMessage("dataStatistics()");

}

while(!feof(fp)) {

fread(&record,sizeof(record),1,fp);

individualPayment=wholeCost(&record); // get
individual balance

paySum+=individualPayment; // gets sum

}

printf("today bill account:\n");

printf("%f\n",paySum); // ummmm......how much ?



fclose(fp);

}



void showMode(int tag,char *mode) {

printf("%s %s mode...\n",((tag==1)?"entering":"exiting"),mode);

}



void logonSystem(void) {

RECORD r={NULL,NULL,NULL,NULL,0,0,0}; //initialize the strucutre
variable

int ch;



while(1) {



logStudentInfo(&r);

storeStudentInfo(&r);

printf("more ?\n: "); // more to log on ?



ch=getche(); // wait for key
press

if(ch=='y') {

printf("\n"); // hurray !

continue; // enter the
next loop

}

else {

printf("\n"); // hey man,
that sucks!

showMode(EXIT,"");

break; // exit from the
loop



}

}

}



void inputStudentID(char *sid) { // input student ID for
later process

printf("enter the student ID for inquire: ");

scanf("%s",sid);

}





void showMainMenu(void) { // the main functionality
reside here



int choice;

char sid[9]; // temporarily storing the
student ID



menu();

while(1) {

choice=getch();

switch(choice) {

case '1':

showMode(ENTER,"log on");

logonSystem();
// log on

break;

case '2':

showMode(ENTER,"log out"); // log
out

inputStudentID(sid);

logoutSystem(sid);

break;

case '3':

showMode(ENTER,"inquire"); //
inquire

inputStudentID(sid);

inquireRecord(sid);

break;

case '4':

showMode(ENTER,"statistics"); //
balance

dataStatistics();

break;

case '5':

credits(); // program
credits

break;

case '6':

menu();

break;

case '7':

systemHelp(); // system help

break;

case '8':

clrscr(); // bye bye !

fprintf(stderr,"EXITING FROM
SYSTEM...");

fprintf(stderr,"BYE BYE!\n");

exit(1);

default:

fprintf(stderr,"invalid option\n");

}

};



}



int main(void)

{

onInitialization();

clrscr();

showMainMenu();



return 0;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top