My Profile
Active Members
TodayLast 7 Days
more...
Awards & Gifts
Online Exams
Fresher Jobs
Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian
cities including Bangalore, Chennai, Hyderabad, Pune or Kochi
Resources
Find educational articles, blogs, discussion threads and other resources.
Colleges
Find details about any college in India or search for courses.
|
Complex programs combining sections
Posted Date: 14 Jun 2008 Resource Type: Articles/Knowledge Sharing Category: Computer & Technology
|
Posted By: Lenin Member Level: Diamond Rating: Points: 1
|
|
|
|
Complex programs combining sections This example program creates and accesses a simple database.
/**********************************************************/ /* A simple Data Base Program */ /**********************************************************/
# include # include # include
# define MAX 100 /* constant*/ struct addr { /*struct called list*/ char name[30] ; char street[40] ; char town[20] ; char county[20] ; char code[10] ; } list[MAX]; /* 100 records*/ main() { int choice;
init_list();
for(;;) { /* initialze the list */ choice = menu_select(); /* get user's selection*/ case 1: enter(); break; case 2: del(); break; /* enter a new entry */
/* delete an entry */ switch(choice) { case 3: show_list(); break; case 4: search(); break; case 5: save(); break; case 6: load(); break; case 7: exit(0); /* display the list */
/* find an entry */
/* save to disk */
/* read from disk */ } } }
/*********************************************************/ /* Function del */ /*********************************************************/ del() { int i; char str[255];
inputs("enter name: " , str , 30); i = find(str); if (i>=0) *list[i].name = '\0' ; else printf("not found\n") ; } /**********************************************************/ /* Function display */ /**********************************************************/ display(int i) { printf("%s\n" , list[i].name); printf("%s\n" , list[i].street); printf("%s\n" , list[i].town); printf("%s\n" , list[i].county); printf("%s\n" , list[i].code); }
/**********************************************************/ /* Function enter */ /**********************************************************/ enter() { int i;
for(;;) { i = find_free(); if(i<0) { /* find a free structure */ printf("list full\n"); return;
inputs("enter name: ", list[i].name,30); if(!*list[i].name) break; /* stop entering */ 16 de 20 16/03/2006 12:53 p.m.
}
inputs("enter street: ", list[i].street, 40); inputs("enter town: ", list[i].town, 20); inputs("enter county: ", list[i].county, 20); inputs("enter Postal code: ", list[i].code, 10); } }
/**********************************************************/ /* Function find */ /**********************************************************/ find(char *name) { int i; for(i=0 ; iif(!strcmp(name ,list[i].name)) break; if(i==MAX) return else return i; }
/**********************************************************/ /* Function find_free */ /**********************************************************/ find_free() { register int i; for(i=0; iif(!*list[i].name) return i; return }
/**********************************************************/ /* Function init_list */ /**********************************************************/ init_list() { register int i; for (i=0 ; i*list[i].name = '\0' }
/**********************************************************/ /* Function inputs */ /**********************************************************/ inputs( char *prompt , char *s , int count) { char str[255];
do { printf(prompt); gets(str); if(strlen(str) 1; 1; ;>=count) printf("\ntoo long\n"); } while(strlen(str)>=count);
strcpy(s , str); }
/**********************************************************/ /* Function load */ /**********************************************************/ load() { FILE *fp;
if ( (fp=fopen("mlist" , "rb")) == NULL) { printf("cannot open file\n"); return; } printf("\nloading file\n"); fread(list , sizeof list , 1 , fp); if (ferror(fp)) printf("An error occurred while reading file.\n"); fclose(fp); }
/**********************************************************/ /* Function menu_select */ /**********************************************************/
menu_select() { char s[80]; int c; printf("1. Enter a name\n") ; printf("2. Delete a name\n") ; printf("3. List the File \n"); printf("4. Search\n") ; printf("5. Save the file\n") ; printf("6. Load the file\n") ; printf("7. Quit\n")
do { ; printf("\nEnter your choice: "); gets(s); c = atoi(s); } while(c<0 || c>7);
return c; }
/**********************************************************/ /* Function save */ /**********************************************************/ save() { FILE *fp;
if ( (fp=fopen("mlist" , "wb")) == NULL) { printf("cannot open file\n"); return; } printf("\nsaving file\n"); fwrite(list , sizeof list , 1 , fp); if (ferror(fp)) printf("An error occurred while writing file.\n"); fclose(fp); }
/**********************************************************/ /* Function search */ /**********************************************************/ search() { int i; char name[30];
inputs("enter name to find: " , name , 30); if ((i=find(name))<0) printf("not found\n"); else display(i); }
/**********************************************************/ /* Function show_list */ /**********************************************************/ show_list() { int i;
for(i=0 ; iif(*list[i].name) { display(i); printf("\n\n"); } }
printf("\n\n"); }
[program]
The second example program uses functions to play a simple game of tic tac toe. #include #include
char matrix[3][3];
main() { char done;
printf("This is the game of tic tac toe...\n"); printf("You will be playing against the computer.\n") ;
done = ' ';
init_matrix();
do { disp_matrix(); get_player_move(); done = check(); if (done != ' ') break; get_computer_move(); done = check();
} while (done == ' ');
if (done == 'X') printf("\n\nYou won!!!\n"); else printf("I won!!!\n");
disp_matrix(); }
/**********************************************************/ /* fuction to initialise matrix */ /**********************************************************/ init_matrix() { int i , j ;
for (i=0 ; i<3 ; i++) for (j=0 ; j<3 ; j++) matrix[i][j] = ' '; }
/**********************************************************/ /* fuction to get players move */ /**********************************************************/ get_player_move() { int x , y ;
printf("Enter coordinates of your X: "); scanf("%d%d" , &x , &y);
x--; y--;
if (matrix[x][y] != ' ') { printf("Invalid move, try again...\n"); get_player_move(); } else matrix[x][y] = 'X'; }
/**********************************************************/ /* fuction to get computer move */ /**********************************************************/ get_computer_move() { int i , j ;
for (i=0 ; i<3 ; i++) { for (j=0 ; j<3 ; j++) if(matrix[i][j]==' ') break; if (matrix[i][j] == ' ') break; } if (i*j == 9) { printf("draw....\n"); exit(0); } else matrix[i][j] = 'O'; }
/**********************************************************/ /* fuction to display matrix */ /**********************************************************/ disp_matrix() { int t ;
printf(" 1 2 3\n"); for (t=0 ; t<3 ; t++) { printf(" %c | %c | %c %d" , matrix[t][0], matrix[t][1], matrix[t][2] , t+1); if (t!=2) printf("\n---|---|---\n"); } printf("\n"); }
/**********************************************************/ /* fuction to check matrix */ /**********************************************************/ check() { int i ;
for (i=0 ; i<3 ; i++) if(matrix[i][0] == matrix[i][1] && matrix[i][0] == matrix[i][2]) return matrix[i][0];
for (i=0 ; i<3 ; i++) if(matrix[0][i] == matrix[1][i] && matrix[0][i] == matrix[2][i]) return matrix[0][i];
if(matrix[0][0] == matrix[1][1] && matrix[1][1] == matrix[2][2]) return matrix[0][0];
if(matrix[0][2] == matrix[1][1] && matrix[1][1] == matrix[2][0]) return matrix[0][2];
return ' '; }
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|
Watch TV Channels
|