الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مساعدة في سؤال عن File Processing

مغلق
بدأه domainA في 26 ديسمبر 2006 · 4 رد · 698 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

كل عام وانتم بخير وعساكم من عواده ..

ندخل للموضوع :unsure:

السؤال :

اقتباس

Design a software application using C++ that helps a bookstore to manage its activities. The bookstore is selling only books and dictionaries. For that purpose, you will define a base class : Document and two derived classes : Book and Dictionary.

The class Document have the following data members:

..

..

..

Class Book inherits from class Document and has the following data members:

..

..

Class Dictionary inherits from class Document and has the following data members:

..

Your application should implemented in a bookstore.cpp file. It should maintain a file called document.dat which stores objects of type Book and/or Dictionary. The bookstore should be able to add new items to the file when it purchases new documents using two global functions:

void addNewBook( const Book& )

void addNewDictionary( const Dictionary& )

Since Book and Dictionary object have different sizes and written in the same file document.dat ( Data file ), you need to maintain a second file called document,ind ( Index File ) that stores for each item in the Data File a corresponded object of type Index where Index is a class that contains the following data members:

int id

long position

int size

enum { Book, Dictionary }

When a document ( Book or Dictionary ) is sold, it should be removed from the Data File ( document.dat ) and its index should be removed from the Index File ( document.ind ). The removed document should be added to a file maintaining sold documents ( document.sol ) as a Document object ( not as a Book or as a Dictionary ).

The following global functions removes an item (Book or Dictionary ) from the Data File and its corresponded index from the Index File and converts the removed item to a Document object that should be appended to the file document.sol

void removeItem( const int )

You should helps the bookstore to compute its benefits ( number of sold items * ( price – cost ) ). The following global function should use the file document.sol in order to compute total benefits of the bookstore.

double computeBenefits()

Also, the bookstore should be able to increase or decrease the price of an item by using the following global function:

void modifyPrice( int id, double newPrice )

if the bookstore buys for example 5 items of the same Book ( same author and same title ), 5 items are added to Data File and the Index File ( these 5 items have different ids ).

المشكلة لدي هي :

عند كتابة أي برنامج يستخدم ملف Random Access File فانه يجب ان اقوم بكتابة 100 objects فارغة سابقا . يعني اسوي for loop من 1 الى 100 واجعلها تكرر دالة الكتابة 100 مرة لكتابة 100 objects فارغ .

مثلاً ، في السؤال الموجود في الاعلى ، فانا احتاج لكتابة الكود التالي :

اقتباس

outFile( “document.dat” , ios::out | iso::binary );

Document blankDocument;

for ( int i =0 ; i < 100 ; i++ )

outFile.write( reinterpret_cast < const char * >( &blankDocument) , sizeof(Document) );

سوف يقوم البرنامج بإنشاء شيء (أوبجيكت) بالقيم الافتراضية ويقوم بكتابته في binary file 100 مره .

المشكلة ان حجم document object يختلف عن حجم book object و dictionary object ( كما هو مكتوب في السؤال ) ، لذلك انا توقفت ولم اعرف مالعمل بعدها وكيف استفيد من ملف جديد Index File ؟

كذلك بالنسبة ل enum الموجودة في index class ، لا اعرف كيف اجعلها في private كـ data member ، وكيف اقوم بعطاء قيمة ابتدائية في constructor ..

كذلك ملف document.sol ، هل اجعله sequential file او binary file ؟

بصراحة هذا البرنامج لم استطع عمل شيء منه الا كتابة الـ classes فقط ، وجزئية الملفات .dat و .ind و .sol محيرتني تماماً !

اتمنى المساعدة في اقرب فرصة وشكراً .

#2

وين الشباب ؟؟ :unsure:

كل هذا عيد :huh:

#3

كل عام وأنت بخير

ما دام عندك ملف index فأنت لا تحتاج تكون ملف fixed random access ( يعني تمر على الملف 100 أو 200 مرة )

فقط عند تكوين أي Object سواء Book أو Dictionary فقط تذهب إلى ملف ال document.dat وتكون ال Object اللي تبيه وتحطه في أي مكان

وبعدين تكون الIndex تبعه ( تحط ال position و size وكل شئ ) وتضيفه للملف ind

وبالنسبة لل enum هل تقصد هكذا

class T{
private:
	enum Document{BOOK , DICTIONARY }  type;
public:
	T(Document t){}
};

والسلام :o

#4

b.m.s

شكرا على الرد اولاً ، لا اعرف كيف اشكرك والله :)

خلينا من enum :D

ممكن تكتب لي مثال عن طريق اضافة Book مثلا ؟ لاني مافهمت الي تقصده بالضبط .. يمكن المثال يوضح المقال (لا تتوقع اني اطلب الحل الكامل او جزء منه ، ابدا والله لا اريد هذا الشيء لكن اريد ان افهم الطريقة التي كتبتها لي) وشكرا مرة اخرى :)

#5

طيب اخوي لو اتبعت الطريقة هذي :

-----------------------------------------------------------------------------------------

(( فقط عند تكوين أي Object سواء Book أو Dictionary فقط تذهب إلى ملف ال document.dat وتكون

ال Object اللي تبيه وتحطه في أي مكان ، وبعدين تكون الIndex تبعه ( تحط ال position و size وكل شئ )

وتضيفه للملف ind ))

-----------------------------------------------------------------------------------------

اذا اردت اضافة Book او Dictionary ثاني راح تصير مشاكل ، مثلاً كيف القى مكان فاضي ؟ يمكن اختار مكان يكون مساحته اصغر من الأوبجكت الي بأضيفه ..

او وش رايك اضيف الIndex اول بعدين اضيف في ملف document.dat ؟

هذا الموضوع مغلق.

مواضيع مشابهة