ورود

View Full Version : سوال: نوشتن case 4 برنامه با تابع



vB.N3T
شنبه 06 خرداد 1391, 17:50 عصر
سلام دوستان این یک برنامه مدیریت کتاب هست
که کیس 4 قرار یک مولف رو بگیره و کتاب های ان رو نمایش بده
تابع شو نوشتم ولی یک ایدی میگیره و اطلاعات رو نمایش میده
اما من میخوام یک مولف رو بگیره و اطلاعات رو نمایش بده ممنون میشم کمکم کنید


#include <iostream.h>
#include <conio.h>
#define size 100

struct node
{
int id;
char moalef[30];
int mojodi;

node *next;
};

node *back, *start = NULL;




node *find_book(int id)
{
node *t, *b;
t = start;
b = NULL;
while (t != NULL)
{
if (t -> id==id)
break;
b = t;
t = t -> next;
}
if (t == NULL)
cout << " not found.\n";
else
back = b;
return t;
}

void delete_book(int id)
{
node *t;
t = find_book(id);
if (t == NULL)
return;
if (back == NULL)
start = t -> next;
else
back -> next = t -> next;
delete t;
}


int main()
{
clrscr();
node *t, *p, *z;
int a;
int b;
do
{
cout<<"__________________________________________\n";
cout << "\n1->Rejistr BOOK \n";
cout << "2->Show All Data (ISBN) \n";
cout << "3->ENTER ISB \n";
cout << "4->search (MOlef) \n" ;
cout << "5->Delete \n";
cout << "6->yek adad dariaft \n\n";
cout << "7->EXIT \n\n";

cout<<"_________________________________________\n";
cout<<"\n";

cout << "Enter 1,2,3,4,5,6,7 :\n";
cin >> a;
switch(a)
{
case 1:
t = new node[1];
cout << "Enter ID :\n ";
cout<<"\n";
cin >> t->id;
cout<<"\n";
cout << "Enter Moalef :\n ";
cout<<"\n";
cin >> t -> moalef;
cout<<"\n";
cout << "Enter mojodi:\n ";
cout<<"\n";
cin >> t -> mojodi;
cout<<"\n";
cout << "Submit Data\n";
cout<<"\n";
cout<"--------------------\n";
cout<<"\n";
z = start;
p = NULL;
while (z != NULL && z -> id < b)
{
p = z;
z = z -> next;
}
if (p == NULL)
{
t -> next = start;
start = t;
}
else
{
t -> next = p -> next;
p -> next = t;
}
break;
case 2:
if (start == NULL)
cout << "List is empty.\n";
p = start;
while (p != NULL)
{
cout << "ID = " << p -> id << " Moalef= " << p -> moalef
<< " Mojodi= " << p -> mojodi<< "\n";
p = p -> next;
}
break;
case 3:
cout << "enter id :\n ";
cin >> b;
p = find_book(b);
if (p != NULL)
cout << "ID = " << p -> id << " moalef= " << p -> moalef
<< " mojodi= " << p -> mojodi<< "\n";
break;

case 4:
cout << "enter id :\n ";
cin >> b;
p = find_book(b);
if (p != NULL)
cout << "ID = " << p -> id << " moalef= " << p -> moalef
<< " mojodi= " << p -> mojodi<< "\n";
break;
case 5:
cout << "enter ID :\n ";
cin >> b;
delete_book(b);
cout<<"Delete ="<<b<<"\n";
break;
}
} while (a != 7);
cout<<"END Program";
getch();
return 0;
}

esmn1900
یک شنبه 07 خرداد 1391, 10:11 صبح
متاسفانه کد شما کمی نامرتب است. بهتر است که هر عملی را در توابع بنویسید و هر case را به آن تابع ارجاع دهید.
تابع جستجوی moalef می‌تواند به این صورت نوشته شود:


...
case 4:
find_moalef();
break;
...

void find_moalef()
{
char mstr[30];
cout<<"enter moalef: ";
cin >> mstr;
t = start;
while ( t != NULL )
{
if (!strcmp ( mstr , t->moalef ))
{
cout << "ID = " << t -> id << " Moalef= " << t -> moalef
<< " Mojodi= " << t -> mojodi<< "\n";
}
t = t -> next;
}
}



البته باید <string.h> را به برنامه خود اضافه کنید.