PDA

View Full Version : کد جستجو



sunlight_sky
جمعه 19 تیر 1388, 13:48 عصر
با سلام کاربر جدید هستم و از عضویت در این انجمن خوشحالم
زیاد با قوانین آشنا نیستم و کاستی ها را ببخشید

برنامه ای نوشتم که برای مطب و در قسمت جستجو مشکل دارم و راهنمایی می خواهم (با code سرچ کنم) با تشکر




include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 100
struct student {
char name [20] ;
int code, sex,nexttime ;
char visit[256] ;
char add[256];
char tel[50];

} list[SIZE] ;
int menu(void) ;
void init_list(void) , enter(void) ;
void display(void) , save(void) ;
void search(void) ;
void load(void) ;
int main()
{
init_list();
for(;;) {
switch(menu()) {
case 'e': enter(); break;
case 'd': display(); break ;
case 's': save() ; break;
case 'r': search() ; break ;
case 'l' : load() ; break ;
case 'q': exit(0) ;
}//end of switch
} //end of for
}
//**********************
void init_list(void)
{
register int t;
for (t = 0 ; t < SIZE ; t++)
*(list[t].name) = '\0' ;
}
//***********************
void enter(void)
{
register int i ;
int row ;
char numstr[20];
for(i = 0; i < SIZE; i++)
if (!(*list[i].name))
break ;
if(i == SIZE) {
printf("\n list is full press a key ...") ;
getch() ;
}
clrscr() ;
gotoxy(10, 2) ;
puts("<< INPUT DATA >>");
gotoxy(1, 3) ;
printf(" name code visit sex(1, 2) add tel next" ) ;
gotoxy(1,4) ;
puts (" ------- ---- ------- --- --------------- ------- -- ");
row = 5 ;
for(;;) {
gotoxy(1, row);
gets(list[i].name);
if(! (*list[i].name))
break ;
gotoxy(17, row);
gets(numstr) ;
list[i].code = atoi(numstr) ;
gotoxy(21, row) ;
gets(list[i].visit);
if(! (*list[i].name))
break ;
gotoxy(39, row) ;
gets(numstr) ;
list[i].sex = atoi(numstr) ;
gotoxy(50, row);
gets(list[i].add);
if(! (*list[i].name))
break ;
gotoxy(68, row);
gets(list[i].tel);
if(! (*list[i].name))
break ;
gotoxy(77, row) ;
gets(numstr) ;
list[i].nexttime = atoi(numstr) ;
row ++ ;
i ++ ;
}
}
//***********************
void display(void)
{
register int t ;
int row ;
clrscr() ;
gotoxy(10, 2) ;
puts("<< OUTPUT DATA >> ") ;
gotoxy(1, 3) ;
printf(" name code visit sex(1,2) add tel next time") ;
gotoxy(1, 4);
printf("------- ---- ------------- -- ------------ ------ -----") ;
row = 7 ;
for(t = 0 ; t < SIZE ; t++)
if(*list[t].name) {
gotoxy(1, row) ;
printf("%s", list[t].name);
gotoxy(21, row) ;
printf("%d", list[t].code);
gotoxy(25, row) ;
printf("%s", list[t].visit) ;
gotoxy(42, row) ;
printf("%d", list[t].sex) ;
gotoxy(51, row) ;
printf("%s", list[t].add) ;
gotoxy(65, row) ;
printf("%s", list[t].tel) ;
gotoxy(73, row) ;
printf("%d", list[t].nexttime) ;



row ++ ;
} //end of if
gotoxy(5, row+2);
printf(" press a key ...");
getch() ;
}
//***********************
void save(void)
{
FILE *fp ;
register int i ;
fp = fopen("st", "wb");
if(!fp) {
printf("\n cannot open file press a key ...");
getch() ;
return ;
}
for(i = 0 ; i < SIZE ; i++)
if(*list[i].name)
fwrite(&list[i], sizeof(struct student), 1, fp) ;
clrscr() ;
gotoxy(20, 10) ;
printf("data saved.press a key.");
getch() ;
}
//************************
void search(void)
{
؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟ ؟؟
}
//*****************
void load(void)
{
FILE *fp ;
register int i ;
fp = fopen("st", "rb");
if(!fp) {
printf("\n cannot open file press a key ...");
getch() ;
return ;
}
init_list() ;
for(i = 0 ; i < SIZE ; i++) {
fread(&list[i], sizeof(struct student), 1, fp);
if(feof(fp)) {
clrscr() ;
gotoxy(20,10) ;
printf("data loaded.press a key.");
getch() ;
return ;
}//end of if
}
}

//*******************
int menu(void)
{
char s[10] ;
clrscr();
do {
gotoxy(20, 4);
printf("E) enter data ");
gotoxy(20, 6);
printf("D) display on screen");
gotoxy(20, 8);
printf("s) save file");
gotoxy(20, 10);
printf("R) search in file");
gotoxy(20, 12);
printf("l) loade in file");
gotoxy(20, 14);
printf("Q) quit");
gotoxy(20, 16);
printf("enter your select:");
gets(s) ;
} while(!strchr("edsrlq", tolower(*s)));
return tolower(*s) ;
}

tdkhakpur
جمعه 19 تیر 1388, 16:04 عصر
سلام
اگر مشکلی داشت ارسال کنید.


************************
struct student search(void)
{
ّ FILE *fp;
struct student tmpst;
bool Find = fasle;
char Name[20];
fp = fopen("st", "rb"
if( fp!=NULL)
{
cout<<"\n"<< "Enter Name >";
cin>>Name;
while(!feof(fp) && !Find )
{
fread( tmpst, sizeof(tmpst), 1, fp)
if( strlen(Name)==strlen(tmpst.name) && memcmp(tmpst.name, Name, strlen(Name))==0)
Find = true;
}
}
fclose(fp);
return tmpst;
}
//*****************

اگر اشتباه املایی داشت اصلاح کنید دستی کد کردم.
تابع رکورد پیدا شده را بر میگرداند.