PDA

View Full Version : آموزش: كد برنامه كتابخانه



oranoos_mt
چهارشنبه 11 خرداد 1390, 00:59 صبح
سلام . من هركاري كردم نفهميدم getState() در برنامه زير چيكار ميكنه.
كد رو ميذارم شمام اجراش كنيد و اگه ميدونيد بهم بگيد getState() كارش چيه؟


#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define SIZE 5
//-------------class
struct book
{

public:
//-------------tavabe
book();
void enter();
char getState();
void print();
//-------------khososiat
int id;
int price;
long date;

char name[10];
char author[10];
char category[10];
//---------------------------
};

book::book()
{
id=0;
price=0;
date=0;
name[0]='\0';
author[0]='\0';
category[0]='\0';
}
//-------------------------------------

void book::enter()
{
clrscr();

cout <<"Name : ";
cin >>name;

cout<< "id : ";
cin >>id;

cout<<"author : ";
cin >>author;

cout<<"category : ";
cin >>category;

cout<<"date : ";
cin>>date;

cout<<"price : ";
cin>>price;
}
//-------------------------------------

void book::print()
{
cout << id<<"\t"<< name<<"\t\t"<< author<<"\t\t"<<category<<"\t\t"<<price<<"\t"<<date<<endl;
}
//-------------------------------------

char book::getState()
{
return name[0];
}
//--------------------------------------

//************************************************** *****************************************
book b[SIZE];
int menu();
int FindIndex();
void report();
int reportid(long id);
void editid(long id);
void deleteid(long id);
void reportname(char name[10]);
void reportcategory(char catname[10]);
void reportdate(long start,long end);

//--------------program
void main()
{
int i,c,id;
long s,e;
char catname[10],name[10];
for(;;) {
clrscr();
c = menu();
switch (c) {
case 1 :
i=FindIndex();
if (i!=-1 )
{
b[i].enter();
}
else
{
cout << "\n List is full. press a key...";
getch();
}
break;
case 2 :
clrscr();
cout<<"Enter ID : ";
cin>>id;
reportid(id);

break;
case 3 :
clrscr();
cout<<"Enter Name : ";
cin>>name;
reportname(name);
break;
case 4 :
clrscr();
cout<<"Enter Category : ";
cin>>catname;
reportcategory(catname);
break;
case 5 :
report();
break;
case 6 :
clrscr();
cout<<"Enter Start Of Period : ";
cin>>s;
cout<<"Enter End Of Period : ";
cin>>e;
reportdate(s,e);
case 7 :
clrscr();
cout<<"Enter ID For Edit : ";
cin>>id;
editid(id);

break;
case 8 :
clrscr();
cout<<"Enter ID For Delete From List : ";
cin>>id;
deleteid(id);
break;
case 9 :
exit(0) ;
} // end of switch
} //end of for(;;)
clrscr();
cout<<"----";


getch();
getch();
}
//----------------tavabe program
int menu()
{
int c;
gotoxy(5, 5);
cout << "1) Enter a Book.";
gotoxy(5, 6);
cout << "2) Search By ID.";
gotoxy(5, 7);
cout << "3) Search By Name.";
gotoxy(5, 8);
cout << "4) Search By Category.";
gotoxy(5, 9);
cout << "5) Report list.";
gotoxy(5, 10);
cout << "6) Caculate Total Price In Period.";
gotoxy(5, 11);
cout << "7) Edit Book.";
gotoxy(5, 12);
cout << "8) Delete Book.";
gotoxy(5, 13);
cout << "9) Exit.";

do {
gotoxy(5, 15);
cout << "Enter your select(1 - 9) : ";
cin >> c;
} while (c < 1 && c >9 );
return c;
}

//------------------get index
int FindIndex()
{
int index;
for(index = 0; index < SIZE && b[index].getState(); index++);
if(index == SIZE)
return -1;
return index;
}
//----------------------------

void report()
{
int i;
clrscr();
cout <<"id"<<"\t"<<"name"<<"\t\t"<<"author"<<"\t\t"<<"category"<<"\t"<<"price"<<"\t"<<"date"<<endl;
for(i = 0; i < SIZE; i ++) {
if(b[i].getState())
b[i].print();
}
getch();
}
//-------------------------------

