9. Structs
To gather the data of one thing in a struct.
Explanation
Section titled “Explanation”- A struct is a type you define yourself, made of fields: each field has a name and a type, one field per line.
- You create a value of it by writing its name, then the values in field order:
طالب("ليلى", 91). ط.اسمreads the fieldاسمfrom the valueط.
Example
Section titled “Example”هيكل طالب: اسم نص درجة عدد64ط = طالب("ليلى", 91)اطبع(ط.اسم, " ", ط.درجة)اطبع(ط)Common mistakes
Section titled “Common mistakes”The number of values does not match the number of fields:
هيكل نقطة: س عدد64ا = نقطة(1, 2)Exit code: 2
In the error catalog: E0425
Exercise
Section titled “Exercise”Define a struct كتاب (book) with a title and a page count, make a list of three books, and print the total number of pages and the longest book.
Show the solution
هيكل كتاب: عنوان نص صفحات عدد64
كتب = [كتاب("الأيام", 160), كتاب("كليلة ودمنة", 290), كتاب("حي بن يقظان", 120)]مجموع = 0اطول = كتب[0]لكل ك في كتب: مجموع += ك.صفحات اذا ك.صفحات > اطول.صفحات: اطول = كاطبع("مجموع الصفحات: ", مجموع)اطبع("أطولها: ", اطول.عنوان, " (", اطول.صفحات, " صفحة)")حرر(كتب)