PDA

View Full Version : مشکل در نوشتن تابع



fshb_ 1370
سه شنبه 25 مرداد 1390, 00:36 صبح
من میخوام ی list بنویسم و در سوال من ی تابع خواسته void visit(void (*pf)(Item &)); که من نمیدونم دقیقا چی رو واسش بفرستم، چون خروجی توابع مثل pf از توع void هست و تابع visit هیچ آرگومانی دریافت نمیکنه. درسته؟؟؟
pr7.h


#ifndef PR7_H_
#define PR7_H_
typedef int Item;
class list
{
private:
static const int SIZE=10;
Item items[SIZE];
int top;
public:
list();
bool checkempty();
bool checkfull();
void add(Item & inst);
void visit(void (*pf)(Item &));
};
#endif


pr7.cpp


#include<iostream>
using namespace std;
#include "PR7.h"
list::list()
{
top=0;
}
bool list::checkempty()
{
if(top<SIZE)
return true;
else
return false;
}
bool list::checkfull()
{
if(top==SIZE)
return true;
else
return false;
}
void list::add(Item & inst)
{
items[top++]=inst;
}
void list::visit(void (*pf)(Item &))
{
//???
}


pr7_chap10.cpp


#include<iostream>
using namespace std;
#include "PR7.h"
int main()
{
list l;
Item c;
cout<<"enter a number for add: "<<endl;
while(cin>>c)
{
if (l.checkempty())
l.add(c);
else
break;
}
return 0;
}


متن سوال انگلیسی هست و من متاسفانه فک کنم نتونستم منظورش درست بفهمم، میتونم سوال اینجا بذارم؟؟:ناراحت:

simul8or
پنج شنبه 27 مرداد 1390, 00:43 صبح
شما اعداد رو می خونید و ذخیره می کنید، مانند پشته. اما ذکر نکردید تابع visit باید چه کاری در برنامه انجام بده؟؟؟ در ضمن شما void (*pf)(Item &) رو به عنوان نوع آرگومان تعیین کردید، که من دقیقا منظور شما رو از به کار بردن pf* در کنار Item& متوجه نمی شم، این تابع قرار است چه کاری انجام دهد؟؟؟

fshb_ 1370
جمعه 28 مرداد 1390, 10:12 صبح
شما اعداد رو می خونید و ذخیره می کنید، مانند پشته. اما ذکر نکردید تابع visit باید چه کاری در برنامه انجام بده؟؟؟ در ضمن شما void (*pf)(Item &) رو به عنوان نوع آرگومان تعیین کردید، که من دقیقا منظور شما رو از به کار بردن pf* در کنار Item& متوجه نمی شم، این تابع قرار است چه کاری انجام دهد؟؟؟


تابع visit تابعی هست که برای انجام هر عملیاتی مثل درج و حذف باید از visit استفاده کرد. (فک کنم، گفتم ک متن سوال درست متوجه نشدم!)
*pf هم به تمام توابعی اشاره میکنه که مقدار برگشتی آنها void و آرگومان آنها از نوع Item و pass-by-ref هست.
مشکل اینجاست که تابع *pf وقتی هیچ مقداری return نمیکنه، پس تابع visit چه چیزی رو به عنوان آرگومان دریافت میکنه تا براساس اون کار کنه؟؟
میتونم متن سوال بذارم؟

_hamid
جمعه 28 مرداد 1390, 11:14 صبح
آره شما متن رو بذاری بهتره.
اینی هم که می بینی یه اشاره گر به تابع است.
void (*pf)(Item &)
فکر کنم سوال می خواد مثل callback ازش استفاده کنه.

fshb_ 1370
جمعه 28 مرداد 1390, 14:56 عصر
آره شما متن رو بذاری بهتره.
اینی هم که می بینی یه اشاره گر به تابع است.
void (*pf)(Item &)
فکر کنم سوال می خواد مثل callback ازش استفاده کنه.


We can describe a simple list as follows:


A simple list can hold zero or more items of some particular type.

You can create an empty list.

You can add items to a list.

You can determine if the list is empty.

You can determine if the list is full.

You can visit each item in a list and perform some action upon it.

As you can see, this list really is simple, not allowing insertion or deletion, for example. The main use of such a list is to provide a simplified programming project. In this case, create a class matching this description. You can implement the list as an array or, if you're familiar with the data type, as a linked list. But the public interface should not depend on your choice. That is, the public interface should not have array indices, pointers to nodes, and so on. It should be expressed in the general concepts of creating a list, adding an item to the list, and so on. The usual way to handle visiting each item and performing an action is to use a function that takes a function pointer as an argument:
void visit(void (*pf)(Item &));
Here pf points to a function (not a member function) that takes a reference to Item argument, where Item is the type for items in the list. The visit() function applies this function to each item in the list.
You also should provide a short program utilizing your design

_hamid
جمعه 28 مرداد 1390, 18:18 عصر
همونطور که فکر می کردم به عنوان callback استفاده می کنه.
آقا من به خود تابع بسنده می کنم.
دیگه خودت اگر تو ساختار لیست میشکل داری پای خودت:
void visit(void(*pf)(item&))
{
for(int i=0;i<SIZE;i++)
pf(items[i]);
}
و همچنین برای استفاده کردن ازش:
void out(int &arg)
{
cout <<arg<<endl;
}
int main()
{
list a;
a.add(3)
a.add(4);
a.add(5);
a.visit(out);
return 0;
}
الان این ساختار لیستی که شما ساختی فقط int می گیره. چرا از template ها استفاده نمی کنی؟
SIZE نباید به صورت static تعریف بشه! اصلا چه کاریه؟!