void reportcategory(char catname[10])
{
int i;
clrscr();
cout <<"id"<<"\t"<<"name"<<"\t\t"<<"author"<<"\t\t"<<"category"<<"\t"<<"price"<<"\t"<<"date"<<endl;
for(i = 0; i < SIZE; i ++) {
if(b[i].getState())
if( strcmp(b[i].category,catname)==0)
b[i].print();
}
getch();
}
//----------------------------------------

void reportname(char name[10])
{
int i;
clrscr();
cout <<"id"<<"\t"<<"name"<<"\t\t"<<"author"<<"\t\t"<<"category"<<"\t"<<"price"<<"\t"<<"date"<<endl;
for(i = 0; i < SIZE; i ++) {
if(b[i].getState())
if( strcmp(b[i].name,name)==0)
b[i].print();
}
getch();
}
//---------------------------------------------
void editid(long id)
{
int i;
clrscr();
if ( reportid(id)==-1)
return ;

for(i = 0; i < SIZE; i ++) {
if(b[i].getState())
if(b[i].id==id)
b[i].enter();
}
getch();
}

//---------------------------------------------
void deleteid(long id)
{
int i;
clrscr();
if ( reportid(id)==-1)
return ;

b[i].name[0]='\0';
getch();
}

//---------------------------------------------
int reportid(long id)
{
int i;
clrscr();
cout <<"id"<<"\t"<<"name"<<"\t\t"<<"author"<<"\t\t"<<"category"<<"\t"<<"price"<<"\t"<<"date"<<endl;
for(i = 0; i < SIZE; i ++)
{
if(b[i].getState())
if(b[i].id==id)
b[i].print();

if(i==SIZE)
{
clrscr();
cout <<"Not Found";
return -1;

}

}
getch();
return 1;
}
//------------------------------------------------

void reportdate(long start,long end)
{
int i;
long total=0;
clrscr();
cout <<"id"<<"\t"<<"name"<<"\t\t"<<"author"<<"\t\t"<<"category"<<"\t"<<"price"<<"\t"<<"date"<<endl;
for(i = 0; i < SIZE; i ++) {
if(b[i].getState())
if( b[i].date>=start && b[i].date<=end)
{
b[i].print();
total+=b[i].price;
}
}
cout <<"Total Cost From "<<start<<" To "<<end<<" : "<<total<<endl;
getch();
}

oranoos_mt
چهارشنبه 11 خرداد 1390, 17:23 عصر
هيشكي نيست كمك كنه؟

oranoos_mt
چهارشنبه 11 خرداد 1390, 18:55 عصر
هيشكي نيست كمك كنه؟

جون من يكي جواب بده............................لطفا خواهش ميكنم

quiet_programmer
پنج شنبه 12 خرداد 1390, 18:54 عصر
با سلام.

اگه دقت کنی اول کار میاد Name[0]l رو برابر Null یا همون 0/ قرار میده یعنی این رشته در ابتدا خالیه. getstate پر یاخالی بودن رشته رو برمیگردونه. مثلا اگه خالی باشه این تابع مقدار Null رو برگشت میده یعنی من خالی هستم در غیر این صورت یه عدد غیر صفر رو برمیگردونه یعنی من پرم.

با این کار دیگه خودش رو با تابع strcmp درگیر نمیکنه. سرعت این کد هم در مقایسه با استفاده از تابع strcmp برای تشخیص خالی یا پر بودن رشته، بیشتره.

امیدوارم که متوجه شده باشی. اگه نه بگو تا با مثال توضیح بدم.
موفق باشی

oranoos_mt
پنج شنبه 12 خرداد 1390, 20:37 عصر
با سلام.

اگه دقت کنی اول کار میاد Name[0]l رو برابر Null یا همون 0/ قرار میده یعنی این رشته در ابتدا خالیه. getstate پر یاخالی بودن رشته رو برمیگردونه. مثلا اگه خالی باشه این تابع مقدار Null رو برگشت میده یعنی من خالی هستم در غیر این صورت یه عدد غیر صفر رو برمیگردونه یعنی من پرم.

با این کار دیگه خودش رو با تابع strcmp درگیر نمیکنه. سرعت این کد هم در مقایسه با استفاده از تابع strcmp برای تشخیص خالی یا پر بودن رشته، بیشتره.

امیدوارم که متوجه شده باشی. اگه نه بگو تا با مثال توضیح بدم.
موفق باشی

آقا درسته. ممنون. كلي گيج ميزدم.