Upload 15 files
Browse files- cards.json +1 -0
- chat_history_db.json +122 -0
- config.py +59 -0
- data_structure/main.txt +21 -0
- data_structure/p1.txt +823 -0
- data_structure/p2.txt +637 -0
- data_structure/p3.txt +1483 -0
- data_structure/p4.txt +559 -0
- data_structure/p5.txt +1382 -0
- data_structure/p6.txt +802 -0
- data_structure/structure.txt +31 -0
- front_end_road.md +530 -0
- json_processor.py +162 -0
- system.txt +826 -0
- users.json +43 -0
cards.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{}
|
chat_history_db.json
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"ุณูู__islamic": {
|
| 3 |
+
"username": "ุณูู",
|
| 4 |
+
"subject_id": "islamic",
|
| 5 |
+
"messages": [
|
| 6 |
+
{
|
| 7 |
+
"role": "user",
|
| 8 |
+
"content": "ู
ุง ูู ุงูู
ูุงุถูุน ุงูู
ุชุงุญุฉุ",
|
| 9 |
+
"timestamp": "2026-04-09T20:05:55.369551+00:00"
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"role": "assistant",
|
| 13 |
+
"content": "ู
ุฑุญุจุงู ุจู ๐ท \nุฃูุง **ู
ุณุงุนุฏ ุงูุชุฑุจูุฉ ุงูุฅุณูุงู
ูุฉ ุงูุฐูู**ุ ููุณุนุฏูู ู
ุณุงุนุฏุชู.\n\nุงูู
ูุงุถูุน ุงูู
ุชุงุญุฉ ูู ูุฐุง ุงูู
ููุฌ ูู:\n\n### ุฃููุงู: ุงููุฑุขู ุงููุฑูู
\n- ุงููุต ุงูุฃูู: **ุนุงูุจุฉ ุงูุธูู
(1)**\n- ุงููุต ุงูุซุงูู: **ุนุงูุจุฉ ุงูุธูู
(2)**\n- ุงููุต ุงูุซุงูุซ: **ุทุบูุงู ุงูู
ุงู**\n- ุงููุต ุงูุฑุงุจุน: **ุนุงูุจุฉ ุทุบูุงู ุงูู
ุงู**\n- ุงููุต ุงูุฎุงู
ุณ: **ุนุงูุจุฉ ุงููุชู ุงูุนู
ุฏ ูุงูุญุฑุงุจุฉ**\n\n### ุซุงููุงู: ุงูุณูุฉ ุงููุจููุฉ\n- ุงูุญุฏูุซ ุงูุฃูู: **ูู ุธู ุงูุฑุญู
ู**\n- ุงูุญุฏูุซ ุงูุซุงูู: **ุญุฑู
ุฉ ูุฌุฑ ุงูู
ุคู
ู**\n- ุงูุญุฏูุซ ุงูุซุงูุซ: **ุนููุจุฉ ุงูุฑุดูุฉ ูู
ุถุงุฑูุง**\n- ุงูุญุฏูุซ ุงูุฑุงุจุน: **ุงุบุชูุงู
ู
ุง ูุง ูู
ูู ุชุนููุถู**\n- ุงูุญุฏูุซ ุงูุฎุงู
ุณ: **ุนูุงู
ุงุช ุงูููุงู**\n- ุงูุญุฏูุซ ุงูุณุงุฏุณ: **ุณุจุงุจ ุงูู
ุคู
ู ูุฃุฎูู**\n- ุงูุญุฏูุซ ุงูุณุงุจุน: **ุฃุฌุฑ ุงูุบุฑุณ ูุงูุฒุฑุน**\n- ุงูุญุฏูุซ ุงูุซุงู
ู: **ุงูู
ุญุจุฉ ูู ุงูุฅุณูุงู
**\n- ุงูุญุฏูุซ ุงูุชุงุณุน: **ู
ู ู
ููุฑุงุช ุงูุฎุทุงูุง**\n- ุงูุญุฏูุซ ุงูุนุงุดุฑ: **ุซูุงุจ ุงูุตุงูุญุฉ ุจูู ุงูู
ุณูู
ูู**\n\n### ุซุงูุซุงู: ุงูุนููุฏุฉ\n- **ุนุงูู
ูุฉ ุฑุณุงูุฉ ุงูุฅุณูุงู
ูุดู
ูููุฉ ุฃุญูุงู
ูุง**\n- **ุฃุฏูุฉ ุนู
ูู
ุงูุฑุณุงูุฉ**\n- **ุดู
ูู ุฃุญูุงู
ุฑุณุงูุฉ ุงูุฅุณูุงู
**\n- **ุฑุณุงูุฉ ุงูุฅุณูุงู
ุฎุงุชู
ุฉ ุงูุฑุณุงูุงุช ุงูุณู
ุงููุฉ**\n- **ุฑุณุงูุฉ ุงูุฅุณูุงู
ูุงุณุฎุฉ ููู ุงูุดุฑุงุฆุน ุงูุณุงุจูุฉ**\n\n### ุฑุงุจุนุงู: ุงูุนุจุงุฏุงุช\n- **ุงูุฒูุงุฉ**\n- **ุตุฏูุฉ ุงููุทุฑ**\n- **ุงูุญุฌ**\n- **ุงูุนู
ุฑุฉ**\n- **ุงูุฃุถุญูุฉ**\n- **ุงูุฐูุงุฉ ุงูุดุฑุนูุฉ**\n\nุฅุฐุง ุฃุฑุฏุชุ ุฃุณุชุทูุน ุฃูุถุงู ุฃู:\n1. ุฃุดุฑุญ ูู **ุฃู ู
ูุถูุน** ู
ููุง ุจุงุฎุชุตุงุฑ \n2. ุฃุถุน ูู **ู
ูุฎุตุงู ููููุฑุณ ููู** \n3. ุฃุณุงุนุฏู ูู **ุงุฎุชูุงุฑ ุงูุฏุฑุณ ุงูู
ูุงุณุจ ููู
ุฑุงุฌุนุฉ** \n\nุงุฎุชุฑ ุฃู ู
ูุถูุน ุชุฑูุฏ ุงูุจุฏุก ุจู ๐",
|
| 14 |
+
"timestamp": "2026-04-09T20:06:04.888062+00:00"
|
| 15 |
+
},
|
| 16 |
+
{
|
| 17 |
+
"role": "user",
|
| 18 |
+
"content": "ุงูุณูุงู
ุนูููู
ุฃูุฏ ุดุฑุญ ุจุณูุท ุฌุฏุงู ุฑุงุจุนุงู ุงูุนุจุงุฏุงุช ุชุญุฏูุฏุงู ุตุฏูุฉ ุงููุทุฑ",
|
| 19 |
+
"timestamp": "2026-04-09T20:07:43.865903+00:00"
|
| 20 |
+
}
|
| 21 |
+
],
|
| 22 |
+
"full_messages": [
|
| 23 |
+
{
|
| 24 |
+
"role": "user",
|
| 25 |
+
"content": "ู
ุง ูู ุงูู
ูุงุถูุน ุงูู
ุชุงุญุฉุ",
|
| 26 |
+
"timestamp": "2026-04-09T20:05:55.369551+00:00"
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"role": "assistant",
|
| 30 |
+
"content": "ู
ุฑุญุจุงู ุจู ๐ท \nุฃูุง **ู
ุณุงุนุฏ ุงูุชุฑุจูุฉ ุงูุฅุณูุงู
ูุฉ ุงูุฐูู**ุ ููุณุนุฏูู ู
ุณุงุนุฏุชู.\n\nุงูู
ูุงุถูุน ุงูู
ุชุงุญุฉ ูู ูุฐุง ุงูู
ููุฌ ูู:\n\n### ุฃููุงู: ุงููุฑุขู ุงููุฑูู
\n- ุงููุต ุงูุฃูู: **ุนุงูุจุฉ ุงูุธูู
(1)**\n- ุงููุต ุงูุซุงูู: **ุนุงูุจุฉ ุงูุธูู
(2)**\n- ุงููุต ุงูุซุงูุซ: **ุทุบูุงู ุงูู
ุงู**\n- ุงููุต ุงูุฑุงุจุน: **ุนุงูุจุฉ ุทุบูุงู ุงูู
ุงู**\n- ุงููุต ุงูุฎุงู
ุณ: **ุนุงูุจุฉ ุงููุชู ุงูุนู
ุฏ ูุงูุญุฑุงุจุฉ**\n\n### ุซุงููุงู: ุงูุณูุฉ ุงููุจููุฉ\n- ุงูุญุฏูุซ ุงูุฃูู: **ูู ุธู ุงูุฑุญู
ู**\n- ุงูุญุฏูุซ ุงูุซุงูู: **ุญุฑู
ุฉ ูุฌุฑ ุงูู
ุคู
ู**\n- ุงูุญุฏูุซ ุงูุซุงูุซ: **ุนููุจุฉ ุงูุฑุดูุฉ ูู
ุถุงุฑูุง**\n- ุงูุญุฏูุซ ุงูุฑุงุจุน: **ุงุบุชูุงู
ู
ุง ูุง ูู
ูู ุชุนููุถู**\n- ุงูุญุฏูุซ ุงูุฎุงู
ุณ: **ุนูุงู
ุงุช ุงูููุงู**\n- ุงูุญุฏูุซ ุงูุณุงุฏุณ: **ุณุจุงุจ ุงูู
ุคู
ู ูุฃุฎูู**\n- ุงูุญุฏูุซ ุงูุณุงุจุน: **ุฃุฌุฑ ุงูุบุฑุณ ูุงูุฒุฑุน**\n- ุงูุญุฏูุซ ุงูุซุงู
ู: **ุงูู
ุญุจุฉ ูู ุงูุฅุณูุงู
**\n- ุงูุญุฏูุซ ุงูุชุงุณุน: **ู
ู ู
ููุฑุงุช ุงูุฎุทุงูุง**\n- ุงูุญุฏูุซ ุงูุนุงุดุฑ: **ุซูุงุจ ุงูุตุงูุญุฉ ุจูู ุงูู
ุณูู
ูู**\n\n### ุซุงูุซุงู: ุงูุนููุฏุฉ\n- **ุนุงูู
ูุฉ ุฑุณุงูุฉ ุงูุฅุณูุงู
ูุดู
ูููุฉ ุฃุญูุงู
ูุง**\n- **ุฃุฏูุฉ ุนู
ูู
ุงูุฑุณุงูุฉ**\n- **ุดู
ูู ุฃุญูุงู
ุฑุณุงูุฉ ุงูุฅุณูุงู
**\n- **ุฑุณุงูุฉ ุงูุฅุณูุงู
ุฎุงุชู
ุฉ ุงูุฑุณุงูุงุช ุงูุณู
ุงููุฉ**\n- **ุฑุณุงูุฉ ุงูุฅุณูุงู
ูุงุณุฎุฉ ููู ุงูุดุฑุงุฆุน ุงูุณุงุจูุฉ**\n\n### ุฑุงุจุนุงู: ุงูุนุจุงุฏุงุช\n- **ุงูุฒูุงุฉ**\n- **ุตุฏูุฉ ุงููุทุฑ**\n- **ุงูุญุฌ**\n- **ุงูุนู
ุฑุฉ**\n- **ุงูุฃุถุญูุฉ**\n- **ุงูุฐูุงุฉ ุงูุดุฑุนูุฉ**\n\nุฅุฐุง ุฃุฑุฏุชุ ุฃุณุชุทูุน ุฃูุถุงู ุฃู:\n1. ุฃุดุฑุญ ูู **ุฃู ู
ูุถูุน** ู
ููุง ุจุงุฎุชุตุงุฑ \n2. ุฃุถุน ูู **ู
ูุฎุตุงู ููููุฑุณ ููู** \n3. ุฃุณุงุนุฏู ูู **ุงุฎุชูุงุฑ ุงูุฏุฑุณ ุงูู
ูุงุณุจ ููู
ุฑุงุฌุนุฉ** \n\nุงุฎุชุฑ ุฃู ู
ูุถูุน ุชุฑูุฏ ุงูุจุฏุก ุจู ๐",
|
| 31 |
+
"timestamp": "2026-04-09T20:06:04.888062+00:00"
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"role": "user",
|
| 35 |
+
"content": "ุงูุณูุงู
ุนูููู
ุฃูุฏ ุดุฑุญ ุจุณูุท ุฌุฏุงู ุฑุงุจุนุงู ุงูุนุจุงุฏุงุช ุชุญุฏูุฏุงู ุตุฏูุฉ ุงููุทุฑ",
|
| 36 |
+
"timestamp": "2026-04-09T20:07:43.865903+00:00"
|
| 37 |
+
}
|
| 38 |
+
],
|
| 39 |
+
"summary": "",
|
| 40 |
+
"message_count": 3,
|
| 41 |
+
"last_file": "p4.txt",
|
| 42 |
+
"subject": "islamic",
|
| 43 |
+
"saved_at": "2026-04-09T23:08:44.443531"
|
| 44 |
+
},
|
| 45 |
+
"ุณูู__biology": {
|
| 46 |
+
"username": "ุณูู",
|
| 47 |
+
"subject_id": "biology",
|
| 48 |
+
"messages": [
|
| 49 |
+
{
|
| 50 |
+
"role": "user",
|
| 51 |
+
"content": "ู
ุฑุญุจุงุ ุนุฑููู ุจููุณู",
|
| 52 |
+
"timestamp": "2026-04-10T12:37:45.932980+00:00"
|
| 53 |
+
},
|
| 54 |
+
{
|
| 55 |
+
"role": "assistant",
|
| 56 |
+
"content": "ุนุฐุฑุงูุ ุญุฏุซ ุฎุทุฃ ูู ุงููุธุงู
. ุญุงูู ู
ุฑุฉ ุฃุฎุฑู.",
|
| 57 |
+
"timestamp": "2026-04-10T12:37:45.935461+00:00"
|
| 58 |
+
}
|
| 59 |
+
],
|
| 60 |
+
"full_messages": [
|
| 61 |
+
{
|
| 62 |
+
"role": "user",
|
| 63 |
+
"content": "ู
ุฑุญุจุงุ ุนุฑููู ุจููุณู",
|
| 64 |
+
"timestamp": "2026-04-10T12:37:45.932980+00:00"
|
| 65 |
+
},
|
| 66 |
+
{
|
| 67 |
+
"role": "assistant",
|
| 68 |
+
"content": "ุนุฐุฑุงูุ ุญุฏุซ ุฎุทุฃ ูู ุงููุธุงู
. ุญุงูู ู
ุฑุฉ ุฃุฎุฑู.",
|
| 69 |
+
"timestamp": "2026-04-10T12:37:45.935461+00:00"
|
| 70 |
+
}
|
| 71 |
+
],
|
| 72 |
+
"summary": "",
|
| 73 |
+
"message_count": 2,
|
| 74 |
+
"last_file": "main.txt",
|
| 75 |
+
"subject": "biology",
|
| 76 |
+
"saved_at": "2026-04-10T15:37:45.948257"
|
| 77 |
+
},
|
| 78 |
+
"ุณูู__physics_mechanic": {
|
| 79 |
+
"username": "ุณูู",
|
| 80 |
+
"subject_id": "physics_mechanic",
|
| 81 |
+
"messages": [],
|
| 82 |
+
"full_messages": [],
|
| 83 |
+
"summary": "",
|
| 84 |
+
"message_count": 0,
|
| 85 |
+
"last_file": null,
|
| 86 |
+
"subject": "physics_mechanic",
|
| 87 |
+
"saved_at": "2026-04-10T15:51:04.726846"
|
| 88 |
+
},
|
| 89 |
+
"ุณูู__it": {
|
| 90 |
+
"username": "ุณูู",
|
| 91 |
+
"subject_id": "it",
|
| 92 |
+
"messages": [
|
| 93 |
+
{
|
| 94 |
+
"role": "user",
|
| 95 |
+
"content": "ู
ุง ูู ุงูู
ูุงุถูุน ุงูู
ุชุงุญุฉุ",
|
| 96 |
+
"timestamp": "2026-04-17T18:08:14.770895+00:00"
|
| 97 |
+
},
|
| 98 |
+
{
|
| 99 |
+
"role": "assistant",
|
| 100 |
+
"content": "ุนุฐุฑุงูุ ุญุฏุซ ุฎุทุฃ ูู ุงููุธุงู
. ุญุงูู ู
ุฑุฉ ุฃุฎุฑู.",
|
| 101 |
+
"timestamp": "2026-04-17T18:08:14.772719+00:00"
|
| 102 |
+
}
|
| 103 |
+
],
|
| 104 |
+
"full_messages": [
|
| 105 |
+
{
|
| 106 |
+
"role": "user",
|
| 107 |
+
"content": "ู
ุง ูู ุงูู
ูุงุถูุน ุงูู
ุชุงุญุฉุ",
|
| 108 |
+
"timestamp": "2026-04-17T18:08:14.770895+00:00"
|
| 109 |
+
},
|
| 110 |
+
{
|
| 111 |
+
"role": "assistant",
|
| 112 |
+
"content": "ุนุฐุฑุงูุ ุญุฏุซ ุฎุทุฃ ูู ุงููุธุงู
. ุญุงูู ู
ุฑุฉ ุฃุฎุฑู.",
|
| 113 |
+
"timestamp": "2026-04-17T18:08:14.772719+00:00"
|
| 114 |
+
}
|
| 115 |
+
],
|
| 116 |
+
"summary": "",
|
| 117 |
+
"message_count": 2,
|
| 118 |
+
"last_file": "main.txt",
|
| 119 |
+
"subject": "it",
|
| 120 |
+
"saved_at": "2026-04-17T21:08:14.783119"
|
| 121 |
+
}
|
| 122 |
+
}
|
config.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Global configuration and constants.
|
| 3 |
+
All settings in one place.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import secrets
|
| 8 |
+
|
| 9 |
+
# โโโ Flask โโโ
|
| 10 |
+
SECRET_KEY = os.environ.get("SECRET_KEY", secrets.token_hex(16))
|
| 11 |
+
DEBUG = os.environ.get("DEBUG", "true").lower() == "true"
|
| 12 |
+
HOST = os.environ.get("HOST", "0.0.0.0")
|
| 13 |
+
PORT = int(os.environ.get("PORT", 5000))
|
| 14 |
+
|
| 15 |
+
# โโโ Database files โโโ
|
| 16 |
+
USERS_DB_FILE = "users.json"
|
| 17 |
+
TELEGRAM_DB_FILE = "users_db.json"
|
| 18 |
+
CARDS_DB_FILE = "cards.json"
|
| 19 |
+
CHAT_HISTORY_DB_FILE = "chat_history_db.json"
|
| 20 |
+
|
| 21 |
+
# โโโ AI endpoints โโโ
|
| 22 |
+
GPT_URL = "https://corvo-ai-gpt-5-4.hf.space/chat"
|
| 23 |
+
TTS_URL = "https://corvo-ai-tts.hf.space"
|
| 24 |
+
TRANSCRIPT_URL = "https://corvo-ai-transcript.hf.space"
|
| 25 |
+
|
| 26 |
+
# โโโ Cloudinary โโโ
|
| 27 |
+
CLOUDINARY_URL = "https://api.cloudinary.com/v1_1/dwsoob1wh/image/upload"
|
| 28 |
+
CLOUDINARY_PRESET = "Cloud-storage"
|
| 29 |
+
|
| 30 |
+
# โโโ Icons8 โโโ
|
| 31 |
+
ICONS8_SEARCH_URL = (
|
| 32 |
+
"https://search-app.icons8.com/api/iconsets/v7/search"
|
| 33 |
+
"?isAnimated=false&language=en&analytics=true"
|
| 34 |
+
"&spellcheck=false&saveAnalytics=true"
|
| 35 |
+
"&amount=10&isOuch=true"
|
| 36 |
+
"&replaceNameWithSynonyms=true"
|
| 37 |
+
"&offset=0&term={term}"
|
| 38 |
+
)
|
| 39 |
+
ICONS8_IMAGE_URL = "https://img.icons8.com/?size=100&id={icon_id}&format=png"
|
| 40 |
+
|
| 41 |
+
# โโโ Audio โโโ
|
| 42 |
+
AUDIO_OUTPUT_DIR = "static/audio"
|
| 43 |
+
MAX_AUDIO_FILES = 200
|
| 44 |
+
|
| 45 |
+
# โโโ Chat history โโโ
|
| 46 |
+
SUMMARY_TRIGGER_COUNT = 10
|
| 47 |
+
MAX_CHAT_HISTORY = 20
|
| 48 |
+
|
| 49 |
+
# โโโ Directories to create on startup โโโ
|
| 50 |
+
REQUIRED_DIRS = [
|
| 51 |
+
"static/audio",
|
| 52 |
+
"templates",
|
| 53 |
+
"FINAL",
|
| 54 |
+
]
|
| 55 |
+
|
| 56 |
+
# โโโ Mistral cookie (for streaming) โโโ
|
| 57 |
+
MISTRAL_COOKIE = os.environ.get("MISTRAL_COOKIE", (
|
| 58 |
+
'''__cf_bm=JrqUGrlLa1VNznB_gcRKdDY.AyPLIvyRVUuKMV0avmc-1771531995-1.0.1.1-gJPuKUhLOcJ6hccPtwfCq6jJOAkQ8xABZJi_RwovhSQrsUTbUv1MCh1IPLvZ3ldPzqHLrPmcWUwLE_q_iVrP.W9PCYLnRq4HMMbVeyuDwlE; _cfuvid=oL4ochUsTJrLY6FlPpKTfocKdvOyum8GRuUBbqS9l8U-1771531995497-0.0.1.1-604800000; csrf_token_1d61ec8f0158ec4868343239ec73dbe1bfebad9908ad860e62f470c767573d0d=6ybeYU0W24tff63a5O/h0wbG6pYuc4w4iKv6SOKnYFc=; ory_session_coolcurranf83m3srkfl=MTc3MTUzMjA2OXxZU1dnRmd5SzNZM3o5VERSMGN5OFBWaHZ4bmdzd2RLOXNTUHNqMVVQbUdqUVFyZXpFN1ZCa0stYmJOR1lmbFNHVWphdWE4UWZ4S2FESjVROVJVYkw3a0MxN01TVVRZSUFGcWdZRHBCSjA2WEFJcFowcERmTWdrTmxuZ01zLWtubWxtd3lELVBBSVhOSzRkS1RRTWlxUHo1WmJwRWZzNlBXOFZWbUQyY2xkRVlOdXczb19kMlA4enc2ZGdVbkhaOUtXNDhEcEtxTVVBUTFYVk5YUXUtbDhjNVBOaG04WDRncmVNeXV4WWZXNC05TTlZRml3Y2hmR1F3QUR4Wk1aS0wwVUFfWGFyb2YxUlUtZXJkMGVhc3B8ITaivuTKOyk2pkoOhT6SPvP7zk-_NWF074pMm1HUl-k=; intercom-device-id-xel0jpx9=16cbae05-b305-4216-9967-e1622600c85c; csrftoken=KevqOi51yhx99LPY2i96wpMb2BxPlbpA; __cflb=0H28vBt3Asif1pksrBB47e5ijRcsvN4rdm4JmqYW3k8; intercom-session-xel0jpx9=K0lUK3E1QkhQemZuN256UXRtUVIxQWRoTVNQVXA2OVhXbDd1UHlGb3hxVmFVN0tJTG5LdFEvelpJck5aR0FjV3VWY0NjR1piMUxUdkZUc09kLzVDMTIrRGZiU3BtRElIQ0FvZUVZdHZyNU5sZU9EU0E3cHFCK1FzdE9kUTFKaG5PclZJTGRxN1c2WUt1NEVLNXBqZUwySStMQ2JHWHo3Z3Vyc2xLSWh2YkVDbWJBOWJWeHpHNFJIUzltZWUvQXVXLS1jdGVHOFVGRTJNS3I2Qm1tWlk3UEVBPT0=--d3764aa89dd7c5400eb27bf4e34ad8c85402eb72'''
|
| 59 |
+
))
|
data_structure/main.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"ุฃูุช ู
ุฏุฑุณ ูุฏูุฏ ูู
ุญุชุฑู ูู ู
ุงุฏุฉ ุชุฑุงููุจ ุจูุงูุงุช. ุงุณู
ู "ู
ุณุงุนุฏ ุชุฑุงููุจ ุจูุงูุงุช ุงูุฐูู".
|
| 2 |
+
**ุชุนููู
ุงุช:**
|
| 3 |
+
- ุฑุญุจ ุจุงูุทูุงุจ ุจุญุฑุงุฑุฉ ุฅุฐุง ูุงูุช ุฃูู ุฑุณุงูุฉ
|
| 4 |
+
- ุฃุฌุจ ุนูู ุงูุฃุณุฆูุฉ ุงูุนุงู
ุฉ ุจุดูู ู
ููุฏ
|
| 5 |
+
- ูุฌู ุงูุทูุงุจ ูุทุฑุญ ุฃุณุฆูุฉ ู
ุญุฏุฏุฉ ุนู ุงููุตูู ุนูุฏ ุงูุญุงุฌุฉ
|
| 6 |
+
- ุงุณุชุฎุฏู
ุงููุบุฉ ุงูุนุฑุจูุฉ
|
| 7 |
+
- ูู ู
ุดุฌุนุงู ูู
ุญูุฒุงู
|
| 8 |
+
- ุฑุงุนู ุณูุงู ุงูู
ุญุงุฏุซุฉ ุงูุณุงุจูุฉ
|
| 9 |
+
- ุฅุฐุง ูุงู ููุงู ู
ูุฎุต ุณุงุจูุ ุงุณุชุฎุฏู
ู ูููู
ู
ุณุชูู ุงูุทุงูุจ
|
| 10 |
+
=== ู
ุนููู
ุงุช ุนุงู
ุฉ ุนู ุงูู
ุงุฏุฉ ===
|
| 11 |
+
|
| 12 |
+
ุงูููุฌ ุงูุฐู ูุฌุจ ุชุฏุฑูุณู
|
| 13 |
+
|
| 14 |
+
C & Data Structures
|
| 15 |
+
|
| 16 |
+
Chapter 1: Address, Pointers, Arrays, and Structures
|
| 17 |
+
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|
+
SUBJECT: Lecture1_20260417_202347
|
| 3 |
+
TOTAL PAGES: 24
|
| 4 |
+
EXTRACTED ON: 2026-04-17 19:28:58
|
| 5 |
+
================================================================================
|
| 6 |
+
|
| 7 |
+
"ุฃูุช ู
ุฏุฑุณ ุฎุจูุฑ ูู
ุญุชุฑู ูู ู
ุงุฏุฉ ุชุฑุงููุจ ุจูุงูุงุช. ุฃูุช ุชุฏุฑูุณ ุทุงูุจุงู ูุชุณุงุนุฏู ุนูู ุงูููู
.
|
| 8 |
+
|
| 9 |
+
=== ู
ุญุชูู ุงููุตู (ุงูู
ุฑุฌุน ุงูุนูู
ู) ===
|
| 10 |
+
|
| 11 |
+
=== ููุงูุฉ ุงูุณูุงู ===
|
| 12 |
+
|
| 13 |
+
**ุชุนููู
ุงุช ู
ูู
ุฉ:**
|
| 14 |
+
- ุงุณุชุฎุฏู
ุงูู
ุญุชูู ุงูุนูู
ู ุฃุนูุงู ูู
ุฑุฌุน ุฃุณุงุณู ููุฅุฌุงุจุฉ
|
| 15 |
+
- ุฑุงุนู ุณูุงู ุงูู
ุญุงุฏุซุฉ ุงูุณุงุจูุฉ ูุงูู
ุณุชูู ุงูุชุนููู
ู ููุทุงูุจ
|
| 16 |
+
- ุฃุฌุจ ุจุดูู ูุงุถุญ ูู
ูุตู ุจุงููุบุฉ ุงูุนุฑุจูุฉ
|
| 17 |
+
- ุงุณุชุฎุฏู
ุงูุฃู
ุซูุฉ ูุงูุชูุถูุญุงุช ุนูุฏ ุงูุญุงุฌุฉ
|
| 18 |
+
- ุฅุฐุง ุณุฃู ุงูุทุงูุจ ุนู ุดูุก ุฎุงุฑุฌ ุงูู
ุญุชูู ุงูู
ุชููุฑุ ุฃุฎุจุฑู ุจุฐูู ุจูุทู
|
| 19 |
+
|
| 20 |
+
================================================================================
|
| 21 |
+
PAGE 1 - page_0001.png
|
| 22 |
+
================================================================================
|
| 23 |
+
|
| 24 |
+
------------TEXT EXTRACTED------------
|
| 25 |
+
C & Data Structures
|
| 26 |
+
|
| 27 |
+
Chapter 1: Address, Pointers, Arrays, and Structures
|
| 28 |
+
|
| 29 |
+
Dr. Amna Ali A Mohamed
|
| 30 |
+
|
| 31 |
+
Faculty of information technology
|
| 32 |
+
|
| 33 |
+
------------PAGE TALK ABOUT------------
|
| 34 |
+
ูุฐู ุงูุตูุญุฉ ูู ุบูุงู ุฃู ู
ูุฏู
ุฉ ูู
ุงุฏุฉ **C & Data Structures**ุ ูุชุญุฏูุฏูุง **ุงููุตู ุงูุฃูู** ุงูุฐู ูุชูุงูู: ุงูุนูุงููู ูู ุงูุฐุงูุฑุฉุ ุงูู
ุคุดุฑุงุชุ ุงูู
ุตูููุงุชุ ูุงูุชุฑุงููุจ ูู ูุบุฉ C.
|
| 35 |
+
ูู
ุง ูุธูุฑ ุงุณู
ุงูุฏูุชูุฑุฉ **Amna Ali A Mohamed** ูุงูุชู
ุงุคูุง ุฅูู **Faculty of information technology**.
|
| 36 |
+
ููุฌุฏ ุนูู ูู
ูู ุงูุตูุญุฉ ุฑุณู
ูุดุฌุฑุฉ ุจูุงูุงุช (Tree) ุจุนููุฏ ู
ุฑูู
ุฉ ู
ุซู 60 ู53 ู3 ูุบูุฑูุงุ ู
ุน ุฃุณูู
ุชูุถุญ ุงูุนูุงูุงุช ุจูู ุงูุนูุฏุ ุจุงูุฅุถุงูุฉ ุฅูู ุดูู ุตุบูุฑ ูุดูุฑ ุฅูู ู
ูููู
ุงูู
ุคุดุฑ ุฃู ุงูุนููุงู.
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
================================================================================
|
| 40 |
+
PAGE 2 - page_0002.png
|
| 41 |
+
================================================================================
|
| 42 |
+
|
| 43 |
+
------------TEXT EXTRACTED------------
|
| 44 |
+
Introduction
|
| 45 |
+
|
| 46 |
+
What Do We Mean by Data?
|
| 47 |
+
|
| 48 |
+
Data in programming functions like nouns, which are objects that store and represent information.
|
| 49 |
+
|
| 50 |
+
These objects could be numbers, text, or complex structures like lists and databases.
|
| 51 |
+
|
| 52 |
+
Actions on data are the verbs, representing operations that manipulate the data. These include actions like:
|
| 53 |
+
|
| 54 |
+
โข Adding new data (inserting elements into a list or database).
|
| 55 |
+
โข Deleting unnecessary or outdated data.
|
| 56 |
+
โข Reading data from a file or database.
|
| 57 |
+
โข Writing data to a file or memory.
|
| 58 |
+
|
| 59 |
+
------------PAGE TALK ABOUT------------
|
| 60 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ูุฏู
ุฉ ูู
ูููู
ุงูุจูุงูุงุช ูู ุงูุจุฑู
ุฌุฉุ ูุชูุถุญ ุฃู ุงูุจูุงูุงุช ุชุดุจู ุงูุฃุณู
ุงุก ูุฃููุง ุชู
ุซู ุฃุดูุงุก ุชุฎุฒู ุงูู
ุนููู
ุงุช.
|
| 61 |
+
ูู
ุง ุชุดุฑุญ ุฃู ุงูุจูุงูุงุช ูุฏ ุชููู ุฃุฑูุงู
ูุง ุฃู ูุตูุตูุง ุฃู ุชุฑุงููุจ ู
ุนูุฏุฉ ู
ุซู ุงูููุงุฆู
ูููุงุนุฏ ุงูุจูุงูุงุช.
|
| 62 |
+
ูุชุนุฑุถ ุฃูุถูุง ุฃู ุงูุนู
ููุงุช ุนูู ุงูุจูุงูุงุช ุชุดุจู ุงูุฃูุนุงูุ ู
ุซู ุงูุฅุถุงูุฉ ูุงูุญุฐู ูุงููุฑุงุกุฉ ูุงููุชุงุจุฉ.
|
| 63 |
+
ุงูุนูุตุฑ ุงูุจุตุฑู ุนุจุงุฑุฉ ุนู ุดุฑูุญุฉ ูุตูุฉ ุชุนููู
ูุฉ ุจุณูุทุฉ ุจุฎูููุฉ ุจูุถุงุก ูุนูุงููู ุจุงุฑุฒุฉ ุจุงูุฎุท ุงูุนุฑูุถ ุฏูู ุตูุฑ ุฃู ู
ุฎุทุทุงุช.
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
================================================================================
|
| 67 |
+
PAGE 3 - page_0003.png
|
| 68 |
+
================================================================================
|
| 69 |
+
|
| 70 |
+
------------TEXT EXTRACTED------------
|
| 71 |
+
Introduction (Cont.)
|
| 72 |
+
|
| 73 |
+
Understanding Data Representation
|
| 74 |
+
|
| 75 |
+
Computers store data as sequences of bits (0s and 1s), but human-
|
| 76 |
+
readable formats are more abstract.
|
| 77 |
+
|
| 78 |
+
For instance:
|
| 79 |
+
|
| 80 |
+
โข A single integer is stored as binary but is represented in decimal for
|
| 81 |
+
human understanding.
|
| 82 |
+
|
| 83 |
+
โข A character is stored using encoding schemes like ASCII or Unicode
|
| 84 |
+
but appears as a readable letter.
|
| 85 |
+
|
| 86 |
+
โข A list (array) is stored in contiguous memory locations but is
|
| 87 |
+
displayed as a structured collection of values.
|
| 88 |
+
|
| 89 |
+
------------PAGE TALK ABOUT------------
|
| 90 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุชู
ุซูู ุงูุจูุงูุงุช ุฏุงุฎู ุงูุญุงุณูุจ ูููู ุชูุฎุฒู ูุนูููุง ุนูู ุดูู ุจุชุงุช 0 ู1ุ ุจููู
ุง ุชุธูุฑ ููู
ุณุชุฎุฏู
ุจุตูุบ ุฃุณูู ููููู
.
|
| 91 |
+
ุชูุถุญ ุฃู
ุซูุฉ ุนูู ุฐูู: ุงูุฃุนุฏุงุฏ ุงูุตุญูุญุฉ ุชูุฎุฒู ุซูุงุฆููุง ูุชูุนุฑุถ ุนุดุฑููุงุ ูุงูุญุฑูู ุชูุฎุฒู ุจุชุฑู
ูุฒุงุช ู
ุซู ASCII ูUnicode.
|
| 92 |
+
ูู
ุง ุชุดุฑุญ ุฃู ุงูููุงุฆู
ุฃู ุงูู
ุตูููุงุช ุชูุฎุฒู ูู ู
ูุงูุน ู
ุชุฌุงูุฑุฉ ูู ุงูุฐุงูุฑุฉ ููููุง ุชูุนุฑุถ ูู
ุฌู
ูุนุฉ ู
ูุธู
ุฉ ู
๏ฟฝ๏ฟฝ ุงูููู
.
|
| 93 |
+
ุงูุนูุตุฑ ุงูุจุตุฑู ุนุจุงุฑุฉ ุนู ุดุฑูุญุฉ ูุตูุฉ ุชุนููู
ูุฉ ููุทุ ุจุฏูู ุตูุฑ ุฃู ู
ุฎุทุทุงุชุ ู
ุน ุนููุงู ูุจูุฑ ูููุงุท ุชุนุฏุงุฏ.
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
================================================================================
|
| 97 |
+
PAGE 4 - page_0004.png
|
| 98 |
+
================================================================================
|
| 99 |
+
|
| 100 |
+
------------TEXT EXTRACTED------------
|
| 101 |
+
1. Data Abstraction
|
| 102 |
+
|
| 103 |
+
Data abstraction is the process of separating the physical storage of data from how it is used in a program.
|
| 104 |
+
|
| 105 |
+
It allows programmers to work with high-level data structures (such as arrays, linked lists, and trees) without needing to understand the underlying memory details.
|
| 106 |
+
|
| 107 |
+
------------PAGE TALK ABOUT------------
|
| 108 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ูููู
**ุชุฌุฑูุฏ ุงูุจูุงูุงุช** ูู ุงูุจุฑู
ุฌุฉุ ููู ูุตู ุทุฑููุฉ ุชุฎุฒูู ุงูุจูุงูุงุช ูุนูููุง ุนู ุทุฑููุฉ ุงุณุชุฎุฏุงู
ูุง ุฏุงุฎู ุงูุจุฑูุงู
ุฌ.
|
| 109 |
+
ุชูุถุญ ุฃู ูุฐุง ุงูู
ูููู
ูุณุงุนุฏ ุงูู
ุจุฑู
ุฌูู ุนูู ุงูุชุนุงู
ู ู
ุน ููุงูู ุจูุงูุงุช ุนุงููุฉ ุงูู
ุณุชูู ู
ุซู ุงูู
ุตูููุงุช ูุงูููุงุฆู
ุงูู
ุฑุชุจุทุฉ ูุงูุฃุดุฌุงุฑ ุจุณูููุฉ.
|
| 110 |
+
ูู
ุง ุชุดูุฑ ุฅูู ุฃู ุงูู
ุจุฑู
ุฌ ูุง ูุญุชุงุฌ ูู
ุนุฑูุฉ ุชูุงุตูู ุงูุฐุงูุฑุฉ ุงูุฏุงุฎููุฉ ุนูุฏ ุงุณุชุฎุฏุงู
ูุฐู ุงูููุงูู.
|
| 111 |
+
ุงูุนูุตุฑ ุงูุจุตุฑู ูู ุงูุตูุญุฉ ุนุจุงุฑุฉ ุนู ุดุฑูุญุฉ ุจุณูุทุฉ ุจุฎูููุฉ ุจูุถุงุก ููุต ุฃุณูุฏ ูุจูุฑ ู
ุน ุนููุงู ุฑุฆูุณู ูุงุถุญ ูู ุงูุฃุนูู.
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
================================================================================
|
| 115 |
+
PAGE 5 - page_0005.png
|
| 116 |
+
================================================================================
|
| 117 |
+
|
| 118 |
+
------------TEXT EXTRACTED------------
|
| 119 |
+
1. Data Abstraction (Cont.)
|
| 120 |
+
|
| 121 |
+
Key Benefit of Data Abstraction
|
| 122 |
+
|
| 123 |
+
โข Simplifies programming by allowing a focus on logic rather than implementation details.
|
| 124 |
+
|
| 125 |
+
โข Enables the design of scalable and modular programs.
|
| 126 |
+
|
| 127 |
+
โข Supports the creation of reusable components that can work across different hardware and software platforms.
|
| 128 |
+
|
| 129 |
+
Data Abstraction: Data abstraction hides the physical representation of data and only exposes logical operations.
|
| 130 |
+
|
| 131 |
+
โข Example: An integer is represented differently across computers, yet operations remain the same.
|
| 132 |
+
|
| 133 |
+
------------PAGE TALK ABOUT------------
|
| 134 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ูููู
**ุชุฌุฑูุฏ ุงูุจูุงูุงุช** ูููุงุฆุฏู ูู ุงูุจุฑู
ุฌุฉ.
|
| 135 |
+
ุชูุถุญ ุฃูู ูุณุงุนุฏ ุงูู
ุจุฑู
ุฌ ุนูู ุงูุชุฑููุฒ ุนูู ู
ูุทู ุงูุจุฑูุงู
ุฌ ุจุฏู ุชูุงุตูู ุงูุชูููุฐ ุงูุฏุงุฎููุฉ.
|
| 136 |
+
ูู
ุง ุชุดูุฑ ุฅูู ุฃูู ูุฏุนู
ุจูุงุก ุจุฑุงู
ุฌ **ูุงุจูุฉ ููุชูุณุน** ู**ู
ูุณู
ุฉ ุฅูู ูุญุฏุงุช** ู
ุน ู
ูููุงุช ูุงุจูุฉ ูุฅุนุงุฏุฉ ุงูุงุณุชุฎุฏุงู
.
|
| 137 |
+
ููุฌุฏ ู
ุซุงู ูุจูู ุฃู ุชู
ุซูู ุงูุนุฏุฏ ุงูุตุญูุญ ูุฏ ูุฎุชูู ุจูู ุงูุฃุฌูุฒุฉุ ููู ุงูุนู
ููุงุช ุนููู ุชุจูู ููุณูุง.
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
================================================================================
|
| 141 |
+
PAGE 6 - page_0006.png
|
| 142 |
+
================================================================================
|
| 143 |
+
|
| 144 |
+
------------TEXT EXTRACTED------------
|
| 145 |
+
2. Data Structures
|
| 146 |
+
|
| 147 |
+
Definition: A structured way to store and organize data efficiently.
|
| 148 |
+
|
| 149 |
+
Example:
|
| 150 |
+
|
| 151 |
+
A library organizes books logically for easy access.
|
| 152 |
+
|
| 153 |
+
Difference between Abstract Data Type (ADT) and Data Structure:
|
| 154 |
+
โ ADT defines the logical view.
|
| 155 |
+
โ Data Structure provides an actual implementation.
|
| 156 |
+
|
| 157 |
+
[Top-right image]
|
| 158 |
+
Leaves of Grass
|
| 159 |
+
Software Engineering in ADA
|
| 160 |
+
Programming in c++
|
| 161 |
+
Biology Today
|
| 162 |
+
C++ Plus
|
| 163 |
+
INTRODUCTION TO CALCULUS
|
| 164 |
+
Gone with the Wind
|
| 165 |
+
HUMAN ANATOMY
|
| 166 |
+
Programming Proverbs
|
| 167 |
+
Adventures in Algebra
|
| 168 |
+
SIMPLY STATISTICS
|
| 169 |
+
Relational Databases
|
| 170 |
+
Romeo and Juliet
|
| 171 |
+
All over the place (Unordered)
|
| 172 |
+
|
| 173 |
+
[Middle-right image]
|
| 174 |
+
Adventures in Algebra
|
| 175 |
+
Biology Today
|
| 176 |
+
Gone with the Wind
|
| 177 |
+
HUMAN ANATOMY
|
| 178 |
+
INTRODUCTION TO CALCULUS
|
| 179 |
+
Leaves of Grass
|
| 180 |
+
Programming Proverbs
|
| 181 |
+
Programming in c++
|
| 182 |
+
Relational Databases
|
| 183 |
+
Romeo and Juliet
|
| 184 |
+
SIMPLY STATISTICS
|
| 185 |
+
Software Engineering in ADA
|
| 186 |
+
Alphabetical order by title
|
| 187 |
+
|
| 188 |
+
[Bottom-right image]
|
| 189 |
+
C++ Plus
|
| 190 |
+
Programming in c++
|
| 191 |
+
Relational Databases
|
| 192 |
+
Software Engineering in ADA
|
| 193 |
+
Programming Proverbs
|
| 194 |
+
SIMPLY STATISTICS
|
| 195 |
+
INTRODUCTION TO CALCULUS
|
| 196 |
+
Adventures in Algebra
|
| 197 |
+
Biology Today
|
| 198 |
+
HUMAN ANATOMY
|
| 199 |
+
Romeo and Juliet
|
| 200 |
+
Leaves of Grass
|
| 201 |
+
Gone with the Wind
|
| 202 |
+
Computer Science
|
| 203 |
+
Math
|
| 204 |
+
Biology
|
| 205 |
+
Literature
|
| 206 |
+
Ordered by subject
|
| 207 |
+
|
| 208 |
+
------------PAGE TALK ABOUT------------
|
| 209 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ู
ูููู
ููุงูู ุงูุจูุงูุงุช ุจุฃููุง ุทุฑููุฉ ู
ูุธู
ุฉ ูุชุฎุฒูู ุงูุจูุงูุงุช ูุชุฑุชูุจูุง ุจููุงุกุฉุ ู
ุน ู
ุซุงู ุชุดุจูู ุงูู
ูุชุจุฉ ุงูุชู ุชุฑุชุจ ุงููุชุจ ูุชุณููู ุงููุตูู ุฅูููุง.
|
| 210 |
+
ูู
ุง ุชูุถุญ ุงููุฑู ุจูู ADT ู Data Structureุ ุญูุซ ุฅู ADT ูู
ุซู ุงููุตู ุฃู ุงูู
ูุธูุฑ ุงูู
ูุทููุ ุจููู
ุง Data Structure ูู ๏ฟฝ๏ฟฝูุชุทุจูู ุงููุนูู ููุฐุง ุงููุตู.
|
| 211 |
+
ุงูุฑุณูู
ุงุช ุนูู ุงููู
ูู ุชุนุฑุถ ูุชุจูุง ู
ุฑุชุจุฉ ุจุซูุงุซ ุญุงูุงุช: ุบูุฑ ู
ุฑุชุจุฉุ ู
ุฑุชุจุฉ ุฃุจุฌุฏููุง ุญุณุจ ุงูุนููุงูุ ูู
ุฑุชุจุฉ ุญุณุจ ุงูู
ูุถูุน.
|
| 212 |
+
ุงููุฏู ู
ู ุงูุตูุฑ ูู ุชูุถูุญ ููู ุฃู ุงูุชูุธูู
ุงูุฌูุฏ ูุฌุนู ุงููุตูู ุฅูู ุงูุจูุงูุงุช ุฃุณูู ูุฃูุถุญ.
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
================================================================================
|
| 216 |
+
PAGE 7 - page_0007.png
|
| 217 |
+
================================================================================
|
| 218 |
+
|
| 219 |
+
------------TEXT EXTRACTED------------
|
| 220 |
+
3. Overview of Data Structures
|
| 221 |
+
|
| 222 |
+
| Data Structure | Strengths | Weaknesses |
|
| 223 |
+
|---|---|---|
|
| 224 |
+
| Arrays | Fast access | Fixed size |
|
| 225 |
+
| Linked Lists | Dynamic size | Slower access |
|
| 226 |
+
| Stacks | Easy LIFO operations | Limited access |
|
| 227 |
+
| Queues | FIFO processing | Limited access |
|
| 228 |
+
| Trees | Hierarchical organization | Complex implementation |
|
| 229 |
+
| Hash Tables | Fast lookups | Potential collisions |
|
| 230 |
+
|
| 231 |
+
------------PAGE TALK ABOUT------------
|
| 232 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ูุธุฑุฉ ุนุงู
ุฉ ุนูู ููุงูู ุงูุจูุงูุงุช ุงูุฃุณุงุณูุฉ ูู
ูุงุฑูุฉ ุณุฑูุนุฉ ุจูููุง.
|
| 233 |
+
ููุฌุฏ ุฌุฏูู ุจุซูุงุซุฉ ุฃุนู
ุฏุฉ: ุงุณู
ูููู ุงูุจูุงูุงุชุ ููุงุท ุงูููุฉุ ูููุงุท ุงูุถุนู.
|
| 234 |
+
ูุนุฑุถ ุฃู
ุซูุฉ ู
ุซู Arrays ูLinked Lists ูStacks ูQueues ูTrees ูHash Tables ู
ุน ู
ูุฒุฉ ูุนูุจ ููู ููุน.
|
| 235 |
+
ุงูุชุตู
ูู
ุจุตุฑููุง ุนุจุงุฑุฉ ุนู ุดุฑูุญุฉ ุชุนููู
ูุฉ ุจุณูุทุฉ ุชุญุชูู ุนููุงููุง ูุจูุฑูุง ูู ุงูุฃุนูู ูุฌุฏูููุง ู
ูุธู
ูุง ูู ุงูู
ูุชุตู ุจุฎูููุงุช ุฑู
ุงุฏูุฉ ู
ุชูุงูุจุฉ.
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
================================================================================
|
| 239 |
+
PAGE 8 - page_0008.png
|
| 240 |
+
================================================================================
|
| 241 |
+
|
| 242 |
+
------------TEXT EXTRACTED------------
|
| 243 |
+
C Language
|
| 244 |
+
|
| 245 |
+
1. Address & Memory Allocation
|
| 246 |
+
Each variable has:
|
| 247 |
+
โข Value (data stored in memory).
|
| 248 |
+
โข Address (location in memory).
|
| 249 |
+
|
| 250 |
+
```c
|
| 251 |
+
#include <stdio.h>
|
| 252 |
+
int main() {
|
| 253 |
+
int i = 10;
|
| 254 |
+
printf("Address of i: %p\n", &i);
|
| 255 |
+
return 0;
|
| 256 |
+
}
|
| 257 |
+
```
|
| 258 |
+
|
| 259 |
+
Explanation
|
| 260 |
+
Memory allocations to the variables can be explained using the following variables:
|
| 261 |
+
1. 100,i 10
|
| 262 |
+
2. 200, j 20
|
| 263 |
+
3. 300,k 30
|
| 264 |
+
|
| 265 |
+
------------PAGE TALK ABOUT------------
|
| 266 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ูููู
ุงูุนููุงู ูุงูุฐุงูุฑุฉ ูู ูุบุฉ Cุ ูุฃู ูู ู
ุชุบูุฑ ูู
ูู ููู
ุฉ ู
ุฎุฒูุฉ ุฏุงุฎู ุงูุฐุงูุฑุฉ ูุนููุงููุง ูุญุฏุฏ ู
ูุงูู.
|
| 267 |
+
ููุฌุฏ ู
ุซุงู ุจุฑู
ุฌู ูุนุฑูู ุงูู
ุชุบูุฑ `i` ุจููู
ุฉ 10 ุซู
ูุทุจุน ุนููุงูู ุจุงุณุชุฎุฏุงู
`&i` ู`printf`.
|
| 268 |
+
ูู
ุง ุชุนุฑุถ ุงูุตูุญุฉ ุดุฑุญูุง ู
ุจุณุทูุง ูุชุฎุตูุต ุงูุฐุงูุฑุฉ ุนุจุฑ ุฃู
ุซูุฉ ูู
ุชุบูุฑุงุช ู
ุซู `i` ู`j` ู`k` ู
ุน ุนูุงููู ูููู
ุงูุชุฑุงุถูุฉ.
|
| 269 |
+
ุจุตุฑููุงุ ุชุญุชูู ุงูุตูุญุฉ ุนูู ุนููุงู ูุจูุฑ "C Language"ุ ูููุงุท ุชุนุฏุงุฏุ ูุตูุฑุฉ ูููุฏ Cุ ููุต ุดุฑุญ ุนูู ุงูุฌูุฉ ุงููู
ูู.
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
================================================================================
|
| 273 |
+
PAGE 9 - page_0009.png
|
| 274 |
+
================================================================================
|
| 275 |
+
|
| 276 |
+
------------TEXT EXTRACTED------------
|
| 277 |
+
1. Address & Memory Allocation
|
| 278 |
+
|
| 279 |
+
Explanation
|
| 280 |
+
Memory allocations to the variables can be explained using the following
|
| 281 |
+
variables:
|
| 282 |
+
1. 100,i 10
|
| 283 |
+
2. 200, j 20
|
| 284 |
+
3. 300,k 30
|
| 285 |
+
|
| 286 |
+
Points to Remember
|
| 287 |
+
โข Each variable has two attributes: address and value.
|
| 288 |
+
โข The address is the location in memory where the value of the variable is stored.
|
| 289 |
+
โข During the lifetime of the variable, the address is not changed but the value may change.
|
| 290 |
+
|
| 291 |
+
------------PAGE TALK ABOUT------------
|
| 292 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ูููู
ุงูุนููุงู ูู ุงูุฐุงูุฑุฉ ูุชุฎุตูุต ุงูุฐุงูุฑุฉ ููู
ุชุบูุฑุงุช ูู ุงูุจุฑู
ุฌุฉ.
|
| 293 |
+
ุชูุถุญ ุฃู ูู ู
ุชุบูุฑ ูู ุฎุงุตูุชุงู ุฃุณุงุณูุชุงู: ุนููุงู ูู ุงูุฐุงูุฑุฉ ูููู
ุฉ ู
ุฎุฒูุฉ ุฏุงุฎูู.
|
| 294 |
+
ุชุนุฑุถ ุฃู
ุซูุฉ ูู
ุชุบูุฑุงุช ู
ุซู i ู j ู k ู
ุน ุนูุงููู ูููู
ู
ุฎุชููุฉ ูุชูุถูุญ ุงูููุฑุฉ.
|
| 295 |
+
ูู
ุง ุชุคูุฏ ุฃู ุนููุงู ุงูู
ุชุบูุฑ ูุจูู ุซุงุจุชูุง ุฃุซูุงุก ุญูุงุชูุ ุจููู
ุง ูู
ูู ุฃู ุชุชุบูุฑ ููู
ุชู.
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
================================================================================
|
| 299 |
+
PAGE 10 - page_0010.png
|
| 300 |
+
================================================================================
|
| 301 |
+
|
| 302 |
+
------------TEXT EXTRACTED------------
|
| 303 |
+
2. Pointers
|
| 304 |
+
|
| 305 |
+
โข A pointer is a variable whose value is also an address. As described earlier, each variable has two attributes: address and value.
|
| 306 |
+
|
| 307 |
+
โข A variable can take any value specified by its data type.
|
| 308 |
+
|
| 309 |
+
โข For example, if the variable i is of the integer type, it can take any value permitted in the range specified by the integer data type.
|
| 310 |
+
|
| 311 |
+
โข A pointer to an integer is a variable that can store the address of that integer.
|
| 312 |
+
|
| 313 |
+
------------PAGE TALK ABOUT------------
|
| 314 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ูููู
ุงูู
ุคุดุฑุงุช (Pointers) ูู ุงูุจุฑู
ุฌุฉุ ูุฃู ุงูู
ุคุดุฑ ูู ู
ุชุบูุฑ ูุฎุฒู ุนููุงููุง ูู ุงูุฐุงูุฑุฉ ุจุฏููุง ู
ู ููู
ุฉ ุนุงุฏูุฉ ููุท.
|
| 315 |
+
ูู
ุง ุชูุถุญ ุฃู ูู ู
ุชุบูุฑ ูู ุฎุงุตูุชุงู: ุงูุนููุงู ูุงูููู
ุฉุ ูุฃู ููุน ุงูุจูุงูุงุช ูุญุฏุฏ ุงูููู
ุงูู
ู
ููุฉ ููู
ุชุบูุฑ.
|
| 316 |
+
ูุชุนุฑุถ ู
ุซุงููุง ุนูู ุงูู
ุชุบูุฑ ุงูุตุญูุญ integerุ ุซู
ุชุดุฑุญ ุฃู ู
ุคุดุฑ ุงูุนุฏุฏ ุงูุตุญูุญ ูู
ููู ุชุฎุฒูู ุนููุงู ูุฐุง ุงูู
ุชุบูุฑ.
|
| 317 |
+
ุงูุนูุตุฑ ุงูุจุตุฑู ุงููุญูุฏ ูู ุชูุณูู ุดุฑูุญุฉ ุนุฑุถ ุจุนููุงู ูุจูุฑ ูููุงุท ุชุนุฏุงุฏุ ููุง ุชูุฌุฏ ุตูุฑ ุฃู ู
ุฎุทุทุงุช.
|
| 318 |
+
|
| 319 |
+
|
| 320 |
+
================================================================================
|
| 321 |
+
PAGE 11 - page_0011.png
|
| 322 |
+
================================================================================
|
| 323 |
+
|
| 324 |
+
------------TEXT EXTRACTED------------
|
| 325 |
+
2. Pointers (cont.)
|
| 326 |
+
|
| 327 |
+
โข A pointer stores the address of another variable.
|
| 328 |
+
|
| 329 |
+
โข Allows indirect access to a variable's value.
|
| 330 |
+
|
| 331 |
+
```c
|
| 332 |
+
#include <stdio.h>
|
| 333 |
+
|
| 334 |
+
int main() {
|
| 335 |
+
int i = 10; // Declare integer variable
|
| 336 |
+
|
| 337 |
+
int *ia; // Declare pointer to integer I
|
| 338 |
+
|
| 339 |
+
ia = &i; // Assign address of i to ia
|
| 340 |
+
|
| 341 |
+
printf("The address of i is %p\n", ia); // Print address of Ia
|
| 342 |
+
|
| 343 |
+
printf("The value at that location is %d\n", *ia); // Print value using pointer
|
| 344 |
+
|
| 345 |
+
*ia = 50; // Modify value using pointer
|
| 346 |
+
|
| 347 |
+
printf("The value of i is %d\n", i); // Print updated value of i
|
| 348 |
+
|
| 349 |
+
return 0;
|
| 350 |
+
}
|
| 351 |
+
```
|
| 352 |
+
|
| 353 |
+
------------PAGE TALK ABOUT------------
|
| 354 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ูููู
ุงูู
ุคุดุฑุงุช (Pointers) ูู ูุบุฉ Cุ ูุฃููุง ุชุฎุฒู ุนููุงู ู
ุชุบูุฑ ุขุฎุฑ ูุชุณู
ุญ ุจุงููุตูู ุบูุฑ ุงูู
ุจุงุดุฑ ุฅูู ููู
ุชู.
|
| 355 |
+
ููุฌุฏ ู
ุซุงู ุจุฑู
ุฌู ูุนุฑูู ู
ุชุบูุฑูุง ุตุญูุญูุง `i` ูู
ุคุดุฑูุง `ia` ูุดูุฑ ุฅููู ุจุงุณุชุฎุฏุงู
ุงูุนุงู
ู `&`.
|
| 356 |
+
ุซู
ููุถุญ ููููุฉ ุทุจุงุนุฉ ุงูุนููุงู ุจุงุณุชุฎุฏุงู
`%p`ุ ููุฑุงุกุฉ ุงูููู
ุฉ ุนุจุฑ ูู ุงูุฅุดุงุฑุฉ `*ia`ุ ุซู
ุชุนุฏูู ููู
ุฉ `i` ู
ู ุฎูุงู ุงูู
ุคุดุฑ ุฅูู `50`.
|
| 357 |
+
ุงูุนูุตุฑ ุงูุจุตุฑู ุงูุฃุณุงุณู ูู ุตูุฏูู ููุฏ ุจุฑู
ุฌู ุฏุงุฎู ุดุฑูุญุฉ ุนุฑุถ ุชุนููู
ูุฉ ุจุนููุงู ูุจูุฑ ูููุทุชูู ุชูุถูุญูุชูู ุฃุนูู ุงูู
ุซุงู.
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
================================================================================
|
| 361 |
+
PAGE 12 - page_0012.png
|
| 362 |
+
================================================================================
|
| 363 |
+
|
| 364 |
+
------------TEXT EXTRACTED------------
|
| 365 |
+
2. Pointers (cont.)
|
| 366 |
+
|
| 367 |
+
Syntax:
|
| 368 |
+
|
| 369 |
+
Declare: int *ptr.
|
| 370 |
+
|
| 371 |
+
Assign address: ptr = &var.
|
| 372 |
+
|
| 373 |
+
Access value: *ptr (dereferencing).
|
| 374 |
+
|
| 375 |
+
```c
|
| 376 |
+
#include <stdio.h>
|
| 377 |
+
|
| 378 |
+
int main() {
|
| 379 |
+
int i = 10; // Declare integer variable
|
| 380 |
+
int *ia; // Declare pointer to integer I
|
| 381 |
+
a = &i; // Assign address of i to ia
|
| 382 |
+
|
| 383 |
+
printf("The address of i is %p\n", ia); // Print address of I
|
| 384 |
+
printf("The value at that location is %d\n", *ia); // Print value using pointer
|
| 385 |
+
|
| 386 |
+
*ia = 50; // Modify value using pointer
|
| 387 |
+
|
| 388 |
+
printf("The value of i is %d\n", i); // Print updated value of i
|
| 389 |
+
|
| 390 |
+
return 0;
|
| 391 |
+
}
|
| 392 |
+
```
|
| 393 |
+
|
| 394 |
+
------------PAGE TALK ABOUT------------
|
| 395 |
+
ุชุดุฑุญ ุงูุตูุญุฉ ู
ูููู
ุงูู
ุคุดุฑุงุช (Pointers) ูู ูุบุฉ C ุจุดูู ู
ุจุณุท.
|
| 396 |
+
ุชูุถุญ ุงูุตูุงุบุฉ ุงูุฃุณุงุณูุฉ: ุชุนุฑูู ู
ุคุดุฑุ ุฅุณูุงุฏ ุนููุงู ู
ุชุบูุฑ ูู ุจุงุณุชุฎุฏุงู
`&`ุ ุซู
ุงููุตูู ุฅูู ุงูููู
ุฉ ุจุงุณุชุฎุฏุงู
`*` (ูู ุงูุฅุดุงุฑุฉ).
|
| 397 |
+
ููุฌุฏ ู
ุซุงู ุจุฑู
ุฌู ูุจููู ุฅูุดุงุก ู
ุชุบูุฑ ุตุญูุญ `i`ุ ุซู
ู
ุคุดุฑ ูุดูุฑ ุฅูููุ ูุทุจุงุนุฉ ุงูุนููุงู ูุงูููู
ุฉุ ูุจุนุฏูุง ุชุนุฏูู ููู
ุฉ `i` ุนุจุฑ ุงูู
ุคุดุฑ.
|
| 398 |
+
ุงูุนูุตุฑ ุงูุจุตุฑู ุงูุฃุณุงุณู ูู ุดุฑูุญุฉ ุชุนููู
ูุฉ ุชุญุชูู ุนููุงููุง ูุจูุฑูุง ูู ุงูุฃุนููุ ููุตูุง ุชูุถูุญููุง ุนูู ุงููู
ููุ ูู
ุฑุจุนูุง ุฑู
ุงุฏููุง ูู ุงูุฃุณูู ุจุฏุงุฎูู ููุฏ ู
ูููู.
|
| 399 |
+
|
| 400 |
+
|
| 401 |
+
================================================================================
|
| 402 |
+
PAGE 13 - page_0013.png
|
| 403 |
+
================================================================================
|
| 404 |
+
|
| 405 |
+
------------TEXT EXTRACTED------------
|
| 406 |
+
3. Arrays
|
| 407 |
+
|
| 408 |
+
โข An array stores multiple values of the same type.
|
| 409 |
+
|
| 410 |
+
โข Elements are accessed using an index (starting from 0).
|
| 411 |
+
|
| 412 |
+
โข Array name is a pointer constant pointing to the first element.
|
| 413 |
+
|
| 414 |
+
```c
|
| 415 |
+
#include <stdio.h>
|
| 416 |
+
|
| 417 |
+
int main() {
|
| 418 |
+
int a[5]; // Declare an array of size 5
|
| 419 |
+
|
| 420 |
+
for(int i = 0; i < 5; i++) {
|
| 421 |
+
a[i] = i; // Assign values to array elements
|
| 422 |
+
}
|
| 423 |
+
|
| 424 |
+
for(int i = 0; i < 5; i++) {
|
| 425 |
+
printf("value in array %d\n", a[i]); // Print array elements
|
| 426 |
+
}
|
| 427 |
+
|
| 428 |
+
return 0;
|
| 429 |
+
}
|
| 430 |
+
```
|
| 431 |
+
|
| 432 |
+
------------PAGE TALK ABOUT------------
|
| 433 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุงูู
ุตูููุงุช ูู ูุบุฉ Cุ ู๏ฟฝ๏ฟฝููุง ุชูุณุชุฎุฏู
ูุชุฎุฒูู ุนุฏุฉ ููู
ู
ู ููุณ ุงูููุน.
|
| 434 |
+
ุชูุถุญ ุฃู ุงููุตูู ุฅูู ุนูุงุตุฑ ุงูู
ุตูููุฉ ูุชู
ุจุงุณุชุฎุฏุงู
ุงูููุฑุณ ููุจุฏุฃ ู
ู 0.
|
| 435 |
+
ูู
ุง ุชุดุฑุญ ุฃู ุงุณู
ุงูู
ุตูููุฉ ูุดูุฑ ุฅูู ุฃูู ุนูุตุฑ ูููุง ูู
ุคุดุฑ ุซุงุจุช.
|
| 436 |
+
ููุฌุฏ ุฃูุถูุง ู
ุซุงู ุจุฑู
ุฌู ูุนุฑูู ู
ุตูููุฉ ู
ู 5 ุนูุงุตุฑุ ุซู
ูู
ูุคูุง ููุทุจุน ููู
ูุง ุจุงุณุชุฎุฏุงู
ุญููุชู for.
|
| 437 |
+
|
| 438 |
+
|
| 439 |
+
================================================================================
|
| 440 |
+
PAGE 14 - page_0014.png
|
| 441 |
+
================================================================================
|
| 442 |
+
|
| 443 |
+
------------TEXT EXTRACTED------------
|
| 444 |
+
3. Arrays
|
| 445 |
+
|
| 446 |
+
Points to Remember
|
| 447 |
+
|
| 448 |
+
1. An array is a composite data structure in which you can store multiple
|
| 449 |
+
values. Array elements are accessed using subscript.
|
| 450 |
+
|
| 451 |
+
2. The subscript operator has the highest precedence. Thus if you write
|
| 452 |
+
a[2]++,it increments the value at location 2 in the array.
|
| 453 |
+
|
| 454 |
+
3. The valid range of subscript is 0 to size โ1.
|
| 455 |
+
|
| 456 |
+
------------PAGE TALK ABOUT------------
|
| 457 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุงูู
ุตูููุงุช (Arrays) ูุฃูู
ุงูููุงุท ุงูุฃุณุงุณูุฉ ุงูู
ุชุนููุฉ ุจูุง ูู ุงูุจุฑู
ุฌุฉ.
|
| 458 |
+
ุชูุถุญ ุฃู ุงูู
ุตูููุฉ ูู ุจููุฉ ุจูุงูุงุช ู
ุฑูุจุฉ ุชูุณุชุฎุฏู
ูุชุฎุฒูู ุนุฏุฉ ููู
ุ ููุชู
ุงููุตูู ุฅูู ุนูุงุตุฑูุง ุจุงุณุชุฎุฏุงู
ุงูููุฑุณ (subscript).
|
| 459 |
+
ูู
ุง ุชุดุฑุญ ุฃู ู
ุนุงู
ู ุงูููุฑุณุฉ ูู ุฃููููุฉ ุนุงููุฉุ ูุฐูู ุงูุชุนุจูุฑ a[2]++ ูุฒูุฏ ููู
ุฉ ุงูุนูุตุฑ ุงูู
ูุฌูุฏ ูู ุงูู
ููุน 2.
|
| 460 |
+
ูุชุฐูุฑ ุฃูุถูุง ุฃู ุงูู
ุฌุงู ุงูุตุญูุญ ููููุงุฑุณ ูุจุฏุฃ ู
ู 0 ูููุชูู ุนูุฏ size โ 1.
|
| 461 |
+
ุงูุนูุตุฑ ุงูุจุตุฑู ุงููุญูุฏ ูู ุชูุณูู ูุตู ุชุนููู
ู ุจุนููุงู ูุจูุฑ ููุงุฆู
ุฉ ู
ุฑูู
ุฉ ุจุฏูู ุตูุฑ ุฃู ู
ุฎุทุทุงุช.
|
| 462 |
+
|
| 463 |
+
|
| 464 |
+
================================================================================
|
| 465 |
+
PAGE 15 - page_0015.png
|
| 466 |
+
================================================================================
|
| 467 |
+
|
| 468 |
+
------------TEXT EXTRACTED------------
|
| 469 |
+
Address of Array Elements
|
| 470 |
+
|
| 471 |
+
โข Each array element has a memory address.
|
| 472 |
+
|
| 473 |
+
โข Elements are stored in consecutive memory locations.
|
| 474 |
+
|
| 475 |
+
#include <stdio.h>
|
| 476 |
+
int main() {
|
| 477 |
+
int a[5];
|
| 478 |
+
for(int i = 0; i < 5; i++) {
|
| 479 |
+
a[i] = i; // Assign values to array
|
| 480 |
+
}
|
| 481 |
+
for(int i = 0; i < 5; i++) {
|
| 482 |
+
printf("value in array %d and address is %p\n", a[i], &a[i]);
|
| 483 |
+
}
|
| 484 |
+
return 0;
|
| 485 |
+
}
|
| 486 |
+
|
| 487 |
+
------------PAGE TALK ABOUT------------
|
| 488 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุนูุงููู ุนูุงุตุฑ ุงูู
ุตูููุฉ ูู ูุบุฉ Cุ ูุฃู ูู ุนูุตุฑ ุฏุงุฎู ุงูู
ุตูููุฉ ูู ุนููุงู ูู ุงูุฐุงูุฑุฉ.
|
| 489 |
+
ูู
ุง ุชูุถูุญ ุฃู ุนูุงุตุฑ ุงูู
ุตูููุฉ ุชูุฎุฒููู ูู ู
ูุงูุน ู
ุชุชุงููุฉ ุฏุงุฎู ุงูุฐุงูุฑุฉ.
|
| 490 |
+
ููุฌุฏ ู
ุซุงู ุจุฑู
ุฌู ูุนุฑูู ู
ุตูููุฉ ู
ู 5 ุนูุงุตุฑุ ุซู
ูู
ูุคูุง ุจููู
ุ ูุจุนุฏ ุฐูู ูุทุจุน ููู
ุฉ ูู ุนูุตุฑ ู
ุน ุนููุงูู ุจุงุณุชุฎุฏุงู
printf.
|
| 491 |
+
ุงูุชุตู
ูู
ุนุจุงุฑุฉ ุนู ุดุฑูุญุฉ ุชุนููู
ูุฉ ุชุญุชูู ุนูู ุนููุงู ูุจูุฑ ูููุทุชูู ุฑุฆูุณูุชูู ู
ุน ุตูุฏูู ููุฏ ุจุฑู
ุฌู ูู ุงูุฃุณูู.
|
| 492 |
+
|
| 493 |
+
|
| 494 |
+
================================================================================
|
| 495 |
+
PAGE 16 - page_0016.png
|
| 496 |
+
================================================================================
|
| 497 |
+
|
| 498 |
+
------------TEXT EXTRACTED------------
|
| 499 |
+
Accessing Arrays Using Pointers
|
| 500 |
+
|
| 501 |
+
โข Arrays can be accessed using pointers.
|
| 502 |
+
โข Pointer arithmetic moves the pointer to the next element.
|
| 503 |
+
|
| 504 |
+
#include <stdio.h>
|
| 505 |
+
|
| 506 |
+
int main() {
|
| 507 |
+
int a[5] = {10, 20, 30, 40, 50};
|
| 508 |
+
|
| 509 |
+
int *ptr = a; // Assign base address of array to pointer
|
| 510 |
+
|
| 511 |
+
for(int i = 0; i < 5; i++) {
|
| 512 |
+
printf("value in array %d and address is %p\n", *ptr, ptr);
|
| 513 |
+
|
| 514 |
+
ptr=ptr+1; // Move pointer to next element
|
| 515 |
+
}
|
| 516 |
+
}
|
| 517 |
+
|
| 518 |
+
return 0;
|
| 519 |
+
|
| 520 |
+
------------PAGE TALK ABOUT------------
|
| 521 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ููููุฉ ุงููุตูู ุฅูู ุนูุงุตุฑ ุงูู
ุตูููุฉ ุจุงุณุชุฎุฏุงู
ุงูู
ุคุดุฑุงุช ูู ูุบุฉ C.
|
| 522 |
+
ุชูุถุญ ุฃู ุงุณู
ุงูู
ุตูููุฉ ูู
ูู ุฅุณูุงุฏู ุฅูู ู
ุคุดุฑ ููุดูุฑ ุฅูู ุฃูู ุนูุตุฑ ูููุงุ ุซู
ูู
ูู ุงูุชููู ุจูู ุงูุนูุงุตุฑ ุจุงุณุชุฎุฏุงู
ุงูุนู
ููุงุช ุงูุญุณุงุจูุฉ ุนูู ุงูู
ุคุดุฑ.
|
| 523 |
+
ููุฌุฏ ู
ุซุงู ุจุฑู
ุฌู ูุนุฑูู ู
ุตูููุฉ ู
ู 5 ุฃุนุฏุงุฏุ ุซู
ูุณุชุฎุฏู
ู
ุคุดุฑูุง ูุทุจุงุนุฉ ููู
ุฉ ูู ุนูุตุฑ ูุนููุงูู ูู ุงูุฐุงูุฑุฉ.
|
| 524 |
+
ุงูุชุตู
ูู
ุนุจุงุฑุฉ ุนู ุดุฑูุญุฉ ุชุนููู
ูุฉ ุจุนููุงู ูุจูุฑ ู
ุน ููุทุชูู ุชูุถูุญูุชููุ ูุฃุณูููุง ุตูุฏูู ูุญุชูู ุนูู ููุฏ ุจุฑู
ุฌู ู
ูููู.
|
| 525 |
+
|
| 526 |
+
|
| 527 |
+
================================================================================
|
| 528 |
+
PAGE 17 - page_0017.png
|
| 529 |
+
================================================================================
|
| 530 |
+
|
| 531 |
+
------------TEXT EXTRACTED------------
|
| 532 |
+
Accessing Arrays Using Pointers
|
| 533 |
+
|
| 534 |
+
Points to Remember
|
| 535 |
+
|
| 536 |
+
1. Array elements can be accessed using pointers.
|
| 537 |
+
|
| 538 |
+
2. The array name is the pointer constant which can be assigned to any pointer variable.
|
| 539 |
+
|
| 540 |
+
#include <stdio.h>
|
| 541 |
+
|
| 542 |
+
int main() {
|
| 543 |
+
|
| 544 |
+
int a[5] = {10, 20, 30, 40, 50};
|
| 545 |
+
|
| 546 |
+
int *ptr = a; // Assign base address of array to pointer
|
| 547 |
+
|
| 548 |
+
for(int i = 0; i < 5; i++) {
|
| 549 |
+
|
| 550 |
+
printf("value in array %d and address is %p\n", *ptr, ptr);
|
| 551 |
+
|
| 552 |
+
ptr=ptr+1; // Move pointer to next element
|
| 553 |
+
|
| 554 |
+
}
|
| 555 |
+
|
| 556 |
+
return 0;
|
| 557 |
+
|
| 558 |
+
}
|
| 559 |
+
|
| 560 |
+
------------PAGE TALK ABOUT------------
|
| 561 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ููููุฉ ุงููุตูู ุฅูู ุนูุงุตุฑ ุงูู
ุตูููุฉ ุจุงุณุชุฎุฏุงู
ุงูู
ุคุดุฑุงุช ูู ูุบุฉ C.
|
| 562 |
+
ุชูุถุญ ุฃู ุงุณู
ุงูู
ุตูููุฉ ูู
ุซู ุนููุงู ุงูุจุฏุงูุฉ ููู
ูู ุฅุณูุงุฏู ุฅูู ู
ุชุบูุฑ ู
ุคุดุฑ.
|
| 563 |
+
ููุฌุฏ ู
ุซุงู ุจุฑู
ุฌู ูุนุฑูู ู
ุตูููุฉ ู
ู 5 ุนูุงุตุฑุ ุซู
ูุณุชุฎุฏู
ู
ุคุดุฑูุง ูุทุจุงุนุฉ ููู
ุฉ ูู ุนูุตุฑ ูุนููุงูู.
|
| 564 |
+
ูู
ุง ูุจููู ุงูู
ุซุงู ุชุญุฑูู ุงูู
ุคุดุฑ ุฅูู ุงูุนูุตุฑ ุงูุชุงูู ุจุงุณุชุฎุฏุงู
`ptr = ptr + 1`.
|
| 565 |
+
|
| 566 |
+
|
| 567 |
+
================================================================================
|
| 568 |
+
PAGE 18 - page_0018.png
|
| 569 |
+
================================================================================
|
| 570 |
+
|
| 571 |
+
------------TEXT EXTRACTED------------
|
| 572 |
+
Manipulating arrays using pointers
|
| 573 |
+
|
| 574 |
+
โข Pointers can be used to manipulate arrays by incrementing or decrementing the pointer.
|
| 575 |
+
โข Pointer arithmetic automatically adjusts the pointer based on the data type size.
|
| 576 |
+
|
| 577 |
+
```c
|
| 578 |
+
#include <stdio.h>
|
| 579 |
+
int main() {
|
| 580 |
+
int a[5] = {10, 20, 30, 40, 50};
|
| 581 |
+
|
| 582 |
+
int *ptr = a; // Assign base address of array to pointer
|
| 583 |
+
for(int i = 0; i < 5; i++) {
|
| 584 |
+
printf("value in array %d and address is %p\n", *ptr, ptr);
|
| 585 |
+
|
| 586 |
+
ptr++; // Move pointer to next element
|
| 587 |
+
}
|
| 588 |
+
|
| 589 |
+
return 0;
|
| 590 |
+
}
|
| 591 |
+
```
|
| 592 |
+
|
| 593 |
+
------------PAGE TALK ABOUT------------
|
| 594 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ููููุฉ ุงูุชุนุงู
ู ู
ุน ุงูู
ุตูููุงุช ุจุงุณุชุฎุฏุงู
ุงูู
ุคุดุฑุงุช ูู ูุบุฉ Cุ ู
ู ุฎูุงู ุชุญุฑูู ุงูู
ุคุดุฑ ุจูู ุนูุงุตุฑ ุงูู
ุตูููุฉ.
|
| 595 |
+
ุชูุถุญ ุฃู ุงูุนู
ููุงุช ุงูุญุณุงุจูุฉ ุนูู ุงูู
ุคุดุฑ ู
ุซู `ptr++` ุชูููู ุชููุงุฆููุง ุญุณุจ ุญุฌู
ููุน ุงูุจูุงูุงุชุ ูููุณ ุจู
ูุฏุงุฑ ุจุงูุช ูุงุญุฏ ููุท.
|
| 596 |
+
ููุฌุฏ ู
ุซุงู ุจุฑู
ุฌู ูุนุฑูู ู
ุตูููุฉ ุฃุนุฏุงุฏ ุตุญูุญุฉุ ุซู
ูุฑุจุท ู
ุคุดุฑูุง ุจุฃูู ุนูุตุฑ ูููุง ููุทุจุน ุงูููู
ุฉ ูุงูุนููุงู ููู ุนูุตุฑ ุฏุงุฎู ุญููุฉ.
|
| 597 |
+
ุงูุนูุตุฑ ุงูุจุตุฑู ุงูุฃุณุงุณู ูู ุนููุงู ูุจูุฑ ูู ุงูุฃุนูู ู
ุน ููุทุชูู ุชูุถูุญูุชููุ ูุฃุณูููุง ุตูุฏูู ุฑู
ุงุฏู ูุญุชูู ุนูู ุงูููุฏ ู
ุน ุชูููู ูุญูู ูุชุนููู ุฃุญู
ุฑ ูุดุฑุญ ุงูุชูุงู ุงูู
ุคุดุฑ ููุนูุตุฑ ุงูุชุงูู.
|
| 598 |
+
|
| 599 |
+
|
| 600 |
+
================================================================================
|
| 601 |
+
PAGE 19 - page_0019.png
|
| 602 |
+
================================================================================
|
| 603 |
+
|
| 604 |
+
------------TEXT EXTRACTED------------
|
| 605 |
+
Manipulating arrays using pointers
|
| 606 |
+
|
| 607 |
+
Explanation:
|
| 608 |
+
|
| 609 |
+
โข ptr++ moves the pointer to the next element in the array.
|
| 610 |
+
โข The pointer is incremented by the size of the data type (e.g., 2 bytes for int).
|
| 611 |
+
|
| 612 |
+
```c
|
| 613 |
+
#include <stdio.h>
|
| 614 |
+
|
| 615 |
+
int main() {
|
| 616 |
+
int a[5] = {10, 20, 30, 40, 50};
|
| 617 |
+
int *ptr = a; // Assign base address of array to pointer
|
| 618 |
+
for(int i = 0; i < 5; i++) {
|
| 619 |
+
printf("value in array %d and address is %p\n", *ptr, ptr);
|
| 620 |
+
ptr++; // Move pointer to next element
|
| 621 |
+
}
|
| 622 |
+
|
| 623 |
+
return 0;
|
| 624 |
+
}
|
| 625 |
+
```
|
| 626 |
+
|
| 627 |
+
------------PAGE TALK ABOUT------------
|
| 628 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ููููุฉ ุงูุชุนุงู
ู ู
ุน ุงูู
ุตูููุงุช ุจุงุณุชุฎุฏุงู
ุงูู
ุคุดุฑุงุช ูู ูุบุฉ C.
|
| 629 |
+
ุชูุถุญ ุฃู ุงูุชุนููู
ุฉ `ptr++` ุชููู ุงูู
ุคุดุฑ ุฅูู ุงูุนูุตุฑ ุงูุชุงูู ูู ุงูู
ุตูููุฉุ ูุฃู ุงูุฒูุงุฏุฉ ุชุชู
ุญุณุจ ุญุฌู
ููุน ุงูุจูุงูุงุช.
|
| 630 |
+
ููุฌุฏ ู
ุซุงู ุจุฑู
ุฌู ูุนุฑูู ู
ุตูููุฉ ู
ู 5 ุนูุงุตุฑุ ุซู
ูุณุชุฎุฏู
ู
ุคุดุฑูุง ููุชููู ุจูููุง ูุทุจุงุนุฉ ุงูููู
ุฉ ูุงูุนููุงู ููู ุนูุตุฑ.
|
| 631 |
+
ุงูุนูุตุฑ ุงูุจุตุฑู ุงูุฃุณุงุณู ูู ุตูุฏูู ูุญุชูู ุนูู ููุฏ ู
ูููู ู
ุน ุชุนููู ูุดุฑุญ ุญุฑูุฉ ุงูู
ุคุดุฑ ุฅูู ุงูุนูุตุฑ ุงูุชุงูู.
|
| 632 |
+
|
| 633 |
+
|
| 634 |
+
================================================================================
|
| 635 |
+
PAGE 20 - page_0020.png
|
| 636 |
+
================================================================================
|
| 637 |
+
|
| 638 |
+
------------TEXT EXTRACTED------------
|
| 639 |
+
Two-Dimensional Arrays
|
| 640 |
+
|
| 641 |
+
โข A two-dimensional array is an array of arrays.
|
| 642 |
+
|
| 643 |
+
โข It is stored in row-major order (all elements of a row are stored consecutively).
|
| 644 |
+
|
| 645 |
+
โข Elements are accessed using two indices: array[row][column].
|
| 646 |
+
|
| 647 |
+
```c
|
| 648 |
+
#include <stdio.h>
|
| 649 |
+
int main() {
|
| 650 |
+
int a[3][2] = {{1, 2}, {3, 4}, {5, 6}}; // Declare and initialize 2D array
|
| 651 |
+
for(int i = 0; i < 3; i++) {
|
| 652 |
+
for(int j = 0; j < 2; j++) {
|
| 653 |
+
printf("a[%d][%d] = %d, Address = %p\n", i, j, a[i][j], &a[i][j]);
|
| 654 |
+
}
|
| 655 |
+
}
|
| 656 |
+
return 0;
|
| 657 |
+
}
|
| 658 |
+
```
|
| 659 |
+
|
| 660 |
+
------------PAGE TALK ABOUT------------
|
| 661 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุงูู
ุตูููุงุช ุซูุงุฆูุฉ ุงูุฃุจุนุงุฏ ูู ูุบุฉ ุงูุจุฑู
ุฌุฉ Cุ ูุชูุถุญ ุฃููุง ุนุจุงุฑุฉ ุนู ู
ุตูููุฉ ู
ู ุงูู
ุตูููุงุช.
|
| 662 |
+
ูู
ุง ุชุดุฑุญ ุฃู ุชุฎุฒูู ุงูุนูุงุตุฑ ูุชู
ุจุทุฑููุฉ row-major orderุ ุฃู ุชูุฎุฒู ุนูุงุตุฑ ูู ุตู ุจุดูู ู
ุชุชุงูู ูู ุงูุฐุงูุฑุฉ.
|
| 663 |
+
ูุชูุถุญ ุฃูุถูุง ุทุฑููุฉ ุงููุตูู ุฅูู ุงูุนูุงุตุฑ ุจุงุณุชุฎุฏุงู
ููุฑุณูู ุจุงูุดูู array[row][column].
|
| 664 |
+
ููุฌุฏ ูู ุงูุฃุณูู ู
ุซุงู ุจุฑู
ุฌู ูุนุฑูู ู
ุตูููุฉ 3ร2ุ ุซู
ูุณุชุฎุฏู
ุญููุชูู ู
ุชุฏุงุฎูุชูู ูุทุจุงุนุฉ ููู
ุงูุนูุงุตุฑ ูุนูุงููููุง ูู ุงูุฐุงูุฑุฉ.
|
| 665 |
+
|
| 666 |
+
|
| 667 |
+
================================================================================
|
| 668 |
+
PAGE 21 - page_0021.png
|
| 669 |
+
================================================================================
|
| 670 |
+
|
| 671 |
+
------------TEXT EXTRACTED------------
|
| 672 |
+
Two-Dimensional Arrays
|
| 673 |
+
|
| 674 |
+
Explanation:
|
| 675 |
+
|
| 676 |
+
โข The array a has 3 rows and 2 columns.
|
| 677 |
+
|
| 678 |
+
โข Elements are accessed using a[i][j], where i is the row index and j is the column index.
|
| 679 |
+
|
| 680 |
+
โข The addresses of elements are printed to show the memory layout (row-major order).
|
| 681 |
+
|
| 682 |
+
#include <stdio.h>
|
| 683 |
+
|
| 684 |
+
int main() {
|
| 685 |
+
|
| 686 |
+
int a[3][2] = {{1, 2}, {3, 4}, {5, 6}}; // Declare and initialize 2D array
|
| 687 |
+
|
| 688 |
+
for(int i = 0; i < 3; i++) {
|
| 689 |
+
|
| 690 |
+
for(int j = 0; j < 2; j++) {
|
| 691 |
+
|
| 692 |
+
printf("a[%d][%d] = %d, Address = %p\n", i, j, a[i][j], &a[i][j]);
|
| 693 |
+
|
| 694 |
+
}
|
| 695 |
+
|
| 696 |
+
}
|
| 697 |
+
|
| 698 |
+
return 0;
|
| 699 |
+
}
|
| 700 |
+
|
| 701 |
+
------------PAGE TALK ABOUT------------
|
| 702 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุงูู
ุตูููุงุช ุซูุงุฆูุฉ ุงูุฃุจุนุงุฏ ูู ูุบุฉ Cุ ูููููุฉ ุชุนุฑูู ู
ุตูููุฉ ุจุงุณู
a ุชุญุชูู ุนูู 3 ุตููู ู2 ุฃุนู
ุฏุฉ.
|
| 703 |
+
ุชูุถุญ ุฃู ุงููุตูู ุฅูู ุงูุนูุงุตุฑ ูุชู
ุจุงุณุชุฎุฏุงู
ุงูุตูุบุฉ a[i][j] ุญูุซ i ุชู
ุซู ุงูุตู ูj ุชู
ุซู ุงูุนู
ูุฏ.
|
| 704 |
+
ูู
ุง ุชุนุฑุถ ู
ุซุงููุง ุจุฑู
ุฌููุง ูุทุจุน ููู
ุฉ ูู ุนูุตุฑ ูุนููุงูู ูู ุงูุฐุงูุฑุฉ ูุดุฑุญ ุชุฑุชูุจ ุงูุชุฎุฒูู row-major order.
|
| 705 |
+
ููุฌุฏ ุชุตู
ูู
ุชุนููู
ู ุจุณูุท ูุชุถู
ู ุนููุงููุง ูุจูุฑูุงุ ููุงุท ุดุฑุญุ ูู
ุฑุจุนูุง ูุญุชูู ุนูู ููุฏ ุจุฑู
ุฌู ู
ูุณู ุจุฃููุงู ู
ุฎุชููุฉ.
|
| 706 |
+
|
| 707 |
+
|
| 708 |
+
================================================================================
|
| 709 |
+
PAGE 22 - page_0022.png
|
| 710 |
+
================================================================================
|
| 711 |
+
|
| 712 |
+
------------TEXT EXTRACTED------------
|
| 713 |
+
Two-Dimensional Arrays
|
| 714 |
+
|
| 715 |
+
Explanation:
|
| 716 |
+
|
| 717 |
+
โข The array a has 3 rows and 2 columns.
|
| 718 |
+
|
| 719 |
+
โข Elements are accessed using a[i][j], where i is the row index and j is the column index.
|
| 720 |
+
|
| 721 |
+
โข The addresses of elements are printed to show the memory layout (row-major order).
|
| 722 |
+
|
| 723 |
+
#include <stdio.h>
|
| 724 |
+
|
| 725 |
+
int main() {
|
| 726 |
+
|
| 727 |
+
int a[3][2] = {{1, 2}, {3, 4}, {5, 6}}; // Declare and initialize 2D array
|
| 728 |
+
|
| 729 |
+
for(int i = 0; i < 3; i++) {
|
| 730 |
+
|
| 731 |
+
for(int j = 0; j < 2; j++) {
|
| 732 |
+
|
| 733 |
+
printf("a[%d][%d] = %d, Address = %p\n", i, j, a[i][j], &a[i][j]);
|
| 734 |
+
|
| 735 |
+
}
|
| 736 |
+
|
| 737 |
+
}
|
| 738 |
+
|
| 739 |
+
return 0;
|
| 740 |
+
}
|
| 741 |
+
|
| 742 |
+
------------PAGE TALK ABOUT------------
|
| 743 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุงูู
ุตูููุงุช ุซูุงุฆูุฉ ุงูุฃุจุนุงุฏ ูู ูุบุฉ Cุ ูููููุฉ ุชุนุฑูู ู
ุตูููุฉ ุจุงุณู
a ุชุญุชูู ุนูู 3 ุตููู ู2 ุฃุนู
ุฏุฉ.
|
| 744 |
+
ุชูุถุญ ุฃู ุงููุตูู ุฅูู ุงูุนูุงุตุฑ ูุชู
ุจุงุณุชุฎุฏุงู
ุงูุตูุบุฉ a[i][j] ุญูุซ i ุชู
ุซู ุงูุตู ูj ุชู
ุซู ุงูุนู
ูุฏ.
|
| 745 |
+
ูู
ุง ุชุนุฑุถ ู
ุซุงููุง ุจุฑู
ุฌููุง ูุทุจุน ููู
ุฉ ูู ุนูุตุฑ ู
ุน ุนููุงูู ูู ุงูุฐุงูุฑุฉ ูุดุฑุญ ุชุฑุชูุจ ุงูุชุฎุฒูู row-major order.
|
| 746 |
+
ููุฌุฏ ุนููุงู ูุจูุฑ ูู ุงูุฃุนูู ูููุงุท ุดุฑุญุ ูุฃุณูููุง ุตูุฏูู ูุญุชูู ุนูู ููุฏ ุจุฑู
ุฌู ู
ูุณู ุจุฃููุงู ู
ุฎุชููุฉ.
|
| 747 |
+
|
| 748 |
+
|
| 749 |
+
================================================================================
|
| 750 |
+
PAGE 23 - page_0023.png
|
| 751 |
+
================================================================================
|
| 752 |
+
|
| 753 |
+
------------TEXT EXTRACTED------------
|
| 754 |
+
Multiple Choice Questions
|
| 755 |
+
|
| 756 |
+
1. What is the output of the following code?
|
| 757 |
+
|
| 758 |
+
```c
|
| 759 |
+
#include <stdio.h>
|
| 760 |
+
int main() {
|
| 761 |
+
int x = 10;
|
| 762 |
+
printf("Address of x: %p", &x);
|
| 763 |
+
return 0;
|
| 764 |
+
}
|
| 765 |
+
```
|
| 766 |
+
|
| 767 |
+
a) 10
|
| 768 |
+
b) Address of x
|
| 769 |
+
c) Compilation error
|
| 770 |
+
d) Runtime error
|
| 771 |
+
|
| 772 |
+
2. What should be filled in the blank to correctly assign the address of x to the pointer ptr?
|
| 773 |
+
|
| 774 |
+
```c
|
| 775 |
+
#include <stdio.h>
|
| 776 |
+
int main() {
|
| 777 |
+
int x = 10;
|
| 778 |
+
int *ptr; ptr = _____; // Missing part
|
| 779 |
+
printf("Value of x: %d", *ptr);
|
| 780 |
+
return 0;
|
| 781 |
+
}
|
| 782 |
+
```
|
| 783 |
+
|
| 784 |
+
a) x
|
| 785 |
+
b) &x
|
| 786 |
+
c) *x
|
| 787 |
+
d) %x
|
| 788 |
+
|
| 789 |
+
------------PAGE TALK ABOUT------------
|
| 790 |
+
ุงูุตูุญุฉ ุชุนุฑุถ ุฃุณุฆูุฉ ุงุฎุชูุงุฑ ู
ู ู
ุชุนุฏุฏ ูู ูุบุฉ C ุญูู ุงูู
ุคุดุฑุงุช ูุงูุนูุงููู ูู ุงูุฐุงูุฑุฉ.
|
| 791 |
+
ุงูุณ๏ฟฝ๏ฟฝุงู ุงูุฃูู ูุฎุชุจุฑ ููู
ุงุณุชุฎุฏุงู
ุงูุนุงู
ู `&` ู
ุน `printf` ูุงูุตูุบุฉ `%p` ูุทุจุงุนุฉ ุนููุงู ุงูู
ุชุบูุฑ `x`.
|
| 792 |
+
ุงูุณุคุงู ุงูุซุงูู ูุฎุชุจุฑ ููููุฉ ุฅุณูุงุฏ ุนููุงู ุงูู
ุชุบูุฑ ุฅูู ู
ุคุดุฑ `ptr` ุซู
ูู ุงูุฅุดุงุฑุฉ ุฅููู ุจุงุณุชุฎุฏุงู
`*ptr`.
|
| 793 |
+
ููุฌุฏ ุชูุณูู ุจุตุฑู ุชุนููู
ู ูุงุถุญ: ุนููุงู ูุจูุฑ ุฃุนูู ุงูุตูุญุฉุ ูู
ุฑุจุนุงุช ููุฏ ุจุฎูููุฉ ูุงุชุญุฉุ ู
ุน ุฎูุงุฑุงุช ุงูุฅุฌุงุจุฉ ู
ุตุทููุฉ ุจุฌุงูุจ ูู ุณุคุงู.
|
| 794 |
+
|
| 795 |
+
|
| 796 |
+
================================================================================
|
| 797 |
+
PAGE 24 - page_0024.png
|
| 798 |
+
================================================================================
|
| 799 |
+
|
| 800 |
+
------------TEXT EXTRACTED------------
|
| 801 |
+
Multiple Choice Questions
|
| 802 |
+
|
| 803 |
+
3. What is the output of the following code?
|
| 804 |
+
|
| 805 |
+
```c
|
| 806 |
+
#include <stdio.h>
|
| 807 |
+
int main() {
|
| 808 |
+
int a[3] = {10, 20, 30};
|
| 809 |
+
int *ptr = a;
|
| 810 |
+
printf("%d", *(++ptr));
|
| 811 |
+
return 0; }
|
| 812 |
+
```
|
| 813 |
+
|
| 814 |
+
a) 10
|
| 815 |
+
b) 20
|
| 816 |
+
c) 30
|
| 817 |
+
d) Compilation error
|
| 818 |
+
|
| 819 |
+
------------PAGE TALK ABOUT------------
|
| 820 |
+
ุงูุตูุญุฉ ุชุนุฑุถ ุณุคุงู ุงุฎุชูุงุฑ ู
ู ู
ุชุนุฏุฏ ูู ูุบุฉ C ุญูู ูุงุชุฌ ุชูููุฐ ููุฏ ูุณุชุฎุฏู
ุงูู
ุตูููุงุช ูุงูู
ุคุดุฑุงุช.
|
| 821 |
+
ุงูููุฏ ูุนุฑูู ู
ุตูููุฉ ุชุญุชูู ุนูู 10 ู20 ู30 ุซู
ูุญุฑูู ุงูู
ุคุดุฑ ุฅูู ุงูุนูุตุฑ ุงูุชุงูู ุจุงุณุชุฎุฏุงู
`++ptr` ูุจู ุงูุทุจุงุนุฉ.
|
| 822 |
+
ููุฌุฏ ุนูู ุงููู
ูู ุฃุฑุจุน ุงุฎุชูุงุฑุงุช ููุฅุฌุงุจุฉ: 10ุ 20ุ 30ุ ุฃู ุฎุทุฃ ูู ุงูุชุฑุฌู
ุฉ.
|
| 823 |
+
ุงูุชุตู
ูู
ุจุณูุทุ ู
ุน ุนููุงู ูุจูุฑ ุฃุนูู ุงูุตูุญุฉ ูุตูุฏูู ุฑู
ุงุฏู ูุญุชูู ุนูู ุงูููุฏ ุงูู
ูููู ุจุตูุบุฉ ุจุฑู
ุฌูุฉ.
|
data_structure/p2.txt
ADDED
|
@@ -0,0 +1,637 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"ุฃูุช ู
ุฏุฑุณ ุฎุจูุฑ ูู
ุญุชุฑู ูู ู
ุงุฏุฉ ุชุฑุงููุจ ุจูุงูุงุช. ุฃูุช ุชุฏุฑูุณ ุทุงูุจุงู ูุชุณุงุนุฏู ุนูู ุงูููู
.
|
| 2 |
+
|
| 3 |
+
=== ู
ุญุชูู ุงููุตู (ุงูู
ุฑุฌุน ุงูุนูู
ู) ===
|
| 4 |
+
|
| 5 |
+
=== ููุงูุฉ ุงูุณูุงู ===
|
| 6 |
+
|
| 7 |
+
**ุชุนููู
ุงุช ู
ูู
ุฉ:**
|
| 8 |
+
- ุงุณุชุฎุฏู
ุงูู
ุญุชูู ุงูุนูู
ู ุฃุนูุงู ูู
ุฑุฌุน ุฃุณุงุณู ููุฅุฌุงุจุฉ
|
| 9 |
+
- ุฑุงุนู ุณูุงู ุงูู
ุญุงุฏุซุฉ ุงูุณุงุจูุฉ ูุงูู
ุณุชูู ุงูุชุนููู
ู ููุทุงูุจ
|
| 10 |
+
- ุฃุฌุจ ุจุดูู ูุงุถุญ ูู
ูุตู ุจุงููุบุฉ ุงูุนุฑุจูุฉ
|
| 11 |
+
- ุงุณุชุฎุฏู
ุงูุฃู
ุซูุฉ ูุงูุชูุถูุญุงุช ุนูุฏ ุงูุญุงุฌุฉ
|
| 12 |
+
- ุฅุฐุง ุณุฃู ุงูุทุงูุจ ุนู ุดูุก ุฎุงุฑุฌ ุงูู
ุญุชูู ุงูู
ุชููุฑุ ุฃุฎุจุฑู ุจุฐูู ุจูุทู
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
================================================================================
|
| 16 |
+
SUBJECT: Lecture2_20260417_203147
|
| 17 |
+
TOTAL PAGES: 17
|
| 18 |
+
EXTRACTED ON: 2026-04-17 19:44:28
|
| 19 |
+
================================================================================
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
================================================================================
|
| 23 |
+
PAGE 1 - page_0001.png
|
| 24 |
+
================================================================================
|
| 25 |
+
|
| 26 |
+
------------TEXT EXTRACTED------------
|
| 27 |
+
C & Data Structures
|
| 28 |
+
|
| 29 |
+
Chapter 1: Address, Pointers, Arrays, and Structures
|
| 30 |
+
|
| 31 |
+
Dr. Amna Ali A Mohamed
|
| 32 |
+
|
| 33 |
+
Faculty of Information Technology
|
| 34 |
+
|
| 35 |
+
60
|
| 36 |
+
53
|
| 37 |
+
3
|
| 38 |
+
49
|
| 39 |
+
46
|
| 40 |
+
1
|
| 41 |
+
2
|
| 42 |
+
48
|
| 43 |
+
16
|
| 44 |
+
25
|
| 45 |
+
40
|
| 46 |
+
|
| 47 |
+
------------PAGE TALK ABOUT------------
|
| 48 |
+
ูุฐู ุงูุตูุญุฉ ูู ุบูุงู ุฃู ู
ูุฏู
ุฉ ูู
ุญุงุถุฑุฉ ูู ู
ุงุฏุฉ **C & Data Structures**ุ ูุชุญุฏูุฏูุง **ุงููุตู ุงูุฃูู** ุงูุฐู ูุชูุงูู: ุงูุนูุงููู (Address)ุ ุงูู
ุคุดุฑุงุช (Pointers)ุ ุงูู
ุตูููุงุช (Arrays)ุ ูุงูููุงูู (Structures).
|
| 49 |
+
ุชุธูุฑ ุฃูุถูุง ุจูุงูุงุช ุงูู
ุญุงุถูุฑุฉ: **Dr. Amna Ali A Mohamed** ู
ู **Faculty of Information Technology**.
|
| 50 |
+
ููุฌุฏ ุนูู ูู
ูู ุงูุตูุญุฉ ุฑุณู
ู ูุดุฌุฑุฉ ุจูุงูุงุช (Tree) ุจุนููุฏ ู
ุฑูู
ุฉ ู
ุซู 60 ู53 ู3 ูุบูุฑูุงุ ู
ุน ุฃุณูู
ุชูุถูุญ ุงูุนูุงูุงุช ุจูู ุงูุนูุฏุ ูู
ุง ูุธูุฑ ู
ุณุชุทูู ุตุบูุฑ ูุดูุฑ ุฅูู ุงูุนูุฏุฉ 60 ูุชู
ุซูู ููุฑุฉ ุงูู
ุคุดุฑ ุฃู ุงูู
ุฑุฌุน.
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
================================================================================
|
| 54 |
+
PAGE 2 - page_0002.png
|
| 55 |
+
================================================================================
|
| 56 |
+
|
| 57 |
+
------------TEXT EXTRACTED------------
|
| 58 |
+
Introduction to Structures
|
| 59 |
+
|
| 60 |
+
โขWhat is a Structure?
|
| 61 |
+
|
| 62 |
+
โข A structure is a user-defined data type in C that allows grouping variables of different
|
| 63 |
+
|
| 64 |
+
data types under a single name.
|
| 65 |
+
|
| 66 |
+
โข Useful for representing real-world entities (e.g., student, employee, book).
|
| 67 |
+
|
| 68 |
+
โขWhy Use Structures?
|
| 69 |
+
|
| 70 |
+
โข Organize related data into a single unit.
|
| 71 |
+
|
| 72 |
+
โข Simplify complex data handling.
|
| 73 |
+
|
| 74 |
+
------------PAGE TALK ABOUT------------
|
| 75 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ูุฏู
ุฉ ูููุงูู ุงูุจูุงูุงุช (Structures) ูู ูุบุฉ C.
|
| 76 |
+
ุชูุถุญ ุฃู ุงูู Structure ูู ููุน ุจูุงูุงุช ูุนุฑููู ุงูู
ุณุชุฎุฏู
ูุชุฌู
ูุน ู
ุชุบูุฑุงุช ู
ู ุฃููุงุน ู
ุฎุชููุฉ ุชุญุช ุงุณู
ูุงุญุฏ.
|
| 77 |
+
ูู
ุง ุชุดุฑุญ ูุงุฆุฏุชู ูู ุชู
ุซูู ููุงูุงุช ู
ู ุงูุนุงูู
ุงูุญูููู ู
ุซู ุงูุทุงูุจ ุฃู ุงูู
ูุธู ุฃู ุงููุชุงุจ.
|
| 78 |
+
ูุชุนุฑุถ ุฃุณุจุงุจ ุงุณุชุฎุฏุงู
ูุ ู
ุซู ุชูุธูู
ุงูุจูุงูุงุช ุงูู
ุฑุชุจุทุฉ ูู ูุญุฏุฉ ูุงุญุฏุฉ ูุชุจุณูุท ุงูุชุนุงู
ู ู
ุน ุงูุจูุงูุงุช ุงูู
ุนูุฏุฉ.
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
================================================================================
|
| 82 |
+
PAGE 3 - page_0003.png
|
| 83 |
+
================================================================================
|
| 84 |
+
|
| 85 |
+
------------TEXT EXTRACTED------------
|
| 86 |
+
Defining a Structure
|
| 87 |
+
|
| 88 |
+
Syntax:
|
| 89 |
+
|
| 90 |
+
struct structure_name {
|
| 91 |
+
data_type member1;
|
| 92 |
+
data_type member2;
|
| 93 |
+
// ...
|
| 94 |
+
};
|
| 95 |
+
|
| 96 |
+
struct student {
|
| 97 |
+
char name[30]; // Member 1: Name of the student
|
| 98 |
+
float marks; // Member 2: Marks of the student
|
| 99 |
+
};
|
| 100 |
+
|
| 101 |
+
Example:
|
| 102 |
+
|
| 103 |
+
struct Student {
|
| 104 |
+
char name[50];
|
| 105 |
+
int rollNo;
|
| 106 |
+
float marks;
|
| 107 |
+
};
|
| 108 |
+
|
| 109 |
+
โข Explanation:
|
| 110 |
+
โข struct Student defines a structure with three members: name, rollNo, and marks.
|
| 111 |
+
|
| 112 |
+
------------PAGE TALK ABOUT------------
|
| 113 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ููููุฉ ุชุนุฑูู Structure ูู ูุบุฉ C/C++ ุจุงุณุชุฎุฏุงู
ุงูููู
ุฉ ุงูู
ูุชุงุญูุฉ `struct`.
|
| 114 |
+
ุชูุถุญ ุฃููุงู ุงูุตูุบุฉ ุงูุนุงู
ุฉ ูุชุนุฑูู ุงูููููุ ุซู
ุชุนุฑุถ ู
ุซุงููุง ุจุณูุทูุง ููููู `student` ูุญุชูู ุนูู ุงูุงุณู
ูุงูุฏุฑุฌุงุช.
|
| 115 |
+
ุจุนุฏ ุฐูู ุชูุฏู
ู
ุซุงููุง ุฃุดู
ู ููููู `Student` ูุถู
`name` ู`rollNo` ู`marks`.
|
| 116 |
+
ุงูุนูุงุตุฑ ุงูุจุตุฑูุฉ ุนุจุงุฑุฉ ุนู ู
ุฑุจุนุงุช ุฑู
ุงุฏูุฉ ุชุญุชูู ุนูู ุฃููุงุฏ ุจุฑู
ุฌูุฉ ู
ุน ุชูููู ูุญูู ูุชู
ููุฒ ุงูููู
ุงุช ูุงูุฃููุงุน ูุงูุฃููุงุณ.
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
================================================================================
|
| 120 |
+
PAGE 4 - page_0004.png
|
| 121 |
+
================================================================================
|
| 122 |
+
|
| 123 |
+
------------TEXT EXTRACTED------------
|
| 124 |
+
Declaring Structure Variables
|
| 125 |
+
|
| 126 |
+
Two Ways to Declare:
|
| 127 |
+
|
| 128 |
+
1. During Definition:
|
| 129 |
+
|
| 130 |
+
struct student {
|
| 131 |
+
char name[30];
|
| 132 |
+
float marks;
|
| 133 |
+
} student1, student2; // Declares variables student1 and student2
|
| 134 |
+
|
| 135 |
+
2. After Definition:
|
| 136 |
+
|
| 137 |
+
struct student {
|
| 138 |
+
char name[30];
|
| 139 |
+
float marks;
|
| 140 |
+
};
|
| 141 |
+
|
| 142 |
+
struct student student3; // Declares a variable student3
|
| 143 |
+
|
| 144 |
+
------------PAGE TALK ABOUT------------
|
| 145 |
+
ุชุดุฑุญ ุงูุตูุญุฉ ุทุฑููุชูู ูุชุนุฑูู ู
ุชุบูุฑุงุช ู
ู ููุน structure ูู ูุบุฉ C.
|
| 146 |
+
ุงูุทุฑููุฉ ุงูุฃููู ุชููู ุฃุซูุงุก ุชุนุฑูู ุงูู struct ููุณูุ ุญูุซ ูุชู
ุฅูุดุงุก student1 ู student2 ู
ุจุงุดุฑุฉ.
|
| 147 |
+
ุงูุทุฑููุฉ ุงูุซุงููุฉ ุชููู ุจุนุฏ ุงูุงูุชูุงุก ู
ู ุชุนุฑูู ุงูู structุ ุซู
ุฅุนูุงู ู
ุชุบูุฑ ุฌุฏูุฏ ู
ุซู student3 ุจุดูู ู
ููุตู.
|
| 148 |
+
ููุฌุฏ ุชูุณูู ุจุตุฑู ุจุณูุท ุนูู ุดูู ุนููุงู ูุจูุฑ ูุตูุฏูููู ุฑู
ุงุฏููู ูุญุชููุงู ุนูู ุฃู
ุซูุฉ ุงูููุฏ ุงูุจุฑู
ุฌู.
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
================================================================================
|
| 152 |
+
PAGE 5 - page_0005.png
|
| 153 |
+
================================================================================
|
| 154 |
+
|
| 155 |
+
------------TEXT EXTRACTED------------
|
| 156 |
+
Accessing Structure Members
|
| 157 |
+
|
| 158 |
+
Dot Operator (.):
|
| 159 |
+
โขUsed to access members of a structure variable.
|
| 160 |
+
|
| 161 |
+
โขExample:
|
| 162 |
+
|
| 163 |
+
struct student student1;
|
| 164 |
+
strcpy(student1.name, " Ali Mohamed ");
|
| 165 |
+
student1.marks = 95.5;
|
| 166 |
+
|
| 167 |
+
Example Program:
|
| 168 |
+
|
| 169 |
+
#include <stdio.h>
|
| 170 |
+
#include <string.h>
|
| 171 |
+
|
| 172 |
+
struct student {
|
| 173 |
+
char name[30];
|
| 174 |
+
float marks;
|
| 175 |
+
};
|
| 176 |
+
|
| 177 |
+
int main() {
|
| 178 |
+
struct student student1;
|
| 179 |
+
strcpy(student1.name, " Ali Mohamed ");
|
| 180 |
+
student1.marks = 95.5;
|
| 181 |
+
printf("Name: %s\n", student1.name);
|
| 182 |
+
printf("Marks: %.2f\n", student1.marks);
|
| 183 |
+
return 0;
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
------------PAGE TALK ABOUT------------
|
| 187 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ููููุฉ ุงููุตูู ุฅูู ุฃุนุถุงุก ุงูู structure ูู ูุบุฉ C ุจุงุณุชุฎุฏุงู
ุนุงู
ู ุงูููุทุฉ (.).
|
| 188 |
+
ุชูุถุญ ุฃู ูุฐุง ุงูุนุงู
ู ููุณุชุฎุฏู
ูููุตูู ุฅูู ุงูู
ุชุบูุฑุงุช ุงูุฏุงุฎููุฉ ุฏุงุฎู ูููู ุงูุจูุงูุงุช ู
ุซู name ู marks.
|
| 189 |
+
ููุฌุฏ ู
ุซุงู ุจุฑู
ุฌู ูุนุฑูู struct ุจุงุณู
student ุซู
ูููุดุฆ ู
ุชุบูุฑูุง student1 ูููุณูุฏ ูู ุงูุงุณู
ูุงูุฏุฑุฌุงุช.
|
| 190 |
+
ูู
ุง ูุนุฑุถ ุงูุจุฑูุงู
ุฌ ุทุจุงุนุฉ ุงูุงุณู
ูุงูุฏุฑุฌุฉุ ู
ุน ูุฌูุฏ ุชูุณูู ุจุตุฑู ูุดู
ู ุนููุงู ูุจูุฑ ูุตูุฏูููู ูุญุชููุงู ุนูู ุฃู
ุซูุฉ ููุฏ.
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
================================================================================
|
| 194 |
+
PAGE 6 - page_0006.png
|
| 195 |
+
================================================================================
|
| 196 |
+
|
| 197 |
+
------------TEXT EXTRACTED------------
|
| 198 |
+
Pointers to Structures
|
| 199 |
+
|
| 200 |
+
โขWhat is a Structure Pointer?
|
| 201 |
+
โข A pointer that stores the address of a structure variable.
|
| 202 |
+
โข Used to access structure members dynamically.
|
| 203 |
+
|
| 204 |
+
โข Syntax:
|
| 205 |
+
struct structure_name *pointer_name;
|
| 206 |
+
|
| 207 |
+
โข Example:
|
| 208 |
+
struct student *ptr;
|
| 209 |
+
|
| 210 |
+
#include <stdio.h>
|
| 211 |
+
#include <string.h>
|
| 212 |
+
|
| 213 |
+
struct student {
|
| 214 |
+
char name[30];
|
| 215 |
+
float marks;
|
| 216 |
+
};
|
| 217 |
+
|
| 218 |
+
int main() {
|
| 219 |
+
struct student student1;
|
| 220 |
+
struct student *ptr;
|
| 221 |
+
|
| 222 |
+
return 0;
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
------------PAGE TALK ABOUT------------
|
| 226 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุงูู
ุคุดุฑุงุช ุฅูู ุงูููุงูู (Pointers to Structures) ูู ูุบุฉ Cุ ูุชูุถุญ ุฃููุง ู
ุชุบูุฑุงุช ุชุฎุฒู ุนููุงู ู
ุชุบูุฑ ู
ู ููุน struct.
|
| 227 |
+
ุชุดุฑุญ ุงูุตูุบุฉ ุงูุนุงู
ุฉ ูุชุนุฑูู ู
ุคุดุฑ ุฅูู ูููู ู
ุซู: `struct structure_name *pointer_name;` ู
ุน ู
ุซุงู ุนูู `struct student *ptr;`.
|
| 228 |
+
ููุฌุฏ ุฃูุถูุง ู
ุซุงู ุจุฑู
ุฌู ูุนุฑูู ูููู `student` ูุญุชูู ุนูู `name` ู`marks`ุ ุซู
ูุนุฑูู ู
ุชุบูุฑูุง ู
ู ูุฐุง ุงูููุน ูู
ุคุดุฑูุง ุฅููู ุฏุงุฎู `main`.
|
| 229 |
+
ุงูุนูุงุตุฑ ุงูุจุตุฑูุฉ ุนุจุงุฑุฉ ุนู ุดุฑูุญุฉ ุชุนููู
ูุฉ ุจุชูุณูู ุนุฑุถ ุชูุฏูู
ูุ ูููุง ุนููุงู ูุจูุฑ ููุตูุต ููุทูุฉ ูู
ุฑุจุนุงุช ุฑู
ุงุฏูุฉ ุชุญุชูู ุนูู ุฃู
ุซูุฉ ุงูููุฏ.
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
================================================================================
|
| 233 |
+
PAGE 7 - page_0007.png
|
| 234 |
+
================================================================================
|
| 235 |
+
|
| 236 |
+
------------TEXT EXTRACTED------------
|
| 237 |
+
Accessing Members Using Structure Pointers
|
| 238 |
+
|
| 239 |
+
Arrow Operator (->):
|
| 240 |
+
|
| 241 |
+
โข Used to access members of a structure using a pointer.
|
| 242 |
+
|
| 243 |
+
โข Example:
|
| 244 |
+
|
| 245 |
+
ptr->name = " Ali Mohamed ";
|
| 246 |
+
ptr->marks = 88.5;
|
| 247 |
+
|
| 248 |
+
Example Program:
|
| 249 |
+
|
| 250 |
+
#include <stdio.h>
|
| 251 |
+
#include <string.h>
|
| 252 |
+
|
| 253 |
+
struct student {
|
| 254 |
+
char name[30];
|
| 255 |
+
float marks;
|
| 256 |
+
};
|
| 257 |
+
|
| 258 |
+
int main() {
|
| 259 |
+
struct student student1;
|
| 260 |
+
struct student *ptr;
|
| 261 |
+
|
| 262 |
+
ptr = &student1; // Point to the structure variable
|
| 263 |
+
strcpy(ptr->name, โAli Mohamedโ);
|
| 264 |
+
ptr->marks = 88.5;
|
| 265 |
+
|
| 266 |
+
printf("Name: %s\n", ptr->name);
|
| 267 |
+
printf("Marks: %.2f\n", ptr->marks);
|
| 268 |
+
|
| 269 |
+
return 0;
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
------------PAGE TALK ABOUT------------
|
| 273 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ููููุฉ ุงููุตูู ุฅูู ุฃุนุถุงุก ุงูู structure ูู ูุบุฉ C ุจุงุณุชุฎุฏุงู
ุงูู
ุคุดุฑ ุนุจุฑ ุงูุนุงู
ู ุงูุณูู
ู (->).
|
| 274 |
+
ุชูุถุญ ุฃู ูุฐุง ุงูุนุงู
ู ููุณุชุฎุฏู
ุจุฏู ุงูููุทุฉ (.) ุนูุฏู
ุง ูุชุนุงู
ู ู
ุน ู
ุคุดุฑ ูุดูุฑ ุฅูู struct.
|
| 275 |
+
ููุฌุฏ ู
ุซุงู ุจุณูุท ูุนููู ุงูุงุณู
ูุงูุฏุฑุฌุงุช ููุทุงูุจ ุจุงุณุชุฎุฏุงู
ptr->name ู ptr->marks.
|
| 276 |
+
ูู
ุง ุชุนุฑุถ ุจุฑูุงู
ุฌูุง ูุงู
ููุง ูุนุฑูู struct student ุซู
ูุฑุจุท ู
ุคุดุฑูุง ุจู ููุทุจุน ุงูููู
.
|
| 277 |
+
ุจุตุฑููุงุ ุงูุตูุญุฉ ุชุญุชูู ุนููุงููุง ูุจูุฑูุงุ ููุงุทูุง ุชูุถูุญูุฉ ุนูู ุงููุณุงุฑุ ูู
ุฑุจุน ููุฏ ุจุฑู
ุฌู ู
ูุณู ุนูู ุงููู
ูู.
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
================================================================================
|
| 281 |
+
PAGE 8 - page_0008.png
|
| 282 |
+
================================================================================
|
| 283 |
+
|
| 284 |
+
------------TEXT EXTRACTED------------
|
| 285 |
+
Dynamic Memory Allocation for Structures
|
| 286 |
+
|
| 287 |
+
โขWhy Use Dynamic Memory?
|
| 288 |
+
โข Allocate memory at runtime for structures.
|
| 289 |
+
โข Useful when the number of structures is not known in advance.
|
| 290 |
+
|
| 291 |
+
โข Using malloc():
|
| 292 |
+
|
| 293 |
+
struct student *ptr;
|
| 294 |
+
ptr = (struct student *)malloc(sizeof(struct student));
|
| 295 |
+
|
| 296 |
+
------------PAGE TALK ABOUT------------
|
| 297 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุงูุชุฎุตูุต ุงูุฏููุงู
ููู ููุฐุงูุฑุฉ ููููุงูู (Structures) ูู ูุบุฉ C.
|
| 298 |
+
ุชูุถุญ ุฃู ุงุณุชุฎุฏุงู
ุงูุฐุงูุฑุฉ ุงูุฏููุงู
ูููุฉ ู
ููุฏ ุนูุฏ ุงูุญุงุฌุฉ ุฅูู ุญุฌุฒ ุงูุฐุงูุฑุฉ ุฃุซูุงุก ุงูุชุดุบููุ ุฎุงุตุฉ ุฅุฐุง ูุงู ุนุฏุฏ ุงูุนูุงุตุฑ ุบูุฑ ู
ุนุฑูู ู
ุณุจููุง.
|
| 299 |
+
ูู
ุง ุชุนุฑุถ ู
ุซุงููุง ุจุงุณุชุฎุฏุงู
ุงูุฏุงูุฉ malloc() ูุฅูุดุงุก ู
ุคุดุฑ ุฅูู struct student ูุญุฌุฒ ู
ุณุงุญุฉ ู
ูุงุณุจุฉ ูู.
|
| 300 |
+
ููุฌุฏ ุนูุตุฑ ุจุตุฑู ุจุณูุท ุนุจุงุฑุฉ ุนู ู
ุฑุจุน/ุฎูููุฉ ูุงุชุญุฉ ูุชู
ููุฒ ู
ูุทุน ุงูููุฏ ุนู ุจุงูู ุงููุต.
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
================================================================================
|
| 304 |
+
PAGE 9 - page_0009.png
|
| 305 |
+
================================================================================
|
| 306 |
+
|
| 307 |
+
------------TEXT EXTRACTED------------
|
| 308 |
+
Dynamic Memory Allocation for Structure's example
|
| 309 |
+
|
| 310 |
+
```c
|
| 311 |
+
#include <stdio.h>
|
| 312 |
+
#include <stdlib.h>
|
| 313 |
+
#include <string.h>
|
| 314 |
+
|
| 315 |
+
struct student {
|
| 316 |
+
char name[30];
|
| 317 |
+
float marks;
|
| 318 |
+
};
|
| 319 |
+
|
| 320 |
+
int main() {
|
| 321 |
+
struct student *ptr;
|
| 322 |
+
|
| 323 |
+
ptr = (struct student *)malloc(sizeof(struct student));
|
| 324 |
+
if (ptr == NULL) {
|
| 325 |
+
printf("Memory allocation failed!\n");
|
| 326 |
+
return 1;
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
strcpy(ptr->name, "Amna Ali");
|
| 330 |
+
ptr->marks = 92.0;
|
| 331 |
+
printf("Name: %s\n", ptr->name);
|
| 332 |
+
printf("Marks: %.2f\n", ptr->marks);
|
| 333 |
+
free(ptr); // Free allocated memory
|
| 334 |
+
|
| 335 |
+
return 0;
|
| 336 |
+
}
|
| 337 |
+
```
|
| 338 |
+
|
| 339 |
+
------------PAGE TALK ABOUT------------
|
| 340 |
+
ุชุดุฑุญ ุงูุตูุญุฉ ู
ุซุงููุง ูู ูุบุฉ C ุนู ุงูุชุฎุตูุต ุงูุฏููุงู
ููู ููุฐุงูุฑุฉ ุจุงุณุชุฎุฏุงู
`malloc` ู
ุน ูููู `struct student`.
|
| 341 |
+
ูุชู
ุฅูุดุงุก ู
ุคุดุฑ ุฅูู ุงูููููุ ุซู
ุญุฌุฒ ุฐุงูุฑุฉ ููุ ูุงูุชุญูู ู
ู ูุฌุงุญ ุงูุญุฌุฒ ุจุงุณุชุฎุฏุงู
`NULL`.
|
| 342 |
+
ุจุนุฏ ุฐูู ูุชู
ุชุฎุฒูู ุงูุงุณู
ูุงูุฏุฑุฌุงุช ุฏุงุฎู ุงููููู ุจุงุณุชุฎุฏุงู
ุงูุนุงู
ู `->` ุซู
ุทุจุงุนุชูุง.
|
| 343 |
+
ูู ุงูููุงูุฉ ูุชู
ุชุญุฑูุฑ ุงูุฐุงูุฑุฉ ุจุงุณุชุฎุฏุงู
`free`ุ ููุธูุฑ ุงูู
ุญุชูู ุฏุงุฎู ุตูุฏูููู ูุตููู ู
ูุณููู ูุนุฑุถ ุงูููุฏ.
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
================================================================================
|
| 347 |
+
PAGE 10 - page_0010.png
|
| 348 |
+
================================================================================
|
| 349 |
+
|
| 350 |
+
------------TEXT EXTRACTED------------
|
| 351 |
+
Passing Structures to Functions
|
| 352 |
+
|
| 353 |
+
1. Pass by Value:
|
| 354 |
+
โขA copy of the structure is passed to the function.
|
| 355 |
+
โขExample:
|
| 356 |
+
|
| 357 |
+
void printStudent(struct student s) {
|
| 358 |
+
printf("Name: %s\n", s.name);
|
| 359 |
+
printf("Marks: %.2f\n", s.marks);
|
| 360 |
+
}
|
| 361 |
+
|
| 362 |
+
2. Pass by Reference (Using Pointers):
|
| 363 |
+
โขThe address of the structure is passed to the function.
|
| 364 |
+
โขExample:
|
| 365 |
+
|
| 366 |
+
void printStudent(struct student *s) {
|
| 367 |
+
printf("Name: %s\n", s->name);
|
| 368 |
+
printf("Marks: %.2f\n", s->marks);
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
Key Points to Remember
|
| 372 |
+
|
| 373 |
+
1. Structures group variables of different data types.
|
| 374 |
+
|
| 375 |
+
2. Use the dot operator (.) to access members of a structure variable.
|
| 376 |
+
|
| 377 |
+
3. Use the arrow operator (->) to access members of a structure using a pointer.
|
| 378 |
+
|
| 379 |
+
4. Dynamic memory allocation allows creating structures at runtime.
|
| 380 |
+
|
| 381 |
+
5. Structures can be passed to functions by value or by reference.
|
| 382 |
+
|
| 383 |
+
------------PAGE TALK ABOUT------------
|
| 384 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ููููุฉ ุชู
ุฑูุฑ ุงูู structures ุฅูู ุงูุฏูุงู ูู ูุบุฉ C ุจุทุฑููุชูู: ุจุงููุณุฎ ุงูู
ุจุงุดุฑ (Pass by Value) ุฃู ุจุงุณุชุฎุฏุงู
ุงูู
ุคุดุฑุงุช (Pass by Reference).
|
| 385 |
+
ุชูุถุญ ุฃู ุงูุชู
ุฑูุฑ ุจุงูููู
ุฉ ูุฑุณู ูุณุฎุฉ ู
ู ุงูู structureุ ุจููู
ุง ุงูุชู
ุฑูุฑ ุจุงูู
ุฑุฌุน ูุฑุณู ุนููุงูู ูู ุงูุฐุงูุฑุฉ.
|
| 386 |
+
ูู
ุง ุชุนุฑุถ ุฃู
ุซูุฉ ุจุฑู
ุฌูุฉ ุชุจูู ุงุณุชุฎุฏุงู
ุงูุนุงู
ู (.) ูููุตูู ุฅูู ุงูุฃุนุถุงุก ู
ุจุงุดุฑุฉุ ูุงูุนุงู
ู (->) ุนูุฏ ุงุณุชุฎุฏุงู
ุงูู
ุคุดุฑุงุช.
|
| 387 |
+
ููุฌุฏ ุชูุณูู ุชุนููู
ู ุจุณูุท ู
ุน ุนููุงู ูุจูุฑุ ูุณู
ูู ููู
ูุงุฑูุฉุ ูู
ุฑุจุนุงุช ุฑู
ุงุฏูุฉ ุชุญุชูู ุนูู ุฃู
ุซูุฉ ุงูููุฏ.
|
| 388 |
+
|
| 389 |
+
|
| 390 |
+
================================================================================
|
| 391 |
+
PAGE 11 - page_0011.png
|
| 392 |
+
================================================================================
|
| 393 |
+
|
| 394 |
+
------------TEXT EXTRACTED------------
|
| 395 |
+
Array of Structures
|
| 396 |
+
|
| 397 |
+
โข What is an Array of Structures?
|
| 398 |
+
|
| 399 |
+
โข An array of structures is a collection of multiple structure variables stored in contiguous memory
|
| 400 |
+
locations.
|
| 401 |
+
โข Useful for managing multiple records of the same type (e.g., students, employees, books).
|
| 402 |
+
|
| 403 |
+
โข Why Use an Array of Structures?
|
| 404 |
+
|
| 405 |
+
โข Simplifies handling of multiple entities with the same attributes.
|
| 406 |
+
โข Enables efficient storage and retrieval of structured data.
|
| 407 |
+
|
| 408 |
+
โข Declaring an Array of Structures
|
| 409 |
+
|
| 410 |
+
Syntax: struct structure_name array_name[size];
|
| 411 |
+
|
| 412 |
+
------------PAGE TALK ABOUT------------
|
| 413 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ูููู
Array of Structures ูู ุงูุจุฑู
ุฌุฉุ ููู ุงุณุชุฎุฏุงู
ู
ุตูููุฉ ูุชุฎุฒูู ุนุฏุฉ ู
ุชุบูุฑุงุช ู
ู ููุน struct ุจุดูู ู
ุชุฌุงูุฑ ูู ุงูุฐุงูุฑุฉ.
|
| 414 |
+
ุชูุถุญ ูุงุฆุฏุชู ูู ุฅุฏุงุฑุฉ ุณุฌูุงุช ู
ุชุนุฏุฏุฉ ู
ู ููุณ ุงูููุน ู
ุซู ุงูุทูุงุจ ุฃู ุงูู
ูุธูููุ ู
ุน ุชุณููู ุงูุชุนุงู
ู ู
ุน ุงูุจูุงูุงุช ุงูู
ูุธู
ุฉ.
|
| 415 |
+
ูู
ุง ุชุนุฑุถ ุณุจุจ ุงุณุชุฎุฏุงู
ู ู
ู ุญูุซ ุงูููุงุกุฉ ูู ุงูุชุฎุฒูู ูุงูุงุณุชุฑุฌุงุนุ ูุชูุฏู
ุตูุบุฉ ุงูุชุตุฑูุญ ุงูุฃุณุงุณูุฉ: `struct structure_name array_name[size];`
|
| 416 |
+
ุงูุนูุตุฑ ุงูุจุตุฑู ุงูุฃุณุงุณู ูู ุดุฑูุญุฉ ุชุนููู
ูุฉ ูุตูุฉ ุชุญุชูู ุนูู ุนููุงู ูุจูุฑ ูููุงุท ุชุนุฏุงุฏุ ู
ุน ุตูุฏูู ุณููู ู
ู
ูุฒ ูุนุฑุถ ุตูุบุฉ ุงูู Syntax.
|
| 417 |
+
|
| 418 |
+
|
| 419 |
+
================================================================================
|
| 420 |
+
PAGE 12 - page_0012.png
|
| 421 |
+
================================================================================
|
| 422 |
+
|
| 423 |
+
------------TEXT EXTRACTED------------
|
| 424 |
+
Array of Structures
|
| 425 |
+
|
| 426 |
+
โข Example:
|
| 427 |
+
|
| 428 |
+
struct Student {
|
| 429 |
+
char name[50];
|
| 430 |
+
int rollNo;
|
| 431 |
+
float marks;
|
| 432 |
+
};
|
| 433 |
+
struct Student students[10]; // Array of 10 students
|
| 434 |
+
|
| 435 |
+
โข Accessing Elements in an Array of Structures
|
| 436 |
+
|
| 437 |
+
Dot Operator (.):
|
| 438 |
+
โข Used to access members of a structure in the array.
|
| 439 |
+
|
| 440 |
+
โข Example:
|
| 441 |
+
|
| 442 |
+
students[0].rollNo = 101; // Access the first student's roll number
|
| 443 |
+
|
| 444 |
+
------------PAGE TALK ABOUT------------
|
| 445 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ูููู
**ู
ุตูููุฉ ู
ู ุงูููุงูู (Array of Structures)** ูู ูุบุฉ C.
|
| 446 |
+
ุชูุถุญ ู
ุซุงููุง ุนูู ูููู ุจุงุณู
**Student** ูุญุชูู ุนูู ุงูุงุณู
ูุฑูู
ุงูุฌููุณ ูุงูุฏุฑุฌุงุชุ ุซู
ุฅูุดุงุก ู
ุตูููุฉ ู
ู 10 ุทูุงุจ.
|
| 447 |
+
ูู
ุง ุชุดุฑุญ ููููุฉ ุงููุตูู ุฅูู ุนูุงุตุฑ ุงูู
ุตูููุฉ ุจุงุณุชุฎุฏุงู
**ุงูู
ุนุงู
ู ุงูููุทู (.)** ูููุตูู ุฅูู ุฃุนุถุงุก ูู ูููู.
|
| 448 |
+
ููุฌุฏ ุชูุณูู ุจุตุฑู ุนูู ุดูู ุดุฑูุญุฉ ุชุนููู
ูุฉ ู
ุน ู
ุฑุจุนุงุช ุฑู
ุงุฏูุฉ ูุนุฑุถ ุฃู
ุซูุฉ ุงูุดููุฑุฉ ุงูุจุฑู
ุฌูุฉ.
|
| 449 |
+
|
| 450 |
+
|
| 451 |
+
================================================================================
|
| 452 |
+
PAGE 13 - page_0013.png
|
| 453 |
+
================================================================================
|
| 454 |
+
|
| 455 |
+
------------TEXT EXTRACTED------------
|
| 456 |
+
#include <stdio.h>
|
| 457 |
+
#include <string.h>
|
| 458 |
+
|
| 459 |
+
struct Student {
|
| 460 |
+
char name[50];
|
| 461 |
+
int rollNo;
|
| 462 |
+
float marks;
|
| 463 |
+
};
|
| 464 |
+
|
| 465 |
+
int main() {
|
| 466 |
+
struct Student students[3]; // Array of 3 students
|
| 467 |
+
|
| 468 |
+
// Input student details
|
| 469 |
+
for (int i = 0; i < 3; i++) {
|
| 470 |
+
printf("Enter details for student %d:\n", i + 1);
|
| 471 |
+
printf("Name: ");
|
| 472 |
+
scanf("%s", students[i].name);
|
| 473 |
+
printf("Roll No: ");
|
| 474 |
+
scanf("%d", &students[i].rollNo);
|
| 475 |
+
printf("Marks: ");
|
| 476 |
+
scanf("%f", &students[i].marks);
|
| 477 |
+
}
|
| 478 |
+
|
| 479 |
+
// Display student details
|
| 480 |
+
printf("\nStudent Details:\n");
|
| 481 |
+
|
| 482 |
+
for (int i = 0; i < 3; i++) {
|
| 483 |
+
printf("\nStudent %d:\n", i + 1);
|
| 484 |
+
printf("Name: %s\n", students[i].name);
|
| 485 |
+
printf("Roll No: %d\n", students[i].rollNo);
|
| 486 |
+
printf("Marks: %.2f\n", students[i].marks);
|
| 487 |
+
}
|
| 488 |
+
|
| 489 |
+
return 0;
|
| 490 |
+
}
|
| 491 |
+
|
| 492 |
+
------------PAGE TALK ABOUT------------
|
| 493 |
+
ุงูุตูุญุฉ ุชุนุฑุถ ู
ุซุงููุง ุจูุบุฉ C ูุงุณุชุฎุฏุงู
`struct` ูุชุนุฑูู ุจูุงูุงุช ุงูุทุงูุจ ู
ุซู ุงูุงุณู
ูุฑูู
ุงูุฌููุณ ูุงูุฏุฑุฌุงุช.
|
| 494 |
+
ูู
ุง ุชูุถุญ ุฅูุดุงุก ู
ุตูููุฉ ู
ู 3 ุทูุงุจ ุซู
ุงุณุชุฎุฏุงู
ุญููุฉ `for` ูุฅุฏุฎุงู ุงูุจูุงูุงุช ููู ุทุงูุจ ุนุจุฑ `scanf`.
|
| 495 |
+
ุจุนุฏ ุฐูู ูุชู
ุนุฑุถ ุจูุงูุงุช ุงูุทูุงุจ ุจุงุณุชุฎุฏุงู
`printf` ุฏุงุฎู ุญููุฉ ุฃุฎุฑู.
|
| 496 |
+
ุงูุนูุตุฑ ุงูุจุตุฑู ุงูุฃุณุงุณู ูู ููุฏ ุจุฑู
ุฌู ู
ูุณู ุจุงูุฃููุงู ูู
ูุณูู
ุฅูู ุฌุฒุฃูู ุฏุงุฎู ุงูุตูุญุฉ.
|
| 497 |
+
|
| 498 |
+
|
| 499 |
+
================================================================================
|
| 500 |
+
PAGE 14 - page_0014.png
|
| 501 |
+
================================================================================
|
| 502 |
+
|
| 503 |
+
------------TEXT EXTRACTED------------
|
| 504 |
+
Dynamic Memory Allocation for an Array of Structures
|
| 505 |
+
|
| 506 |
+
โข Why Use Dynamic Memory?
|
| 507 |
+
|
| 508 |
+
โข Allocate memory at runtime for an array of structures.
|
| 509 |
+
|
| 510 |
+
โข Useful when the number of structures is not known at compile time.
|
| 511 |
+
|
| 512 |
+
โข Using malloc():
|
| 513 |
+
|
| 514 |
+
struct Student *students;
|
| 515 |
+
students = (struct Student *)malloc(n * sizeof(struct Student));
|
| 516 |
+
|
| 517 |
+
โข Example Program:
|
| 518 |
+
|
| 519 |
+
------------PAGE TALK ABOUT------------
|
| 520 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุงูุชุฎุตูุต ุงูุฏููุงู
ููู ููุฐุงูุฑุฉ ูู
ุตูููุฉ ู
ู ุงูููุงูู (Structures) ูู ูุบุฉ C.
|
| 521 |
+
ุชูุถุญ ูู
ุงุฐุง ูุณุชุฎุฏู
ุงูุฐุงูุฑุฉ ุงูุฏููุงู
ูููุฉุ ุฎุงุตุฉ ุนูุฏู
ุง ูููู ุนุฏุฏ ุงูุนูุงุตุฑ ุบูุฑ ู
ุนุฑูู ุฃุซูุงุก ุงูุชุฑุฌู
ุฉ.
|
| 522 |
+
ูู
ุง ุชุนุฑุถ ู
ุซุงููุง ุจุงุณุชุฎุฏุงู
ุงูุฏุงูุฉ malloc() ูุญุฌุฒ ู
ุณุงุญุฉ ูุนุฏุฏ n ู
ู ุงูุนูุงุตุฑ ู
ู ุงูููุน Student.
|
| 523 |
+
ููุฌุฏ ุนูุตุฑ ุจุตุฑู ุนุจุงุฑุฉ ุนู ู
ุฑุจุน ุฑู
ุงุฏู ูุญุชูู ุนูู ู
ูุทุน ุงูููุฏ ุงูุจุฑู
ุฌู ูุชูุถูุญ ุทุฑููุฉ ุงูุญุฌุฒ.
|
| 524 |
+
|
| 525 |
+
|
| 526 |
+
================================================================================
|
| 527 |
+
PAGE 15 - page_0015.png
|
| 528 |
+
================================================================================
|
| 529 |
+
|
| 530 |
+
------------TEXT EXTRACTED------------
|
| 531 |
+
#include <stdio.h>
|
| 532 |
+
#include <stdlib.h>
|
| 533 |
+
#include <string.h>
|
| 534 |
+
struct Student {
|
| 535 |
+
char name[50];
|
| 536 |
+
int rollNo;
|
| 537 |
+
float marks;
|
| 538 |
+
};
|
| 539 |
+
int main() {
|
| 540 |
+
int n;
|
| 541 |
+
struct Student *students;
|
| 542 |
+
|
| 543 |
+
// Input the number of students
|
| 544 |
+
printf("Enter the number of students: ");
|
| 545 |
+
scanf("%d", &n);
|
| 546 |
+
|
| 547 |
+
// Dynamically allocate memory for an array of structures
|
| 548 |
+
students = (struct Student *)malloc(n * sizeof(struct Student));
|
| 549 |
+
if (students == NULL) {
|
| 550 |
+
printf("Memory allocation failed!\n");
|
| 551 |
+
return 1;
|
| 552 |
+
}
|
| 553 |
+
|
| 554 |
+
// Input student details
|
| 555 |
+
for (int i = 0; i < n; i++) {
|
| 556 |
+
printf("\nEnter details for student %d:\n", i + 1);
|
| 557 |
+
printf("Name: ");
|
| 558 |
+
scanf("%s", students[i].name);
|
| 559 |
+
printf("Roll No: ");
|
| 560 |
+
scanf("%d", &students[i].rollNo);
|
| 561 |
+
printf("Marks: ");
|
| 562 |
+
scanf("%f", &students[i].marks);
|
| 563 |
+
}
|
| 564 |
+
|
| 565 |
+
// Display student details
|
| 566 |
+
printf("\nStudent Details:\n");
|
| 567 |
+
for (int i = 0; i < n; i++) {
|
| 568 |
+
printf("\nStudent %d:\n", i + 1);
|
| 569 |
+
printf("Name: %s\n", students[i].name);
|
| 570 |
+
printf("Roll No: %d\n", students[i].rollNo);
|
| 571 |
+
printf("Marks: %.2f\n", students[i].marks);
|
| 572 |
+
}
|
| 573 |
+
|
| 574 |
+
// Free allocated memory
|
| 575 |
+
free(students);
|
| 576 |
+
return 0;
|
| 577 |
+
}
|
| 578 |
+
|
| 579 |
+
------------PAGE TALK ABOUT------------
|
| 580 |
+
ุงูุตูุญุฉ ุชุนุฑุถ ู
ุซุงููุง ุจุฑู
ุฌููุง ุจูุบุฉ C ูุดุฑุญ ุงุณุชุฎุฏุงู
`struct` ูุชุฎุฒูู ุจูุงูุงุช ุงูุทูุงุจ ู
ุซู ุงูุงุณู
ูุฑูู
ุงูุฌููุณ ูุงูุฏุฑุฌุงุช.
|
| 581 |
+
ูู
ุง ุชูุถุญ ููููุฉ ุญุฌุฒ ุงูุฐุงูุฑุฉ ุฏููุงู
ููููุง ุจุงุณุชุฎุฏุงู
`malloc` ูู
ุตูููุฉ ู
ู ุงูููุงูู ุญุณุจ ุนุฏุฏ ุงูุทูุงุจ ุงูุฐู ูุฏุฎูู ุงูู
ุณุชุฎุฏู
.
|
| 582 |
+
ุงูุจุฑูุงู
ุฌ ููุฑุฃ ุจูุงูุงุช ูู ุทุงูุจ ุจุงุณุชุฎุฏุงู
`scanf` ุซู
ูุทุจุนูุง ุจุงุณุชุฎุฏุงู
`printf` ุฏุงุฎู ุญููุงุช `for`.
|
| 583 |
+
ููู ุงูููุงูุฉ ูุญุฑุฑ ุงูุฐุงูุฑุฉ ุงูู
ุญุฌูุฒุฉ ุจุงุณุชุฎุฏุงู
`free`.
|
| 584 |
+
ุงูุนูุตุฑ ุงูุจุตุฑู ุงููุญูุฏ ูู ุชูุณูู ุงูููุฏ ุจุฃููุงู ู
ุฎุชููุฉ ูุชู
ููุฒ ุงูููู
ุงุช ุงูู
ูุชุงุญูุฉ ูุงูุฏูุงู ูุงูู
ุชุบูุฑุงุช.
|
| 585 |
+
|
| 586 |
+
|
| 587 |
+
================================================================================
|
| 588 |
+
PAGE 16 - page_0016.png
|
| 589 |
+
================================================================================
|
| 590 |
+
|
| 591 |
+
------------TEXT EXTRACTED------------
|
| 592 |
+
Key Points to Remember
|
| 593 |
+
|
| 594 |
+
1. Structures group variables of different data types.
|
| 595 |
+
|
| 596 |
+
2. Use the dot operator (.) to access members of a structure variable.
|
| 597 |
+
|
| 598 |
+
3. Use the arrow operator (->) to access members of a structure using a pointer.
|
| 599 |
+
|
| 600 |
+
4. Dynamic memory allocation allows creating structures at runtime.
|
| 601 |
+
|
| 602 |
+
5. Structures can be passed to functions by value or by reference.
|
| 603 |
+
|
| 604 |
+
------------PAGE TALK ABOUT------------
|
| 605 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุฃูู
ุงูููุงุท ุงูุฃุณุงุณูุฉ ุงูู
ุชุนููุฉ ุจู Structures ูู ุงูุจุฑู
ุฌุฉ.
|
| 606 |
+
ุชูุถุญ ุฃู ุงูู structure ุชูุณุชุฎุฏู
ูุชุฌู
ูุน ู
ุชุบูุฑุงุช ู
ู ุฃููุงุน ุจูุงูุงุช ู
ุฎุชููุฉ ุฏุงุฎู ููุงู ูุงุญุฏ.
|
| 607 |
+
ูู
ุง ุชุดุฑุญ ุทุฑููุชูู ูููุตูู ุฅูู ุงูุฃุนุถุงุก: ุจุงุณุชุฎุฏุงู
ุงูููุทุฉ (.) ุฃู ุงูุณูู
(->) ุนูุฏ ุงูุชุนุงู
ู ู
ุน ุงูู
ุคุดุฑุงุช.
|
| 608 |
+
ูุชุดูุฑ ุฃูุถูุง ุฅูู ุฅู
ูุงููุฉ ุฅูุดุงุก structures ุฏููุงู
ููููุง ููุช ุงูุชุดุบูู ูุชู
ุฑูุฑูุง ุฅูู ุงูุฏูุงู ุจุงูููู
ุฉ ุฃู ุจุงูู
ุฑุฌุน.
|
| 609 |
+
ุชุญุชูู ุงูุตูุญุฉ ุนูู ุชุตู
ูู
ุจุณูุท ุฌุฏูุง ุนุจุงุฑุฉ ุนู ุนููุงู ูุจูุฑ ููุงุฆู
ุฉ ู
ุฑูู
ุฉ ุจุฏูู ุตูุฑ ุฃู ู
ุฎุทุทุงุช.
|
| 610 |
+
|
| 611 |
+
|
| 612 |
+
================================================================================
|
| 613 |
+
PAGE 17 - page_0017.png
|
| 614 |
+
================================================================================
|
| 615 |
+
|
| 616 |
+
------------TEXT EXTRACTED------------
|
| 617 |
+
Practice Questions
|
| 618 |
+
|
| 619 |
+
Question 1: Define a Structure Employee
|
| 620 |
+
|
| 621 |
+
Problem Statement:
|
| 622 |
+
Define a structure Employee with members id, name, and salary. Write a program to input and display employee details.
|
| 623 |
+
|
| 624 |
+
Question 2: Use a Structure Pointer
|
| 625 |
+
|
| 626 |
+
Problem Statement:
|
| 627 |
+
Modify the above program to use a structure pointer.
|
| 628 |
+
|
| 629 |
+
Question 3: Dynamically Allocate Memory for an Array of Structures
|
| 630 |
+
Problem Statement:
|
| 631 |
+
Write a program to dynamically allocate memory for an array of structures and store student details.
|
| 632 |
+
|
| 633 |
+
------------PAGE TALK ABOUT------------
|
| 634 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุฃุณุฆูุฉ ุชุฏุฑูุจูุฉ ูู ูุบุฉ ุงูุจุฑู
ุฌุฉ C ุญูู ุงุณุชุฎุฏุงู
ุงูู Structures.
|
| 635 |
+
ุชุชุถู
ู ุชุนุฑูู ูููู Employeeุ ุซู
ุชุนุฏูู ุงูุจุฑูุงู
ุฌ ูุงุณุชุฎุฏุงู
ู
ุคุดุฑ ุฅูู ุงููููู.
|
| 636 |
+
ูู
ุง ุชุนุฑุถ ุณุคุงููุง ุนู ุงูุญุฌุฒ ุงูุฏููุงู
ููู ููุฐุงูุฑุฉ ูู
ุตูููุฉ ู
ู ุงูููุงูู ูุชุฎุฒูู ุจูุงูุงุช ุงูุทูุงุจ.
|
| 637 |
+
ุงูุชุตู
ูู
ุจุณูุท ุฌุฏูุง: ุฎูููุฉ ุจูุถุงุก ููุตูุต ูุจูุฑุฉ ุฏุงููุฉ ุจุฏูู ุตูุฑ ุฃู ู
ุฎุทุทุงุช ุฃู ุฑุณูู
ุจูุงููุฉ.
|
data_structure/p3.txt
ADDED
|
@@ -0,0 +1,1483 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"ุฃูุช ู
ุฏุฑุณ ุฎุจูุฑ ูู
ุญุชุฑู ูู ู
ุงุฏุฉ ุชุฑุงููุจ ุจูุงูุงุช. ุฃูุช ุชุฏุฑูุณ ุทุงูุจุงู ูุชุณุงุนุฏู ุนูู ุงูููู
.
|
| 2 |
+
|
| 3 |
+
=== ู
ุญุชูู ุงููุตู (ุงูู
ุฑุฌุน ุงูุนูู
ู) ===
|
| 4 |
+
|
| 5 |
+
=== ููุงูุฉ ุงูุณูุงู ===
|
| 6 |
+
|
| 7 |
+
**ุชุนููู
ุงุช ู
ูู
ุฉ:**
|
| 8 |
+
- ุงุณุชุฎุฏู
ุงูู
ุญุชูู ุงูุนูู
ู ุฃุนูุงู ูู
ุฑุฌุน ุฃุณุงุณู ููุฅุฌุงุจุฉ
|
| 9 |
+
- ุฑุงุนู ุณูุงู ุงูู
ุญุงุฏุซุฉ ุงูุณุงุจูุฉ ูุงูู
ุณุชูู ุงูุชุนููู
ู ููุทุงูุจ
|
| 10 |
+
- ุฃุฌุจ ุจุดูู ูุงุถุญ ูู
ูุตู ุจุงููุบุฉ ุงูุนุฑุจูุฉ
|
| 11 |
+
- ุงุณุชุฎุฏู
ุงูุฃู
ุซูุฉ ูุงูุชูุถูุญุงุช ุนูุฏ ุงูุญุงุฌุฉ
|
| 12 |
+
- ุฅุฐุง ุณุฃู ุงูุทุงูุจ ุนู ุดูุก ุฎุงุฑุฌ ุงูู
ุญุชูู ุงูู
ุชููุฑุ ุฃุฎุจุฑู ุจุฐูู ุจูุทู
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
================================================================================
|
| 16 |
+
SUBJECT: Lecture3_20260417_203423
|
| 17 |
+
TOTAL PAGES: 47
|
| 18 |
+
EXTRACTED ON: 2026-04-17 19:47:06
|
| 19 |
+
================================================================================
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
================================================================================
|
| 23 |
+
PAGE 1 - page_0001.png
|
| 24 |
+
================================================================================
|
| 25 |
+
|
| 26 |
+
------------TEXT EXTRACTED------------
|
| 27 |
+
C & Data Structures
|
| 28 |
+
|
| 29 |
+
Chapter 3: function & recursion
|
| 30 |
+
|
| 31 |
+
Dr. Amna Ali A Mohamed
|
| 32 |
+
|
| 33 |
+
Faculty of Information Technology
|
| 34 |
+
|
| 35 |
+
------------PAGE TALK ABOUT------------
|
| 36 |
+
ูุฐู ุตูุญุฉ ุบูุงู/ุนููุงู ูู
ุญุงุถุฑุฉ ูู ู
ุงุฏุฉ **C & Data Structures**ุ ูุชุญุฏูุฏูุง **Chapter 2: function & recursion**.
|
| 37 |
+
ุชูุถุญ ุงุณู
ุงูุฏูุชูุฑุฉ **Amna Ali A Mohamed** ูุงูุชู
ุงุกูุง ุฅูู **Faculty of Information Technology**.
|
| 38 |
+
ููุฌุฏ ุนูู ูู
ูู ุงูุตูุญุฉ ุฑุณู
ูุดุฌุฑุฉ ุจูุงูุงุช/ุดุฌุฑุฉ ุซูุงุฆูุฉ ุจุนููุฏ ู
ุฑูู
ุฉ ู
ุซู 60 ู53 ู3 ูุบูุฑูุงุ ู
ุน ุฃุณูู
ุชูุถุญ ุงูุนูุงูุงุช ุจูู ุงูุนูุฏ.
|
| 39 |
+
ุงูุตูุญุฉ ุชู
ููุฏูุฉ ูุชุนุฑุถ ู
ูุถูุน ุงููุตู ุจุดูู ุนุงู
ู
ุน ุนูุตุฑ ุจุตุฑู ู
ุฑุชุจุท ุจููุงูู ุงูุจูุงูุงุช ูุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู.
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
================================================================================
|
| 43 |
+
PAGE 2 - page_0002.png
|
| 44 |
+
================================================================================
|
| 45 |
+
|
| 46 |
+
------------TEXT EXTRACTED------------
|
| 47 |
+
Introduction to Functions
|
| 48 |
+
|
| 49 |
+
โข What is a Function?
|
| 50 |
+
โข A function is a block of code that performs a specific task.
|
| 51 |
+
โข It provides modularity and reusability in programming.
|
| 52 |
+
|
| 53 |
+
โข Why Use Functions?
|
| 54 |
+
โข Breaks down complex tasks into smaller, manageable tasks.
|
| 55 |
+
โข Avoids code duplication.
|
| 56 |
+
โข Improves readability and maintainability.
|
| 57 |
+
|
| 58 |
+
------------PAGE TALK ABOUT------------
|
| 59 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ูุฏู
ุฉ ูู ุงูุฏูุงู ุงูุจุฑู
ุฌูุฉ ูุฃูู
ูุชูุง ูู ูุชุงุจุฉ ุงูุจุฑุงู
ุฌ.
|
| 60 |
+
ุชูุถุญ ุฃู ุงูุฏุงูุฉ ูู ุฌุฒุก ู
ู ุงูููุฏ ูููุฐ ู
ูู
ุฉ ู
ุญุฏุฏุฉุ ูุชุณุงุนุฏ ุนูู ุฅุนุงุฏุฉ ุงุณุชุฎุฏุงู
ุงูููุฏ ูุชูุธูู
ู.
|
| 61 |
+
ูู
ุง ุชุดุฑุญ ููุงุฆุฏ ุงุณุชุฎุฏุงู
ุงูุฏูุงู ู
ุซู ุชูุณูู
ุงูู
ูุงู
ุงููุจูุฑุฉ ุฅูู ุฃุฌุฒุงุก ุฃุตุบุฑุ ุชูููู ุชูุฑุงุฑ ุงูููุฏุ ูุชุญุณูู ุงููุฑุงุกุฉ ูุงูุตูุงูุฉ.
|
| 62 |
+
ุงูุชุตู
ูู
ุนุจุงุฑุฉ ุนู ุดุฑูุญุฉ ุนุฑุถ ุจุณูุทุฉ ุชุญุชูู ุนูู ุนููุงู ูุจูุฑ ูููุงุท ุชุนุฏุงุฏ ููุทุ ุจุฏูู ุตูุฑ ุฃู ู
ุฎุทุทุงุช.
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
================================================================================
|
| 66 |
+
PAGE 3 - page_0003.png
|
| 67 |
+
================================================================================
|
| 68 |
+
|
| 69 |
+
------------TEXT EXTRACTED------------
|
| 70 |
+
Introduction to Functions
|
| 71 |
+
|
| 72 |
+
โข Defining a Function
|
| 73 |
+
|
| 74 |
+
return_type function_name(parameter_list) {
|
| 75 |
+
|
| 76 |
+
// Function body
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
โข Example:
|
| 80 |
+
|
| 81 |
+
int add(int x, int y) {
|
| 82 |
+
int z; z = x + y;
|
| 83 |
+
return z;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
โข Explanation:
|
| 87 |
+
โข int is the return type.
|
| 88 |
+
โข add is the function name.
|
| 89 |
+
โข int x, int y are the parameters.
|
| 90 |
+
โข The function returns the sum of x and y.
|
| 91 |
+
|
| 92 |
+
------------PAGE TALK ABOUT------------
|
| 93 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ูุฏู
ุฉ ุฅูู ุงูุฏูุงู ูู ุงูุจุฑู
ุฌุฉ ูููููุฉ ุชุนุฑูู ุงูุฏุงูุฉ ุจุดูู ุนุงู
ุจุงุณุชุฎุฏุงู
ููุน ุงูุฅุฑุฌุงุน ูุงุณู
ุงูุฏุงูุฉ ูุงูู
ุนุงู
ูุงุช.
|
| 94 |
+
ุชุนุฑุถ ู
ุซุงููุง ุนู
ูููุง ูุฏุงูุฉ ุงุณู
ูุง add ุชุณุชูุจู ู
ุชุบูุฑูู ู
ู ุงูููุน int ูุชูุฑุฌุน ู
ุฌู
ูุนูู
ุง.
|
| 95 |
+
ูู
ุง ุชุดุฑุญ ู
ูููุงุช ุงูู
ุซุงู: ููุน ุงูุฅุฑุฌุงุนุ ุงุณู
ุงูุฏุงูุฉุ ูุงูู
ุนุงู
ูุงุชุ ู
ุน ุชูุถูุญ ุฃู ุงููุงุชุฌ ูู ู
ุฌู
ูุน x ู y.
|
| 96 |
+
ููุฌุฏ ุชูุณูู ุจุตุฑู ุนูู ุดูู ุดุฑูุญุฉ ุนุฑุถ ุชุนููู
ูุฉ ู
ุน ุนููุงู ูุจูุฑ ูู
ุฑุจุนุงุช ุฑู
ุงุฏูุฉ ุชุญุชูู ุนูู ุฃู
ุซูุฉ ุงูููุฏ.
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
================================================================================
|
| 100 |
+
PAGE 4 - page_0004.png
|
| 101 |
+
================================================================================
|
| 102 |
+
|
| 103 |
+
------------TEXT EXTRACTED------------
|
| 104 |
+
Calling a Function
|
| 105 |
+
|
| 106 |
+
Syntax:
|
| 107 |
+
|
| 108 |
+
function_name(arguments);
|
| 109 |
+
|
| 110 |
+
โข Example:
|
| 111 |
+
|
| 112 |
+
#include <stdio.h>
|
| 113 |
+
|
| 114 |
+
int add(int x, int y) {
|
| 115 |
+
return x + y;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
int main() {
|
| 119 |
+
int a = 10, b = 20, result;
|
| 120 |
+
|
| 121 |
+
result = add(a, b); // Function call
|
| 122 |
+
printf("Sum: %d\n", result);
|
| 123 |
+
return 0;
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
Explanation:
|
| 127 |
+
โข The add function is called with arguments a and b.
|
| 128 |
+
โข The result is stored in result and printed.
|
| 129 |
+
|
| 130 |
+
------------PAGE TALK ABOUT------------
|
| 131 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ููููุฉ ุงุณุชุฏุนุงุก ุงูุฏุงูุฉ ูู ูุบุฉ C ุจุงุณุชุฎุฏุงู
ุงูุตูุบุฉ ุงูุนุงู
ุฉ: function_name(arguments);
|
| 132 |
+
ุชูุถุญ ู
ุซุงููุง ุนู
ูููุง ูุฏุงูุฉ ุงุณู
ูุง add ุชุณุชูุจู ู
ุชุบูุฑูู ูุชุนูุฏ ู
ุฌู
ูุนูู
ุงุ ุซู
ูุชู
ุงุณุชุฏุนุงุคูุง ุฏุงุฎู ุงูุฏุงูุฉ main.
|
| 133 |
+
ูู
ุง ุชุดุฑุญ ุฃู ุงูููู
ุฉ ุงููุงุชุฌุฉ ู
ู ุงูุงุณุชุฏุนุงุก ุชูุฎุฒู ูู ุงูู
ุชุบูุฑ result ุซู
ุชูุทุจุน ุจุงุณุชุฎุฏุงู
printf.
|
| 134 |
+
ููุฌุฏ ุชูุณูู ุจุตุฑู ุชุนููู
ู ูุชุถู
ู ุนููุงููุง ูุจูุฑูุงุ ู
ุฑุจุนูุง ูุตูุบุฉ ุงูุงุณุชุฏุนุงุกุ ูู
ุฑุจุน ููุฏ ูุนุฑุถ ุงูู
ุซุงู ุงูุจุฑู
ุฌู.
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
================================================================================
|
| 138 |
+
PAGE 5 - page_0005.png
|
| 139 |
+
================================================================================
|
| 140 |
+
|
| 141 |
+
------------TEXT EXTRACTED------------
|
| 142 |
+
Parameter Passing
|
| 143 |
+
|
| 144 |
+
โข Pass by Value:
|
| 145 |
+
โข The value of the actual parameter is copied to the formal parameter.
|
| 146 |
+
โข Changes to the formal parameter do not affect the actual parameter.
|
| 147 |
+
|
| 148 |
+
#include <stdio.h>
|
| 149 |
+
|
| 150 |
+
void increment(int x) {
|
| 151 |
+
x++;
|
| 152 |
+
printf("Inside function: %d\n", x);
|
| 153 |
+
}
|
| 154 |
+
int main() {
|
| 155 |
+
int a = 10;
|
| 156 |
+
increment(a); // Pass by value
|
| 157 |
+
printf("Outside function: %d\n", a);
|
| 158 |
+
return 0;
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
โข Explanation:
|
| 162 |
+
โข The value of a is copied to x.
|
| 163 |
+
โข Changes to x do not affect a.
|
| 164 |
+
|
| 165 |
+
------------PAGE TALK ABOUT------------
|
| 166 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ูููู
ุชู
ุฑูุฑ ุงูู
ุนุงู
ูุงุช ูู ุงูุจุฑู
ุฌุฉุ ูุจุงูุชุญุฏูุฏ ุฃุณููุจ **ุงูุชู
ุฑูุฑ ุจุงูููู
ุฉ (Pass by Value)**.
|
| 167 |
+
ุชูุถุญ ุฃู ููู
ุฉ ุงูู
ุชุบูุฑ ุงูุฃุตูู ุชููุณุฎ ุฅูู ุงูู
ุชุบูุฑ ุฏุงุฎู ุงูุฏุงูุฉุ ูุฐูู ุฃู ุชุนุฏูู ุฏุงุฎู ุงูุฏุงูุฉ ูุง ูุบููุฑ ุงูู
ุชุบูุฑ ุงูุฃุตูู.
|
| 168 |
+
ููุฌุฏ ู
ุซุงู ุจูุบุฉ C ูุฏุงูุฉ `increment` ุชุฒูุฏ ููู
ุฉ `x` ูุชุทุจุนูุง ุฏุงุฎู ุงูุฏุงูุฉุ ุจููู
ุง ุชุจูู ููู
ุฉ `a` ุฎุงุฑุฌ ุงูุฏุงูุฉ ูู
ุง ูู.
|
| 169 |
+
ุงูุชุตู
ูู
ุนุจุงุฑุฉ ุนู ุดุฑูุญุฉ ุนุฑุถ ุชุนููู
ูุฉ ุชุญุชูู ุนููุงููุง ูุจูุฑูุงุ ููุงุทูุง ุชูุถูุญูุฉุ ูู
ุฑุจุน ููุฏ ู
ุน ูุณู
ุดุฑุญ ุนูู ุงููู
ูู.
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
================================================================================
|
| 173 |
+
PAGE 6 - page_0006.png
|
| 174 |
+
================================================================================
|
| 175 |
+
|
| 176 |
+
------------TEXT EXTRACTED------------
|
| 177 |
+
Call by Reference
|
| 178 |
+
|
| 179 |
+
โข Pass by Reference:
|
| 180 |
+
โข The address of the actual parameter is passed to the formal parameter.
|
| 181 |
+
โข Changes to the formal parameter affect the actual parameter.
|
| 182 |
+
|
| 183 |
+
โข Example:
|
| 184 |
+
|
| 185 |
+
```c
|
| 186 |
+
#include <stdio.h>
|
| 187 |
+
void increment(int *x) {
|
| 188 |
+
(*x)++;
|
| 189 |
+
printf("Inside function: %d\n", *x);
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
int main() {
|
| 193 |
+
int a = 10;
|
| 194 |
+
|
| 195 |
+
increment(&a); // Pass by reference
|
| 196 |
+
printf("Outside function: %d\n", a);
|
| 197 |
+
return 0;
|
| 198 |
+
}
|
| 199 |
+
```
|
| 200 |
+
|
| 201 |
+
Explanation:
|
| 202 |
+
โข The address of a is passed to x.
|
| 203 |
+
โข Changes to *x affect a.
|
| 204 |
+
|
| 205 |
+
------------PAGE TALK ABOUT------------
|
| 206 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ูููู
**ุงูุชู
ุฑูุฑ ุจุงูู
ุฑุฌุน (Call/Pass by Reference)** ูู ุงูุจุฑู
ุฌุฉ ุจูุบุฉ C.
|
| 207 |
+
ุชูุถุญ ุฃู ุงูุฏุงูุฉ ุชุณุชูุจู **ุนููุงู ุงูู
ุชุบูุฑ** ูููุณ ูุณุฎุฉ ู
ู ููู
ุชูุ ูุฐูู ุฃู ุชุนุฏูู ุฏุงุฎู ุงูุฏุงูุฉ ููุนูุณ ุนูู ุงูู
ุชุบูุฑ ุงูุฃุตูู.
|
| 208 |
+
ููุฌุฏ ู
ุซุงู ุจุฑู
ุฌู ูุฏุงูุฉ `increment` ุชุณุชูุจู ู
ุคุดุฑูุง `int *x` ูุชุฒูุฏ ููู
ุฉ ุงูู
ุชุบูุฑ ุจุงุณุชุฎุฏุงู
`(*x)++`.
|
| 209 |
+
ูู
ุง ุชุนุฑุถ ุงูุตูุญุฉ ุดุฑุญูุง ุฌุงูุจููุง ูุจูู ุฃู ุนููุงู ุงูู
ุชุบูุฑ `a` ููู
ุฑุฑ ุฅูู `x`ุ ูุจุงูุชุงูู ุชุชุบูุฑ ููู
ุฉ `a` ุฏุงุฎู ูุฎุงุฑุฌ ุงูุฏุงูุฉ.
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
================================================================================
|
| 213 |
+
PAGE 7 - page_0007.png
|
| 214 |
+
================================================================================
|
| 215 |
+
|
| 216 |
+
------------TEXT EXTRACTED------------
|
| 217 |
+
Call by Reference
|
| 218 |
+
|
| 219 |
+
Passing Arrays to Functions
|
| 220 |
+
|
| 221 |
+
How to Pass Arrays?
|
| 222 |
+
|
| 223 |
+
โข Pass the array name (which is a pointer to the first element of the array).
|
| 224 |
+
|
| 225 |
+
โข The size of the array is usually passed as an additional parameter.
|
| 226 |
+
|
| 227 |
+
Passing a 1D Array to a Function Syntax:
|
| 228 |
+
|
| 229 |
+
return_type function_name(data_type array[], int size);
|
| 230 |
+
|
| 231 |
+
------------PAGE TALK ABOUT------------
|
| 232 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุชู
ุฑูุฑ ุงูู
ุตูููุงุช ุฅูู ุงูุฏูุงู ุจุงุณุชุฎุฏุงู
ุฃุณููุจ **Call by Reference**.
|
| 233 |
+
ุชูุถุญ ุฃู ุงุณู
ุงูู
ุตูููุฉ ููู
ุฑููุฑ ููุฏุงูุฉ ูุฃูู ูุดูุฑ ุฅูู ุฃูู ุนูุตุฑ ูููุงุ ู๏ฟฝ๏ฟฝุงูุจูุง ูุชู
ุชู
ุฑูุฑ ุญุฌู
ุงูู
ุตูููุฉ ูู
ุนุงู
ู ุฅุถุงูู.
|
| 234 |
+
ูู
ุง ุชุนุฑุถ ุตูุบุฉ ุนุงู
ุฉ ูุชู
ุฑูุฑ ู
ุตูููุฉ ุฃุญุงุฏูุฉ ุงูุจุนุฏ ุฅูู ุฏุงูุฉ.
|
| 235 |
+
ุงูุชุตู
ูู
ุนุจุงุฑุฉ ุนู ุดุฑูุญุฉ ุชุนููู
ูุฉ ุจุณูุทุฉ ุชุญุชูู ุนูู ุนููุงู ูุจูุฑ ูููุงุท ุชุนุฏุงุฏ ูู
ุฑุจุน ุฑู
ุงุฏู ูู ุงูุฃุณูู ุจุฏุงุฎูู ุตูุบุฉ ุงูููุฏ.
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
================================================================================
|
| 239 |
+
PAGE 8 - page_0008.png
|
| 240 |
+
================================================================================
|
| 241 |
+
|
| 242 |
+
------------TEXT EXTRACTED------------
|
| 243 |
+
Passing a 1D Array to a Function example
|
| 244 |
+
|
| 245 |
+
#include <stdio.h>
|
| 246 |
+
|
| 247 |
+
void printArray(int arr[], int n) {
|
| 248 |
+
for (int i = 0; i < n; i++) {
|
| 249 |
+
printf("%d ", arr[i]); } printf("\n");
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
int main() {
|
| 253 |
+
int arr[] = {1, 2, 3, 4, 5};
|
| 254 |
+
int size = sizeof(arr) / sizeof(arr[0]);
|
| 255 |
+
|
| 256 |
+
printArray(arr, size); // Pass array to function
|
| 257 |
+
|
| 258 |
+
return 0;
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
Explanation:
|
| 262 |
+
โขThe array arr is passed to the function printArray.
|
| 263 |
+
โขThe function processes each element of the array.
|
| 264 |
+
|
| 265 |
+
------------PAGE TALK ABOUT------------
|
| 266 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ููููุฉ ุชู
ุฑูุฑ ู
ุตูููุฉ ุฃุญุงุฏูุฉ ุงูุจุนุฏ 1D Array ุฅูู ุฏุงูุฉ ูู ูุบุฉ C.
|
| 267 |
+
ููุฌุฏ ู
ุซุงู ุจุฑู
ุฌู ููุถุญ ุฏุงูุฉ ุจุงุณู
printArray ุชุณุชูุจู ุงูู
ุตูููุฉ ูุญุฌู
ูุง ุซู
ุชุทุจุน ุนูุงุตุฑูุง ุจุงุณุชุฎุฏุงู
ุญููุฉ for.
|
| 268 |
+
ูู
ุง ููุถุญ ุงูุจุฑูุงู
ุฌ ุฏุงุฎู ุงูุฏุงูุฉ main ููููุฉ ุชุนุฑูู ุงูู
ุตูููุฉ ูุญุณุงุจ ุนุฏุฏ ุนูุงุตุฑูุง ุจุงุณุชุฎุฏุงู
sizeof ุซู
ุฅุฑุณุงููุง ุฅูู ุงูุฏุงูุฉ.
|
| 269 |
+
ุงูุชุตู
ูู
ูุญุชูู ุนูู ุนููุงู ูุจูุฑุ ูู
ุฑุจุน ุฑู
ุงุฏู ููู ุงูููุฏ ุงูุจุฑู
ุฌูุ ูุนูู ุงููู
ูู ูุณู
ุดุฑุญ ู
ุฎุชุตุฑ ุจููุงุท.
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
================================================================================
|
| 273 |
+
PAGE 9 - page_0009.png
|
| 274 |
+
================================================================================
|
| 275 |
+
|
| 276 |
+
------------TEXT EXTRACTED------------
|
| 277 |
+
Modifying a 1D Array in a Function example
|
| 278 |
+
|
| 279 |
+
#include <stdio.h>
|
| 280 |
+
|
| 281 |
+
void incrementArray(int arr[], int n) {
|
| 282 |
+
for (int i = 0; i < n; i++) {
|
| 283 |
+
arr[i] += 1; // Modify each element of the array
|
| 284 |
+
}
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
int main() {
|
| 288 |
+
int arr[] = {1, 2, 3, 4, 5};
|
| 289 |
+
int size = sizeof(arr) / sizeof(arr[0]);
|
| 290 |
+
|
| 291 |
+
incrementArray(arr, size); // Pass array to function
|
| 292 |
+
|
| 293 |
+
printf("Modified Array:\n");
|
| 294 |
+
for (int i = 0; i < size; i++) {
|
| 295 |
+
printf("%d ", arr[i]);
|
| 296 |
+
}
|
| 297 |
+
printf("\n");
|
| 298 |
+
return 0;
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
Explanation:
|
| 302 |
+
โข The function incrementArray modifies each element of the array.
|
| 303 |
+
โข Changes are reflected in the original array.
|
| 304 |
+
|
| 305 |
+
------------PAGE TALK ABOUT------------
|
| 306 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ุซุงู ุจูุบุฉ C ููุถุญ ููููุฉ ุชุนุฏูู ู
ุตูููุฉ ุฃุญุงุฏูุฉ ุงูุจุนุฏ ุฏุงุฎู ุฏุงูุฉ.
|
| 307 |
+
ุงูุฏุงูุฉ `incrementArray` ุชุณุชูุจู ุงูู
ุตูููุฉ ูุญุฌู
ูุงุ ุซู
ุชุฒูุฏ ูู ุนูุตุฑ ุจู
ูุฏุงุฑ 1 ุจุงุณุชุฎุฏุงู
ุญููุฉ `for`.
|
| 308 |
+
ูู `main` ูุชู
ุฅูุดุงุก ุงูู
ุตูููุฉ ูุญุณุงุจ ุญุฌู
ูุง ุซู
ุชู
ุฑูุฑูุง ุฅูู ุงูุฏุงูุฉุ ูุจุนุฏ ุฐูู ุทุจุงุนุฉ ุงูููู
ุงูู
ุนุฏูุฉ.
|
| 309 |
+
ููุฌุฏ ุนูุตุฑ ุจุตุฑู ุนุจุงุฑุฉ ุนู ุตูุฏูู ูุจูุฑ ูุญุชูู ุงูููุฏ ุงูุจุฑู
ุฌูุ ูุนูู ุงููู
ูู ูุณู
ุจุนููุงู "Explanation" ูุดุฑุญ ุฃู ุงูุชุนุฏููุงุช ุชุธูุฑ ู
ุจุงุดุฑุฉ ูู ุงูู
ุตูููุฉ ุงูุฃุตููุฉ.
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
================================================================================
|
| 313 |
+
PAGE 10 - page_0010.png
|
| 314 |
+
================================================================================
|
| 315 |
+
|
| 316 |
+
------------TEXT EXTRACTED------------
|
| 317 |
+
Passing a 2D Array to a Function
|
| 318 |
+
|
| 319 |
+
Syntax:
|
| 320 |
+
|
| 321 |
+
return_type function_name(data_type array[][cols], int rows);
|
| 322 |
+
|
| 323 |
+
#include <stdio.h>
|
| 324 |
+
|
| 325 |
+
void printMatrix(int mat[][3], int rows) {
|
| 326 |
+
for (int i = 0; i < rows; i++) {
|
| 327 |
+
for (int j = 0; j < 3; j++) {
|
| 328 |
+
printf("%d ", mat[i][j]);
|
| 329 |
+
}
|
| 330 |
+
printf("\n");
|
| 331 |
+
}
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
int main() {
|
| 335 |
+
int mat[2][3] = {{1, 2, 3}, {4, 5, 6}};
|
| 336 |
+
|
| 337 |
+
printMatrix(mat, 2); // Pass 2D array to function
|
| 338 |
+
return 0;
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
Explanation:
|
| 342 |
+
โข The 2D array mat is passed to the function printMatrix.
|
| 343 |
+
โข The function processes each element of the 2D array.
|
| 344 |
+
|
| 345 |
+
------------PAGE TALK ABOUT------------
|
| 346 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ููููุฉ ุชู
ุฑูุฑ ู
ุตูููุฉ ุซูุงุฆูุฉ ุงูุฃุจุนุงุฏ ุฅูู ุฏุงูุฉ ูู ูุบุฉ C.
|
| 347 |
+
ุชูุถุญ ุงูุตูุบุฉ ุงูุนุงู
ุฉ ูุชู
ุฑูุฑ ุงูู
ุตูููุฉุ ู
ุน ู
ุซุงู ุนู
ูู ูุฏุงูุฉ ุงุณู
ูุง printMatrix ุชุณุชูุจู ู
ุตูููุฉ ูุชุทุจุน ุนูุงุตุฑูุง ุจุงุณุชุฎุฏุงู
ุญููุงุช ุชูุฑุงุฑ ู
ุชุฏุงุฎูุฉ.
|
| 348 |
+
ููุฌุฏ ุฃูุถูุง ุดุฑุญ ู
ุฎุชุตุฑ ูุจูู ุฃู ุงูู
ุตูููุฉ mat ูุชู
ุฅุฑุณุงููุง ุฅูู ุงูุฏุงูุฉ ูู
ุนุงูุฌุฉ ูู ุนูุตุฑ ูููุง.
|
| 349 |
+
ุงูุชุตู
ูู
ุจุตุฑููุง ูุญุชูู ุนูู ุนููุงู ูุจูุฑุ ุตูุฏูู ูุตูุบุฉ ุงูู syntaxุ ูุตูุฏูู ููุฏ ๏ฟฝ๏ฟฝุฑู
ุฌู ู
ุน ูุณู
ุดุฑุญ ููุทู ุนูู ุงููู
ูู.
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
================================================================================
|
| 353 |
+
PAGE 11 - page_0011.png
|
| 354 |
+
================================================================================
|
| 355 |
+
|
| 356 |
+
------------TEXT EXTRACTED------------
|
| 357 |
+
Passing Arrays to Functions using pointer
|
| 358 |
+
|
| 359 |
+
Why Use Pointers with Arrays?
|
| 360 |
+
|
| 361 |
+
โข Pointers provide direct access to memory locations, making array manipulation efficient.
|
| 362 |
+
|
| 363 |
+
โข Passing arrays to functions using pointers avoids copying the entire array.
|
| 364 |
+
|
| 365 |
+
โขKey Concepts:
|
| 366 |
+
|
| 367 |
+
โข Array name is a pointer to the first element of the array.
|
| 368 |
+
|
| 369 |
+
โข Pointer arithmetic can be used to traverse arrays.
|
| 370 |
+
|
| 371 |
+
------------PAGE TALK ABOUT------------
|
| 372 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุงุณุชุฎุฏุงู
ุงูู
ุคุดุฑุงุช (Pointers) ุนูุฏ ุชู
ุฑูุฑ ุงูู
ุตูููุงุช ุฅูู ุงูุฏูุงู ูู ุงูุจุฑู
ุฌุฉ.
|
| 373 |
+
ุชูุถุญ ุฃู ุงูู
ุคุดุฑุงุช ุชุฌุนู ุงููุตูู ุฅูู ู
ูุงูุน ุงูุฐุงูุฑุฉ ู
ุจุงุดุฑูุง ูุฃูุซุฑ ููุงุกุฉุ ูู
ุง ุชู
ูุน ูุณุฎ ุงูู
ุตูููุฉ ูุงู
ูุฉ ุนูุฏ ุชู
ุฑูุฑูุง ููุฏูุงู.
|
| 374 |
+
ูู
ุง ุชุนุฑุถ ู
ูุงููู
ุฃุณุงุณูุฉ ู
ุซู ุฃู ุงุณู
ุงูู
ุตูููุฉ ูุดูุฑ ุฅูู ุฃูู ุนูุตุฑ ูููุงุ ูุฅู
ูุงููุฉ ุงุณุชุฎุฏุงู
ุงูุนู
ููุงุช ุงูุญุณุงุจูุฉ ุนูู ุงูู
ุคุดุฑุงุช ููุชููู ุจูู ุนูุงุตุฑ ุงูู
ุตูููุฉ.
|
| 375 |
+
ุงูุชุตู
ูู
ุนุจุงุฑุฉ ุนู ุดุฑูุญุฉ ูุตูุฉ ุจุณูุทุฉ ุจุฎูููุฉ ุจูุถุงุก ูุนูุงููู ุบุงู
ูุฉ ู
ุน ููุงุท ุชุนุฏุงุฏุ ุจุฏูู ุตูุฑ ุฃู ู
ุฎุทุทุงุช.
|
| 376 |
+
|
| 377 |
+
|
| 378 |
+
================================================================================
|
| 379 |
+
PAGE 12 - page_0012.png
|
| 380 |
+
================================================================================
|
| 381 |
+
|
| 382 |
+
------------TEXT EXTRACTED------------
|
| 383 |
+
Passing Arrays to Functions using pointer
|
| 384 |
+
|
| 385 |
+
Syntax:
|
| 386 |
+
|
| 387 |
+
return_type function_name(data_type *array, int size);
|
| 388 |
+
|
| 389 |
+
Example:
|
| 390 |
+
|
| 391 |
+
#include <stdio.h>
|
| 392 |
+
|
| 393 |
+
void printArray(int *arr, int n) {
|
| 394 |
+
for (int i = 0; i < n; i++) {
|
| 395 |
+
printf("%d ", *(arr + i)); // Access array elements using pointer arithmetic
|
| 396 |
+
}
|
| 397 |
+
printf("\n");
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
int main() {
|
| 401 |
+
int arr[] = {1, 2, 3, 4, 5};
|
| 402 |
+
int size = sizeof(arr) / sizeof(arr[0]);
|
| 403 |
+
printArray(arr, size); // Pass array using pointer
|
| 404 |
+
return 0;
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
Explanation:
|
| 408 |
+
โข The array arr is passed as a pointer to the function printArray.
|
| 409 |
+
โข Pointer arithmetic (*(arr + i)) is used to access array elements.
|
| 410 |
+
|
| 411 |
+
------------PAGE TALK ABOUT------------
|
| 412 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ููููุฉ ุชู
ุฑูุฑ ุงูู
ุตูููุงุช ุฅูู ุงูุฏูุงู ูู ูุบุฉ C ุจุงุณุชุฎุฏุงู
ุงูู
ุคุดุฑุงุช (pointers).
|
| 413 |
+
ุชุญุชูู ุนูู ุตูุบุฉ ุนุงู
ุฉ ูุชุนุฑูู ุฏุงูุฉ ุชุณุชูุจู ู
ุตูููุฉ ูู
ุคุดุฑ ู
ุน ุญุฌู
ูุง.
|
| 414 |
+
ููุฌุฏ ู
ุซุงู ุจุฑู
ุฌู ูุฏุงูุฉ printArray ุชุทุจุน ุนูุงุตุฑ ุงูู
ุตูููุฉ ุจุงุณุชุฎุฏุงู
*(arr + i) ุจุฏู ุงูููุฑุณุฉ ุงูุชูููุฏูุฉ.
|
| 415 |
+
ุจุตุฑููุงุ ุงูุตูุญุฉ ู
ูุธู
ุฉ ุจุนูุงููู ูุงุถุญุฉ ู
ุซู Syntax ูExample ูExplanation ู
ุน ู
ุฑุจุนุงุช ุฑู
ุงุฏูุฉ ูุนุฑุถ ุงูููุฏ ูุงููุตูุต ุงูุชุนููู
ูุฉ.
|
| 416 |
+
|
| 417 |
+
|
| 418 |
+
================================================================================
|
| 419 |
+
PAGE 13 - page_0013.png
|
| 420 |
+
================================================================================
|
| 421 |
+
|
| 422 |
+
------------TEXT EXTRACTED------------
|
| 423 |
+
Passing Structures to Functions
|
| 424 |
+
|
| 425 |
+
โข How to Pass Structures?
|
| 426 |
+
|
| 427 |
+
Pass by value (a copy of the structure is passed).
|
| 428 |
+
|
| 429 |
+
Pass by reference (a pointer to the structure is passed).
|
| 430 |
+
|
| 431 |
+
โข Passing a Structure by Value
|
| 432 |
+
|
| 433 |
+
Syntax:
|
| 434 |
+
|
| 435 |
+
return_type function_name(struct structure_name variable);
|
| 436 |
+
|
| 437 |
+
------------PAGE TALK ABOUT------------
|
| 438 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ููููุฉ ุชู
ุฑูุฑ ุงูู structures ุฅูู ุงูุฏูุงู ูู ุงูุจุฑู
ุฌุฉ.
|
| 439 |
+
ุชูุถุญ ุทุฑููุชูู ุฃุณุงุณูุชูู: ุงูุชู
ุฑูุฑ ุจุงูููู
ุฉ ุญูุซ ูุชู
ุฅุฑุณุงู ูุณุฎุฉ ู
ู ุงูู structureุ ูุงูุชู
ุฑูุฑ ุจุงูู
ุฑุฌุน ุญูุซ ูุชู
ุฅุฑุณุงู ู
ุคุดุฑ ุฅูููุง.
|
| 440 |
+
ูู
ุง ุชุฑูุฒ ุงูุตูุญุฉ ุนูู ุงูุชู
ุฑูุฑ ุจุงูููู
ุฉ ูุชุนุฑุถ ุงูุตูุบุฉ ุงูุนุงู
ุฉ ููุชุงุจุฉ ุฏุงูุฉ ุชุณุชูุจู structure ููุณูุท.
|
| 441 |
+
ููุฌุฏ ุนูุตุฑ ุจุตุฑู ุนุจุงุฑุฉ ุนู ู
ุฑุจุน ุฑู
ุงุฏู ูุญุชูู ุนูู ุณุทุฑ ุงูุตูุงุบุฉ ุงูุจุฑู
ุฌูุฉ ู
ุน ุชู
ููุฒ ูููู ูููุต.
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
================================================================================
|
| 445 |
+
PAGE 14 - page_0014.png
|
| 446 |
+
================================================================================
|
| 447 |
+
|
| 448 |
+
------------TEXT EXTRACTED------------
|
| 449 |
+
Passing Structures to Functions
|
| 450 |
+
|
| 451 |
+
Passing a Structure by Value example:
|
| 452 |
+
|
| 453 |
+
```c
|
| 454 |
+
#include <stdio.h>
|
| 455 |
+
struct Student {
|
| 456 |
+
char name[50];
|
| 457 |
+
int rollNo;
|
| 458 |
+
float marks;
|
| 459 |
+
};
|
| 460 |
+
|
| 461 |
+
void printStudent(struct Student s) {
|
| 462 |
+
printf("Name: %s\n", s.name);
|
| 463 |
+
printf("Roll No: %d\n", s.rollNo);
|
| 464 |
+
printf("Marks: %.2f\n", s.marks);
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
int main() {
|
| 468 |
+
struct Student student1 = {"John Doe", 101, 95.5};
|
| 469 |
+
printStudent(student1); // Pass by value
|
| 470 |
+
return 0;
|
| 471 |
+
}
|
| 472 |
+
```
|
| 473 |
+
|
| 474 |
+
Explanation:
|
| 475 |
+
โข A copy of the structure student1 is passed to the function printStudent.
|
| 476 |
+
โข Changes to the structure inside the function do not affect the original structure.
|
| 477 |
+
|
| 478 |
+
------------PAGE TALK ABOUT------------
|
| 479 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุชู
ุฑูุฑ ุงูู structure ุฅูู ุงูุฏูุงู ูู ูุบุฉ C ุจุทุฑููุฉ pass by value.
|
| 480 |
+
ุชูุถุญ ุงูู
ุซุงู ุจุงุณุชุฎุฏุงู
struct Student ูุญุชูู ุนูู ุงูุงุณู
ูุฑูู
ุงูุฌููุณ ูุงูุฏุฑุฌุงุชุ ุซู
ูุชู
ุฅุฑุณุงูู ุฅูู ุงูุฏุงูุฉ printStudent ูุนุฑุถ ุงูุจูุงูุงุช.
|
| 481 |
+
ุงูููุฑุฉ ุงูุฃุณุงุณูุฉ ูู ุฃู ุงูุฏุงูุฉ ุชุณุชูุจู ูุณุฎุฉ ู
ู ุงูู structure ูููุณ ุงูุฃุตูุ ูุฐูู ุฃู ุชุนุฏูู ุฏุงุฎู ุงูุฏุงูุฉ ูุง ูุคุซุฑ ุนูู ุงูู
ุชุบูุฑ ุงูุฃุตูู.
|
| 482 |
+
ููุฌุฏ ุนูุตุฑ ุจุตุฑู ุฑุฆูุณู ุนุจุงุฑุฉ ุนู ุตูุฏูู ููุฏ ุนูู ุงููุณุงุฑุ ูุนูู ุงููู
ูู ูุณู
ุดุฑุญ ู
ุฎุชุตุฑ ุจููุงุท ููุถุญ ุงูููุฑุฉ.
|
| 483 |
+
|
| 484 |
+
|
| 485 |
+
================================================================================
|
| 486 |
+
PAGE 15 - page_0015.png
|
| 487 |
+
================================================================================
|
| 488 |
+
|
| 489 |
+
------------TEXT EXTRACTED------------
|
| 490 |
+
Passing Structures to Functions
|
| 491 |
+
|
| 492 |
+
Passing a Structure by Reference
|
| 493 |
+
|
| 494 |
+
Syntax:
|
| 495 |
+
|
| 496 |
+
return_type function_name(struct structure_name *variable);
|
| 497 |
+
|
| 498 |
+
------------PAGE TALK ABOUT------------
|
| 499 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ููููุฉ ุชู
ุฑูุฑ ุงูู structure ุฅูู ุงูุฏูุงู ูู ูุบุฉ C ุจุงุณุชุฎุฏุงู
ุงูู
ุฑุฌุน (by reference).
|
| 500 |
+
ุชูุถุญ ุงูุตูุงุบุฉ ุงูุนุงู
ุฉ ูุชุนุฑูู ุฏุงูุฉ ุชุณุชูุจู ู
ุคุดุฑูุง ุฅูู structure ุจุฏููุง ู
ู ุชู
ุฑูุฑ ูุณุฎุฉ ูุงู
ูุฉ ู
ูู.
|
| 501 |
+
ูุฐุง ุงูุฃุณููุจ ูููุฏ ูู ุชูููู ุงููุณุฎ ูุชุญุณูู ุงูุฃุฏุงุกุ ูู
ุง ูุณู
ุญ ููุฏุงูุฉ ุจุชุนุฏูู ุจูุงูุงุช ุงูู structure ุงูุฃุตููุฉ.
|
| 502 |
+
ููุฌุฏ ุนูุตุฑ ุจุตุฑู ุจุณูุท ุนุจุงุฑุฉ ุนู ุตูุฏูู ุฑู
ุงุฏู ูุญุชูู ุณุทุฑ ุงูุตูุงุบุฉ ุงูุจุฑู
ุฌูุฉ ู
ุน ุชู
ููุฒ ูููู ูุฃุฌุฒุงุก ุงูููุฏ.
|
| 503 |
+
|
| 504 |
+
|
| 505 |
+
================================================================================
|
| 506 |
+
PAGE 16 - page_0016.png
|
| 507 |
+
================================================================================
|
| 508 |
+
|
| 509 |
+
------------TEXT EXTRACTED------------
|
| 510 |
+
Passing Structures to Functions
|
| 511 |
+
|
| 512 |
+
Passing a Structure by Reference example
|
| 513 |
+
|
| 514 |
+
#include <stdio.h>
|
| 515 |
+
|
| 516 |
+
struct Student {
|
| 517 |
+
char name[50];
|
| 518 |
+
int rollNo;
|
| 519 |
+
float marks;
|
| 520 |
+
};
|
| 521 |
+
|
| 522 |
+
void updateMarks(struct Student *s) {
|
| 523 |
+
s->marks += 10; // Modify the structure using a pointer
|
| 524 |
+
}
|
| 525 |
+
|
| 526 |
+
int main() {
|
| 527 |
+
struct Student student1 = {"John Doe", 101, 95.5};
|
| 528 |
+
updateMarks(&student1); // Pass by reference
|
| 529 |
+
printf("Updated Marks: %.2f\n", student1.marks);
|
| 530 |
+
return 0;
|
| 531 |
+
}
|
| 532 |
+
|
| 533 |
+
Explanation:
|
| 534 |
+
โข The address of the structure student1 is passed to the function updateMarks.
|
| 535 |
+
โข Changes to the structure inside the function affect the original structure.
|
| 536 |
+
|
| 537 |
+
------------PAGE TALK ABOUT------------
|
| 538 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุชู
ุฑูุฑ ุงูู structure ุฅูู ุงูุฏูุงู ูู ูุบุฉ C ุจุงุณุชุฎุฏุงู
ุงูู
ุฑุฌุน ุนุจุฑ ุงูู
ุคุดุฑ.
|
| 539 |
+
ููุถุญ ุงูู
ุซุงู ุจููุฉ Student ุงูุชู ุชุญุชูู ุนูู ุงูุงุณู
ูุงูุฑูู
ูุงูุฏุฑุฌุงุชุ ุซู
ุฏุงูุฉ updateMarks ุชุณุชูุจู ู
ุคุดุฑูุง ุฅูู ุงูุจููุฉ ูุชุฒูุฏ marks ุจู
ูุฏุงุฑ 10.
|
| 540 |
+
ูู
ุง ูุจูู ูู ุงูุฏุงูุฉ main ุฅูุดุงุก ูุงุฆู student1 ูุชู
ุฑูุฑ ุนููุงูู ููุฏุงูุฉ ุจุงุณุชุฎุฏุงู
&student1ุ ูุฐูู ูุชู
ุชุนุฏูู ุงูููู
ุฉ ุงูุฃุตููุฉ ู
ุจุงุดุฑุฉ.
|
| 541 |
+
ุจุตุฑููุงุ ุงูุตูุญุฉ ุนุจุงุฑุฉ ุนู ุดุฑูุญุฉ ุชุนููู
ูุฉ ูููุง ุนููุงู ูุจูุฑ ุจุงูุฃุนููุ ูู
ุฑุจุน ููุฏ ุนูู ุงููุณุงุฑุ ููุต ุดุฑุญ ููุทู ุนูู ุงููู
ูู.
|
| 542 |
+
|
| 543 |
+
|
| 544 |
+
================================================================================
|
| 545 |
+
PAGE 17 - page_0017.png
|
| 546 |
+
================================================================================
|
| 547 |
+
|
| 548 |
+
------------TEXT EXTRACTED------------
|
| 549 |
+
Passing Structures to Functions
|
| 550 |
+
|
| 551 |
+
Passing an Array of Structures to Functions
|
| 552 |
+
|
| 553 |
+
Why Pass an Array of Structures?
|
| 554 |
+
|
| 555 |
+
โข To process multiple records of structured data within a function.
|
| 556 |
+
|
| 557 |
+
โข Useful for operations like sorting, searching, or updating.
|
| 558 |
+
|
| 559 |
+
How to Pass an Array of Structures?
|
| 560 |
+
|
| 561 |
+
โข Pass the array name (which is a pointer to the first element of the array).
|
| 562 |
+
|
| 563 |
+
Syntax:
|
| 564 |
+
|
| 565 |
+
return_type function_name(struct structure_name array[], int size);
|
| 566 |
+
|
| 567 |
+
------------PAGE TALK ABOUT------------
|
| 568 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ููููุฉ ุชู
ุฑูุฑ ุงูู
ุตูููุงุช ู
ู ุงูููุงูู (Structures) ุฅูู ุงูุฏูุงู ูู ุงูุจุฑู
ุฌุฉุ ููู
ุงุฐุง ูุณุชุฎุฏู
ุฐูู ุนูุฏ ุงูุชุนุงู
ู ู
ุน ุนุฏุฉ ุณุฌูุงุช ุจูุงูุงุช.
|
| 569 |
+
ุชูุถุญ ุงูููุงุฆุฏ ู
ุซู ุชูููุฐ ุนู
ููุงุช ุงููุฑุฒ ูุงูุจุญุซ ูุงูุชุญุฏูุซ ุฏุงุฎู ุงูุฏุงูุฉ ุจุณูููุฉ.
|
| 570 |
+
ูู
ุง ุชุดุฑุญ ุฃู ุงุณู
ุงูู
ุตูููุฉ ููู
ุฑููุฑ ุฅูู ุงูุฏุงูุฉ ูุฃูู ูุดูุฑ ุฅูู ุฃูู ุนูุตุฑ ูููุง.
|
| 571 |
+
ูู๏ฟฝ๏ฟฝุฏ ุนูุตุฑ ุจุตุฑู ุจุณูุท ุนุจุงุฑุฉ ุนู ู
ุฑุจุน ุฑู
ุงุฏู ูู ุงูุฃุณูู ูุญุชูู ุนูู ุตูุบุฉ ุงูููุฏ ุงูุฎุงุตุฉ ุจุชู
ุฑูุฑ ู
ุตูููุฉ ู
ู ุงูููุงูู ุฅูู ุงูุฏุงูุฉ.
|
| 572 |
+
|
| 573 |
+
|
| 574 |
+
================================================================================
|
| 575 |
+
PAGE 18 - page_0018.png
|
| 576 |
+
================================================================================
|
| 577 |
+
|
| 578 |
+
------------TEXT EXTRACTED------------
|
| 579 |
+
Passing an Array of Structures to Functions example:
|
| 580 |
+
|
| 581 |
+
#include <stdio.h>
|
| 582 |
+
struct Student {
|
| 583 |
+
char name[50];
|
| 584 |
+
int rollNo;
|
| 585 |
+
float marks;
|
| 586 |
+
};
|
| 587 |
+
void printStudents(struct Student students[], int n) {
|
| 588 |
+
for (int i = 0; i < n; i++) {
|
| 589 |
+
printf("Student %d:\n", i + 1);
|
| 590 |
+
printf("Name: %s\n", students[i].name);
|
| 591 |
+
printf("Roll No: %d\n", students[i].rollNo);
|
| 592 |
+
printf("Marks: %.2f\n", students[i].marks);
|
| 593 |
+
}
|
| 594 |
+
}
|
| 595 |
+
|
| 596 |
+
int main() {
|
| 597 |
+
struct Student students[3] = {
|
| 598 |
+
{"Alice", 101, 95.5},
|
| 599 |
+
{"Bob", 102, 88.0},
|
| 600 |
+
{"Charlie", 103, 92.5} };
|
| 601 |
+
printStudents(students, 3); // Pass array of structures
|
| 602 |
+
return 0;
|
| 603 |
+
}
|
| 604 |
+
|
| 605 |
+
Explanation:
|
| 606 |
+
โข The array students is passed to the function printStudents.
|
| 607 |
+
โข The function processes each element of the array.
|
| 608 |
+
|
| 609 |
+
------------PAGE TALK ABOUT------------
|
| 610 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ููููุฉ ุชู
ุฑูุฑ ู
ุตูููุฉ ู
ู ุงูููุงูู (Array of Structures) ุฅูู ุฏุงูุฉ ูู ูุบุฉ C.
|
| 611 |
+
ุชูุถุญ ู
ุซุงููุง ุจุงุณุชุฎุฏุงู
ูููู Student ูุญุชูู ุนูู ุงูุงุณู
ูุฑูู
ุงูุฌููุณ ูุงูุฏุฑุฌุงุชุ ุซู
ุชู
ุฑูุฑ ู
ุตูููุฉ ู
ู ุงูุทูุงุจ ุฅูู ุงูุฏุงูุฉ printStudents.
|
| 612 |
+
ุงูุฏุงูุฉ ุชุณุชุฎุฏู
ุญููุฉ for ูุงุณุชุนุฑุงุถ ูู ุนูุตุฑ ูู ุงูู
ุตูููุฉ ูุทุจุงุนุฉ ุจูุงูุงุชู.
|
| 613 |
+
ููุฌุฏ ุชูุณูู ุจุตุฑู ุจุณูุท: ุนููุงู ูุจูุฑ ุจุงูุฃุนููุ ููุฏ ุจุฑู
ุฌู ุนูู ุงููุณุงุฑุ ููุณู
ุดุฑุญ ู
ุฎุชุตุฑ ุจููุงุท ุนูู ุงููู
ูู.
|
| 614 |
+
|
| 615 |
+
|
| 616 |
+
================================================================================
|
| 617 |
+
PAGE 19 - page_0019.png
|
| 618 |
+
================================================================================
|
| 619 |
+
|
| 620 |
+
------------TEXT EXTRACTED------------
|
| 621 |
+
Modifying an Array of Structures in a
|
| 622 |
+
Function example:
|
| 623 |
+
|
| 624 |
+
Explanation:
|
| 625 |
+
|
| 626 |
+
โข The function updateMarks modifies
|
| 627 |
+
|
| 628 |
+
the marks of each student in the array.
|
| 629 |
+
|
| 630 |
+
โข Changes are reflected in the original array.
|
| 631 |
+
|
| 632 |
+
#include <stdio.h>
|
| 633 |
+
struct Student {
|
| 634 |
+
char name[50];
|
| 635 |
+
int rollNo;
|
| 636 |
+
float marks;
|
| 637 |
+
};
|
| 638 |
+
|
| 639 |
+
void updateMarks(struct Student students[], int n) {
|
| 640 |
+
for (int i = 0; i < n; i++) {
|
| 641 |
+
students[i].marks += 10; // Update marks for each student
|
| 642 |
+
}
|
| 643 |
+
}
|
| 644 |
+
int main() {
|
| 645 |
+
struct Student students[3] = {
|
| 646 |
+
{"Alice", 101, 95.5},
|
| 647 |
+
{"Bob", 102, 88.0},
|
| 648 |
+
{"Charlie", 103, 92.5}
|
| 649 |
+
};
|
| 650 |
+
updateMarks(students, 3); // Pass array of structures
|
| 651 |
+
printf("Updated Marks:\n");
|
| 652 |
+
for (int i = 0; i < 3; i++) {
|
| 653 |
+
printf("Name: %s, Marks: %.2f\n", students[i].name, students[i].marks);
|
| 654 |
+
}
|
| 655 |
+
return 0;
|
| 656 |
+
}
|
| 657 |
+
|
| 658 |
+
------------PAGE TALK ABOUT------------
|
| 659 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ุซุงู ุจูุบุฉ C ููุถุญ ููููุฉ ุชุนุฏูู ู
ุตูููุฉ ู
ู ุงูููุงูู (structures) ุฏุงุฎู ุฏุงูุฉ.
|
| 660 |
+
ุชูุนุฑูู ุงูุตูุญุฉ ูููู Student ูุญุชูู ุนูู ุงูุงุณู
ูุฑูู
ุงูุทุงูุจ ูุงูุฏุฑุฌุงุชุ ุซู
ุชุณุชุฎุฏู
ุงูุฏุงูุฉ updateMarks ูุฅุถุงูุฉ 10 ุฏุฑุฌุงุช ููู ุทุงูุจ.
|
| 661 |
+
ูู
ุง ุชูุถูุญ ุฃู ุงูุชุนุฏููุงุช ุฏุงุฎู ุงูุฏุงูุฉ ุชูุนูุณ ู
ุจุงุดุฑุฉ ุนูู ุงูู
ุตูููุฉ ุงูุฃุตููุฉ ูุฃู ุงูู
ุตูููุฉ ุชูู
ุฑููุฑ ููุฏุงูุฉ.
|
| 662 |
+
ุงูุชุตู
ูู
ูุญุชูู ุนูู ุนููุงู ูุดุฑุญ ููุทู ูู ุงูุฌูุฉ ุงููุณุฑูุ ูููุฏ ุจุฑู
ุฌู ู
ูููู ูู
ูุณู ูู ุงูุฌูุฉ ุงููู
ูู.
|
| 663 |
+
|
| 664 |
+
|
| 665 |
+
================================================================================
|
| 666 |
+
PAGE 20 - page_0020.png
|
| 667 |
+
================================================================================
|
| 668 |
+
|
| 669 |
+
------------TEXT EXTRACTED------------
|
| 670 |
+
Introduction to Recursion
|
| 671 |
+
|
| 672 |
+
How Recursion Works (Stack Mechanism)
|
| 673 |
+
|
| 674 |
+
Recursion and the Call Stack:
|
| 675 |
+
Each recursive call creates a new stack frame.
|
| 676 |
+
The stack grows until the base case is reached.
|
| 677 |
+
Once the base case is reached, the stack unwinds, and results are returned.
|
| 678 |
+
|
| 679 |
+
Example: Factorial of 3
|
| 680 |
+
|
| 681 |
+
#include <stdio.h>
|
| 682 |
+
|
| 683 |
+
int factorial(int n) {
|
| 684 |
+
if (n == 0) return 1; // Base case
|
| 685 |
+
return n * factorial(n - 1); // Recursive case
|
| 686 |
+
}
|
| 687 |
+
|
| 688 |
+
int main()
|
| 689 |
+
{
|
| 690 |
+
int n = 3;
|
| 691 |
+
printf("Factorial of %d is %d\n", n, factorial(n));
|
| 692 |
+
return 0;
|
| 693 |
+
}
|
| 694 |
+
|
| 695 |
+
------------PAGE TALK ABOUT------------
|
| 696 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ูุฏู
ุฉ ูู ู
ูููู
ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู (Recursion) ูุขููุฉ ุนู
ูู ุจุงุณุชุฎุฏุงู
ู
ูุฏุณ ุงูุงุณุชุฏุนุงุกุงุช (Call Stack).
|
| 697 |
+
ุชูุถุญ ุฃู ูู ุงุณุชุฏุนุงุก ุฐุงุชู ููุดุฆ ุฅุทุงุฑูุง ุฌุฏูุฏูุง ูู ุงูู
ูุฏุณุ ููุณุชู
ุฑ ุงูุชุฑุงูู
ุญุชู ุงููุตูู ุฅูู ุงูุญุงูุฉ ุงูุฃุณุงุณู๏ฟฝ๏ฟฝ.
|
| 698 |
+
ุจุนุฏ ุงููุตูู ุฅูู ุงูุญุงูุฉ ุงูุฃุณุงุณูุฉ ูุจุฏุฃ ุงูู
ูุฏุณ ูู ุงูุชููู ูุชูุนุงุฏ ุงููุชุงุฆุฌ ุชุฏุฑูุฌููุง.
|
| 699 |
+
ููุฌุฏ ู
ุซุงู ุจุฑู
ุฌู ุจูุบุฉ C ูุญุณุงุจ !3 ุจุงุณุชุฎุฏุงู
ุฏุงูุฉ factorialุ ู
ุน ุชู
ููุฒ ุงูุญุงูุฉ ุงูุฃุณุงุณูุฉ ูุงูุญุงูุฉ ุงูุชูุฑุงุฑูุฉ ุฏุงุฎู ุตูุฏูู ููุฏ ุจุตุฑู.
|
| 700 |
+
|
| 701 |
+
|
| 702 |
+
================================================================================
|
| 703 |
+
PAGE 21 - page_0021.png
|
| 704 |
+
================================================================================
|
| 705 |
+
|
| 706 |
+
------------TEXT EXTRACTED------------
|
| 707 |
+
Factorial (N!)
|
| 708 |
+
|
| 709 |
+
โข N! = (N-1)! * N [for N > 1]
|
| 710 |
+
|
| 711 |
+
โข 1! = 1
|
| 712 |
+
|
| 713 |
+
โข 3!
|
| 714 |
+
= 2! * 3
|
| 715 |
+
= (1! * 2) * 3
|
| 716 |
+
= 1 * 2 * 3
|
| 717 |
+
|
| 718 |
+
โข Recursive design:
|
| 719 |
+
โข Decomposition: (N-1)!
|
| 720 |
+
โข Composition: * N
|
| 721 |
+
โข Base case: 1!
|
| 722 |
+
|
| 723 |
+
------------PAGE TALK ABOUT------------
|
| 724 |
+
ุชุดุฑุญ ุงูุตูุญุฉ ู
ูููู
ุงูู
ุถุฑูุจ (Factorial) ููุฑู
ุฒ ูู ุจู N!ุ ูุชุนุฑุถ ุชุนุฑููู ุงูุนูุฏู: โN! = (N-1)! ร N ุนูุฏู
ุง ูููู N > 1ุ ู
ุน ุงูุญุงูุฉ ุงูุฃุณุงุณูุฉ 1! = 1.
|
| 725 |
+
ูู
ุง ุชูุฏู
ู
ุซุงููุง ุนูู ุญุณุงุจ 3! ุฎุทูุฉ ุจุฎุทูุฉ ุญุชู ุงููุตูู ุฅูู 1 ร 2 ร 3.
|
| 726 |
+
ูุชูุถุญ ููุฑุฉ ุงูุชุตู
ูู
ุงูุนูุฏู ุนุจุฑ ุซูุงุซุฉ ุนูุงุตุฑ: ุงูุชูููู ุฅูู (N-1)!ุ ุซู
ุงูุชุฑููุจ ุจุถุฑุจ ุงููุงุชุฌ ูู Nุ ูุฃุฎูุฑูุง ุงูุญุงูุฉ ุงูุฃุณุงุณูุฉ 1!.
|
| 727 |
+
ุจุตุฑููุงุ ุงูุตูุญุฉ ุนุจุงุฑุฉ ุนู ุดุฑูุญุฉ ุชุนููู
ูุฉ ุจุณูุทุฉ ุชุญุชูู ุนูู ุนููุงู ูุจูุฑ ูููุงุท ุชุนุฏุงุฏุ ู
ุน ุชู
ููุฒ ุจุนุถ ุงูุฃุฌุฒุงุก ุจุงูุฃููุงู ูุฅุจุฑุงุฒ ุนูุงุตุฑ ุงูุนูุฏูุฉ.
|
| 728 |
+
|
| 729 |
+
|
| 730 |
+
================================================================================
|
| 731 |
+
PAGE 22 - page_0022.png
|
| 732 |
+
================================================================================
|
| 733 |
+
|
| 734 |
+
------------TEXT EXTRACTED------------
|
| 735 |
+
Factorial example
|
| 736 |
+
|
| 737 |
+
#include <stdio.h>
|
| 738 |
+
int factorial(int n) {
|
| 739 |
+
if (n == 0) return 1; // Base case
|
| 740 |
+
return n * factorial(n - 1); // Recursive case
|
| 741 |
+
}
|
| 742 |
+
|
| 743 |
+
int main()
|
| 744 |
+
{
|
| 745 |
+
int n = 5;
|
| 746 |
+
printf("Factorial of %d is %d\n", n, factorial(n));
|
| 747 |
+
return 0;
|
| 748 |
+
}
|
| 749 |
+
|
| 750 |
+
------------PAGE TALK ABOUT------------
|
| 751 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ุซุงู ุจุฑู
ุฌู ูุญุณุงุจ ุงูุนุงู
ููุฉ (Factorial) ุจุงุณุชุฎุฏุงู
ูุบุฉ C.
|
| 752 |
+
ููุถุญ ุงูููุฏ ู
ูููู
ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู Recursion ู
ู ุฎูุงู ุญุงูุชูู: ุงูุญุงูุฉ ุงูุฃุณุงุณูุฉ ุนูุฏู
ุง n == 0ุ ูุงูุญุงูุฉ ุงูุชูุฑุงุฑูุฉ ุจุฅุฑุฌุงุน n * factorial(n - 1).
|
| 753 |
+
ูู
ุง ูุญุชูู ุนูู ุฏุงูุฉ main ูุชุฌุฑุจุฉ ุงูู
ุซุงู ุนูุฏ n = 5 ูุทุจุงุนุฉ ุงููุงุชุฌ.
|
| 754 |
+
ุจุตุฑููุงุ ุงูุตูุญุฉ ุนุจุงุฑุฉ ุนู ุดุฑูุญุฉ ุชุนููู
ูุฉ ุจุนููุงู ูุจูุฑ ูู ุงูุฃุนููุ ูุจุฏุงุฎููุง ู
ุฑุจุน ุฑู
ุงุฏู ูุงุชุญ ูุญุชูู ุนูู ุงูููุฏ ุงูุจุฑู
ุฌู.
|
| 755 |
+
|
| 756 |
+
|
| 757 |
+
================================================================================
|
| 758 |
+
PAGE 23 - page_0023.png
|
| 759 |
+
================================================================================
|
| 760 |
+
|
| 761 |
+
------------TEXT EXTRACTED------------
|
| 762 |
+
int factorial(int 3) {
|
| 763 |
+
if (n == 0) return 1; // Base case
|
| 764 |
+
return 3 * factorial(2); // Recursive case
|
| 765 |
+
}
|
| 766 |
+
|
| 767 |
+
------------PAGE TALK ABOUT------------
|
| 768 |
+
ุงูุตูุญุฉ ุชุนุฑุถ ู
ุซุงููุง ุจุฑู
ุฌููุง ูุฏุงูุฉ factorial ุจุงุณุชุฎุฏุงู
ู
ูููู
ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู (Recursion).
|
| 769 |
+
ููุถุญ ุงูููุฏ ุญุงูุชูู: ุงูุญุงูุฉ ุงูุฃุณุงุณูุฉ ุนูุฏู
ุง ุชููู ุงูููู
ุฉ 0 ูููุฑุฌุน 1ุ ูุงูุญุงูุฉ ุงูุชูุฑุงุฑูุฉ ุงูุชู ุชุณุชุฏุนู ุงูุฏุงูุฉ ู
ุฑุฉ ุฃุฎุฑู.
|
| 770 |
+
ููุฌุฏ ุฎุทุฃ ุฃู ุชุจุณูุท ูู ุงูููู
ุงูุธุงูุฑุฉ ุฏุงุฎู ุงูู
ุซุงู ู
ุซู int 3 ู factorial(2) ุจุฏู ุงุณุชุฎุฏุงู
ุงูู
ุชุบูุฑ n ุจุดูู ุตุญูุญ.
|
| 771 |
+
ุงูุนูุงุตุฑ ุงูุจุตุฑูุฉ ุนุจุงุฑุฉ ุนู ู
ุฑุจุน ุฑู
ุงุฏู ูุงุชุญ ูุญุชูู ุนูู ููุฏ ู
ูููู ุจุฃุณููุจ ุนุฑุถ ุชุนููู
ู.
|
| 772 |
+
|
| 773 |
+
|
| 774 |
+
================================================================================
|
| 775 |
+
PAGE 24 - page_0024.png
|
| 776 |
+
================================================================================
|
| 777 |
+
|
| 778 |
+
------------TEXT EXTRACTED------------
|
| 779 |
+
int factorial(int 3) {
|
| 780 |
+
if (n == 0) return 1; // Base case
|
| 781 |
+
return 3 * factorial(2); // Recursive case
|
| 782 |
+
}
|
| 783 |
+
|
| 784 |
+
int factorial(int 2) {
|
| 785 |
+
if (n == 0) return 1; // Base case
|
| 786 |
+
return 2 * factorial(1); // Recursive case
|
| 787 |
+
}
|
| 788 |
+
|
| 789 |
+
------------PAGE TALK ABOUT------------
|
| 790 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ูููู
ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู (Recursion) ูู ุฏุงูุฉ ุญุณุงุจ ุงูู
ุถุฑูุจ factorial.
|
| 791 |
+
ุชูุถุญ ุฃู ุงูุฏุงูุฉ ุชุญุชูู ุนูู ุญุงูุฉ ุฃุณุงุณูุฉ Base case ุนูุฏู
ุง ุชููู n == 0ุ ูุญุงูุฉ้ๅฝูุฉ Recursive case ุชุณุชุฏุนู ูููุง ุงูุฏุงูุฉ ููุณูุง ุจููู
ุฉ ุฃุตุบุฑ.
|
| 792 |
+
ููุฌุฏ ุตูุฏููุงู ูุญุชููุงู ุนูู ู
ุซุงููู ููุฏุงูุฉ ุนูุฏ ุงูููู
3 ู2ุ ู
ุน ุณูู
ู
ูุญูู ุจูููู
ุง ููุถุญ ุงูุชูุงู ุงูุงุณุชุฏุนุงุก ู
ู factorial(3) ุฅูู factorial(2).
|
| 793 |
+
ุงููุฏู ู
ู ุงูุฑุณู
ูู ุดุฑุญ ููู ุชุชุฏุฑุฌ ุงูุงุณุชุฏุนุงุกุงุช ุญุชู ุงููุตูู ุฅูู ุงูุญุงูุฉ ุงูุฃุณุงุณูุฉ ุซู
ุงูุนูุฏุฉ ุจุงููุชูุฌุฉ.
|
| 794 |
+
|
| 795 |
+
|
| 796 |
+
================================================================================
|
| 797 |
+
PAGE 25 - page_0025.png
|
| 798 |
+
================================================================================
|
| 799 |
+
|
| 800 |
+
------------TEXT EXTRACTED------------
|
| 801 |
+
int factorial(int 3) {
|
| 802 |
+
if (n == 0) return 1; // Base case
|
| 803 |
+
return 3 * factorial(2); // Recursive case
|
| 804 |
+
}
|
| 805 |
+
|
| 806 |
+
int factorial(int 2) {
|
| 807 |
+
if (n == 0) return 1; // Base case
|
| 808 |
+
return 2 * factorial(1); // Recursive case
|
| 809 |
+
}
|
| 810 |
+
|
| 811 |
+
int factorial(int 1) {
|
| 812 |
+
if (n == 0) return 1; // Base case
|
| 813 |
+
return 1 * factorial(0); // Recursive case
|
| 814 |
+
}
|
| 815 |
+
|
| 816 |
+
------------PAGE TALK ABOUT------------
|
| 817 |
+
ุชูุถุญ ุงูุตูุญุฉ ู
ูููู
ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู (Recursion) ู
ู ุฎูุงู ุฏุงูุฉ factorial.
|
| 818 |
+
ุชูุธูุฑ ุงูุฃู
ุซูุฉ ููู ูุชู
ุงุณุชุฏุนุงุก ุงูุฏุงูุฉ ูููุณูุง ุชุฏุฑูุฌููุง: ู
ู 3 ุฅูู 2 ุซู
ุฅูู 1 ุซู
0.
|
| 819 |
+
ููุฌุฏ ุชู
ููุฒ ุจูู ุญุงูุชูู: ุงูุญุงูุฉ ุงูุฃุณุงุณูุฉ Base case ุนูุฏู
ุง n == 0ุ ูุงูุญุงูุฉ ุงูุชูุฑุงุฑูุฉ Recursive case.
|
| 820 |
+
ูู
ุง ุชุนุฑุถ ุงูุตูุฑุฉ ุฃุณูู
ุงู ู
ูุญููุฉ ุจูู ู
ุฑุจุนุงุช ุงูููุฏ ูุชูุถูุญ ุชุณูุณู ุงูุชูุงู ุงูุงุณุชุฏุนุงุกุงุช ุจูู ูู ุฎุทูุฉ.
|
| 821 |
+
|
| 822 |
+
|
| 823 |
+
================================================================================
|
| 824 |
+
PAGE 26 - page_0026.png
|
| 825 |
+
================================================================================
|
| 826 |
+
|
| 827 |
+
------------TEXT EXTRACTED------------
|
| 828 |
+
int factorial(int 3) {
|
| 829 |
+
if (n == 0) return 1; // Base case
|
| 830 |
+
return 3 * factorial(2); // Recursive case
|
| 831 |
+
}
|
| 832 |
+
|
| 833 |
+
int factorial(int 2) {
|
| 834 |
+
if (n == 0) return 1; // Base case
|
| 835 |
+
return 2 * factorial(1); // Recursive case
|
| 836 |
+
}
|
| 837 |
+
|
| 838 |
+
int factorial(int 1) {
|
| 839 |
+
if (n == 0) return 1; // Base case
|
| 840 |
+
return 2 * factorial(1); // Recursive case
|
| 841 |
+
}
|
| 842 |
+
|
| 843 |
+
int factorial(int 0) {
|
| 844 |
+
if (n == 0) return 1; // Base case
|
| 845 |
+
return n * factorial(n - 1); // Recursive case
|
| 846 |
+
}
|
| 847 |
+
|
| 848 |
+
------------PAGE TALK ABOUT------------
|
| 849 |
+
ุชูุถุญ ุงูุตูุญุฉ ู
ูููู
ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู Recursion ู
ู ุฎูุงู ุฏุงูุฉ factorial ูููู ุชูุชูู ุงูููู
ู
ู factorial(3) ุฅูู factorial(0).
|
| 850 |
+
ูุธูุฑ ูู ูู ุตูุฏูู ุฌุฒุก ู
ู ุงูููุฏ ู
ุน ุญุงูุชูู: ุงูุญุงูุฉ ุงูุฃุณุงุณูุฉ Base case ุนูุฏู
ุง n == 0ุ ูุงูุญุงูุฉ ุงูุชูุฑุงุฑูุฉ Recursive case ุงูุชู ุชุณุชุฏุนู ุงูุฏุงูุฉ ุจููู
ุฉ ุฃุตุบุฑ.
|
| 851 |
+
ุชูุฌุฏ ุฃุณูู
ู
ูุญููุฉ ูุฑุฏูุฉ ุจูู ุงูุตูุงุฏูู ูุชู
ุซูู ุชุณูุณู ุงูุงุณุชุฏุนุงุกุงุช ุฎุทูุฉ ุจุฎุทูุฉ ุญุชู ุงููุตูู ุฅูู ุงูุญุงูุฉ ุงูุฃุณุงุณูุฉ.
|
| 852 |
+
ุงูุตูุฑุฉ ุชุดุฑุญ ุจุตุฑููุง ููู ูุชู
ุชูููู ุญุณุงุจ ุงูู
ุถุฑูุจ ุชุฏุฑูุฌููุง ุนุจุฑ ุนุฏุฉ ุงุณุชุฏุนุงุกุงุช ู
ุชุชุงููุฉ.
|
| 853 |
+
|
| 854 |
+
|
| 855 |
+
================================================================================
|
| 856 |
+
PAGE 27 - page_0027.png
|
| 857 |
+
================================================================================
|
| 858 |
+
|
| 859 |
+
------------TEXT EXTRACTED------------
|
| 860 |
+
int factorial(int 3) {
|
| 861 |
+
if (n == 0) return 1; // Base case
|
| 862 |
+
return 3 * factorial(2); // Recursive case
|
| 863 |
+
}
|
| 864 |
+
|
| 865 |
+
int factorial(int 2) {
|
| 866 |
+
if (n == 0) return 1; // Base case
|
| 867 |
+
return 2 * factorial(1); // Recursive case
|
| 868 |
+
}
|
| 869 |
+
|
| 870 |
+
int factorial(int 1) {
|
| 871 |
+
if (n == 0) return 1; // Base case
|
| 872 |
+
return 1 * factorial(0); // Recursive case
|
| 873 |
+
}
|
| 874 |
+
|
| 875 |
+
int factorial(int 0) {
|
| 876 |
+
if (n == 0) return 1; // Base case
|
| 877 |
+
return n * factorial(n - 1); // Recursive case
|
| 878 |
+
}
|
| 879 |
+
|
| 880 |
+
------------PAGE TALK ABOUT------------
|
| 881 |
+
ุชุดุฑุญ ุงูุตูุญุฉ ู
ูููู
ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู (Recursion) ู
ู ุฎูุงู ุฏุงูุฉ ุญุณุงุจ ุงูู
ุถุฑูุจ factorial ุจุฏุกูุง ู
ู 3 ุซู
2 ุซู
1 ุญุชู 0.
|
| 882 |
+
ุชูุถุญ ุฃู ูู ุงุณุชุฏุนุงุก ูููุฐ ุงูุญุงูุฉ ุงูุชูุฑุงุฑูุฉ Recursive caseุ ุจููู
ุง ุนูุฏ ุงููุตูู ุฅูู n == 0 ูุชู
ุชูููุฐ ุงูุญุงูุฉ ุงูุฃุณุงุณูุฉ Base case ูุฅุฑุฌุงุน 1.
|
| 883 |
+
ููุฌุฏ ู
ุฎุทุท ุจุตุฑู ุนูู ุดูู ุตูุงุฏูู ููุฏ ู
ุชุฏุฑุฌุฉ ูุจูู ุชุณูุณู ุงูุงุณุชุฏุนุงุกุงุชุ ู
ุน ุณูู
ูุฑุฏู ู
ูุญูู ูุดูุฑ ุฅูู ุงูุงูุชูุงู ูุญู ุงูุญุงูุฉ ุงูุฃุณุงุณูุฉ ุนูุฏ factorial(0).
|
| 884 |
+
ุงููุฏู ู
ู ุงูุฑุณู
ูู ุชูุถูุญ ููู ุชุชููู ุงูุฏุงูุฉ ุงูุนูุฏูุฉ ุนูุฏ ุงูุดุฑุท ุงูุฃุณุงุณู ุซู
ุชุจุฏุฃ ุงูููู
ูู ุงูุฑุฌูุน ููุญุณุงุจ ุงูููุงุฆู.
|
| 885 |
+
|
| 886 |
+
|
| 887 |
+
================================================================================
|
| 888 |
+
PAGE 28 - page_0028.png
|
| 889 |
+
================================================================================
|
| 890 |
+
|
| 891 |
+
------------TEXT EXTRACTED------------
|
| 892 |
+
int factorial(int 3) {
|
| 893 |
+
if (n == 0) return 1; // Base case
|
| 894 |
+
return 3 * factorial(2); // Recursive case
|
| 895 |
+
}
|
| 896 |
+
|
| 897 |
+
int factorial(int 2) {
|
| 898 |
+
if (n == 0) return 1; // Base case
|
| 899 |
+
return 2 * factorial(1); // Recursive case
|
| 900 |
+
}
|
| 901 |
+
|
| 902 |
+
int factorial(int 1) {
|
| 903 |
+
if (n == 0) return 1; // Base case
|
| 904 |
+
return 1 * 1; // Recursive case
|
| 905 |
+
}
|
| 906 |
+
|
| 907 |
+
int factorial(int 0) {
|
| 908 |
+
if (n == 0) return 1; // Base case
|
| 909 |
+
return n * factorial(n - 1); // Recursive case
|
| 910 |
+
}
|
| 911 |
+
|
| 912 |
+
------------PAGE TALK ABOUT------------
|
| 913 |
+
ุชูุถุญ ุงูุตูุญุฉ ููุฑุฉ ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู Recursion ู
ู ุฎูุงู ู
ุซุงู ุฏุงูุฉ factorial ุจุฏุกูุง ู
ู 3 ุซู
2 ุซู
1 ุซู
0.
|
| 914 |
+
ุชูุธูุฑ ุฃู ูู ุงุณุชุฏุนุงุก ููุชุธุฑ ูุชูุฌุฉ ุงูุงุณุชุฏุนุงุก ุงูุชุงูู ุญุชู ุงููุตูู ุฅูู ุงูุญุงูุฉ ุงูุฃุณุงุณูุฉ Base case ุนูุฏ n == 0 ุญูุซ ุชูุฑุฌุน ุงูุฏุงูุฉ 1.
|
| 915 |
+
ุจุนุฏูุง ูุชู
ุงูุฑุฌูุน ููุฎูู ูุญุณุงุจ ุงูููู
ุชุฏุฑูุฌููุง ู
ุซู 1 ร 1 ุซู
2 ร factorial(1) ุซู
3 ร factorial(2).
|
| 916 |
+
ููุฌุฏ ู
ุฎุทุท ุจุตุฑู ุนูู ุดูู ุตูุงุฏูู ููุฏ ู
ุชุฏุฑุฌุฉ ู
ุน ุณูู
ูุฑุฏู ูุฑุจุท ุจูู factorial(1) ู factorial(0) ูุชูุถูุญ ู
ุณุงุฑ ุงูุชูููุฐ.
|
| 917 |
+
|
| 918 |
+
|
| 919 |
+
================================================================================
|
| 920 |
+
PAGE 29 - page_0029.png
|
| 921 |
+
================================================================================
|
| 922 |
+
|
| 923 |
+
------------TEXT EXTRACTED------------
|
| 924 |
+
int factorial(int 3) {
|
| 925 |
+
if (n == 0) return 1; // Base case
|
| 926 |
+
return 3 * factorial(2); // Recursive case
|
| 927 |
+
}
|
| 928 |
+
|
| 929 |
+
int factorial(int 2) {
|
| 930 |
+
if (n == 0) return 1; // Base case
|
| 931 |
+
return 2 * factorial(1); // Recursive case
|
| 932 |
+
}
|
| 933 |
+
|
| 934 |
+
int factorial(int 1) {
|
| 935 |
+
if (n == 0) return 1; // Base case
|
| 936 |
+
return 1; // Recursive case
|
| 937 |
+
}
|
| 938 |
+
|
| 939 |
+
int factorial(int 0) {
|
| 940 |
+
if (n == 0) return 1; // Base case
|
| 941 |
+
return n * factorial(n - 1); // Recursive case
|
| 942 |
+
}
|
| 943 |
+
|
| 944 |
+
------------PAGE TALK ABOUT------------
|
| 945 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุดุฑุญ ุงูุนูุฏูุฉ (Recursion) ู
ู ุฎูุงู ุฏุงูุฉ ุญุณุงุจ ุงูู
ุถุฑูุจ factorial.
|
| 946 |
+
ุชูุธูุฑ ููู ูุชู
ุงุณุชุฏุนุงุก ุงูุฏุงูุฉ ุชุฏุฑูุฌููุง ู
ู factorial(3) ุฅูู factorial(2) ุซู
factorial(1) ุญุชู ุงููุตูู ุฅูู factorial(0).
|
| 947 |
+
ูู
ุง ุชูุถูุญ ุงููุฑู ุจูู ุงูุญุงูุฉ ุงูุฃุณุงุณูุฉ Base case ููู `if (n == 0) return 1;` ูุงูุญุงูุฉ ุงูุนูุฏูุฉ Recursive case.
|
| 948 |
+
ููุฌุฏ ู
ุฎุทุท ุจุตุฑู ุนูู ุดูู ุตูุงุฏูู ููุฏ ู
ุชุชุงุจุนุฉ ู
ุน ุณูู
ูุฑุฏู ูุฑุจุท ุงูุงุณุชุฏุนุงุกุงุช ูููุถุญ ุงููุตูู ุฅูู ุงูุญุงูุฉ ุงูุฃุณุงุณูุฉ ุซู
ุงูุฑุฌูุน ุจุงููุชูุฌุฉ.
|
| 949 |
+
|
| 950 |
+
|
| 951 |
+
================================================================================
|
| 952 |
+
PAGE 30 - page_0030.png
|
| 953 |
+
================================================================================
|
| 954 |
+
|
| 955 |
+
------------TEXT EXTRACTED------------
|
| 956 |
+
int factorial(int 3) {
|
| 957 |
+
if (n == 0) return 1; // Base case
|
| 958 |
+
return 3 * factorial(2); // Recursive case
|
| 959 |
+
}
|
| 960 |
+
|
| 961 |
+
int factorial(int 2) {
|
| 962 |
+
if (n == 0) return 1; // Base case
|
| 963 |
+
return 2 * factorial(1); // Recursive case
|
| 964 |
+
}
|
| 965 |
+
|
| 966 |
+
int factorial(int 1) {
|
| 967 |
+
if (n == 0) return 1; // Base case
|
| 968 |
+
return 1; // Recursive case
|
| 969 |
+
}
|
| 970 |
+
|
| 971 |
+
------------PAGE TALK ABOUT------------
|
| 972 |
+
ุชูุถุญ ุงูุตูุญุฉ ู
ูููู
ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู (Recursion) ู
ู ุฎูุงู ู
ุซุงู ุฏุงูุฉ factorial.
|
| 973 |
+
ุชูุธูุฑ ุงูู
ุฑุจุนุงุช ุงูุซูุงุซุฉ ุชุชุจุน ุชูููุฐ ุงูุฏุงูุฉ ููููู
3 ุซู
2 ุซู
1ุ ู
ุน ูุฌูุฏ ุญุงูุฉ ุฃุณุงุณูุฉ ูุญุงูุฉ ุชูุฑุงุฑูุฉ.
|
| 974 |
+
ุงูุฃุณูู
ุงููุฑุฏูุฉ ุจูู ุงูู
ุฑุจุนุงุช ุชุจููู ุงูุชูุงู ุงูุงุณุชุฏุนุงุกุงุช ุจูู ุงูุฏูุงู ุจุดูู ู
ุชุณูุณู.
|
| 975 |
+
ุงูููุฑุฉ ุงูุนุงู
ุฉ ูู ุดุฑุญ ููู ุชูุญุณุจ factorial ุฎุทูุฉ ุจุฎุทูุฉ ุญุชู ุงููุตูู ุฅูู ุงูููู
ุฉ ุงูููุงุฆูุฉ.
|
| 976 |
+
|
| 977 |
+
|
| 978 |
+
================================================================================
|
| 979 |
+
PAGE 31 - page_0031.png
|
| 980 |
+
================================================================================
|
| 981 |
+
|
| 982 |
+
------------TEXT EXTRACTED------------
|
| 983 |
+
int factorial(int 3) {
|
| 984 |
+
if (n == 0) return 1; // Base case
|
| 985 |
+
return 3 * factorial(2); // Recursive case
|
| 986 |
+
}
|
| 987 |
+
|
| 988 |
+
int factorial(int 2) {
|
| 989 |
+
if (n == 0) return 1; // Base case
|
| 990 |
+
return 2 * 1 // Recursive case
|
| 991 |
+
}
|
| 992 |
+
|
| 993 |
+
int factorial(int 1) {
|
| 994 |
+
if (n == 0) return 1; // Base case
|
| 995 |
+
return 1; // Recursive case
|
| 996 |
+
}
|
| 997 |
+
|
| 998 |
+
------------PAGE TALK ABOUT------------
|
| 999 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุดุฑุญ ู
ูููู
ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู (Recursion) ุจุงุณุชุฎุฏุงู
ุฏุงูุฉ factorial.
|
| 1000 |
+
ุชูุธูุฑ ุงูุฃู
ุซูุฉ ููู ูุชู
ุญุณุงุจ !3 ุนุจุฑ ุงุณุชุฏุนุงุก factorial(2) ุซู
factorial(1) ุญุชู ุงููุตูู ุฅูู ุงูุญุงูุฉ ุงูุฃุณุงุณูุฉ.
|
| 1001 |
+
ููุงู ุฃุณูู
ู
ูุญููุฉ ุจุงูููู ุงููุฑุฏู ุชุฑุจุท ุจูู ู
ุฑุจุนุงุช ุงูุดููุฑุฉ ูุชูุถูุญ ุชุณูุณู ุงูุงุณุชุฏุนุงุกุงุช ูุนูุฏุฉ ุงูููู
.
|
| 1002 |
+
ุงูุฑูู
1 ุจุงูููู ุงูุฃุญู
ุฑ ูุจุฑุฒ ุงูููู
ุฉ ุงูู
ุนุงุฏุฉ ูู ุงูู
ุฑุงุญู ุงูุฃุฎูุฑุฉ ู
ู ุงูุชูููุฐ.
|
| 1003 |
+
|
| 1004 |
+
|
| 1005 |
+
================================================================================
|
| 1006 |
+
PAGE 32 - page_0032.png
|
| 1007 |
+
================================================================================
|
| 1008 |
+
|
| 1009 |
+
------------TEXT EXTRACTED------------
|
| 1010 |
+
int factorial(int 3) {
|
| 1011 |
+
if (n == 0) return 1; // Base case
|
| 1012 |
+
return 3 * factorial(2); // Recursive case
|
| 1013 |
+
}
|
| 1014 |
+
|
| 1015 |
+
int factorial(int 2) {
|
| 1016 |
+
if (n == 0) return 1; // Base case
|
| 1017 |
+
return 2 // Recursive case
|
| 1018 |
+
}
|
| 1019 |
+
|
| 1020 |
+
int factorial(int 1) {
|
| 1021 |
+
if (n == 0) return 1; // Base case
|
| 1022 |
+
return 1; // Recursive case
|
| 1023 |
+
}
|
| 1024 |
+
|
| 1025 |
+
------------PAGE TALK ABOUT------------
|
| 1026 |
+
ุชูุถุญ ุงูุตูุญุฉ ููุฑุฉ ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู (Recursion) ูู ุฏุงูุฉ factorial ู
ู ุฎูุงู ุชุชุจุน ุงูููู
3 ุซู
2 ุซู
1.
|
| 1027 |
+
ููุฌุฏ ุซูุงุซ ูุชู ููุฏ ุชู
ุซู ู
ุฑุงุญู ู
ุฎุชููุฉ ู
ู ุงุณุชุฏุนุงุก ุงูุฏุงูุฉุ ู
ุน ุชู
ููุฒ ุญุงูุฉ ุงูุฃุณุงุณ ูุญุงูุฉ ุงูุงุณุชุฏุนุงุก ุงูุชูุฑุงุฑู.
|
| 1028 |
+
ุงูุฃุณูู
ุงููุฑุฏูุฉ ุงูู
ูุญููุฉ ุจูู ุงููุชู ุชุดุฑุญ ุงูุชูุงู ุงูุชูููุฐ ู
ู ุงุณุชุฏุนุงุก ุฅูู ุขุฎุฑ ุจุดูู ุจุตุฑู.
|
| 1029 |
+
ุงููุฏู ู
ู ุงูุฑุณู
ูู ุชุจุณูุท ููููุฉ ูุฒูู ุงูุฏุงูุฉ ุชุฏุฑูุฌููุง ุญุชู ุชุตู ุฅูู ุงูุญุงูุฉ ุงูุฃุณุงุณูุฉ ุซู
ุชุนูุฏ ุจุงููุชุงุฆุฌ.
|
| 1030 |
+
|
| 1031 |
+
|
| 1032 |
+
================================================================================
|
| 1033 |
+
PAGE 33 - page_0033.png
|
| 1034 |
+
================================================================================
|
| 1035 |
+
|
| 1036 |
+
------------TEXT EXTRACTED------------
|
| 1037 |
+
int factorial(int 3) {
|
| 1038 |
+
if (n == 0) return 1; // Base case
|
| 1039 |
+
return 3 * 2; // Recursive case
|
| 1040 |
+
|
| 1041 |
+
int factorial(int 2) {
|
| 1042 |
+
if (n == 0) return 1; // Base case
|
| 1043 |
+
return 2 // Recursive case
|
| 1044 |
+
}
|
| 1045 |
+
|
| 1046 |
+
------------PAGE TALK ABOUT------------
|
| 1047 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุดุฑุญ ู
ูููู
ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู (Recursion) ูู ุฏุงูุฉ factorial.
|
| 1048 |
+
ุชูุธูุฑ ู
ุฑุจุนุงุช ููุฏ ุชู
ุซู ุงุณุชุฏุนุงุก ุงูุฏุงูุฉ ุนูุฏ ุงูููู
ุชูู 3 ู2 ู
ุน ุชูุถูุญ ุญุงูุฉ ุงูุฃุณุงุณ ูุญุงูุฉ ุงูุชูุฑุงุฑ.
|
| 1049 |
+
ููุฌุฏ ุณูู
ูุฑุฏู ูุจูุฑ ูุฑุจุท ุจูู ุงูู
ุฑุจุนูู ูููุถุญ ุงูุชูุงู ุงูุชูููุฐ ู
ู factorial(3) ุฅูู factorial(2).
|
| 1050 |
+
ุงููุฏู ุงูุจุตุฑู ูู ุจูุงู ููู ุชุณุชุฏุนู ุงูุฏุงูุฉ ููุณูุง ุชุฏุฑูุฌููุง ุญุชู ุชุตู ุฅูู ุญุงูุฉ ุงูุฃุณุงุณ.
|
| 1051 |
+
|
| 1052 |
+
|
| 1053 |
+
================================================================================
|
| 1054 |
+
PAGE 34 - page_0034.png
|
| 1055 |
+
================================================================================
|
| 1056 |
+
|
| 1057 |
+
------------TEXT EXTRACTED------------
|
| 1058 |
+
int factorial(int 3) {
|
| 1059 |
+
if (n == 0) return 1; // Base case
|
| 1060 |
+
return 6; // Recursive case
|
| 1061 |
+
}
|
| 1062 |
+
|
| 1063 |
+
------------PAGE TALK ABOUT------------
|
| 1064 |
+
ุงูุตูุญุฉ ุชุนุฑุถ ู
ุซุงููุง ุจุฑู
ุฌููุง ูุฏุงูุฉ factorial ู
ูุชูุจุฉ ุจูุบุฉ ุดุจููุฉ ุจู C/C++ ูุดุฑุญ ู
ูููู
ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู (Recursion).
|
| 1065 |
+
ููุฌุฏ ุชูุถูุญ ูุญุงูุฉ ุงูุฃุณุงุณ `Base case` ุนูุฏู
ุง ุชููู ููู
ุฉ `n` ุตูุฑูุงุ ูุญุงูุฉ ุงูุงุณุชุฏุนุงุก ุงูุชูุฑุงุฑู `Recursive case`.
|
| 1066 |
+
ุชุธูุฑ ุจุนุถ ุงูุฃุฌุฒุงุก ู
ู
ูุฒุฉ ุจุงูุฃููุงูุ ู
ุซู ุงูุฑูู
3 ูู ุชุนุฑูู ุงูุฏุงูุฉ ูุงูุฑูู
6 ูู ุฌู
ูุฉ ุงูุฅุฑุฌุงุน ุจุงูููู ุงูุฃุญู
ุฑ.
|
| 1067 |
+
ุงูุฎูููุฉ ุจุณูุทุฉ ุฌุฏูุง ูุชุญุชูู ููุท ุนูู ู
ุฑุจุน ูุงุชุญ ุจุฏุงุฎูู ุงูููุฏ ุฏูู ุฃู ุฑุณูู
ุฃู ู
ุฎุทุทุงุช ุฅุถุงููุฉ.
|
| 1068 |
+
|
| 1069 |
+
|
| 1070 |
+
================================================================================
|
| 1071 |
+
PAGE 35 - page_0035.png
|
| 1072 |
+
================================================================================
|
| 1073 |
+
|
| 1074 |
+
------------TEXT EXTRACTED------------
|
| 1075 |
+
Fibonacci Numbers
|
| 1076 |
+
|
| 1077 |
+
โข The Nแตสฐ Fibonacci number is the sum of the previous two Fibonacci numbers
|
| 1078 |
+
|
| 1079 |
+
โข 0, 1, 1, 2, 3, 5, 8, 13, ...
|
| 1080 |
+
|
| 1081 |
+
โข Recursive Design:
|
| 1082 |
+
โข Decomposition & Composition
|
| 1083 |
+
โข fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)
|
| 1084 |
+
โข Base case:
|
| 1085 |
+
โข fibonacci(1) = 0
|
| 1086 |
+
โข fibonacci(2) = 1
|
| 1087 |
+
|
| 1088 |
+
------------PAGE TALK ABOUT------------
|
| 1089 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ุชุชุงููุฉ ููุจููุงุชุดู ูููููุฉ ุญุณุงุจ ุงูุญุฏ ุงููููู ูููุง ุจุงุนุชุจุงุฑู ู
ุฌู
ูุน ุงูุญุฏูู ุงูุณุงุจููู.
|
| 1090 |
+
ุชูุถุญ ุฃู
ุซูุฉ ูุฃูู ุฃุนุฏุงุฏ ุงูู
ุชุชุงููุฉ ู
ุซู: 0ุ 1ุ 1ุ 2ุ 3ุ 5ุ 8ุ 13.
|
| 1091 |
+
ูู
ุง ุชุดุฑุญ ุงูุชุตู
ูู
ุงูุชูุฑุงุฑู (Recursive Design) ู
ู ุฎูุงู ุงูุนูุงูุฉ: fibonacci(n) = fibonacci(n-1) + fibonacci(n-2).
|
| 1092 |
+
ูุชุนุฑุถ ุญุงูุชู ุงูุฃุณุงุณ ูุจุฏุก ุงูุงุณุชุฏุนุงุก ุงูุชูุฑุงุฑู: fibonacci(1)=0 ู fibonacci(2)=1.
|
| 1093 |
+
|
| 1094 |
+
|
| 1095 |
+
================================================================================
|
| 1096 |
+
PAGE 36 - page_0036.png
|
| 1097 |
+
================================================================================
|
| 1098 |
+
|
| 1099 |
+
------------TEXT EXTRACTED------------
|
| 1100 |
+
Fibonacci Numbers
|
| 1101 |
+
|
| 1102 |
+
Fibonacci Sequence Definition
|
| 1103 |
+
|
| 1104 |
+
The Fibonacci sequence is defined as:
|
| 1105 |
+
โข fib(0) = 0
|
| 1106 |
+
โข fib(1) = 1
|
| 1107 |
+
โข fib(n) = fib(n-1) + fib(n-2) for n > 1
|
| 1108 |
+
|
| 1109 |
+
For n = 4, the expected Fibonacci number is 3 (since the sequence is: 0, 1, 1, 2, 3).
|
| 1110 |
+
|
| 1111 |
+
#include <stdio.h>
|
| 1112 |
+
int fibonacci(int n) {
|
| 1113 |
+
if (n <= 1) return n; // Base case
|
| 1114 |
+
return fibonacci(n - 1) + fibonacci(n - 2); // Binary recursive call
|
| 1115 |
+
}
|
| 1116 |
+
|
| 1117 |
+
int main() {
|
| 1118 |
+
int n = 4;
|
| 1119 |
+
printf("Fibonacci number at position %d is %d\n", n, fibonacci(n));
|
| 1120 |
+
return 0;
|
| 1121 |
+
}
|
| 1122 |
+
|
| 1123 |
+
------------PAGE TALK ABOUT------------
|
| 1124 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ุชุชุงููุฉ ููุจููุงุชุดู ูุชุนุฑูููุง ุงูุฑูุงุถู ุจุงุณุชุฎุฏุงู
ุงูููู
ุงูุฃุณุงุณูุฉ fib(0)=0 ู fib(1)=1ุ ุซู
ุงูุนูุงูุฉ ุงูุชูุฑุงุฑูุฉ ูุจุงูู ุงูุญุฏูุฏ.
|
| 1125 |
+
ูู
ุง ุชุนุฑุถ ู
ุซุงููุง ุนูุฏ n = 4 ุญูุซ ุชููู ุงููุชูุฌุฉ 3ุ ู
ุน ุชูุถูุญ ุชุณูุณู ุงูุฃุนุฏุงุฏ: 0ุ 1ุ 1ุ 2ุ 3.
|
| 1126 |
+
ููุฌุฏ ูู ุงูุฌูุฉ ุงููู
ูู ู
ุฑุจุน ุจุตุฑู ูุญุชูู ุนูู ููุฏ ุจูุบุฉ C ูุทุจู ุฏุงูุฉ fibonacci ุจุทุฑููุฉ ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู recursion.
|
| 1127 |
+
ุงูุชุตู
ูู
ุจุณูุท ุชุนููู
ูุ ุจุนููุงู ูุจูุฑ ูู ุงูุฃุนูู ููุตูุต ุดุฑุญ ุนูู ุงููุณุงุฑ ูููุฏ ุจุฑู
ุฌู ู
ูุณู ุนูู ุงููู
ูู.
|
| 1128 |
+
|
| 1129 |
+
|
| 1130 |
+
================================================================================
|
| 1131 |
+
PAGE 37 - page_0037.png
|
| 1132 |
+
================================================================================
|
| 1133 |
+
|
| 1134 |
+
------------TEXT EXTRACTED------------
|
| 1135 |
+
Fibonacci Numbers
|
| 1136 |
+
|
| 1137 |
+
Recursive Execution Tree for fibonacci(4)
|
| 1138 |
+
|
| 1139 |
+
1.Initial Call: fibonacci(4)
|
| 1140 |
+
โขSince 4 > 1, it calls fibonacci(3) + fibonacci(2).
|
| 1141 |
+
|
| 1142 |
+
2.First Subtree: fibonacci(3)
|
| 1143 |
+
|
| 1144 |
+
โข3 > 1 โ Calls fibonacci(2) + fibonacci(1).
|
| 1145 |
+
โขfibonacci(2) โ Calls fibonacci(1) + fibonacci(0).
|
| 1146 |
+
โขfibonacci(1) โ Returns 1 (base case).
|
| 1147 |
+
โขfibonacci(0) โ Returns 0 (base case).
|
| 1148 |
+
โขResult: 1 + 0 = 1.
|
| 1149 |
+
โขfibonacci(1) โ Returns 1.
|
| 1150 |
+
โขResult: 1 (from fib(2)) + 1 (from fib(1)) = 2.
|
| 1151 |
+
|
| 1152 |
+
3.Second Subtree: fibonacci(2)
|
| 1153 |
+
โข2 > 1 โ Calls fibonacci(1) + fibonacci(0).
|
| 1154 |
+
|
| 1155 |
+
โขfibonacci(1) โ Returns 1.
|
| 1156 |
+
โขfibonacci(0) โ Returns 0.
|
| 1157 |
+
โขResult: 1 + 0 = 1.
|
| 1158 |
+
|
| 1159 |
+
4.Final Sum:
|
| 1160 |
+
โขfibonacci(4) = fibonacci(3) + fibonacci(2) = 2 + 1 = 3.
|
| 1161 |
+
|
| 1162 |
+
#include <stdio.h>
|
| 1163 |
+
int fibonacci(int n) {
|
| 1164 |
+
if (n <= 1) return n; // Base case
|
| 1165 |
+
return fibonacci(n - 1) + fibonacci(n - 2); // Binary recursive call
|
| 1166 |
+
}
|
| 1167 |
+
|
| 1168 |
+
int main() {
|
| 1169 |
+
int n = 4;
|
| 1170 |
+
printf("Fibonacci number at position %d is %d\n", n, fibonacci(n));
|
| 1171 |
+
return 0;
|
| 1172 |
+
}
|
| 1173 |
+
|
| 1174 |
+
------------PAGE TALK ABOUT------------
|
| 1175 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุดุฑุญ ุฃุนุฏุงุฏ ููุจููุงุชุดู ุจุงุณุชุฎุฏุงู
ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู recursion ูุญุณุงุจ fibonacci(4).
|
| 1176 |
+
ุชูุถุญ ุฎุทูุงุช ุงูุชูููุฐ ุนูู ุดูู ุดุฌุฑุฉ ุงุณุชุฏุนุงุก: ุชุจุฏุฃ ู
ู fibonacci(4)ุ ุซู
ุชุชูุฑุน ุฅูู fibonacci(3) ู fibonacci(2)ุ ู
ุน ุงููุตูู ุฅูู ุญุงูุชู ุงูุฃุณุงุณ fibonacci(1) ู fibonacci(0).
|
| 1177 |
+
ูู
ุง ุชุนุฑุถ ููุฏ ุจูุบุฉ C ูุนุฑูู ุฏุงูุฉ fibonacci ููุทุจุน ุงููุงุชุฌ ุนูุฏ n = 4.
|
| 1178 |
+
ุจุตุฑููุงุ ุงูุตูุญุฉ ู
ูุณูู
ุฉ ุฅูู ูุณู
ูู: ุงููุณุงุฑ ููุดุฑุญ ุงููุตู ุงูู
ุชุณูุณูุ ูุงููู
ูู ูุนุฑุถ ุงูููุฏ ุงูุจุฑู
ุฌู ุจุฃููุงู ูุงุชุญุฉ.
|
| 1179 |
+
|
| 1180 |
+
|
| 1181 |
+
================================================================================
|
| 1182 |
+
PAGE 38 - page_0038.png
|
| 1183 |
+
================================================================================
|
| 1184 |
+
|
| 1185 |
+
------------TEXT EXTRACTED------------
|
| 1186 |
+
Fibonacci Numbers
|
| 1187 |
+
|
| 1188 |
+
#include <stdio.h>
|
| 1189 |
+
|
| 1190 |
+
int fibonacci(int n) {
|
| 1191 |
+
if (n <= 1) return n; // Base case
|
| 1192 |
+
return fibonacci(n - 1) + fibonacci(n - 2); // Binary recursive call
|
| 1193 |
+
}
|
| 1194 |
+
|
| 1195 |
+
int main() {
|
| 1196 |
+
int n = 4;
|
| 1197 |
+
printf("Fibonacci number at position %d is %d\n", n, fibonacci(n));
|
| 1198 |
+
return 0;
|
| 1199 |
+
}
|
| 1200 |
+
|
| 1201 |
+
Visualization of Recursive Calls
|
| 1202 |
+
|
| 1203 |
+
fib(4)
|
| 1204 |
+
= fib(3) + fib(2)
|
| 1205 |
+
= [fib(2) + fib(1)] + [fib(1) + fib(0)]
|
| 1206 |
+
= [ (fib(1) + fib(0)) + 1 ] + [1 + 0]
|
| 1207 |
+
= [ (1 + 0) + 1 ] + 1
|
| 1208 |
+
= 2 + 1
|
| 1209 |
+
= 3
|
| 1210 |
+
|
| 1211 |
+
------------PAGE TALK ABOUT------------
|
| 1212 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ุฃุฑูุงู
ููุจููุงุชุดู ุจุงุณุชุฎุฏุงู
ุงูุจุฑู
ุฌุฉ ุงูุชูุฑุงุฑูุฉ ุงูุงุณุชุฏุนุงุฆูุฉ (Recursion) ุจูุบุฉ C.
|
| 1213 |
+
ุชุญุชูู ุนูู ุฏุงูุฉ `fibonacci(n)` ู
ุน ุญุงูุฉ ุฃุณุงุณูุฉ ุนูุฏู
ุง ูููู `n <= 1`ุ ุซู
ุชุณุชุฏุนู ููุณูุง ู
ุฑุชูู ูุญุณุงุจ `fibonacci(n-1)` ู`fibonacci(n-2)`.
|
| 1214 |
+
ููุฌุฏ ู
ุซุงู ูู `main` ุญูุซ `n = 4` ููุชู
ุทุจุงุนุฉ ุงููุงุชุฌุ ูุงููุชูุฌุฉ ุงูููุงุฆูุฉ ูู `3`.
|
| 1215 |
+
ูู
ุง ุชุนุฑุถ ุงูุตูุญุฉ ุชุตูุฑูุง ูุตููุง ูุชุณูุณู ุงูุงุณุชุฏุนุงุกุงุช ุงูุชูุฑุงุฑูุฉ ูู `fib(4)` ุฎุทูุฉ ุจุฎุทูุฉ ุญุชู ุงููุตูู ุฅูู ุงูููู
ุงูุฃุณุงุณูุฉ `fib(1)` ู`fib(0)`.
|
| 1216 |
+
|
| 1217 |
+
|
| 1218 |
+
================================================================================
|
| 1219 |
+
PAGE 39 - page_0039.png
|
| 1220 |
+
================================================================================
|
| 1221 |
+
|
| 1222 |
+
------------TEXT EXTRACTED------------
|
| 1223 |
+
Types of Recursion
|
| 1224 |
+
|
| 1225 |
+
โข Linear Recursion:
|
| 1226 |
+
โข The function makes a single call to itself.
|
| 1227 |
+
โข Example: Factorial calculation.
|
| 1228 |
+
|
| 1229 |
+
โข Tail Recursion:
|
| 1230 |
+
โข The recursive call is the last operation in the function.
|
| 1231 |
+
โข Example: GCD calculation.
|
| 1232 |
+
|
| 1233 |
+
โข Binary Recursion:
|
| 1234 |
+
โข The function makes two recursive calls.
|
| 1235 |
+
โข Example: Fibonacci sequence.
|
| 1236 |
+
|
| 1237 |
+
โข Mutual Recursion:
|
| 1238 |
+
โข Two or more functions call each other.
|
| 1239 |
+
โข Example: Determining if a number is even or odd.
|
| 1240 |
+
|
| 1241 |
+
------------PAGE TALK ABOUT------------
|
| 1242 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุฃููุงุน ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู (Recursion) ูู ุงูุจุฑู
ุฌุฉ.
|
| 1243 |
+
ุชูุถุญ ุฃุฑุจุนุฉ ุฃููุงุน ุฑุฆูุณูุฉ: ุงูุงุณุชุฏุนุงุก ุงูุฎุทูุ ูุงูุฐููููุ ูุงูุซูุงุฆูุ ูุงูู
ุชุจุงุฏูุ ู
ุน ุชุนุฑูู ู
ุฎุชุตุฑ ููู ููุน.
|
| 1244 |
+
ูู
ุง ุชุนุฑุถ ุฃู
ุซูุฉ ุชุทุจูููุฉ ู
ุซู ุญุณุงุจ ุงูู
ุถุฑูุจุ ุงููุงุณู
ุงูู
ุดุชุฑู ุงูุฃูุจุฑุ ูู
ุชุชุงููุฉ ููุจููุงุชุดูุ ูุชุญุฏูุฏ ุงูุฒูุฌู ุฃู ุงููุฑุฏู.
|
| 1245 |
+
ุงูุนูุตุฑ ุงูุจุตุฑู ุนุจุงุฑุฉ ุนู ุดุฑูุญุฉ ุชุนููู
ูุฉ ุจุณูุทุฉ ุจุฎูููุฉ ุจูุถุงุก ููุตูุต ูุจูุฑุฉ ูุนูุงููู ููุทูุฉ ุฏูู ุตูุฑ ุฃู ู
ุฎุทุทุงุช.
|
| 1246 |
+
|
| 1247 |
+
|
| 1248 |
+
================================================================================
|
| 1249 |
+
PAGE 40 - page_0040.png
|
| 1250 |
+
================================================================================
|
| 1251 |
+
|
| 1252 |
+
------------TEXT EXTRACTED------------
|
| 1253 |
+
Stack Overheads in Recursion
|
| 1254 |
+
|
| 1255 |
+
What are Stack Overheads?
|
| 1256 |
+
|
| 1257 |
+
โข Each recursive call consumes stack space.
|
| 1258 |
+
โข Excessive recursion can lead to stack overflow.
|
| 1259 |
+
|
| 1260 |
+
Example:
|
| 1261 |
+
|
| 1262 |
+
#include <stdio.h>
|
| 1263 |
+
|
| 1264 |
+
void recursiveFunction(int n) {
|
| 1265 |
+
if (n == 0)
|
| 1266 |
+
return;
|
| 1267 |
+
printf("Depth: %d\n", n);
|
| 1268 |
+
recursiveFunction(n - 1);
|
| 1269 |
+
}
|
| 1270 |
+
|
| 1271 |
+
int main() {
|
| 1272 |
+
recursiveFunction(5);
|
| 1273 |
+
return 0;
|
| 1274 |
+
}
|
| 1275 |
+
|
| 1276 |
+
Explanation:
|
| 1277 |
+
|
| 1278 |
+
โข Each call to recursiveFunction adds a new stack frame.
|
| 1279 |
+
โข The stack grows with each recursive call.
|
| 1280 |
+
|
| 1281 |
+
------------PAGE TALK ABOUT------------
|
| 1282 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุงูุญู
ู ุงูุฒุงุฆุฏ ุนูู ุงูู
ูุฏุณ ุนูุฏ ุงุณุชุฎุฏุงู
ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู (Recursion)ุ ูุฃู ูู ุงุณุชุฏุนุงุก ุฌุฏูุฏ ูุณุชููู ู
ุณุงุญุฉ ู
ู ุงูุฐุงูุฑุฉ.
|
| 1283 |
+
ุชูุถุญ ุฃู ุงูุชูุฑุงุฑ ุงูุนู
ูู ุฃู ุงูู
ูุฑุท ูุฏ ูุคุฏู ุฅูู ุญุฏูุซ Stack Overflow.
|
| 1284 |
+
ููุฌุฏ ู
ุซุงู ุจุฑู
ุฌู ุจูุบุฉ C ูุฏุงูุฉ recursiveFunction ุชุณุชุฏุนู ููุณูุง ุญุชู ุชุตู ุฅูู n == 0.
|
| 1285 |
+
ูู
ุง ุชุนุฑุถ ุงูุตูุญุฉ ุดุฑุญูุง ุฌุงูุจููุง ูุจูู ุฃู ูู ุงุณุชุฏุนุงุก ูุถูู stack frame ุฌุฏูุฏูุงุ ู
ุน ุชุตู
ูู
ุชุนููู
ู ุจุณูุท ูุญุชูู ุนูู ุนููุงู ูุจูุฑ ูููุฏ ุฏุงุฎู ุตูุฏูู ูุงุชุญ ุงูููู.
|
| 1286 |
+
|
| 1287 |
+
|
| 1288 |
+
================================================================================
|
| 1289 |
+
PAGE 41 - page_0041.png
|
| 1290 |
+
================================================================================
|
| 1291 |
+
|
| 1292 |
+
------------TEXT EXTRACTED------------
|
| 1293 |
+
Key Points to Remember
|
| 1294 |
+
|
| 1295 |
+
1. Functions provide modularity and reusability.
|
| 1296 |
+
|
| 1297 |
+
2. Parameters can be passed by value or by reference.
|
| 1298 |
+
|
| 1299 |
+
3. Recursion is a powerful technique but can lead to stack overheads.
|
| 1300 |
+
|
| 1301 |
+
4. Tail recursion can be optimized by the compiler.
|
| 1302 |
+
|
| 1303 |
+
------------PAGE TALK ABOUT------------
|
| 1304 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุฃูู
ุงูููุงุท ุงูุชู ูุฌุจ ุชุฐูุฑูุง ูู ุงูุจุฑู
ุฌุฉุ ุฎุงุตุฉ ู
ุง ูุชุนูู ุจุงูุฏูุงู ูุชู
ุฑูุฑ ุงูู
ุนุงู
ูุงุช ูุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู.
|
| 1305 |
+
ุชูุถุญ ุฃู ุงูุฏูุงู ุชุณุงุนุฏ ุนูู ุชูุธูู
ุงูููุฏ ูุฅุนุงุฏุฉ ุงุณุชุฎุฏุงู
ูุ ูุฃู ุงูู
ุนุงู
ูุงุช ูู
ูู ุชู
ุฑูุฑูุง ุจุงูููู
ุฉ ุฃู ุจุงูู
ุฑุฌุน.
|
| 1306 |
+
ูู
ุง ุชุดูุฑ ุฅูู ุฃู recursion ุชูููุฉ ูููุฉ ููููุง ูุฏ ุชุณุจุจ ุงุณุชููุงููุง ุฅุถุงูููุง ููู stack.
|
| 1307 |
+
ูุชุฐูุฑ ุฃูุถูุง ุฃู tail recursion ูุฏ ูุชู
ุชุญุณููู ุจูุงุณุทุฉ ุงูู
ุชุฑุฌู
.
|
| 1308 |
+
ุงูุชุตู
ูู
ุจุตุฑููุง ุจุณูุท ุฌุฏูุง: ุนููุงู ูุจูุฑ ูู ุงูุฃุนูู ู
ุน ูุงุฆู
ุฉ ู
ุฑูู
ุฉ ุนูู ุฎูููุฉ ุจูุถุงุก ุฏูู ุตูุฑ ุฃู ู
ุฎุทุทุงุช.
|
| 1309 |
+
|
| 1310 |
+
|
| 1311 |
+
================================================================================
|
| 1312 |
+
PAGE 42 - page_0042.png
|
| 1313 |
+
================================================================================
|
| 1314 |
+
|
| 1315 |
+
------------TEXT EXTRACTED------------
|
| 1316 |
+
Exercises
|
| 1317 |
+
|
| 1318 |
+
1. Write a recursive function to calculate the sum of digits of a number.
|
| 1319 |
+
2. Write a program to GCD Calculation Using Recursion
|
| 1320 |
+
3. Write a recursive function to calculate the power of a number.
|
| 1321 |
+
4. Write two mutually recursive functions to determine if a number is a multiple of 3 or 5
|
| 1322 |
+
|
| 1323 |
+
------------PAGE TALK ABOUT------------
|
| 1324 |
+
ุงูุตูุญุฉ ุชุนุฑุถ ุดุฑูุญุฉ ุจุนููุงู "Exercises" ุชุญุชูู ุนูู ุฃุฑุจุนุฉ ุชู
ุงุฑูู ุนู ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู (Recursion).
|
| 1325 |
+
ุงูุชู
ุงุฑูู ุชุดู
ู: ุญุณุงุจ ู
ุฌู
ูุน ุฃุฑูุงู
ุนุฏุฏุ ุฅูุฌุงุฏ ุงููุงุณู
ุงูู
ุดุชุฑู ุงูุฃูุจุฑ GCD ุจุงุณุชุฎุฏุงู
recursionุ ูุญุณุงุจ ููุฉ ุนุฏุฏ.
|
| 1326 |
+
ูู
ุง ุชุชุถู
ู ุชู
ุฑูููุง ุนู ุงูุฏูุงู ุฐุงุช ุงูุงุณุชุฏุนุงุก ุงูู
ุชุจุงุฏู ูุชุญุฏูุฏ ู
ุง ุฅุฐุง ูุงู ุงูุนุฏุฏ ู
ู ู
ุถุงุนูุงุช 3 ุฃู 5.
|
| 1327 |
+
ุงูุชุตู
ูู
ุจุณูุท ุฌุฏูุง: ุฎูููุฉ ุจูุถุงุกุ ุนููุงู ูุจูุฑ ูู ุงูุฃุนููุ ููุงุฆู
ุฉ ู
ุฑูู
ุฉ ุจุฏูู ุตูุฑ ุฃู ู
ุฎุทุทุงุช.
|
| 1328 |
+
|
| 1329 |
+
|
| 1330 |
+
================================================================================
|
| 1331 |
+
PAGE 43 - page_0043.png
|
| 1332 |
+
================================================================================
|
| 1333 |
+
|
| 1334 |
+
------------TEXT EXTRACTED------------
|
| 1335 |
+
Solutions
|
| 1336 |
+
|
| 1337 |
+
------------PAGE TALK ABOUT------------
|
| 1338 |
+
ูุฐู ุงูุตูุญุฉ ุนุจุงุฑุฉ ุนู ุดุฑูุญุฉ ุนููุงู ุจุณูุทุฉ ุชุดูุฑ ุฅูู ูุณู
**ุงูุญููู**.
|
| 1339 |
+
ูุง ุชุญุชูู ุนูู ุฃู ุดุฑุญ ุฅุถุงูู ุฃู ู
ุนุงุฏูุงุช ุฃู ุนูุงุตุฑ ุชุนููู
ูุฉ ุฃุฎุฑู.
|
| 1340 |
+
ุงูุฎูููุฉ ุจูุถุงุก ุจุงููุงู
ูุ ููู ุงูู
ูุชุตู ููู
ุฉ **Solutions** ุจุฎุท ุฃุณูุฏ ูุงุถุญ.
|
| 1341 |
+
ูุง ุชูุฌุฏ ุตูุฑ ุฃู ู
ุฎุทุทุงุช ุฃู ุฑุณูู
ุจูุงููุฉ ูู ุงูุตูุญุฉ.
|
| 1342 |
+
|
| 1343 |
+
|
| 1344 |
+
================================================================================
|
| 1345 |
+
PAGE 44 - page_0044.png
|
| 1346 |
+
================================================================================
|
| 1347 |
+
|
| 1348 |
+
------------TEXT EXTRACTED------------
|
| 1349 |
+
Sum of Digits
|
| 1350 |
+
|
| 1351 |
+
#include <stdio.h>
|
| 1352 |
+
|
| 1353 |
+
int sumOfDigits(int n) {
|
| 1354 |
+
if (n == 0) return 0; // Base case
|
| 1355 |
+
return (n % 10) + sumOfDigits(n / 10); // Linear recursive call
|
| 1356 |
+
}
|
| 1357 |
+
|
| 1358 |
+
int main() {
|
| 1359 |
+
int n = 12345;
|
| 1360 |
+
printf("Sum of digits of %d is %d\n", n, sumOfDigits(n));
|
| 1361 |
+
return 0;
|
| 1362 |
+
}
|
| 1363 |
+
|
| 1364 |
+
------------PAGE TALK ABOUT------------
|
| 1365 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุจุฑูุงู
ุฌ ุจูุบุฉ C ูุญุณุงุจ ู
ุฌู
ูุน ุฃุฑูุงู
ุนุฏุฏ ุตุญูุญ ุจุงุณุชุฎุฏุงู
ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู (Recursion).
|
| 1366 |
+
ุชูุถุญ ุงูุฏุงูุฉ `sumOfDigits` ุญุงูุฉ ุงูุชููู ุนูุฏู
ุง ุชุตุจุญ ุงูููู
ุฉ 0ุ ุซู
ุชุฌู
ุน ุขุฎุฑ ุฑูู
`n % 10` ู
ุน ูุงุชุฌ ุงุณุชุฏุนุงุก ุงูุฏุงูุฉ ุนูู `n / 10`.
|
| 1367 |
+
ููุฌุฏ ุฃูุถูุง ู
ุซุงู ูู `main` ูุณุชุฎุฏู
ุงูุนุฏุฏ 12345 ููุทุจุน ู
ุฌู
ูุน ุฃุฑูุงู
ู.
|
| 1368 |
+
ุงูุนูุตุฑ ุงูุจุตุฑู ุนุจุงุฑุฉ ุนู ุตูุญุฉ ุจุณูุทุฉ ุชุญุชูู ุนูู ุนููุงู "Sum of Digits" ูููุฏ ุจุฑู
ุฌู ููุท ุฏูู ุตูุฑ ุฃู ู
ุฎุทุทุงุช.
|
| 1369 |
+
|
| 1370 |
+
|
| 1371 |
+
================================================================================
|
| 1372 |
+
PAGE 45 - page_0045.png
|
| 1373 |
+
================================================================================
|
| 1374 |
+
|
| 1375 |
+
------------TEXT EXTRACTED------------
|
| 1376 |
+
Recursive Function for GCD
|
| 1377 |
+
|
| 1378 |
+
#include <stdio.h>
|
| 1379 |
+
|
| 1380 |
+
int gcd(int a, int b) {
|
| 1381 |
+
if (b == 0) return a; // Base case
|
| 1382 |
+
return gcd(b, a % b); // Recursive call
|
| 1383 |
+
}
|
| 1384 |
+
|
| 1385 |
+
int main() {
|
| 1386 |
+
int a = 56, b = 98;
|
| 1387 |
+
printf("GCD of %d and %d is %d\n", a, b, gcd(a, b));
|
| 1388 |
+
return 0;
|
| 1389 |
+
}
|
| 1390 |
+
|
| 1391 |
+
Step-by-Step Execution
|
| 1392 |
+
|
| 1393 |
+
โข Input: a = 56, b = 98
|
| 1394 |
+
|
| 1395 |
+
โข Step 1: gcd(56, 98)
|
| 1396 |
+
โข b โ 0, so call gcd(98, 56 % 98) โ gcd(98, 56)
|
| 1397 |
+
|
| 1398 |
+
โข Step 2: gcd(98, 56)
|
| 1399 |
+
โข b โ 0, so call gcd(56, 98 % 56) โ gcd(56, 42)
|
| 1400 |
+
|
| 1401 |
+
โข Step 3: gcd(56, 42)
|
| 1402 |
+
โข b โ 0, so call gcd(42, 56 % 42) โ gcd(42, 14)
|
| 1403 |
+
|
| 1404 |
+
โข Step 4: gcd(42, 14)
|
| 1405 |
+
โข b โ 0, so call gcd(14, 42 % 14) โ gcd(14, 0)
|
| 1406 |
+
|
| 1407 |
+
โข Step 5: gcd(14, 0)
|
| 1408 |
+
โข b == 0, so return 14 (base case).
|
| 1409 |
+
|
| 1410 |
+
โข Final Result: 14
|
| 1411 |
+
|
| 1412 |
+
------------PAGE TALK ABOUT------------
|
| 1413 |
+
ุชุดุฑุญ ุงูุตูุญุฉ ุฏุงูุฉ้ๅฝ ูู ูุบุฉ C ูุฅูุฌุงุฏ ุงููุงุณู
ุงูู
ุดุชุฑู ุงูุฃูุจุฑ (GCD) ุจุงุณุชุฎุฏุงู
ุฎูุงุฑุฒู
ูุฉ ุฅูููุฏุณ.
|
| 1414 |
+
ุชุญุชูู ุนูู ููุฏ ูุงู
ู ููู ุฏุงูุฉ `gcd` ู
ุน ุญุงูุฉ ุฃุณุงุณูุฉ ุนูุฏู
ุง `b == 0`ุ ุซู
ุงุณุชุฏุนุงุก้ๅฝ ุจุงุณุชุฎุฏุงู
`gcd(b, a % b)`.
|
| 1415 |
+
ูู
ุง ุชุนุฑุถ ุชูููุฐูุง ุฎุทูุฉ ุจุฎุทูุฉ ููู
ุฏุฎููู 56 ู98 ุญุชู ุงููุตูู ุฅูู ุงููุงุชุฌ ุงูููุงุฆู 14.
|
| 1416 |
+
ุงูุชุตู
ูู
ู
ูุณูู
ุจุตุฑููุง ุฅูู ูุณู
ูู: ุงูููุฏ ุงูุจุฑู
ุฌู ุนูู ุงููุณุงุฑุ ูุดุฑุญ ุงูุชุชุจุน ุงูู
ุฑุญูู ุจุงูููุงุท ุนูู ุงููู
ูู.
|
| 1417 |
+
|
| 1418 |
+
|
| 1419 |
+
================================================================================
|
| 1420 |
+
PAGE 46 - page_0046.png
|
| 1421 |
+
================================================================================
|
| 1422 |
+
|
| 1423 |
+
------------TEXT EXTRACTED------------
|
| 1424 |
+
Power Calculation
|
| 1425 |
+
|
| 1426 |
+
#include <stdio.h>
|
| 1427 |
+
|
| 1428 |
+
int power(int x, int n) {
|
| 1429 |
+
if (n == 0) return 1; // Base case
|
| 1430 |
+
return x * power(x, n - 1); // Binary recursive call
|
| 1431 |
+
}
|
| 1432 |
+
|
| 1433 |
+
int main() {
|
| 1434 |
+
int x = 2, n = 5;
|
| 1435 |
+
printf("%d^%d is %d\n", x, n, power(x, n));
|
| 1436 |
+
return 0;
|
| 1437 |
+
}
|
| 1438 |
+
|
| 1439 |
+
------------PAGE TALK ABOUT------------
|
| 1440 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุจุฑูุงู
ุฌ ุจูุบุฉ C ูุญุณุงุจ ุงูููุฉ \(x^n\) ุจุงุณุชุฎุฏุงู
ุงูุงุณุชุฏุนุงุก ุงูุฐุงุชู (Recursion).
|
| 1441 |
+
ุชูุถุญ ุงูุฏุงูุฉ `power` ุญุงูุฉ ุงูุฃุณุงุณ ุนูุฏู
ุง ุชููู `n == 0` ูุชุฑุฌุน 1ุ ุซู
ุชุณุชุฏุนู ููุณูุง ูุชูููู ููู
ุฉ `n` ุชุฏุฑูุฌููุง.
|
| 1442 |
+
ูู ุงูุฏุงูุฉ `main` ุชู
ุงุณุชุฎุฏุงู
ุงูููู
`x = 2` ู `n = 5` ุซู
ุทุจุงุนุฉ ุงููุงุชุฌ.
|
| 1443 |
+
ุงูุนูุตุฑ ุงูุจุตุฑู ุงููุญูุฏ ูู ุนููุงู "Power Calculation" ู
ุน ุชูุณูู ูุตู ุจุณูุท ูุนุฑุถ ุงูููุฏ ุจุฏูู ุฃู ุตูุฑ ุฃู ู
ุฎุทุทุงุช.
|
| 1444 |
+
|
| 1445 |
+
|
| 1446 |
+
================================================================================
|
| 1447 |
+
PAGE 47 - page_0047.png
|
| 1448 |
+
================================================================================
|
| 1449 |
+
|
| 1450 |
+
------------TEXT EXTRACTED------------
|
| 1451 |
+
#include <stdio.h>
|
| 1452 |
+
int isMultipleOf3(int n);
|
| 1453 |
+
int isMultipleOf5(int n);
|
| 1454 |
+
int isMultipleOf3(int n) {
|
| 1455 |
+
if (n == 0) return 1; // Base case
|
| 1456 |
+
if (n < 0) return 0;
|
| 1457 |
+
return isMultipleOf5(n - 3); // Mutual recursive call
|
| 1458 |
+
}
|
| 1459 |
+
int isMultipleOf5(int n) {
|
| 1460 |
+
if (n == 0) return 1; // Base case
|
| 1461 |
+
if (n < 0) return 0;
|
| 1462 |
+
return isMultipleOf3(n - 5); // Mutual recursive call
|
| 1463 |
+
}
|
| 1464 |
+
int main() {
|
| 1465 |
+
int n = 15;
|
| 1466 |
+
if (isMultipleOf3(n)) {
|
| 1467 |
+
printf("%d is a multiple of 3\n", n);
|
| 1468 |
+
} else if (isMultipleOf5(n)) {
|
| 1469 |
+
printf("%d is a multiple of 5\n", n);
|
| 1470 |
+
} else {
|
| 1471 |
+
printf("%d is not a multiple of 3 or 5\n", n);
|
| 1472 |
+
}
|
| 1473 |
+
|
| 1474 |
+
return 0;
|
| 1475 |
+
}
|
| 1476 |
+
|
| 1477 |
+
Multiple of 3 or 5
|
| 1478 |
+
|
| 1479 |
+
------------PAGE TALK ABOUT------------
|
| 1480 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุจุฑูุงู
ุฌ ุจูุบุฉ C ููุชุญูู ู
ู
ุง ุฅุฐุง ูุงู ุงูุนุฏุฏ ู
ู ู
ุถุงุนูุงุช 3 ุฃู 5 ุจุงุณุชุฎุฏุงู
ุงูุงุณุชุฏุนุงุก ุงูุชุนุงูุฏู ุงูู
ุชุจุงุฏู ุจูู ุฏุงูุชูู.
|
| 1481 |
+
ุชูุถุญ ุงูุฏุงูุชุงู isMultipleOf3 ู isMultipleOf5 ุญุงูุงุช ุงูุชููู ุนูุฏู
ุง ูุตุจุญ ุงูุนุฏุฏ 0 ุฃู ุฃูู ู
ู 0ุ ุซู
ุชุณุชุฏุนู ูู ุฏุงูุฉ ุงูุฃุฎุฑู ุจุนุฏ ุทุฑุญ 3 ุฃู 5.
|
| 1482 |
+
ูู ุงูุฏุงูุฉ main ูุชู
ุงุฎุชุจุงุฑ ุงูุนุฏุฏ 15 ูุทุจุงุนุฉ ู
ุง ุฅุฐุง ูุงู ู
ู ู
ุถุงุนูุงุช 3 ุฃู 5 ุฃู ููุณ ูุฐูู.
|
| 1483 |
+
ููุฌุฏ ุนูุตุฑ ุจุตุฑู ุจุณูุท ุนูู ูู
ูู ุงูุตูุญุฉ ุนุจุงุฑุฉ ุนู ุนููุงู ูุตู: "Multiple of 3 or 5" ุจุฏูู ู
ุฎุทุทุงุช ุฃู ุตูุฑ ุฅุถุงููุฉ.
|
data_structure/p4.txt
ADDED
|
@@ -0,0 +1,559 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"ุฃูุช ู
ุฏุฑุณ ุฎุจูุฑ ูู
ุญุชุฑู ูู ู
ุงุฏุฉ ุชุฑุงููุจ ุจูุงูุงุช. ุฃูุช ุชุฏุฑูุณ ุทุงูุจุงู ูุชุณุงุนุฏู ุนูู ุงูููู
.
|
| 2 |
+
|
| 3 |
+
=== ู
ุญุชูู ุงููุตู (ุงูู
ุฑุฌุน ุงูุนูู
ู) ===
|
| 4 |
+
|
| 5 |
+
=== ููุงูุฉ ุงูุณูุงู ===
|
| 6 |
+
|
| 7 |
+
**ุชุนููู
ุงุช ู
ูู
ุฉ:**
|
| 8 |
+
- ุงุณุชุฎุฏู
ุงูู
ุญุชูู ุงูุนูู
ู ุฃุนูุงู ูู
ุฑุฌุน ุฃุณุงุณู ููุฅุฌุงุจุฉ
|
| 9 |
+
- ุฑุงุนู ุณูุงู ุงูู
ุญุงุฏุซุฉ ุงูุณุงุจูุฉ ูุงูู
ุณุชูู ุงูุชุนููู
ู ููุทุงูุจ
|
| 10 |
+
- ุฃุฌุจ ุจุดูู ูุงุถุญ ูู
ูุตู ุจุงููุบุฉ ุงูุนุฑุจูุฉ
|
| 11 |
+
- ุงุณุชุฎุฏู
ุงูุฃู
ุซูุฉ ูุงูุชูุถูุญุงุช ุนูุฏ ุงูุญุงุฌุฉ
|
| 12 |
+
- ุฅุฐุง ุณุฃู ุงูุทุงูุจ ุนู ุดูุก ุฎุงุฑุฌ ุงูู
ุญุชูู ุงูู
ุชููุฑุ ุฃุฎุจุฑู ุจุฐูู ุจูุทู
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
================================================================================
|
| 16 |
+
SUBJECT: L4_Stack(part 1)_20260417_203612
|
| 17 |
+
TOTAL PAGES: 7
|
| 18 |
+
EXTRACTED ON: 2026-04-17 19:54:19
|
| 19 |
+
================================================================================
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
================================================================================
|
| 23 |
+
PAGE 1 - page_0001.png
|
| 24 |
+
================================================================================
|
| 25 |
+
|
| 26 |
+
------------TEXT EXTRACTED------------
|
| 27 |
+
ITGS 220
|
| 28 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 29 |
+
|
| 30 |
+
Stack
|
| 31 |
+
|
| 32 |
+
1.ย ย STACK:
|
| 33 |
+
|
| 34 |
+
1.1 Introduction to Stacks
|
| 35 |
+
A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle.
|
| 36 |
+
The last element added to the stack will be the first one to be removed.
|
| 37 |
+
Stacks are sometimes known as LIFO (last in, first out) lists.
|
| 38 |
+
|
| 39 |
+
Key Characteristics:
|
| 40 |
+
โขย ย ย ย Ordered collection of elements
|
| 41 |
+
โขย ย ย ย Two main operations:ย ย push (add) and pop (remove)
|
| 42 |
+
โขย ย ย ย Access is restricted to the top element only
|
| 43 |
+
โขย ย ย ย Fixed size when implemented using arrays
|
| 44 |
+
|
| 45 |
+
Ways to implement Stack
|
| 46 |
+
1.ย ย Implement stack using Array
|
| 47 |
+
2.ย ย Implement stack using LinkedList
|
| 48 |
+
|
| 49 |
+
1.2 Operations on Stack and How to Implement:
|
| 50 |
+
The fundamental operations we'll implement:
|
| 51 |
+
1.ย ย push() - Add an element to the top of the stack
|
| 52 |
+
2.ย ย pop() - Remove the top element from the stack
|
| 53 |
+
3.ย ย peek() - View the top element without removing it
|
| 54 |
+
4.ย ย isEmpty() - Check if stack is empty
|
| 55 |
+
5.ย ย isFull() - Check if stack is full
|
| 56 |
+
6.ย ย display() - Display all elements in the stack
|
| 57 |
+
|
| 58 |
+
Push 1
|
| 59 |
+
Push 2
|
| 60 |
+
Push 3
|
| 61 |
+
Pop 3
|
| 62 |
+
3
|
| 63 |
+
2
|
| 64 |
+
1
|
| 65 |
+
Stack
|
| 66 |
+
1
|
| 67 |
+
Stack
|
| 68 |
+
2
|
| 69 |
+
1
|
| 70 |
+
Stack
|
| 71 |
+
Pop 1
|
| 72 |
+
Pop 2
|
| 73 |
+
peek 2
|
| 74 |
+
2
|
| 75 |
+
1
|
| 76 |
+
Stack
|
| 77 |
+
1
|
| 78 |
+
Stack
|
| 79 |
+
Stack
|
| 80 |
+
Stack Operations
|
| 81 |
+
|
| 82 |
+
โPushโ is the term used to insert an element into a stack. โPopโ is the term used to delete an element from the stack.
|
| 83 |
+
|
| 84 |
+
1
|
| 85 |
+
|
| 86 |
+
------------PAGE TALK ABOUT------------
|
| 87 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุจููุฉ ุงูุจูุงูุงุช Stack ูู
ุจุฏุฃ ุนู
ููุง LIFOุ ุญูุซ ุขุฎุฑ ุนูุตุฑ ูุฏุฎู ูู ุฃูู ุนูุตุฑ ูุฎุฑุฌ.
|
| 88 |
+
ุชูุถุญ ุงูุฎุตุงุฆุต ุงูุฃุณุงุณูุฉ ููู Stack ู
ุซู ุฃู ุงููุตูู ูููู ููุท ุฅูู ุงูุนูุตุฑ ุงูุนูููุ ูุฃูู
ุงูุนู
ููุงุช ู
ุซู push ู pop ู peek ู isEmpty ู isFull ู display.
|
| 89 |
+
ูู
ุง ุชุฐูุฑ ุทุฑููุชูู ูุชูููุฐ ุงูู Stack: ุจุงุณุชุฎุฏุงู
Array ุฃู LinkedList.
|
| 90 |
+
ููุฌุฏ ู
ุฎุทุท ุจุตุฑู ูุดุฑุญ ุชุณูุณู ุงูุนู
ููุงุช ุนูู ุงูู
ูุฏุณ: ุฅุฏุฎุงู ุงูุนูุงุตุฑ 1 ุซู
2 ุซู
3ุ ุซู
ุญุฐู ุงูุนูุงุตุฑ ู
ู ุงูุฃุนููุ ู
ุน ู
ุซุงู ุนูู peek ูุฅุธูุงุฑ ุงูุนูุตุฑ ุงูุนููู ุฏูู ุญุฐูู.
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
================================================================================
|
| 94 |
+
PAGE 2 - page_0002.png
|
| 95 |
+
================================================================================
|
| 96 |
+
|
| 97 |
+
------------TEXT EXTRACTED------------
|
| 98 |
+
All insertions and deletions take place at the same end, so the last element added to the stack will be the first element removed from the stack. When a stack is created, the stack base remains fixed while the stack top changes as elements are added and removed. The most accessible element is the top and the least accessible element is the bottom of the stack.
|
| 99 |
+
|
| 100 |
+
1.3 Array Implementation
|
| 101 |
+
|
| 102 |
+
Let us consider a stack with 6 elements capacity. This is called as the size of the stack. The number of elements to be added should not exceed the maximum size of the stack. If we attempt to add new element beyond the maximum size, we will encounter a stack overflow condition. Similarly, you cannot remove elements beyond the base of the stack. If such is the case, we will reach a stack underflow condition.
|
| 103 |
+
|
| 104 |
+
When an element is added to a stack, the operation is performed by push(). Figure 1 shows the creation of a stack and addition of elements using push().
|
| 105 |
+
|
| 106 |
+
Top=-1
|
| 107 |
+
Empty
|
| 108 |
+
Stack
|
| 109 |
+
|
| 110 |
+
4
|
| 111 |
+
3
|
| 112 |
+
2
|
| 113 |
+
1
|
| 114 |
+
0
|
| 115 |
+
|
| 116 |
+
Top
|
| 117 |
+
Insert
|
| 118 |
+
11
|
| 119 |
+
|
| 120 |
+
11
|
| 121 |
+
|
| 122 |
+
4
|
| 123 |
+
3
|
| 124 |
+
2
|
| 125 |
+
1
|
| 126 |
+
0
|
| 127 |
+
|
| 128 |
+
Top
|
| 129 |
+
Insert
|
| 130 |
+
22
|
| 131 |
+
|
| 132 |
+
22
|
| 133 |
+
11
|
| 134 |
+
|
| 135 |
+
4
|
| 136 |
+
3
|
| 137 |
+
2
|
| 138 |
+
1
|
| 139 |
+
0
|
| 140 |
+
|
| 141 |
+
Top
|
| 142 |
+
Insert
|
| 143 |
+
33
|
| 144 |
+
|
| 145 |
+
33
|
| 146 |
+
22
|
| 147 |
+
11
|
| 148 |
+
|
| 149 |
+
4
|
| 150 |
+
3
|
| 151 |
+
2
|
| 152 |
+
1
|
| 153 |
+
0
|
| 154 |
+
|
| 155 |
+
Figure 1. Push operations on stack
|
| 156 |
+
|
| 157 |
+
When an element is taken off from the stack, the operation is performed by pop(). Figure 2 shows a stack initially with three elements and shows the deletion of elements using pop().
|
| 158 |
+
|
| 159 |
+
TOP
|
| 160 |
+
Initial
|
| 161 |
+
Stack
|
| 162 |
+
|
| 163 |
+
33
|
| 164 |
+
22
|
| 165 |
+
11
|
| 166 |
+
|
| 167 |
+
4
|
| 168 |
+
3
|
| 169 |
+
2
|
| 170 |
+
1
|
| 171 |
+
0
|
| 172 |
+
|
| 173 |
+
TOP
|
| 174 |
+
POP 33
|
| 175 |
+
|
| 176 |
+
22
|
| 177 |
+
11
|
| 178 |
+
|
| 179 |
+
4
|
| 180 |
+
3
|
| 181 |
+
2
|
| 182 |
+
1
|
| 183 |
+
0
|
| 184 |
+
|
| 185 |
+
TOP
|
| 186 |
+
POP 22
|
| 187 |
+
|
| 188 |
+
11
|
| 189 |
+
|
| 190 |
+
4
|
| 191 |
+
3
|
| 192 |
+
2
|
| 193 |
+
1
|
| 194 |
+
0
|
| 195 |
+
|
| 196 |
+
TOP
|
| 197 |
+
|
| 198 |
+
4
|
| 199 |
+
3
|
| 200 |
+
2
|
| 201 |
+
1
|
| 202 |
+
0
|
| 203 |
+
|
| 204 |
+
Figure 2. Pop operations on stack
|
| 205 |
+
|
| 206 |
+
2
|
| 207 |
+
|
| 208 |
+
------------PAGE TALK ABOUT------------
|
| 209 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุชูููุฐ ุจููุฉ ุงูุจูุงูุงุช Stack ุจุงุณุชุฎุฏุงู
ุงูู
ุตูููุฉุ ูุชุดุฑุญ ู
ุจุฏุฃ LIFO ุญูุซ ุขุฎุฑ ุนูุตุฑ ูุฏุฎู ูู ุฃูู ุนูุตุฑ ูุฎุฑุฌ.
|
| 210 |
+
ูู
ุง ุชูุถุญ ู
ูููู
ู stack overflow ุนูุฏ ู
ุญุงููุฉ ุงูุฅุถุงูุฉ ุจุนุฏ ุงูุณุนุฉ ุงููุตููุ ูstack underflow ุนูุฏ ุงูุญุฐู ู
ู ู
ูุฏุณ ูุงุฑุบ.
|
| 211 |
+
ููุฌุฏ ุดููุงู ุชูุถูุญูุงู: ุงูุฃูู ูุจูู ุนู
ููุงุช push ุจุฅุฏุฎุงู 11 ุซู
22 ุซู
33 ู
ุน ุชุญุฑู ุงูู
ุคุดุฑ Topุ ูุงูุซุงูู ูุจูู ุนู
ููุงุช pop ุจุฅุฒุงูุฉ 33 ุซู
22 ุซู
ุงููุตูู ุฅูู ู
ูุฏุณ ูุงุฑุบ.
|
| 212 |
+
ุงูุฑุณูู
ุงุช ุชุนุฑุถ ุฎูุงูุง ุงูู
ูุฏุณ ุฑุฃุณูุงู ู
ุน ุงูููุงุฑุณ ู
ู 0 ุฅูู 4 ูู
ููุน ุงูู
ุคุดุฑ Top ูู ูู ุฎุทูุฉ.
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
================================================================================
|
| 216 |
+
PAGE 3 - page_0003.png
|
| 217 |
+
================================================================================
|
| 218 |
+
|
| 219 |
+
------------TEXT EXTRACTED------------
|
| 220 |
+
- **Structure Definition**
|
| 221 |
+
|
| 222 |
+
โโ#define MAX 10 // Maximum size of stack
|
| 223 |
+
|
| 224 |
+
โโstruct Stack {
|
| 225 |
+
|
| 226 |
+
โโโโint items[MAX]; // Array to store stack elements
|
| 227 |
+
|
| 228 |
+
โโโโint top; // Index of top element
|
| 229 |
+
|
| 230 |
+
โโ};
|
| 231 |
+
|
| 232 |
+
- **Initialization**
|
| 233 |
+
|
| 234 |
+
โโvoid initialize(struct Stack *s) {
|
| 235 |
+
|
| 236 |
+
โโโโs->top = -1; // -1 indicates empty stack
|
| 237 |
+
|
| 238 |
+
โโ}
|
| 239 |
+
|
| 240 |
+
- **Checking Stack Status**
|
| 241 |
+
|
| 242 |
+
โโint isEmpty(struct Stack *s) {
|
| 243 |
+
|
| 244 |
+
โโโโreturn (s->top == -1);
|
| 245 |
+
|
| 246 |
+
โโ}
|
| 247 |
+
|
| 248 |
+
โโint isFull(struct Stack *s) {
|
| 249 |
+
|
| 250 |
+
โโโโreturn (s->top == MAX - 1);
|
| 251 |
+
|
| 252 |
+
โโ}
|
| 253 |
+
|
| 254 |
+
- **Push Operation**
|
| 255 |
+
|
| 256 |
+
โโvoid push(struct Stack *s, int value) {
|
| 257 |
+
|
| 258 |
+
โโโโif (isFull(s)) {
|
| 259 |
+
|
| 260 |
+
โโโโโโprintf("Stack Overflow! Cannot push %d\n", value);
|
| 261 |
+
|
| 262 |
+
โโโโ} else {
|
| 263 |
+
|
| 264 |
+
โโโโโโs->top++;
|
| 265 |
+
|
| 266 |
+
โโโโโโs->items[s->top] = value;
|
| 267 |
+
|
| 268 |
+
โโโโโโprintf("%d pushed to stack\n", value);
|
| 269 |
+
|
| 270 |
+
โโโโ}
|
| 271 |
+
|
| 272 |
+
โโ}
|
| 273 |
+
|
| 274 |
+
- **Pop Operation**
|
| 275 |
+
|
| 276 |
+
โโint pop(struct Stack *s) {
|
| 277 |
+
|
| 278 |
+
โโโโif (isEmpty(s)) {
|
| 279 |
+
|
| 280 |
+
โโโโโโprintf("Stack Underflow! Cannot pop\n");
|
| 281 |
+
|
| 282 |
+
โโโโโโreturn -1; // Error value
|
| 283 |
+
|
| 284 |
+
โโโโ} else {
|
| 285 |
+
|
| 286 |
+
โโโโโโint value = s->items[s->top];
|
| 287 |
+
|
| 288 |
+
3
|
| 289 |
+
|
| 290 |
+
------------PAGE TALK ABOUT------------
|
| 291 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุชูููุฐ ุจููุฉ ุงูุจูุงูุงุช Stack ุจุงุณุชุฎุฏุงู
ูุบุฉ C ู
ู ุฎูุงู ู
ุตูููุฉ ุซุงุจุชุฉ ุงูุญุฌู
.
|
| 292 |
+
ุชุดุฑุญ ุชุนุฑูู ุงููููู struct Stackุ ูุชููุฆุฉ ุงูู
ูุฏุณุ ุซู
ุฏูุงู ุงูุชุญูู ูู ุงูู
ูุฏุณ ูุงุฑุบ ุฃู ู
ู
ุชูุฆ.
|
| 293 |
+
ูู
ุง ุชุนุฑุถ ุนู
ููุฉ push ูุฅุถุงูุฉ ุนูุตุฑ ู
ุน ู
ุนุงูุฌุฉ ุญุงูุฉ overflowุ ูุนู
ููุฉ pop ูุฅุฒุงูุฉ ุนูุตุฑ ู
ุน ู
ุนุงูุฌุฉ underflow.
|
| 294 |
+
ุงูุตูุญุฉ ุนุจุงุฑุฉ ุนู ูุต ุจุฑู
ุฌู ู
ูุธู
ุจุนูุงููู ูุฑุนูุฉ ูููุงุทุ ููุง ุชุญุชูู ุนูู ุตูุฑ ุฃู ู
ุฎุทุทุงุชุ ููุท ุฑูู
ุงูุตูุญุฉ 3 ูู ุงูุฃุณูู.
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
================================================================================
|
| 298 |
+
PAGE 4 - page_0004.png
|
| 299 |
+
================================================================================
|
| 300 |
+
|
| 301 |
+
------------TEXT EXTRACTED------------
|
| 302 |
+
s->top--;
|
| 303 |
+
return value;
|
| 304 |
+
}
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
- **Peek Operation**
|
| 308 |
+
|
| 309 |
+
int peek(struct Stack *s) {
|
| 310 |
+
|
| 311 |
+
if (isEmpty(s)) {
|
| 312 |
+
|
| 313 |
+
printf("Stack is empty\n");
|
| 314 |
+
|
| 315 |
+
return -1;
|
| 316 |
+
|
| 317 |
+
} else {
|
| 318 |
+
|
| 319 |
+
return s->items[s->top];
|
| 320 |
+
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
- **Display Operation**
|
| 326 |
+
|
| 327 |
+
void display(struct Stack *s) {
|
| 328 |
+
|
| 329 |
+
if (isEmpty(s)) {
|
| 330 |
+
|
| 331 |
+
printf("Stack is empty\n");
|
| 332 |
+
|
| 333 |
+
} else {
|
| 334 |
+
|
| 335 |
+
printf("Stack elements:\n");
|
| 336 |
+
|
| 337 |
+
for (int i = s->top; i >= 0; i--) {
|
| 338 |
+
|
| 339 |
+
printf("%d\n", s->items[i]);
|
| 340 |
+
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
}
|
| 346 |
+
|
| 347 |
+
**Key Points to Remember**
|
| 348 |
+
|
| 349 |
+
1. **Stack Pointer (top):**
|
| 350 |
+
1. Initialized to -1 (empty stack)
|
| 351 |
+
2. Points to the current top element
|
| 352 |
+
3. Incremented before push, decremented after pop
|
| 353 |
+
|
| 354 |
+
2. **Stack Overflow:**
|
| 355 |
+
1. Occurs when trying to push to a full stack (top == MAX-1)
|
| 356 |
+
2. Prevented by checking isFull() before push
|
| 357 |
+
|
| 358 |
+
3. **Stack Underflow:**
|
| 359 |
+
|
| 360 |
+
4
|
| 361 |
+
|
| 362 |
+
------------PAGE TALK ABOUT------------
|
| 363 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุนู
ููุงุช ุงูู
ูุฏุณ (Stack) ูู ูุบุฉ Cุ ูุฎุตูุตูุง ุนู
ููุชู **peek** ู**display** ู
ุน ุฌุฒุก ุธุงูุฑ ู
ู ููุงูุฉ ุฏุงูุฉ **pop**.
|
| 364 |
+
ุชูุถุญ ุฏุงูุฉ **peek** ููููุฉ ุฅุฑุฌุงุน ุงูุนูุตุฑ ุงูู
ูุฌูุฏ ุฃุนูู ุงูู
ูุฏุณ ู
ุน ุงูุชุญูู ุฃูููุง ุฅุฐุง ูุงู ุงูู
ูุฏุณ ูุงุฑุบูุง.
|
| 365 |
+
ูู
ุง ุชุดุฑุญ ุฏุงูุฉ **display** ุทุฑููุฉ ุนุฑุถ ุฌู
ูุน ุนูุงุตุฑ ุงูู
ูุฏุณ ู
ู ุงูุฃุนูู ุฅูู ุงูุฃุณูู ุจุงุณุชุฎุฏุงู
ุญููุฉ `for`.
|
| 366 |
+
ููู ุงูุฃุณูู ุชูุฌุฏ ููุงุท ู
ูู
ุฉ ุชูุฎุต ู
ูููู
ุงูู
ุคุดุฑ `top`ุ ูุญุงูุชู **Stack Overflow** ู**Stack Underflow**.
|
| 367 |
+
ูุง ุชูุฌุฏ ุตูุฑ ุฃู ู
ุฎุทุทุงุชุ ููุท ูุตูุต ุชุนููู
ูุฉ ูููุฏ ุจุฑู
ุฌู ู
ูุณู ุนูู ุดูู ููุงุท ูุนูุงููู.
|
| 368 |
+
|
| 369 |
+
|
| 370 |
+
================================================================================
|
| 371 |
+
PAGE 5 - page_0005.png
|
| 372 |
+
================================================================================
|
| 373 |
+
|
| 374 |
+
------------TEXT EXTRACTED------------
|
| 375 |
+
1. Occurs when trying to pop from an empty stack (top == -1)
|
| 376 |
+
|
| 377 |
+
2. Prevented by checking isEmpty() before pop
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
1.4 Source code for stack operations, using array:
|
| 381 |
+
|
| 382 |
+
#include <stdio.h>
|
| 383 |
+
#define MAX 10
|
| 384 |
+
|
| 385 |
+
struct Stack {
|
| 386 |
+
int items[MAX];
|
| 387 |
+
int top;
|
| 388 |
+
};
|
| 389 |
+
|
| 390 |
+
// Function prototypes
|
| 391 |
+
void initialize(struct Stack *s);
|
| 392 |
+
int isEmpty(struct Stack *s);
|
| 393 |
+
int isFull(struct Stack *s);
|
| 394 |
+
void push(struct Stack *s, int value);
|
| 395 |
+
int pop(struct Stack *s);
|
| 396 |
+
int peek(struct Stack *s);
|
| 397 |
+
void display(struct Stack *s);
|
| 398 |
+
|
| 399 |
+
int main() {
|
| 400 |
+
struct Stack myStack;
|
| 401 |
+
initialize(&myStack);
|
| 402 |
+
int choice, value;
|
| 403 |
+
|
| 404 |
+
while (1) {
|
| 405 |
+
printf("\nStack Operations:\n");
|
| 406 |
+
printf("1. Push\n");
|
| 407 |
+
printf("2. Pop\n");
|
| 408 |
+
printf("3. Peek\n");
|
| 409 |
+
printf("4. Display\n");
|
| 410 |
+
printf("5. Exit\n");
|
| 411 |
+
printf("Enter your choice: ");
|
| 412 |
+
scanf("%d", &choice);
|
| 413 |
+
|
| 414 |
+
switch (choice) {
|
| 415 |
+
case 1:
|
| 416 |
+
printf("Enter value to push: ");
|
| 417 |
+
scanf("%d", &value);
|
| 418 |
+
push(&myStack, value);
|
| 419 |
+
break;
|
| 420 |
+
case 2:
|
| 421 |
+
value = pop(&myStack);
|
| 422 |
+
if (value != -1) {
|
| 423 |
+
printf("Popped value: %d\n", value);
|
| 424 |
+
}
|
| 425 |
+
break;
|
| 426 |
+
case 3:
|
| 427 |
+
value = peek(&myStack);
|
| 428 |
+
if (value != -1) {
|
| 429 |
+
printf("Top element: %d\n", value);
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
+
break;
|
| 433 |
+
case
|
| 434 |
+
|
| 435 |
+
5
|
| 436 |
+
|
| 437 |
+
------------PAGE TALK ABOUT------------
|
| 438 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุจููุฉ ุงูุจูุงูุงุช Stack ุจุงุณุชุฎุฏุงู
ุงูู
ุตูููุฉ ูู ูุบุฉ Cุ ู
ุน ุชูุถูุญ ุญุงูุฉ underflow ุนูุฏ ู
ุญุงููุฉ ุชูููุฐ pop ุนูู ู
ูุฏุณ ูุงุฑุบ ุนูุฏู
ุง ุชููู ููู
ุฉ top = -1.
|
| 439 |
+
ูู
ุง ุชุฐูุฑ ุฃู ุชุฌูุจ ูุฐู ุงูู
ุดููุฉ ูููู ุนุจุฑ ุงูุชุญูู ู
ู isEmpty() ูุจู ุชูููุฐ pop.
|
| 440 |
+
ุชุญุชูู ุงูุตูุญุฉ ุนูู ููุฏ ู
ุตุฏุฑ ูุนุฑูู struct Stack ููุนุฑุถ ุฏูุงู ุฃุณุงุณูุฉ ู
ุซู initialize ูpush ูpop ูpeek ูdisplay.
|
| 441 |
+
ููุฌุฏ ุฃูุถูุง ุฌุฒุก ู
ู ุจุฑูุงู
ุฌ main ูุนุฑุถ ูุงุฆู
ุฉ ุชูุงุนููุฉ ููู
ุณุชุฎุฏู
ูุชูููุฐ ุนู
ููุงุช ุงูู
ูุฏุณ ู
ุซู ุงูุฅุถุงูุฉ ูุงูุญุฐู ูุนุฑุถ ุงูุนูุตุฑ ุงูุนููู.
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
================================================================================
|
| 445 |
+
PAGE 6 - page_0006.png
|
| 446 |
+
================================================================================
|
| 447 |
+
|
| 448 |
+
------------TEXT EXTRACTED------------
|
| 449 |
+
}
|
| 450 |
+
break;
|
| 451 |
+
case 4:
|
| 452 |
+
display(&myStack);
|
| 453 |
+
break;
|
| 454 |
+
case 5:
|
| 455 |
+
return 0;
|
| 456 |
+
default:
|
| 457 |
+
printf("Invalid choice\n");
|
| 458 |
+
}
|
| 459 |
+
}
|
| 460 |
+
}
|
| 461 |
+
|
| 462 |
+
return 0;
|
| 463 |
+
}
|
| 464 |
+
|
| 465 |
+
void initialize(struct Stack *s) {
|
| 466 |
+
s->top = -1; // -1 indicates empty stack
|
| 467 |
+
}
|
| 468 |
+
|
| 469 |
+
int isEmpty(struct Stack *s) {
|
| 470 |
+
return (s->top == -1);
|
| 471 |
+
}
|
| 472 |
+
|
| 473 |
+
int isFull(struct Stack *s) {
|
| 474 |
+
return (s->top == MAX - 1);
|
| 475 |
+
}
|
| 476 |
+
|
| 477 |
+
void push(struct Stack *s, int value) {
|
| 478 |
+
if (isFull(s)) {
|
| 479 |
+
printf("Stack Overflow! Cannot push %d\n", value);
|
| 480 |
+
} else {
|
| 481 |
+
s->top++;
|
| 482 |
+
s->items[s->top] = value;
|
| 483 |
+
printf("%d pushed to stack\n", value);
|
| 484 |
+
}
|
| 485 |
+
}
|
| 486 |
+
|
| 487 |
+
int pop(struct Stack *s) {
|
| 488 |
+
if (isEmpty(s)) {
|
| 489 |
+
printf("Stack Underflow! Cannot pop\n");
|
| 490 |
+
return -1; // Error value
|
| 491 |
+
} else {
|
| 492 |
+
int value = s->items[s->top];
|
| 493 |
+
s->top--;
|
| 494 |
+
return value;
|
| 495 |
+
}
|
| 496 |
+
}
|
| 497 |
+
|
| 498 |
+
int peek(struct Stack *s) {
|
| 499 |
+
if (isEmpty(s)) {
|
| 500 |
+
printf("Stack is empty\n");
|
| 501 |
+
return -1;
|
| 502 |
+
} else {
|
| 503 |
+
return s->items[s->top];
|
| 504 |
+
}
|
| 505 |
+
}
|
| 506 |
+
|
| 507 |
+
void display(struct Stack *s) {
|
| 508 |
+
|
| 509 |
+
6
|
| 510 |
+
|
| 511 |
+
------------PAGE TALK ABOUT------------
|
| 512 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุฌุฒุก ู
ู ุจุฑูุงู
ุฌ ุจูุบุฉ C ูุชูููุฐ ุจููุฉ ุงูุจูุงูุงุช Stack ุจุงุณุชุฎุฏุงู
ุงูู
ุตูููุฉ.
|
| 513 |
+
ุชูุถุญ ุฏูุงู ุฃุณุงุณูุฉ ู
ุซู initialize ู isEmpty ู isFull ู push ู pop ู peekุ ู
ุน ุฌุฒุก ู
ู ูุงุฆู
ุฉ ุฎูุงุฑุงุช ุจุงุณุชุฎุฏุงู
switch-case.
|
| 514 |
+
ูู
ุง ุชุนุฑุถ ู
ุนุงูุฌุฉ ุญุงูุชู overflow ู underflow ุนูุฏ ุงูุฅุถุงูุฉ ุฃู ุงูุญุฐู ู
ู ุงูู
ูุฏุณ.
|
| 515 |
+
ูุง ุชูุฌุฏ ุตูุฑ ุฃู ู
ุฎุทุทุงุชุ ููุท ููุฏ ุจุฑู
ุฌู ู
ูุณู ูุฑูู
ุงูุตูุญุฉ 6 ูู ุงูุฃุณูู.
|
| 516 |
+
|
| 517 |
+
|
| 518 |
+
================================================================================
|
| 519 |
+
PAGE 7 - page_0007.png
|
| 520 |
+
================================================================================
|
| 521 |
+
|
| 522 |
+
------------TEXT EXTRACTED------------
|
| 523 |
+
if (isEmpty(s)) {
|
| 524 |
+
printf("Stack is empty\n");
|
| 525 |
+
} else {
|
| 526 |
+
printf("Stack elements:\n");
|
| 527 |
+
for (int i = s->top; i >= 0; i--) {
|
| 528 |
+
printf("%d\n", s->items[i]);
|
| 529 |
+
}
|
| 530 |
+
}
|
| 531 |
+
}
|
| 532 |
+
|
| 533 |
+
1.5 Applications of Stacks
|
| 534 |
+
|
| 535 |
+
Stacks are widely used in computer science for solving various problems. Some key applications include:
|
| 536 |
+
|
| 537 |
+
โข Stack is used by compilers to check for balancing of parentheses, brackets and braces.
|
| 538 |
+
โข Stack is used to evaluate a postfix expression.
|
| 539 |
+
โข Stack is used to convert an infix expression into postfix/prefix form.
|
| 540 |
+
โข In recursion, all intermediate arguments and return values are stored on the processorโs stack.
|
| 541 |
+
โข During a function call the return address and arguments are pushed onto a stack and on return they are popped off.
|
| 542 |
+
|
| 543 |
+
Expression Evaluation:
|
| 544 |
+
o Stacks are used to evaluate arithmetic expressions written in infix, prefix,
|
| 545 |
+
or postfix notation.
|
| 546 |
+
o Infix Notation: Operators are placed between operands (e.g., a + b).
|
| 547 |
+
o Postfix Notation: Operators follow operands (e.g., a b +).
|
| 548 |
+
o Prefix Notation: Operators precede operands (e.g., + a b).
|
| 549 |
+
2. Infix to Postfix/Prefix Conversion:
|
| 550 |
+
o Stacks are used to convert infix expressions to postfix or prefix notation,
|
| 551 |
+
which are easier for computers to evaluate.
|
| 552 |
+
|
| 553 |
+
7
|
| 554 |
+
|
| 555 |
+
------------PAGE TALK ABOUT------------
|
| 556 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุชุทุจููุงุช ุจููุฉ ุงูุจูุงูุงุช Stack ูู ุนููู
ุงูุญุงุณูุจุ ู
ุซู ูุญุต ุชูุงุฒู ุงูุฃููุงุณุ ุชูููู
ุงูุชุนุจูุฑุงุชุ ูุชุญููู ุงูุตูุบ ุจูู infix ูpostfix ูprefix.
|
| 557 |
+
ูู
ุง ุชุดุฑุญ ุฏูุฑ ุงูู Stack ูู ุงูุงุณุชุฏุนุงุก ุงูุชุนุงูุฏู recursion ูุงุณุชุฏุนุงุก ุงูุฏูุงูุ ุญูุซ ุชูุฎุฒู ุงูุนูุงููู ูุงููุณุงุฆุท ู
ุคูุชูุง ุซู
ุชูุฒุงู ุนูุฏ ุงูุนูุฏุฉ.
|
| 558 |
+
ููุฌุฏ ูู ุฃุนูู ุงูุตูุญุฉ ู
ูุทุน ููุฏ ุจูุบุฉ C ูุนุฑุถ ุนูุงุตุฑ ุงูู Stack ุฃู ุทุจุงุนุฉ ุฑุณุงูุฉ ุฅุฐุง ูุงู ูุงุฑุบูุง.
|
| 559 |
+
ุงูุตูุญุฉ ูุตูุฉ ุจุงููุงู
ู ุชูุฑูุจูุง ููุง ุชุญุชูู ุนูู ุตูุฑ ุฃู ู
ุฎุทุทุงุชุ ููุท ุนูุงููู ูููุงุท ุชุนุฏุงุฏ ูุฑูู
ุงูุตูุญุฉ ูู ุงูุฃุณูู.
|
data_structure/p5.txt
ADDED
|
@@ -0,0 +1,1382 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"ุฃูุช ู
ุฏุฑุณ ุฎุจูุฑ ูู
ุญุชุฑู ูู ู
ุงุฏุฉ ุชุฑุงููุจ ุจูุงูุงุช. ุฃูุช ุชุฏุฑูุณ ุทุงูุจุงู ูุชุณุงุนุฏู ุนูู ุงูููู
.
|
| 2 |
+
|
| 3 |
+
=== ู
ุญุชูู ุงููุตู (ุงูู
ุฑุฌุน ุงูุนูู
ู) ===
|
| 4 |
+
|
| 5 |
+
=== ููุงูุฉ ุงูุณูุงู ===
|
| 6 |
+
|
| 7 |
+
**ุชุนููู
ุงุช ู
ูู
ุฉ:**
|
| 8 |
+
- ุงุณุชุฎุฏู
ุงูู
ุญุชูู ุงูุนูู
ู ุฃุนูุงู ูู
ุฑุฌุน ุฃุณุงุณู ููุฅุฌุงุจุฉ
|
| 9 |
+
- ุฑุงุนู ุณูุงู ุงูู
ุญุงุฏุซุฉ ุงูุณุงุจูุฉ ูุงูู
ุณุชูู ุงูุชุนููู
ู ููุทุงูุจ
|
| 10 |
+
- ุฃุฌุจ ุจุดูู ูุงุถุญ ูู
ูุตู ุจุงููุบุฉ ุงูุนุฑุจูุฉ
|
| 11 |
+
- ุงุณุชุฎุฏู
ุงูุฃู
ุซูุฉ ูุงูุชูุถูุญุงุช ุนูุฏ ุงูุญุงุฌุฉ
|
| 12 |
+
- ุฅุฐุง ุณุฃู ุงูุทุงูุจ ุนู ุดูุก ุฎุงุฑุฌ ุงูู
ุญุชูู ุงูู
ุชููุฑุ ุฃุฎุจุฑู ุจุฐูู ุจูุทู
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
================================================================================
|
| 16 |
+
SUBJECT: L5_Stack(part 2)_20260417_203645
|
| 17 |
+
TOTAL PAGES: 21
|
| 18 |
+
EXTRACTED ON: 2026-04-17 19:55:32
|
| 19 |
+
================================================================================
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
================================================================================
|
| 23 |
+
PAGE 1 - page_0001.png
|
| 24 |
+
================================================================================
|
| 25 |
+
|
| 26 |
+
------------TEXT EXTRACTED------------
|
| 27 |
+
ITGS 220
|
| 28 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 29 |
+
|
| 30 |
+
Stack
|
| 31 |
+
|
| 32 |
+
**1.3ย ย ย ย ย Algebraic Expressions:**
|
| 33 |
+
|
| 34 |
+
An algebraic expression is a legal combination of operators and operands. Operand is the quantity on which a mathematical operation is performed. Operand may be a variable like x, y, z or a constant like 5, 4, 6 etc. Operator is a symbol which signifies a mathematical or logical operation between the operands. Examples of familiar operators include +, -, * , / , ^ etc.
|
| 35 |
+
|
| 36 |
+
An algebraic expression can be represented using three different notations. They are infix, postfix and prefix notations:
|
| 37 |
+
|
| 38 |
+
**Infix:**
|
| 39 |
+
It is the form of an arithmetic expression in which we fix (place) the arithmetic operator in between the two operands.
|
| 40 |
+
|
| 41 |
+
Example: (A + B) * (C - D)
|
| 42 |
+
|
| 43 |
+
**Prefix:**
|
| 44 |
+
It is the form of an arithmetic notation in which we fix (place) the arithmetic operator before (pre) its two operands. The prefix notation is called aspolish notation (due to the polish mathematician Jan Lukasiewicz in the year 1920).
|
| 45 |
+
|
| 46 |
+
Example: * + A B - C D
|
| 47 |
+
|
| 48 |
+
**Postfix:**
|
| 49 |
+
It is the form of an arithmetic expression in which we fix (place) the arithmetic operator after (post) its two operands. The postfix notation is called as suffix notation and is also referred to reverse polish notation.
|
| 50 |
+
|
| 51 |
+
Example: A B + C D - *
|
| 52 |
+
|
| 53 |
+
The three important features of postfix expression are:
|
| 54 |
+
|
| 55 |
+
1. The operands maintain the same order as in the equivalent infix expression.
|
| 56 |
+
|
| 57 |
+
2. The parentheses are not needed to designate the expression un-ambiguously.
|
| 58 |
+
|
| 59 |
+
3. While evaluating the postfix expression the priority of the operators is no longer relevant.
|
| 60 |
+
|
| 61 |
+
We consider five binary operations: +, -, *, / and $ or ^ (exponentiation). For these binary operations, the following in the order of precedence (highest to lowest):
|
| 62 |
+
|
| 63 |
+
| OPERATOR | PRECEDENCE | VALUE |
|
| 64 |
+
|----------|------------|-------|
|
| 65 |
+
| Exponentiation ($ or โ or ^) | Highest | 3 |
|
| 66 |
+
| *, / | Next highest | 2 |
|
| 67 |
+
| +, - | Lowest | 1 |
|
| 68 |
+
|
| 69 |
+
**1.4ย ย ย ย ย Converting expressions using Stack:**
|
| 70 |
+
|
| 71 |
+
1
|
| 72 |
+
|
| 73 |
+
------------PAGE TALK ABOUT------------
|
| 74 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุงูุชุนุงุจูุฑ ุงูุฌุจุฑูุฉ ูุนูุงุตุฑูุง ุงูุฃุณุงุณูุฉ: ุงูู
ุนุงู
ูุงุช (operands) ูุงูุนูุงู
ู/ุงูุนู
ููุงุช (operators).
|
| 75 |
+
ูู
ุง ุชุดุฑุญ ุทุฑู ูุชุงุจุฉ ุงูุชุนุจูุฑ ุงูุฌุจุฑู ุงูุซูุงุซ: Infix ูPrefix ูPostfix ู
ุน ุฃู
ุซูุฉ ููู ููุน.
|
| 76 |
+
ุชูุถุญ ุฃูุถูุง ู
ู
ูุฒุงุช ุตูุบุฉ Postfix ู
ุซู ุนุฏู
ุงูุญุงุฌุฉ ุฅูู ุงูุฃููุงุณ ูุนุฏู
ุฃูู
ูุฉ ุฃููููุฉ ุงูุนู
ููุงุช ุฃุซูุงุก ุงูุชูููู
.
|
| 77 |
+
ููุฌุฏ ุฌุฏูู ูุจูู ุฃููููุฉ ุงูุนู
ููุงุช ุงูุญุณุงุจูุฉ: ุงูุฃุณ ุฃุนูู ุฃููููุฉุ ุซู
ุงูุถุฑุจ ูุงููุณู
ุฉุ ุซู
ุงูุฌู
ุน ูุงูุทุฑุญ.
|
| 78 |
+
ุงูุตูุญุฉ ุนุจุงุฑุฉ ุนู ู
ูุงุญุธุงุช ุฏุฑุงุณูุฉ ุจุนููุงู "Stack" ู
ุน ุชูุณูู ุฃูุงุฏูู
ู ูุฌุฏูู ุจุณูุท ูู ุงูุฃุณูู.
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
================================================================================
|
| 82 |
+
PAGE 2 - page_0002.png
|
| 83 |
+
================================================================================
|
| 84 |
+
|
| 85 |
+
------------TEXT EXTRACTED------------
|
| 86 |
+
ITGS 220
|
| 87 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 88 |
+
|
| 89 |
+
Let us convert the expressions from one type to another. These can be done as follows:
|
| 90 |
+
|
| 91 |
+
1. Infix to postfix
|
| 92 |
+
2. Infix to prefix
|
| 93 |
+
3. Postfix to infix
|
| 94 |
+
4. Postfix to prefix
|
| 95 |
+
5. Prefix to infix
|
| 96 |
+
6. Prefix to postfix
|
| 97 |
+
|
| 98 |
+
**1.4.1 Conversion from infix to postfix:**
|
| 99 |
+
|
| 100 |
+
Procedure to convert from infix expression to postfix expression is as follows:
|
| 101 |
+
|
| 102 |
+
1. Scan the infix expression from left to right.
|
| 103 |
+
|
| 104 |
+
ย ย ย ย a)ย ย ย If the scanned symbol is left parenthesis, push it onto the stack.
|
| 105 |
+
|
| 106 |
+
ย ย ย ย b)ย ย ย If the scanned symbol is an operand, then place directly in the postfix
|
| 107 |
+
ย ย ย ย ย ย ย ย ย expression (output).
|
| 108 |
+
|
| 109 |
+
ย ย ย ย c)ย ย ย If the symbol scanned is a right parenthesis, then go on popping all the
|
| 110 |
+
ย ย ย ย ย ย ย ย ย items from the stack and place them in the postfix expression till we get
|
| 111 |
+
ย ย ย ย ย ย ย ย ย the matching left parenthesis.
|
| 112 |
+
|
| 113 |
+
ย ย ย ย d)ย ย ย If the scanned symbol is an operator, then go on removing all the
|
| 114 |
+
ย ย ย ย ย ย ย ย ย operators from the stack and place them in the postfix expression, if and
|
| 115 |
+
ย ย ย ย ย ย ย ย ย only if the precedence of the operator which is on the top of the stack is
|
| 116 |
+
ย ย ย ย ย ย ย ย ย greater than (or greater than or equal to) the precedence of the scanned
|
| 117 |
+
ย ย ย ย ย ย ย ย ย operator and push the scanned operator onto the stack otherwise, push
|
| 118 |
+
ย ย ย ย ย ย ย ย ย the scanned operator onto the stack.
|
| 119 |
+
|
| 120 |
+
2
|
| 121 |
+
|
| 122 |
+
------------PAGE TALK ABOUT------------
|
| 123 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ู
ูุถูุน ุชุญููู ุงูุตูุบ ุงูุชุนุจูุฑูุฉ ุจูู ุงูุฃููุงุน ุงูู
ุฎุชููุฉ ู
ุซู infix ู postfix ู prefix.
|
| 124 |
+
ุชุฑููุฒ ุจุดูู ุฎุงุต ุนูู ุทุฑููุฉ ุงูุชุญููู ู
ู infix ุฅูู postfix ุจุงุณุชุฎุฏุงู
ุงูู
ูุฏุณ (stack).
|
| 125 |
+
ุชูุถุญ ุฎุทูุงุช ุงูู
ุณุญ ู
ู ุงููุณุงุฑ ุฅูู ุงููู
ูู ูููููุฉ ุงูุชุนุงู
ู ู
ุน ุงูุฃููุงุณุ ุงูู
ุนุงู
ูุงุชุ ูุงูุนูุงู
ู ุญุณุจ ุงูุฃููููุฉ.
|
| 126 |
+
ูุง ุชูุฌุฏ ุตูุฑ ุฃู ู
ุฎุทุทุงุชุ ููุท ูุต ู
ูุณู ุฃูุงุฏูู
ู ู
ุน ุนููุงู ุงูู
ูุฑุฑ ูุงุณู
ุงูุฏูุชูุฑุฉ ูุฑูู
ุงูุตูุญุฉ.
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
================================================================================
|
| 130 |
+
PAGE 3 - page_0003.png
|
| 131 |
+
================================================================================
|
| 132 |
+
|
| 133 |
+
------------TEXT EXTRACTED------------
|
| 134 |
+
ITGS 220
|
| 135 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 136 |
+
|
| 137 |
+
Example 1:
|
| 138 |
+
|
| 139 |
+
Convert ((A โ (B + C)) * D) โ (E + F) infix expression to postfix form:
|
| 140 |
+
|
| 141 |
+
| SYMBOL | POSTFIX STRING | STACK | REMARKS |
|
| 142 |
+
|---|---|---|---|
|
| 143 |
+
| ( | | ( | |
|
| 144 |
+
| ( | | ( ( | |
|
| 145 |
+
| A | A | ( ( | |
|
| 146 |
+
| - | A | ( ( - | |
|
| 147 |
+
| ( | A | ( ( - ( | |
|
| 148 |
+
| B | A B | ( ( - ( | |
|
| 149 |
+
| + | A B | ( ( - ( + | |
|
| 150 |
+
| C | A B C | ( ( - ( + | |
|
| 151 |
+
| ) | A B C + | ( ( - | |
|
| 152 |
+
| ) | A B C + - | ( | |
|
| 153 |
+
| * | A B C + - | ( * | |
|
| 154 |
+
| D | A B C + - D | ( * | |
|
| 155 |
+
| ) | A B C + - D * | | |
|
| 156 |
+
| โ | A B C + - D * | โ | |
|
| 157 |
+
| ( | A B C + - D * | โ ( | |
|
| 158 |
+
| E | A B C + - D * E | โ ( | |
|
| 159 |
+
| + | A B C + - D * E | โ ( + | |
|
| 160 |
+
| F | A B C + - D * E F | โ ( + | |
|
| 161 |
+
| ) | A B C + - D * E F + | โ | |
|
| 162 |
+
| End of string | A B C + - D * E F + โ | | The input is now empty. Pop the output symbols from the stack until it is empty. |
|
| 163 |
+
|
| 164 |
+
Example 2:
|
| 165 |
+
|
| 166 |
+
Convert a + b * c + (d * e + f) * g the infix expression into postfix form.
|
| 167 |
+
|
| 168 |
+
| SYMBOL | POSTFIX STRING | STACK | REMARKS |
|
| 169 |
+
|---|---|---|---|
|
| 170 |
+
| A | A | | |
|
| 171 |
+
| + | A | + | |
|
| 172 |
+
| B | a b | + | |
|
| 173 |
+
|
| 174 |
+
------------PAGE TALK ABOUT------------
|
| 175 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ุชุญููู ุงูุชุนุจูุฑ ุงูุฑูุงุถู ู
ู ุงูุตูุบุฉ ุงููุณุทูุฉ Infix ุฅูู ุงูุตูุบุฉ ุงููุงุญูุฉ Postfix ุจุงุณุชุฎุฏุงู
ุงูู
ูุฏุณ Stack.
|
| 176 |
+
ููุฌุฏ ู
ุซุงู ุฃูู ู
ุญููู ุฎุทูุฉ ุจุฎุทูุฉ ุฏุงุฎู ุฌุฏูู ููุถุญ ุงูุฑู
ุฒ ุงูุญุงููุ ูุงุชุฌ ุงูู postfixุ ูู
ุญุชูู ุงูู
ูุฏุณ ุจุนุฏ ูู ุฎุทูุฉ.
|
| 177 |
+
ูู
ุง ุชุนุฑุถ ู
ุซุงููุง ุซุงูููุง ูุจุฏุฃ ุจููุณ ุงูููุฑุฉ ููู ุงูุฌุฏูู ููู ุบูุฑ ู
ูุชู
ู.
|
| 178 |
+
ุงูุนูุงุตุฑ ุงูุจุตุฑูุฉ ุนุจุงุฑุฉ ุนู ุตูุญุฉ ู
ุญุงุถุฑุฉ ุฃูุงุฏูู
ูุฉ ู
ูุธู
ุฉ ุชุญุชูู ุนูุงูููุ ุฌุฏุงููุ ูุฎุท ุฃููู ูู ุฃุนูู ุงูุตูุญุฉ ุฏูู ุตูุฑ ุฃุฎุฑู.
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
================================================================================
|
| 182 |
+
PAGE 4 - page_0004.png
|
| 183 |
+
================================================================================
|
| 184 |
+
|
| 185 |
+
------------TEXT EXTRACTED------------
|
| 186 |
+
ITGS 220
|
| 187 |
+
Dr. Amna Ali A Mohamed Fall 2025
|
| 188 |
+
|
| 189 |
+
----------------------------------------------------------------
|
| 190 |
+
|
| 191 |
+
| * | a b | + * | |
|
| 192 |
+
|---|-----|-----|---|
|
| 193 |
+
| C | a b c | + * | |
|
| 194 |
+
| + | a b c * + | + | |
|
| 195 |
+
| ( | a b c * + | + ( | |
|
| 196 |
+
| D | a b c * + d | + ( | |
|
| 197 |
+
| * | a b c * + d | + ( * | |
|
| 198 |
+
| E | a b c * + d e | + ( * | |
|
| 199 |
+
| + | a b c * + d e * | + ( + | |
|
| 200 |
+
| F | a b c * + d e * f | + ( + | |
|
| 201 |
+
| ) | a b c * + d e * f + | + | |
|
| 202 |
+
| * | a b c * + d e * f + | + * | |
|
| 203 |
+
| G | a b c * + d e * f + g | + * | |
|
| 204 |
+
| End of string | a b c * + d e * f + g * + | The input is now empty. Pop the output symbols from the stack until it is empty. |
|
| 205 |
+
|
| 206 |
+
Example 3:
|
| 207 |
+
|
| 208 |
+
Convert the following infix expression A + B * C โ D / E * H into its equivalent postfix expression.
|
| 209 |
+
|
| 210 |
+
| SYMBOL | POSTFIX STRING | STACK | REMARKS |
|
| 211 |
+
|--------|----------------|-------|---------|
|
| 212 |
+
| A | A | | |
|
| 213 |
+
| + | A | + | |
|
| 214 |
+
| B | A B | + | |
|
| 215 |
+
| * | A B | + * | |
|
| 216 |
+
| C | A B C | + * | |
|
| 217 |
+
| - | A B C * + | - | |
|
| 218 |
+
| D | A B C * + D | - | |
|
| 219 |
+
| / | A B C * + D | - / | |
|
| 220 |
+
| E | A B C * + D E | - / | |
|
| 221 |
+
| * | A B C * + D E / | - * | |
|
| 222 |
+
| H | A B C * + D E / H | - * | |
|
| 223 |
+
| End of string | A B C * + D E / H * - | The input is now empty. Pop the output symbols from the stack until it is empty. |
|
| 224 |
+
|
| 225 |
+
Example 4:
|
| 226 |
+
|
| 227 |
+
Convert the following infix expression A + (B * C โ (D / E โ F) * G) * H into its equivalent postfix expression.
|
| 228 |
+
|
| 229 |
+
| SYMBOL | POSTFIX STRING | STACK | REMARKS |
|
| 230 |
+
|--------|----------------|-------|---------|
|
| 231 |
+
| A | A | | |
|
| 232 |
+
| + | A | + | |
|
| 233 |
+
|
| 234 |
+
------------PAGE TALK ABOUT------------
|
| 235 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ุชุญููู ุงูุชุนุจูุฑุงุช ุงูุญุณุงุจูุฉ ู
ู ุงูุตูุบุฉ ุงููุณุทูุฉ (Infix) ุฅูู ุงูุตูุบุฉ ุงููุงุญูุฉ (Postfix) ุจุงุณุชุฎุฏุงู
ุงูู
ูุฏุณ (Stack).
|
| 236 |
+
ุชุญุชูู ุนูู ุฌุฏุงูู ุชูุถุญ ุฎุทูุฉ ุจุฎุทูุฉ ููู ูุชุบูุฑ ูู ู
ู ุงูู Postfix String ูุงูู Stack ุนูุฏ ูุฑุงุกุฉ ูู ุฑู
ุฒ ู
ู ุงูุชุนุจูุฑ.
|
| 237 |
+
ููุฌุฏ ู
ุซุงู ู
ูุชู
ู ุจุนููุงู Example 3 ููุชุนุจูุฑ: A + B * C โ D / E * Hุ ู
ุน ุงููุชูุฌุฉ ุงูููุงุฆูุฉ ุจุตูุบุฉ Postfix.
|
| 238 |
+
ูู
ุง ุชุนุฑุถ ุจุฏุงูุฉ Example 4 ููุชุนุจูุฑ: A + (B * C โ (D / E โ F) * G) * Hุ ููู ุงูุฌุฏูู ุงูุธุงูุฑ ูู ุบูุฑ ู
ูุชู
ู ูู ูุฐู ุงูุตูุญุฉ.
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
================================================================================
|
| 242 |
+
PAGE 5 - page_0005.png
|
| 243 |
+
================================================================================
|
| 244 |
+
|
| 245 |
+
------------TEXT EXTRACTED------------
|
| 246 |
+
ITGS 220
|
| 247 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 248 |
+
|
| 249 |
+
(ย ย ย ย ย ย ย ย ย Aย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย + (
|
| 250 |
+
Bย ย ย ย ย ย ย ย ย A Bย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย + (
|
| 251 |
+
*ย ย ย ย ย ย ย ย ย A Bย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย + ( *
|
| 252 |
+
Cย ย ย ย ย ย ย ย ย A B Cย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย + ( *
|
| 253 |
+
-ย ย ย ย ย ย ย ย ย A B C *ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย + ( -
|
| 254 |
+
(ย ย ย ย ย ย ย ย ย A B C *ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย + ( - (
|
| 255 |
+
Dย ย ย ย ย ย ย ย ย A B C * Dย ย ย ย ย ย ย ย ย ย ย ย ย + ( - (
|
| 256 |
+
/ย ย ย ย ย ย ย ย ย A B C * Dย ย ย ย ย ย ย ย ย ย ย ย ย + ( - ( /
|
| 257 |
+
Eย ย ย ย ย ย ย ย ย A B C * D Eย ย ย ย ย ย ย ย ย ย ย + ( - ( /
|
| 258 |
+
โย ย ย ย ย ย ย ย ย A B C * D Eย ย ย ย ย ย ย ย ย ย ย + ( - ( / โ
|
| 259 |
+
Fย ย ย ย ย ย ย ย ย A B C * D E Fย ย ย ย ย ย ย ย ย + ( - ( / โ
|
| 260 |
+
)ย ย ย ย ย ย ย ย ย A B C * D E F โ /ย ย ย ย ย + ( -
|
| 261 |
+
*ย ย ย ย ย ย ย ย ย A B C * D E F โ /ย ย ย ย ย + ( - *
|
| 262 |
+
Gย ย ย ย ย ย ย ย ย A B C * D E F โ / Gย ย ย + ( - *
|
| 263 |
+
)ย ย ย ย ย ย ย ย ย A B C * D E F โ / G * -ย ย ย +
|
| 264 |
+
*ย ย ย ย ย ย ย ย ย A B C * D E F โ / G * -ย ย ย + *
|
| 265 |
+
Hย ย ย ย ย ย ย ย ย A B C * D E F โ / G * - Hย + *
|
| 266 |
+
End of
|
| 267 |
+
stringย ย ย ย A B C * D E F โ / G * - H * +ย ย ย ย The input is now empty. Pop the output
|
| 268 |
+
symbols from the stack until it is empty.
|
| 269 |
+
|
| 270 |
+
1.4.2 Program to convert an infix to postfix expression:
|
| 271 |
+
|
| 272 |
+
#include <stdio.h>
|
| 273 |
+
#include <stdlib.h>
|
| 274 |
+
#include <ctype.h>
|
| 275 |
+
#include <string.h>
|
| 276 |
+
|
| 277 |
+
#define MAX_SIZE 100
|
| 278 |
+
|
| 279 |
+
// Function prototypes
|
| 280 |
+
void push(char stack[], int *top, char item);
|
| 281 |
+
char pop(char stack[], int *top);
|
| 282 |
+
int precedence(char operator);
|
| 283 |
+
void infixToPostfix(char infix[], char postfix[]);
|
| 284 |
+
|
| 285 |
+
int main() {
|
| 286 |
+
ย ย ย ย char infix[MAX_SIZE], postfix[MAX_SIZE];
|
| 287 |
+
|
| 288 |
+
ย ย ย ย printf("Enter infix expression: ");
|
| 289 |
+
ย ย ย ย fgets(infix, MAX_SIZE, stdin);
|
| 290 |
+
ย ย ย ย infix[strlen(infix)-1] = '\0'; // Remove newline character
|
| 291 |
+
|
| 292 |
+
ย ย ย ย infixToPostfix(infix, postfix);
|
| 293 |
+
ย ย ย ย printf("Postfix expression: %s\n", postfix);
|
| 294 |
+
|
| 295 |
+
ย ย ย ย return 0;
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
// Push operation for stack
|
| 299 |
+
void push(char stack[], int *top, char item) {
|
| 300 |
+
ย ย ย ย if (*top >= MAX_SIZE - 1) {
|
| 301 |
+
ย ย ย ย ย ย ย ย printf("Stack Overflow\n");
|
| 302 |
+
ย ย ย ย ย ย ย ย exit(1);
|
| 303 |
+
|
| 304 |
+
5
|
| 305 |
+
|
| 306 |
+
------------PAGE TALK ABOUT------------
|
| 307 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ุชุญููู ุงูุชุนุจูุฑ ู
ู Infix ุฅูู Postfix ุจุงุณุชุฎุฏุงู
ุงูู
ูุฏุณ (Stack).
|
| 308 |
+
ููุฌุฏ ุฌุฏูู ุชุชุจูุน ููุถุญ ุฎุทูุฉ ุจุฎุทูุฉ ููู ูุชู
ูุฑุงุกุฉ ุงูุฑู
ูุฒุ ูุจูุงุก ูุงุชุฌ ุงูู postfixุ ูุชุญุฏูุซ ู
ุญุชูู ุงูู stack ุญุชู ููุงูุฉ ุงูุณูุณูุฉ.
|
| 309 |
+
ูู
ุง ุชุญุชูู ุงูุตูุญุฉ ุนูู ุจุฏุงูุฉ ุจุฑูุงู
ุฌ ุจูุบุฉ C ูุชูููุฐ ูุฐุง ุงูุชุญูููุ ู
ุน ุชุถู
ูู ุงูู
ูุชุจุงุชุ ุชุนุฑูู MAX_SIZEุ ูุชุตุฑูุญุงุช ุงูุฏูุงู ูุงูุฏุงูุฉ main.
|
| 310 |
+
ุงูุฌุฒุก ุงูุณููู ูุนุฑุถ ุจุฏุงูุฉ ุฏุงูุฉ push ุงูุฎุงุตุฉ ุจุงูู
ูุฏุณุ ุจููู
ุง ุงูุนูุตุฑ ุงูุจุตุฑู ุงูุฃุณุงุณู ูู ุงูุฌุฏูู ุงูุฐู ูุดุฑุญ ุณูุฑ ุงูุฎูุงุฑุฒู
ูุฉ ุนู
ูููุง.
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
================================================================================
|
| 314 |
+
PAGE 6 - page_0006.png
|
| 315 |
+
================================================================================
|
| 316 |
+
|
| 317 |
+
------------TEXT EXTRACTED------------
|
| 318 |
+
ITGS 220
|
| 319 |
+
Dr. Amna Ali A Mohamed Fall 2025
|
| 320 |
+
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
stack[++(*top)] = item;
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
// Pop operation for stack
|
| 327 |
+
char pop(char stack[], int *top) {
|
| 328 |
+
if (*top < 0) {
|
| 329 |
+
printf("Stack Underflow\n");
|
| 330 |
+
exit(1);
|
| 331 |
+
}
|
| 332 |
+
return stack[(*top)--];
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
// Get precedence of operators
|
| 336 |
+
int precedence(char operator) {
|
| 337 |
+
switch(operator) {
|
| 338 |
+
case '^':
|
| 339 |
+
return 4;
|
| 340 |
+
case '*':
|
| 341 |
+
case '/':
|
| 342 |
+
return 3;
|
| 343 |
+
case '+':
|
| 344 |
+
case '-':
|
| 345 |
+
return 2;
|
| 346 |
+
default:
|
| 347 |
+
return 0;
|
| 348 |
+
}
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
// Convert infix to postfix
|
| 352 |
+
void infixToPostfix(char infix[], char postfix[]) {
|
| 353 |
+
char stack[MAX_SIZE];
|
| 354 |
+
int top = -1;
|
| 355 |
+
int i = 0, j = 0;
|
| 356 |
+
char item, x;
|
| 357 |
+
|
| 358 |
+
push(stack, &top, '(');
|
| 359 |
+
strcat(infix, ")");
|
| 360 |
+
|
| 361 |
+
for (item = infix[i]; item != '\0'; item = infix[++i]) {
|
| 362 |
+
if (item == '(') {
|
| 363 |
+
push(stack, &top, item);
|
| 364 |
+
}
|
| 365 |
+
else if (isalnum(item)) {
|
| 366 |
+
postfix[j++] = item;
|
| 367 |
+
}
|
| 368 |
+
else if (item == ')') {
|
| 369 |
+
while ((x = pop(stack, &top)) != '(') {
|
| 370 |
+
postfix[j++] = x;
|
| 371 |
+
}
|
| 372 |
+
}
|
| 373 |
+
else {
|
| 374 |
+
while (precedence(stack[top]) >= precedence(item)) {
|
| 375 |
+
postfix[j++] = pop(stack, &top);
|
| 376 |
+
}
|
| 377 |
+
push(stack, &top, item);
|
| 378 |
+
}
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
postfix[j] = '\0';
|
| 382 |
+
|
| 383 |
+
6
|
| 384 |
+
|
| 385 |
+
------------PAGE TALK ABOUT------------
|
| 386 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ุฌุฒุกูุง ู
ู ุจุฑูุงู
ุฌ ุจูุบุฉ C ูุชุญููู ุงูุชุนุจูุฑ ู
ู Infix ุฅูู Postfix ุจุงุณุชุฎุฏุงู
ุงูู
ูุฏุณ (Stack).
|
| 387 |
+
ุชุชุถู
ู ุฏูุงู ุฃุณุงุณูุฉ ู
ุซู pop ูุฅุฒุงูุฉ ุนูุตุฑ ู
ู ุงูู
ูุฏุณุ ูprecedence ูุชุญุฏูุฏ ุฃููููุฉ ุงูุนู
ููุงุช ุงูุญุณุงุจูุฉ.
|
| 388 |
+
ูู
ุง ุชุนุฑุถ ุฏุงูุฉ infixToPostfix ุงูุชู ุชู
ุฑ ุนูู ุฑู
ูุฒ ุงูุชุนุจูุฑ ูุชุชุนุงู
ู ู
ุน ุงูุฃููุงุณ ูุงูุนูุงู
ู ูุงูู
ุนุงู
ูุงุช ููู ุฎูุงุฑุฒู
ูุฉ ุงูุชุญููู.
|
| 389 |
+
ุจุตุฑููุงุ ุงูุตูุญุฉ ุนุจุงุฑุฉ ุนู ู
ูุงุญุธุงุช/ุดุฑูุญุฉ ุฃูุงุฏูู
ูุฉ ุชุญุชูู ุนูู ุฑุฃุณ ุงูุตูุญุฉ ุจุงุณู
ุงูู
ูุฑุฑ ูุงูุฏูุชูุฑุฉ ูุงููุตู ุงูุฏุฑุงุณู ูุฑูู
ุงูุตูุญุฉ 6.
|
| 390 |
+
|
| 391 |
+
|
| 392 |
+
================================================================================
|
| 393 |
+
PAGE 7 - page_0007.png
|
| 394 |
+
================================================================================
|
| 395 |
+
|
| 396 |
+
------------TEXT EXTRACTED------------
|
| 397 |
+
ITGS 220
|
| 398 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 399 |
+
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
**1.4.3 Conversion from infix to prefix:**
|
| 403 |
+
|
| 404 |
+
The precedence rules for converting an expression from infix to prefix are identical. The only change from postfix conversion is that traverse the expression from right to left and the operator is placed before the operands rather than after them. **The prefix form of a complex expression is not the mirror image of the postfix form.**
|
| 405 |
+
|
| 406 |
+
**Example 1:**
|
| 407 |
+
|
| 408 |
+
Convert the infix expression A + B - C into prefix expression.
|
| 409 |
+
|
| 410 |
+
| SYMBOL | PREFIX STRING | STACK | REMARKS |
|
| 411 |
+
|--------|---------------|-------|---------|
|
| 412 |
+
| C | C | | |
|
| 413 |
+
| - | C | - | |
|
| 414 |
+
| B | B C | - | |
|
| 415 |
+
| + | B C | - + | |
|
| 416 |
+
| A | A B C | - + | |
|
| 417 |
+
| End of string | - + A B C | | The input is now empty. Pop the output symbols from the stack until it is empty. |
|
| 418 |
+
|
| 419 |
+
**Example 2:**
|
| 420 |
+
|
| 421 |
+
Convert the infix expression (A + B) * (C - D) into prefix expression.
|
| 422 |
+
|
| 423 |
+
| SYMBOL | PREFIX STRING | STACK | REMARKS |
|
| 424 |
+
|--------|---------------|-------|---------|
|
| 425 |
+
| ) | | ) | |
|
| 426 |
+
| D | D | ) | |
|
| 427 |
+
| - | D | ) - | |
|
| 428 |
+
| C | C D | ) - | |
|
| 429 |
+
| ( | - C D | | |
|
| 430 |
+
| * | - C D | * | |
|
| 431 |
+
| ) | - C D | * ) | |
|
| 432 |
+
| B | B - C D | * ) | |
|
| 433 |
+
| + | B - C D | * ) + | |
|
| 434 |
+
| A | A B - C D | * ) + | |
|
| 435 |
+
| ( | + A B - C D | * | |
|
| 436 |
+
| End of string | * + A B - C D | | The input is now empty. Pop the output symbols from the stack until it is empty. |
|
| 437 |
+
|
| 438 |
+
7
|
| 439 |
+
|
| 440 |
+
------------PAGE TALK ABOUT------------
|
| 441 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุชุญููู ุงูุชุนุจูุฑุงุช ุงูุฑูุงุถูุฉ ู
ู ุงูุตูุบุฉ ุงููุณุทูุฉ Infix ุฅูู ุงูุตูุบุฉ ุงูุณุงุจูุฉ Prefix ุจุงุณุชุฎุฏุงู
ููุงุนุฏ ุงูุฃููููุฉ ููุณูุง ุงูู
ุณุชุฎุฏู
ุฉ ูู ุงูุชุญููู ุฅูู Postfix.
|
| 442 |
+
ุชูุถุญ ุฃู ุงููุฑู ุงูุฃุณุงุณู ูู ูุฑุงุกุฉ ุงูุชุนุจูุฑ ู
ู ุงููู
ูู ุฅูู ุงููุณุงุฑุ ููุถุน ุงูุนุงู
ู ูุจู ุงูู
ุนุงู
ูุงุช.
|
| 443 |
+
ุชุญุชูู ุงูุตูุญุฉ ุนูู ู
ุซุงููู ู
ุญููููู ุฎุทูุฉ ุจุฎุทูุฉ ุฏุงุฎู ุฌุฏุงูู: ุงูุฃูู ููุชุนุจูุฑ A + B - Cุ ูุงูุซุงูู ููุชุนุจูุฑ (A + B) * (C - D).
|
| 444 |
+
ุงูุนูุงุตุฑ ุงูุจุตุฑูุฉ ุงูุฃุณุงุณูุฉ ูู ุฌุฏุงูู ุจุฃุนู
ุฏุฉ: ุงูุฑู
ุฒุ ุณูุณูุฉ ุงูู Prefixุ ุงูู
ูุฏุณ Stackุ ูุงูู
ูุงุญุธุงุชุ ู
ุน ุชุชุจุน ุชุบููุฑ ุงูููู
ูู ูู ุฎุทูุฉ.
|
| 445 |
+
|
| 446 |
+
|
| 447 |
+
================================================================================
|
| 448 |
+
PAGE 8 - page_0008.png
|
| 449 |
+
================================================================================
|
| 450 |
+
|
| 451 |
+
------------TEXT EXTRACTED------------
|
| 452 |
+
ITGS 220
|
| 453 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 454 |
+
|
| 455 |
+
Example 3:
|
| 456 |
+
|
| 457 |
+
Convert the infix expression A โ B * C โ D + E / F / (G + H) into prefix expression.
|
| 458 |
+
|
| 459 |
+
| SYMBOL | PREFIX STRING | STACK | REMARKS |
|
| 460 |
+
|---|---|---|---|
|
| 461 |
+
| ) | | ) | |
|
| 462 |
+
| H | H | ) | |
|
| 463 |
+
| + | H | ) + | |
|
| 464 |
+
| G | G H | ) + | |
|
| 465 |
+
| ( | + G H | | |
|
| 466 |
+
| / | + G H | / | |
|
| 467 |
+
| F | F + G H | / | |
|
| 468 |
+
| / | F + G H | / / | |
|
| 469 |
+
| E | E F + G H | / / | |
|
| 470 |
+
| + | / / E F + G H | + | |
|
| 471 |
+
| D | D / / E F + G H | + | |
|
| 472 |
+
| - | D / / E F + G H | + - | |
|
| 473 |
+
| C | C D / / E F + G H | + - | |
|
| 474 |
+
| * | C D / / E F + G H | + - * | |
|
| 475 |
+
| B | B C D / / E F + G H | + - * | |
|
| 476 |
+
| โ | B C D / / E F + G H | + - * โ | |
|
| 477 |
+
| A | A B C D / / E F + G H | + - * โ | |
|
| 478 |
+
| End of string | + - * โ A B C D / / E F + G H | The input is now empty. Pop the output symbols from the stack until it is empty. | |
|
| 479 |
+
|
| 480 |
+
1.4.4 Program to convert an infix to prefix expression:
|
| 481 |
+
|
| 482 |
+
#include <stdio.h>
|
| 483 |
+
#include <string.h>
|
| 484 |
+
#include <ctype.h>
|
| 485 |
+
#include <stdlib.h>
|
| 486 |
+
|
| 487 |
+
#define MAX 100
|
| 488 |
+
|
| 489 |
+
// Function prototypes
|
| 490 |
+
void push(char item);
|
| 491 |
+
char pop();
|
| 492 |
+
int isOperator(char ch);
|
| 493 |
+
int precedence(char ch);
|
| 494 |
+
void reverseString(char* str);
|
| 495 |
+
void infixToPrefix(char* infix, char* prefix);
|
| 496 |
+
|
| 497 |
+
// Global stack variables
|
| 498 |
+
char stack[MAX];
|
| 499 |
+
int top = -1;
|
| 500 |
+
|
| 501 |
+
int main() {
|
| 502 |
+
ย ย ย ย char infix[MAX], prefix[MAX];
|
| 503 |
+
ย ย ย ย printf("Enter infix expression: ");
|
| 504 |
+
|
| 505 |
+
8
|
| 506 |
+
|
| 507 |
+
------------PAGE TALK ABOUT------------
|
| 508 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ู
ุซุงููุง ูุชุญููู ุชุนุจูุฑ ุฑูุงุถู ู
ู ุงูุตูุบุฉ ุงููุณุทูุฉ Infix ุฅูู ุงูุตูุบุฉ ุงูุณุงุจูุฉ Prefix ุจุงุณุชุฎุฏุงู
ุงูู
ูุฏุณ Stack.
|
| 509 |
+
ููุฌุฏ ุฌุฏูู ููุถุญ ุฎุทูุฉ ุจุฎุทูุฉ ููู ุชุชู
ู
ุนุงูุฌุฉ ุงูุฑู
ูุฒุ ูู
ุง ุงูุฐู ููุถุงู ุฅูู ุงูู prefix string ูู
ุง ุงูุฐู ููุฎุฒู ูู ุงูู stack.
|
| 510 |
+
ุงูู
ุซุงู ุงูู
ุณุชุฎุฏู
ูู: A โ B * C โ D + E / F / (G + H)ุ ูุงููุชูุฌุฉ ุงูููุงุฆูุฉ ุงูุธุงูุฑุฉ ูู: + - * โ A B C D / / E F + G H.
|
| 511 |
+
ูู
ุง ุชุนุฑุถ ุงูุตูุญุฉ ุจุฏุงูุฉ ุจุฑูุงู
ุฌ ุจูุบุฉ C ูุชูููุฐ ุงูุชุญูููุ ููุชุถู
ู ุงูู
ูุชุจุงุช ุงูู
ุทููุจุฉุ ุชุนุฑูู MAXุ ูุชุตุฑูุญุงุช ุงูุฏูุงู ูุงูู
ุชุบูุฑุงุช ุงูุนุงู
ุฉ.
|
| 512 |
+
|
| 513 |
+
|
| 514 |
+
================================================================================
|
| 515 |
+
PAGE 9 - page_0009.png
|
| 516 |
+
================================================================================
|
| 517 |
+
|
| 518 |
+
------------TEXT EXTRACTED------------
|
| 519 |
+
ITGS 220
|
| 520 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 521 |
+
|
| 522 |
+
fgets(infix, MAX, stdin);
|
| 523 |
+
|
| 524 |
+
infix[strlen(infix)-1] = '\0'; // Remove newline character
|
| 525 |
+
|
| 526 |
+
infixToPrefix(infix, prefix);
|
| 527 |
+
printf("Prefix expression: %s\n", prefix);
|
| 528 |
+
|
| 529 |
+
return 0;
|
| 530 |
+
}
|
| 531 |
+
|
| 532 |
+
void push(char item) {
|
| 533 |
+
if (top >= MAX-1) {
|
| 534 |
+
printf("Stack Overflow\n");
|
| 535 |
+
return;
|
| 536 |
+
}
|
| 537 |
+
stack[++top] = item;
|
| 538 |
+
}
|
| 539 |
+
|
| 540 |
+
char pop() {
|
| 541 |
+
if (top < 0) {
|
| 542 |
+
printf("Stack Underflow\n");
|
| 543 |
+
return '\0';
|
| 544 |
+
}
|
| 545 |
+
return stack[top--];
|
| 546 |
+
}
|
| 547 |
+
|
| 548 |
+
int isOperator(char ch) {
|
| 549 |
+
return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^');
|
| 550 |
+
}
|
| 551 |
+
|
| 552 |
+
int precedence(char ch) {
|
| 553 |
+
switch(ch) {
|
| 554 |
+
case '^': return 4;
|
| 555 |
+
case '*':
|
| 556 |
+
case '/': return 3;
|
| 557 |
+
case '+':
|
| 558 |
+
case '-': return 2;
|
| 559 |
+
default: return 0;
|
| 560 |
+
}
|
| 561 |
+
}
|
| 562 |
+
|
| 563 |
+
void reverseString(char* str) {
|
| 564 |
+
int n = strlen(str);
|
| 565 |
+
for (int i = 0; i < n/2; i++) {
|
| 566 |
+
char temp = str[i];
|
| 567 |
+
str[i] = str[n-i-1];
|
| 568 |
+
str[n-i-1] = temp;
|
| 569 |
+
}
|
| 570 |
+
}
|
| 571 |
+
|
| 572 |
+
void infixToPrefix(char* infix, char* prefix) {
|
| 573 |
+
char tempInfix[MAX], tempPrefix[MAX];
|
| 574 |
+
int i = 0, j = 0;
|
| 575 |
+
|
| 576 |
+
// Step 1: Reverse the infix expression
|
| 577 |
+
strcpy(tempInfix, infix);
|
| 578 |
+
reverseString(tempInfix);
|
| 579 |
+
|
| 580 |
+
// Step 2: Replace '(' with ')' and vice versa
|
| 581 |
+
for (i = 0; tempInfix[i] != '\0'; i++) {
|
| 582 |
+
|
| 583 |
+
9
|
| 584 |
+
|
| 585 |
+
------------PAGE TALK ABOUT------------
|
| 586 |
+
ุงูุตูุญุฉ ุชุนุฑุถ ุฌุฒุกูุง ู
ู ููุฏ ุจูุบุฉ C ูุชุญููู ุงูุชุนุจูุฑ ู
ู Infix ุฅูู Prefix ุจุงุณุชุฎุฏุงู
ุงูู
ูุฏุณ Stack.
|
| 587 |
+
ุชุดุฑุญ ุงูุฏูุงู ุงูุฃุณุงุณูุฉ ู
ุซู push ู popุ ููุญุต ู
ุง ุฅุฐุง ูุงู ุงูุฑู
ุฒ ุนุงู
ููุง ุฑูุงุถููุงุ ูุชุญุฏูุฏ ุฃููููุฉ ุงูุนู
ููุงุช ู
ุซู ^ ู * ู / ู + ู -.
|
| 588 |
+
ูู
ุง ุชุญุชูู ุนูู ุฏุงูุฉ ูุนูุณ ุงููุต reverseString ูุจุฏุงูุฉ ุฏุงูุฉ infixToPrefix ุงูุชู ุชุจุฏุฃ ุจุนูุณ ุงูุชุนุจูุฑ ุซู
ุชุจุฏูู ุงูุฃููุงุณ.
|
| 589 |
+
ุจุตุฑููุงุ ุงูุตูุญุฉ ุนุจุงุฑุฉ ุนู ูุฑูุฉ ู
ุญุงุถุฑุฉ ุฃูุงุฏูู
ูุฉ ูููุง ุนููุงู ุงูู
ูุฑุฑ ูุงุณู
ุงูุฏูุชูุฑุฉ ูุงููุตู ุงูุฏุฑุงุณู ูุฑูู
ุงูุตูุญุฉ 9ุ ููุง ุชูุฌุฏ ุตูุฑ ุฃู ู
ุฎุทุทุงุช.
|
| 590 |
+
|
| 591 |
+
|
| 592 |
+
================================================================================
|
| 593 |
+
PAGE 10 - page_0010.png
|
| 594 |
+
================================================================================
|
| 595 |
+
|
| 596 |
+
------------TEXT EXTRACTED------------
|
| 597 |
+
ITGS 220
|
| 598 |
+
Dr. Amna Ali A Mohamed
|
| 599 |
+
|
| 600 |
+
Fall 2025
|
| 601 |
+
|
| 602 |
+
if (tempInfix[i] == '(') tempInfix[i] = ')';
|
| 603 |
+
else if (tempInfix[i] == ')') tempInfix[i] = '(';
|
| 604 |
+
}
|
| 605 |
+
|
| 606 |
+
// Step 3: Convert to postfix with modified operator handling
|
| 607 |
+
push('(');
|
| 608 |
+
strcat(tempInfix, ")");
|
| 609 |
+
|
| 610 |
+
i = 0;
|
| 611 |
+
j = 0;
|
| 612 |
+
char item = tempInfix[i];
|
| 613 |
+
|
| 614 |
+
while (item != '\0') {
|
| 615 |
+
if (item == '(') {
|
| 616 |
+
push(item);
|
| 617 |
+
}
|
| 618 |
+
else if (isalnum(item)) {
|
| 619 |
+
tempPrefix[j++] = item;
|
| 620 |
+
}
|
| 621 |
+
else if (isOperator(item)) {
|
| 622 |
+
while (top >= 0 && stack[top] != '(' &&
|
| 623 |
+
precedence(stack[top]) > precedence(item)) {
|
| 624 |
+
tempPrefix[j++] = pop();
|
| 625 |
+
}
|
| 626 |
+
push(item);
|
| 627 |
+
}
|
| 628 |
+
else if (item == ')') {
|
| 629 |
+
while (stack[top] != '(') {
|
| 630 |
+
tempPrefix[j++] = pop();
|
| 631 |
+
}
|
| 632 |
+
pop(); // Remove '(' from stack
|
| 633 |
+
}
|
| 634 |
+
i++;
|
| 635 |
+
item = tempInfix[i];
|
| 636 |
+
}
|
| 637 |
+
|
| 638 |
+
tempPrefix[j] = '\0';
|
| 639 |
+
|
| 640 |
+
// Step 4: Reverse the postfix expression to get prefix
|
| 641 |
+
strcpy(prefix, tempPrefix);
|
| 642 |
+
reverseString(prefix);
|
| 643 |
+
}
|
| 644 |
+
|
| 645 |
+
10
|
| 646 |
+
|
| 647 |
+
------------PAGE TALK ABOUT------------
|
| 648 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุฌุฒุก ู
ู ููุฏ ุจูุบุฉ C ูุชุญููู ุงูุชุนุจูุฑ ู
ู infix ุฅูู prefix ุจุงุณุชุฎุฏุงู
ุงูู
ูุฏุณ (stack).
|
| 649 |
+
ุชูุถุญ ุงูุฎุทูุงุช ุชุจุฏูู ุงูุฃููุงุณ ุฃููุงูุ ุซู
ุชุญููู ุงูุชุนุจูุฑ ุฅูู postfix ุจุทุฑููุฉ ู
ุนุฏูุฉ ุชุนุชู
ุฏ ุนูู ุฃููููุฉ ุงูุนู
ููุงุช.
|
| 650 |
+
ุจุนุฏ ุฐูู ูุชู
ุชุฎุฒูู ุงููุงุชุฌ ูู tempPrefix ุซู
ุนูุณู ููุญุตูู ุนูู prefix ุงูููุงุฆู.
|
| 651 |
+
ุงูุนูุงุตุฑ ุงูุจุตุฑูุฉ ูู ุงูุตูุญุฉ ุนุจุงุฑุฉ ุนู ุชูุณูู ู
ุญุงุถุฑุฉ ุฌุงู
ุนูุฉ ูุชุถู
ู ุงุณู
ุงูู
ูุฑุฑ ูุงูู
ุฏุฑุณุฉ ูุงููุตู ุงูุฏุฑุงุณู ูุฑูู
ุงูุตูุญุฉ ููุทุ ููุง ุชูุฌุฏ ุตูุฑ ุฃู ู
ุฎุทุทุงุช.
|
| 652 |
+
|
| 653 |
+
|
| 654 |
+
================================================================================
|
| 655 |
+
PAGE 11 - page_0011.png
|
| 656 |
+
================================================================================
|
| 657 |
+
|
| 658 |
+
------------TEXT EXTRACTED------------
|
| 659 |
+
ITGS 220
|
| 660 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 661 |
+
|
| 662 |
+
**1.4.5 Conversion from postfix to infix:**
|
| 663 |
+
|
| 664 |
+
Procedure to convert postfix expression to infix expression is as follows:
|
| 665 |
+
|
| 666 |
+
1. Scan the postfix expression from left to right.
|
| 667 |
+
|
| 668 |
+
2. If the scanned symbol is an operand, then push it onto the stack.
|
| 669 |
+
|
| 670 |
+
3. If the scanned symbol is an operator, pop two symbols from the stack and create it as a string by placing the operator in between the operands and push it onto the stack.
|
| 671 |
+
|
| 672 |
+
4. Repeat steps 2 and 3 till the end of the expression.
|
| 673 |
+
|
| 674 |
+
**Example:**
|
| 675 |
+
|
| 676 |
+
Convert the following postfix expression A B C * D E F ^ / G * - H * + into its equivalent infix expression.
|
| 677 |
+
|
| 678 |
+
**Symbol**ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย **Stack**ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย **Remarks**
|
| 679 |
+
|
| 680 |
+
Aย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Aย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Push A
|
| 681 |
+
|
| 682 |
+
Bย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Aย ย ย Bย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Push B
|
| 683 |
+
|
| 684 |
+
Cย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Aย ย ย Bย ย ย Cย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Push C
|
| 685 |
+
|
| 686 |
+
*ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Aย ย ย (B*C)ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Pop two operands and place the operator in between the operands and push the string.
|
| 687 |
+
|
| 688 |
+
Dย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Aย ย ย (B*C)ย ย ย Dย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Push D
|
| 689 |
+
|
| 690 |
+
Eย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Aย ย ย (B*C)ย ย ย Dย ย ย Eย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Push E
|
| 691 |
+
|
| 692 |
+
Fย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Aย ย ย (B*C)ย ย ย Dย ย ย Eย ย ย Fย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Push F
|
| 693 |
+
|
| 694 |
+
^ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Aย ย ย (B*C)ย ย ย Dย ย ย (E^F)ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Pop two operands and place the operator in between the operands and push the string.
|
| 695 |
+
|
| 696 |
+
/
|
| 697 |
+
|
| 698 |
+
Aย ย ย (B*C)ย ย ย (D/(E^F))ย ย ย ย ย ย ย ย ย ย Pop two operands and place the operator in between the operands and push the string.
|
| 699 |
+
|
| 700 |
+
Gย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Aย ย ย (B*C)ย ย ย (D/(E^F))ย ย ย Gย ย ย ย ย ย ย ย ย ย ย ย ย Push G
|
| 701 |
+
|
| 702 |
+
*ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Aย ย ย (B*C)ย ย ย ((D/(E^F))*G)ย ย ย ย ย ย ย ย ย ย ย ย ย Pop two operands and place the operator in between the operands and push the string.
|
| 703 |
+
|
| 704 |
+
-ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Aย ย ย ((B*C) โ ((D/(E^F))*G))ย ย ย ย ย ย ย ย ย ย ย Pop two operands and place the operator in between the operands and push the string.
|
| 705 |
+
|
| 706 |
+
Hย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Aย ย ย ((B*C) โ ((D/(E^F))*G))ย ย ย Hย ย ย ย ย ย ย Push H
|
| 707 |
+
|
| 708 |
+
*ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Aย ย ย (((B*C) โ ((D/(E^F))*G)) * H)ย ย ย ย ย Pop two operands and place the operator in between the operands and push the string.
|
| 709 |
+
|
| 710 |
+
+ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย (A + (((B*C) โ ((D/(E^F))*G)) * H))ย ย ย Pop two operands and place the operator in between the operands and push the string.
|
| 711 |
+
|
| 712 |
+
End of
|
| 713 |
+
stringย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย The input is now empty. The string formed is infix.
|
| 714 |
+
|
| 715 |
+
11
|
| 716 |
+
|
| 717 |
+
------------PAGE TALK ABOUT------------
|
| 718 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ุทุฑููุฉ ุชุญููู ุงูุชุนุจูุฑ ู
ู **Postfix** ุฅูู **Infix** ุจุงุณุชุฎุฏุงู
**ุงูู
ูุฏุณ (Stack)**.
|
| 719 |
+
ุชุฐูุฑ ุงูุฎุทูุงุช ุงูุฃุณุงุณูุฉ: ูุฑุงุกุฉ ุงูุฑู
ูุฒ ู
ู ุงููุณุงุฑ ูููู
ููุ ุฏูุน ุงูู
ุนุงู
ูุงุช ุฅูู ุงูู
ูุฏุณุ ูุนูุฏ ุธููุฑ ุนุงู
ู ูุชู
ุณุญุจ ู
ุนุงู
ููู ูุชูููู ุชุนุจูุฑ infix ุซู
ุฅุนุงุฏุชู ููู
ูุฏุณ.
|
| 720 |
+
ููุฌุฏ ู
ุซุงู ุชูุตููู ููุชุนุจูุฑ: **A B C * D E F ^ / G * - H * +** ู
ุน ุฌุฏูู ููุถุญ ูู ูู ุฎุทูุฉ ุงูุฑู
ุฒ ุงูู
ูุฑูุกุ ุญุงูุฉ ุงูู
ูุฏุณุ ูุงูู
ูุงุญุธุฉ.
|
| 721 |
+
ุงูุนูุตุฑ ุงูุจุตุฑู ุงูุฃุณุงุณู ูู **ุฌุฏูู ุชุชุจุนู** ูุจูู ุชุทูุฑ ู
ุญุชูู ุงูู
ูุฏุณ ุญุชู ุงููุตูู ุฅูู ุงูุชุนุจูุฑ ุงูููุงุฆู: **(A + (((B*C) โ ((D/(E^F))*G)) * H))**.
|
| 722 |
+
|
| 723 |
+
|
| 724 |
+
================================================================================
|
| 725 |
+
PAGE 12 - page_0012.png
|
| 726 |
+
================================================================================
|
| 727 |
+
|
| 728 |
+
------------TEXT EXTRACTED------------
|
| 729 |
+
ITGS 220
|
| 730 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 731 |
+
|
| 732 |
+
____________________________________________________________________
|
| 733 |
+
|
| 734 |
+
1.4.6ย ย Program to convert postfix to infix expression:
|
| 735 |
+
|
| 736 |
+
#include <stdio.h>
|
| 737 |
+
#include <stdlib.h>
|
| 738 |
+
#include <string.h>
|
| 739 |
+
#include <ctype.h>
|
| 740 |
+
|
| 741 |
+
#define MAX 100
|
| 742 |
+
|
| 743 |
+
// Stack structure to hold strings
|
| 744 |
+
typedef struct {
|
| 745 |
+
ย ย ย ย char data[MAX][MAX];
|
| 746 |
+
ย ย ย ย int top;
|
| 747 |
+
} Stack;
|
| 748 |
+
|
| 749 |
+
// Function prototypes
|
| 750 |
+
void initStack(Stack *s);
|
| 751 |
+
int isFull(Stack *s);
|
| 752 |
+
int isEmpty(Stack *s);
|
| 753 |
+
void push(Stack *s, char *str);
|
| 754 |
+
void pop(Stack *s, char *str);
|
| 755 |
+
void postfixToInfix(char *postfix, char *infix);
|
| 756 |
+
|
| 757 |
+
int main() {
|
| 758 |
+
ย ย ย ย char postfix[MAX], infix[MAX];
|
| 759 |
+
|
| 760 |
+
ย ย ย ย printf("Enter postfix expression: ");
|
| 761 |
+
ย ย ย ย fgets(postfix, MAX, stdin);
|
| 762 |
+
ย ย ย ย postfix[strlen(postfix)-1] = '\0'; // Remove newline character
|
| 763 |
+
|
| 764 |
+
ย ย ย ย postfixToInfix(postfix, infix);
|
| 765 |
+
ย ย ย ย printf("Infix expression: %s\n", infix);
|
| 766 |
+
|
| 767 |
+
ย ย ย ย return 0;
|
| 768 |
+
}
|
| 769 |
+
|
| 770 |
+
// Initialize stack
|
| 771 |
+
void initStack(Stack *s) {
|
| 772 |
+
ย ย ย ย s->top = -1;
|
| 773 |
+
}
|
| 774 |
+
|
| 775 |
+
// Check if stack is full
|
| 776 |
+
int isFull(Stack *s) {
|
| 777 |
+
ย ย ย ย return s->top == MAX - 1;
|
| 778 |
+
}
|
| 779 |
+
|
| 780 |
+
// Check if stack is empty
|
| 781 |
+
int isEmpty(Stack *s) {
|
| 782 |
+
ย ย ย ย return s->top == -1;
|
| 783 |
+
}
|
| 784 |
+
|
| 785 |
+
// Push operation
|
| 786 |
+
void push(Stack *s, char *str) {
|
| 787 |
+
ย ย ย ย if (isFull(s)) {
|
| 788 |
+
ย ย ย ย ย ย ย ย printf("Stack Overflow\n");
|
| 789 |
+
ย ย ย ย ย ย ย ย return;
|
| 790 |
+
ย ย ย ย }
|
| 791 |
+
ย ย ย ย strcpy(s->data[++s->top], str);
|
| 792 |
+
}
|
| 793 |
+
|
| 794 |
+
12
|
| 795 |
+
|
| 796 |
+
------------PAGE TALK ABOUT------------
|
| 797 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุจุฑูุงู
ุฌ ุจูุบุฉ C ูุชุญููู ุงูุชุนุจูุฑ ู
ู postfix ุฅูู infix ุจุงุณุชุฎุฏุงู
ุจููุฉ ุงูู
ูุฏุณ Stack.
|
| 798 |
+
ุชุญุชูู ุนูู ุชุถู
ูู ุงูู
ูุชุจุงุชุ ูุชุนุฑูู ุซุงุจุช MAXุ ุซู
ุชุนุฑูู struct ููู
ูุฏุณ ูุชุฎุฒูู ุณูุงุณู ูุตูุฉ.
|
| 799 |
+
ูู
ุง ุชุนุฑุถ ุชุฑููุณุงุช ุงูุฏูุงู ุงูุฃุณุงุณูุฉ ู
ุซู ุงูุชููุฆุฉุ ููุญุต ุงูุงู
ุชูุงุก ูุงููุฑุงุบุ ูุนู
ููุชู push ู popุ ูุฏุงูุฉ ุงูุชุญููู postfixToInfix.
|
| 800 |
+
ููุฌุฏ ุฃูุถูุง ุฌุฒุก ู
ู ุงูุฏุงูุฉ main ููุฑุงุกุฉ ุงูุชุนุจูุฑ ู
ู ุงูู
ุณุชุฎุฏู
ูุทุจุงุนุฉ ุงููุงุชุฌุ ู
ุน ุจุฏุงูุฉ ุชูููุฐ ุฏูุงู ุงูู
ูุฏุณ.
|
| 801 |
+
|
| 802 |
+
|
| 803 |
+
================================================================================
|
| 804 |
+
PAGE 13 - page_0013.png
|
| 805 |
+
================================================================================
|
| 806 |
+
|
| 807 |
+
------------TEXT EXTRACTED------------
|
| 808 |
+
ITGS 220
|
| 809 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 810 |
+
|
| 811 |
+
// Pop operation
|
| 812 |
+
void pop(Stack *s, char *str) {
|
| 813 |
+
ย ย ย ย if (isEmpty(s)) {
|
| 814 |
+
ย ย ย ย ย ย ย ย printf("Stack Underflow\n");
|
| 815 |
+
ย ย ย ย ย ย ย ย str[0] = '\0';
|
| 816 |
+
ย ย ย ย ย ย ย ย return;
|
| 817 |
+
ย ย ย ย }
|
| 818 |
+
ย ย ย ย strcpy(str, s->data[s->top--]);
|
| 819 |
+
}
|
| 820 |
+
|
| 821 |
+
// Function to convert postfix to infix
|
| 822 |
+
void postfixToInfix(char *postfix, char *infix) {
|
| 823 |
+
ย ย ย ย Stack s;
|
| 824 |
+
ย ย ย ย initStack(&s);
|
| 825 |
+
ย ย ย ย char temp[MAX], op1[MAX], op2[MAX];
|
| 826 |
+
|
| 827 |
+
ย ย ย ย for (int i = 0; postfix[i] != '\0'; i++) {
|
| 828 |
+
ย ย ย ย ย ย ย ย char ch = postfix[i];
|
| 829 |
+
|
| 830 |
+
ย ย ๏ฟฝ๏ฟฝย ย ย ย ย // If operand, push to stack
|
| 831 |
+
ย ย ย ย ย ย ย ย if (isalnum(ch)) {
|
| 832 |
+
ย ย ย ย ย ย ย ย ย ย ย ย temp[0] = ch;
|
| 833 |
+
ย ย ย ย ย ย ย ย ย ย ย ย temp[1] = '\0';
|
| 834 |
+
ย ย ย ย ย ย ย ย ย ย ย ย push(&s, temp);
|
| 835 |
+
ย ย ย ย ย ย ย ย }
|
| 836 |
+
ย ย ย ย ย ย ย ย // If operator, pop two operands and combine with operator
|
| 837 |
+
ย ย ย ย ย ย ย ย else {
|
| 838 |
+
ย ย ย ย ย ย ย ย ย ย ย ย pop(&s, op2);
|
| 839 |
+
ย ย ย ย ย ย ย ย ย ย ย ย pop(&s, op1);
|
| 840 |
+
|
| 841 |
+
ย ย ย ย ย ย ย ย ย ย ย ย // Format the expression with parentheses
|
| 842 |
+
ย ย ย ย ย ย ย ย ย ย ย ย sprintf(temp, "(%s%c%s)", op1, ch, op2);
|
| 843 |
+
ย ย ย ย ย ย ย ย ย ย ย ย push(&s, temp);
|
| 844 |
+
ย ย ย ย ย ย ย ย }
|
| 845 |
+
ย ย ย ย }
|
| 846 |
+
|
| 847 |
+
ย ย ย ย // The final infix expression is at the top of stack
|
| 848 |
+
ย ย ย ย pop(&s, infix);
|
| 849 |
+
}
|
| 850 |
+
|
| 851 |
+
1.4.7ย ย Conversion from prefix to infix:
|
| 852 |
+
|
| 853 |
+
Procedure to convert prefix expression to infix expression is as follows:
|
| 854 |
+
|
| 855 |
+
1.ย ย Scan the prefix expression from right to left (reverse order).
|
| 856 |
+
|
| 857 |
+
2.ย ย If the scanned symbol is an operand, then push it onto the stack.
|
| 858 |
+
|
| 859 |
+
3.ย ย If the scanned symbol is an operator, pop two symbols from the stack and create
|
| 860 |
+
ย ย ย ย it as a string by placing the operator in between the operands and push it onto
|
| 861 |
+
ย ย ย ย the stack.
|
| 862 |
+
|
| 863 |
+
4.ย ย Repeat steps 2 and 3 till the end of the expression.
|
| 864 |
+
|
| 865 |
+
Example:
|
| 866 |
+
|
| 867 |
+
Convert the following prefix expression + A * - * B C * / D ^ E F G H into its equivalent
|
| 868 |
+
infix expression.
|
| 869 |
+
|
| 870 |
+
13
|
| 871 |
+
|
| 872 |
+
------------PAGE TALK ABOUT------------
|
| 873 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ุชุญููู ุงูุชุนุงุจูุฑ ุจุงุณุชุฎุฏุงู
ุงูู
ูุฏุณ (Stack)ุ ูุชุนุฑุถ ููุฏ ุจูุบุฉ C ูุนู
ููุฉ `pop` ูุฏุงูุฉ ุชุญููู ุงูุชุนุจูุฑ ู
ู postfix ุฅูู infix.
|
| 874 |
+
ุจุนุฏ ุฐูู ุชุจุฏุฃ ููุฑุฉ ุฌุฏูุฏุฉ ุจุนููุงู **Conversion from prefix to infix** ูุชูุถุญ ุฎุทูุงุช ุงูุชุญููู ู
ู prefix ุฅูู infix ุฎุทูุฉ ุจุฎุทูุฉ.
|
| 875 |
+
ูู
ุง ุชุญุชูู ุงูุตูุญุฉ ุนูู ู
ุซุงู ูุชุนุจูุฑ prefix ู
ุทููุจ ุชุญูููู ุฅูู infix.
|
| 876 |
+
ุจุตุฑููุงุ ุงูุตูุญุฉ ุนุจุงุฑุฉ ุนู ู
ูุงุญุธุงุช/ุดุฑูุญุฉ ุฏุฑุงุณูุฉ ูุตูุฉ ููุท ุจุฏูู ุตูุฑ ุฃู ู
ุฎุทุทุงุชุ ู
ุน ุนููุงู ุงูู
ูุฑุฑ ูุงุณู
ุงูุฏูุชูุฑุฉ ูุงููุตู ุงูุฏุฑุงุณู ูุฑูู
ุงูุตูุญุฉ.
|
| 877 |
+
|
| 878 |
+
|
| 879 |
+
================================================================================
|
| 880 |
+
PAGE 14 - page_0014.png
|
| 881 |
+
================================================================================
|
| 882 |
+
|
| 883 |
+
------------TEXT EXTRACTED------------
|
| 884 |
+
ITGS 220
|
| 885 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 886 |
+
|
| 887 |
+
Symbolย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Stackย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Remarks
|
| 888 |
+
|
| 889 |
+
Hย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Hย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Push H
|
| 890 |
+
|
| 891 |
+
Gย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Hย ย Gย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Push G
|
| 892 |
+
|
| 893 |
+
Fย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Hย ย Gย ย Fย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Push F
|
| 894 |
+
|
| 895 |
+
Eย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Hย ย Gย ย Fย ย Eย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Push E
|
| 896 |
+
|
| 897 |
+
^ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Hย ย Gย ย (E^F)ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Pop two operands and place the operator
|
| 898 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย in between the operands and push the
|
| 899 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย string.
|
| 900 |
+
|
| 901 |
+
Dย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Hย ย Gย ย (E^F)ย ย Dย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Push D
|
| 902 |
+
|
| 903 |
+
/ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Hย ย Gย ย (D/(E^F))ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Pop two operands and place the operator
|
| 904 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย in between the operands and push the
|
| 905 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย string.
|
| 906 |
+
|
| 907 |
+
*ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Hย ย ((D/(E^F))*G)ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Pop two operands and place the operator
|
| 908 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย in between the operands and push the
|
| 909 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย string.
|
| 910 |
+
|
| 911 |
+
Cย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Hย ย ((D/(E^F))*G)ย ย Cย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Push C
|
| 912 |
+
|
| 913 |
+
Bย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Hย ย ((D/(E^F))*G)ย ย Cย ย Bย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Push B
|
| 914 |
+
|
| 915 |
+
*ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Hย ย ((D/(E^F))*G)ย ย (B*C)ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Pop two operands and place the operator
|
| 916 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย in front the operands and push the
|
| 917 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย string.
|
| 918 |
+
|
| 919 |
+
-ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Hย ย ((B*C)-((D/(E^F))*G))ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Pop two operands and place the operator
|
| 920 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย in front the operands and push the
|
| 921 |
+
|
| 922 |
+
14
|
| 923 |
+
|
| 924 |
+
------------PAGE TALK ABOUT------------
|
| 925 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ุงุณุชุฎุฏุงู
ุงูู
ูุฏุณ (Stack) ูู
ุนุงูุฌุฉ ุชุนุจูุฑ ุฑูุงุถู ุฑู
ุฒู ุฎุทูุฉ ุจุฎุทูุฉ.
|
| 926 |
+
ููุฌุฏ ุฌุฏูู ุจุซูุงุซุฉ ุฃุนู
ุฏุฉ: ุงูุฑู
ุฒุ ุญุงูุฉ ุงูู
ูุฏุณุ ูุงูู
ูุงุญุธุงุชุ ููุจูู ู
ุชู ูุชู
ุฏูุน ุงูุนูุงุตุฑ ู
ุซู H ูG ูF ูE ุฅูู ุงูู
ูุฏุณ.
|
| 927 |
+
ูู
ุง ููุถุญ ููู ูุชู
ุณุญุจ ู
ุนุงู
ููู ุนูุฏ ุธููุฑ ุนุงู
ู ู
ุซู ^ ุฃู / ุฃู * ุฃู - ุซู
ุชูููู ุชุนุจูุฑ ุฌุฏูุฏ ู
ุซู (E^F) ู(D/(E^F)).
|
| 928 |
+
ุจุตุฑููุงุ ุชูุฌุฏ ุฑุณูู
ุงุช ุตูุงุฏูู ุชู
ุซู ุฎูุงูุง ุงูู
ูุฏุณ ูู ูู ุฎุทูุฉุ ู
ุน ุนุฑุถ ู
ุญุชูู ุงูู
ูุฏุณ ุจุนุฏ ูู ุนู
ููุฉ.
|
| 929 |
+
|
| 930 |
+
|
| 931 |
+
================================================================================
|
| 932 |
+
PAGE 15 - page_0015.png
|
| 933 |
+
================================================================================
|
| 934 |
+
|
| 935 |
+
------------TEXT EXTRACTED------------
|
| 936 |
+
ITGS 220
|
| 937 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 938 |
+
|
| 939 |
+
string.
|
| 940 |
+
|
| 941 |
+
*
|
| 942 |
+
(((B*C)-((D)/(E^F))*G))*H)
|
| 943 |
+
|
| 944 |
+
Pop two operands and place the operator
|
| 945 |
+
in front the operands and push the
|
| 946 |
+
string.
|
| 947 |
+
|
| 948 |
+
A
|
| 949 |
+
(((B*C)-((D)/(E^F))*G))*H)ย ย ย ย A
|
| 950 |
+
|
| 951 |
+
Push A
|
| 952 |
+
|
| 953 |
+
+
|
| 954 |
+
(A+((((B*C)-((D)/(E^F))*G))*H))
|
| 955 |
+
|
| 956 |
+
Pop two operands and place the operator
|
| 957 |
+
in front the operands and push the
|
| 958 |
+
string.
|
| 959 |
+
|
| 960 |
+
End of
|
| 961 |
+
string
|
| 962 |
+
|
| 963 |
+
The input is now empty. The string formed is infix.
|
| 964 |
+
|
| 965 |
+
1.4.8 Program to convert prefix to infix expression:
|
| 966 |
+
|
| 967 |
+
#include <stdio.h>
|
| 968 |
+
#include <stdlib.h>
|
| 969 |
+
#include <string.h>
|
| 970 |
+
#include <ctype.h>
|
| 971 |
+
|
| 972 |
+
#define MAX 100
|
| 973 |
+
|
| 974 |
+
// Stack structure to hold strings
|
| 975 |
+
typedef struct {
|
| 976 |
+
char data[MAX][MAX];
|
| 977 |
+
int top;
|
| 978 |
+
} Stack;
|
| 979 |
+
|
| 980 |
+
// Function prototypes
|
| 981 |
+
void initStack(Stack *s);
|
| 982 |
+
int isFull(Stack *s);
|
| 983 |
+
int isEmpty(Stack *s);
|
| 984 |
+
void push(Stack *s, char *str);
|
| 985 |
+
void pop(Stack *s, char *str);
|
| 986 |
+
void prefixToInfix(char *prefix, char *infix);
|
| 987 |
+
void reverseString(char *str);
|
| 988 |
+
|
| 989 |
+
int main() {
|
| 990 |
+
char prefix[MAX], infix[MAX];
|
| 991 |
+
|
| 992 |
+
printf("Enter prefix expression: ");
|
| 993 |
+
fgets(prefix, MAX, stdin);
|
| 994 |
+
prefix[strlen(prefix)-1] = '\0'; // Remove newline character
|
| 995 |
+
|
| 996 |
+
prefixToInfix(prefix, infix);
|
| 997 |
+
printf("Infix expression: %s\n", infix);
|
| 998 |
+
|
| 999 |
+
return 0;
|
| 1000 |
+
}
|
| 1001 |
+
|
| 1002 |
+
// Initialize stack
|
| 1003 |
+
void initStack(Stack *s) {
|
| 1004 |
+
s->top = -1;
|
| 1005 |
+
}
|
| 1006 |
+
|
| 1007 |
+
// Check if stack is full
|
| 1008 |
+
int isFull(Stack *s) {
|
| 1009 |
+
return s->top == MAX - 1;
|
| 1010 |
+
}
|
| 1011 |
+
|
| 1012 |
+
15
|
| 1013 |
+
|
| 1014 |
+
------------PAGE TALK ABOUT------------
|
| 1015 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ุชุญููู ุงูุชุนุจูุฑ ู
ู **Prefix ุฅูู Infix** ุจุงุณุชุฎุฏุงู
**Stack**.
|
| 1016 |
+
ูู ุงูุฃุนูู ููุฌุฏ ู
ุฎุทุท ุชุชุจูุน ููุถุญ ุฎุทูุงุช ุฅุฏุฎุงู ุงูุฑู
ูุฒ ุฅูู ุงูู
ูุฏุณ ุซู
ุนูุฏ ุธููุฑ ุนุงู
ู ูุชู
ุณุญุจ ู
ุนุงู
ููู ูุชูููู ุชุนุจูุฑ infix ู
ุซู: `(A+((((B*C)-((D)/(E^F))*G))*H))`.
|
| 1017 |
+
ูู ุงูุฃุณูู ููุฌุฏ ุจุฏุงูุฉ ุจุฑูุงู
ุฌ ุจูุบุฉ **C** ูุนุฑูู ุงูู
ูุฏุณุ ูุงูุฏูุงู ุงูุฃุณุงุณูุฉ ู
ุซู `push` ู`pop` ู`prefixToInfix`ุ ู
ุน ุฌุฒุก ู
ู `main` ููุฑุงุกุฉ ุงูุชุนุจูุฑ ูุทุจุงุนุฉ ุงููุงุชุฌ.
|
| 1018 |
+
ูู
ุง ุชุธูุฑ ุนูุงุตุฑ ุชูุณูููุฉ ููุตูุญุฉ ู
ุซู ุนููุงู ุงูู
ูุฑุฑุ ุงุณู
ุงูุฏูุชูุฑุฉุ ุงููุตู ุงูุฏุฑุงุณูุ ูุฑูู
ุงูุตูุญุฉ.
|
| 1019 |
+
|
| 1020 |
+
|
| 1021 |
+
================================================================================
|
| 1022 |
+
PAGE 16 - page_0016.png
|
| 1023 |
+
================================================================================
|
| 1024 |
+
|
| 1025 |
+
------------TEXT EXTRACTED------------
|
| 1026 |
+
ITGS 220
|
| 1027 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 1028 |
+
|
| 1029 |
+
}
|
| 1030 |
+
|
| 1031 |
+
// Check if stack is empty
|
| 1032 |
+
int isEmpty(Stack *s) {
|
| 1033 |
+
ย ย ย ย return s->top == -1;
|
| 1034 |
+
}
|
| 1035 |
+
|
| 1036 |
+
// Push operation
|
| 1037 |
+
void push(Stack *s, char *str) {
|
| 1038 |
+
ย ย ย ย if (isFull(s)) {
|
| 1039 |
+
ย ย ย ย ย ย ย ๏ฟฝ๏ฟฝprintf("Stack Overflow\n");
|
| 1040 |
+
ย ย ย ย ย ย ย ย return;
|
| 1041 |
+
ย ย ย ย }
|
| 1042 |
+
ย ย ย ย strcpy(s->data[++s->top], str);
|
| 1043 |
+
}
|
| 1044 |
+
|
| 1045 |
+
// Pop operation
|
| 1046 |
+
void pop(Stack *s, char *str) {
|
| 1047 |
+
ย ย ย ย if (isEmpty(s)) {
|
| 1048 |
+
ย ย ย ย ย ย ย ย printf("Stack Underflow\n");
|
| 1049 |
+
ย ย ย ย ย ย ย ย str[0] = '\0';
|
| 1050 |
+
ย ย ย ย ย ย ย ย return;
|
| 1051 |
+
ย ย ย ย }
|
| 1052 |
+
ย ย ย ย strcpy(str, s->data[s->top--]);
|
| 1053 |
+
}
|
| 1054 |
+
|
| 1055 |
+
// Reverse a string
|
| 1056 |
+
void reverseString(char *str) {
|
| 1057 |
+
ย ย ย ย int n = strlen(str);
|
| 1058 |
+
ย ย ย ย for (int i = 0; i < n/2; i++) {
|
| 1059 |
+
ย ย ย ย ย ย ย ย char temp = str[i];
|
| 1060 |
+
ย ย ย ย ย ย ย ย str[i] = str[n-i-1];
|
| 1061 |
+
ย ย ย ย ย ย ย ย str[n-i-1] = temp;
|
| 1062 |
+
ย ย ย ย }
|
| 1063 |
+
}
|
| 1064 |
+
|
| 1065 |
+
// Function to convert prefix to infix
|
| 1066 |
+
void prefixToInfix(char *prefix, char *infix) {
|
| 1067 |
+
ย ย ย ย Stack s;
|
| 1068 |
+
ย ย ย ย initStack(&s);
|
| 1069 |
+
ย ย ย ย char temp[MAX], op1[MAX], op2[MAX];
|
| 1070 |
+
|
| 1071 |
+
ย ย ย ย // Reverse the prefix expression
|
| 1072 |
+
ย ย ย ย reverseString(prefix);
|
| 1073 |
+
|
| 1074 |
+
ย ย ย ย for (int i = 0; prefix[i] != '\0'; i++) {
|
| 1075 |
+
ย ย ย ย ย ย ย ย char ch = prefix[i];
|
| 1076 |
+
|
| 1077 |
+
ย ย ย ย ย ย ย ย // If operand, push to stack
|
| 1078 |
+
ย ย ย ย ย ย ย ย if (isalnum(ch)) {
|
| 1079 |
+
ย ย ย ย ย ย ย ย ย ย ย ย temp[0] = ch;
|
| 1080 |
+
ย ย ย ย ย ย ย ย ย ย ย ย temp[1] = '\0';
|
| 1081 |
+
ย ย ย ย ย ย ย ย ย ย ย ย push(&s, temp);
|
| 1082 |
+
ย ย ย ย ย ย ย ย }
|
| 1083 |
+
ย ย ย ย ย ย ย ย // If operator, pop two operands and combine with operator
|
| 1084 |
+
ย ย ย ย ย ย ย ย else {
|
| 1085 |
+
ย ย ย ย ย ย ย ย ย ย ย ย pop(&s, op1);
|
| 1086 |
+
ย ย ย ย ย ย ย ย ย ย ย ย pop(&s, op2);
|
| 1087 |
+
|
| 1088 |
+
ย ย ย ย ย ย ย ย ย ย ย ย // Format the expression with parentheses
|
| 1089 |
+
ย ย ย ย ย ย ย ย ย ย ย ย sprintf(temp, "(%s%c%s)", op1, ch, op2);
|
| 1090 |
+
|
| 1091 |
+
16
|
| 1092 |
+
|
| 1093 |
+
------------PAGE TALK ABOUT------------
|
| 1094 |
+
ุงูุตูุญุฉ ุชุนุฑุถ ุฌุฒุกูุง ู
ู ููุฏ ุจูุบุฉ C ูุดุฑุญ ุงุณุชุฎุฏุงู
ุงูู
ูุฏุณ (Stack) ูุชุญููู ุงูุชุนุจูุฑ ู
ู Prefix ุฅูู Infix.
|
| 1095 |
+
ุชุชุถู
ู ุฏูุงู ูุญุต ุงููุฑุงุบ isEmptyุ ูุงูุฅุถุงูุฉ pushุ ูุงูุญุฐู popุ ูุนูุณ ุงููุต reverseString.
|
| 1096 |
+
ูู
ุง ุชุดุฑุญ ุจุฏุงูุฉ ุฏุงูุฉ prefixToInfix ุงูุชู ุชุนูุณ ุงูุชุนุจูุฑ ุซู
ุชู
ุฑ ุนูู ูู ุฑู
ุฒุ ุฅุฐุง ูุงู operand ูุชู
ุฏูุนู ููู
ูุฏุณุ ูุฅุฐุง ูุงู operator ูุชู
ุณุญุจ ุนูุตุฑูู ูุฏู
ุฌูู
ุง ุฏุงุฎู ุฃููุงุณ.
|
| 1097 |
+
ููุฌุฏ ุชูุณูู ุฃูุงุฏูู
ู ุฃุนูู ุงูุตูุญุฉ ุจุงุณู
ุงูู
ูุฑุฑ ITGS 220 ูุงุณู
ุงูุฏูุชูุฑุฉ ูุงููุตู ุงูุฏุฑุงุณูุ ู
ุน ุฑูู
ุงูุตูุญุฉ 16 ุฃุณูู ุงููู
ูู.
|
| 1098 |
+
|
| 1099 |
+
|
| 1100 |
+
================================================================================
|
| 1101 |
+
PAGE 17 - page_0017.png
|
| 1102 |
+
================================================================================
|
| 1103 |
+
|
| 1104 |
+
------------TEXT EXTRACTED------------
|
| 1105 |
+
ITGS 220
|
| 1106 |
+
Dr. Amna Ali A Mohamed Fall 2025
|
| 1107 |
+
|
| 1108 |
+
push(&s, temp);
|
| 1109 |
+
}
|
| 1110 |
+
}
|
| 1111 |
+
|
| 1112 |
+
// The final infix expression is at the top of stack
|
| 1113 |
+
pop(&s, infix);
|
| 1114 |
+
}
|
| 1115 |
+
|
| 1116 |
+
1.5 Evaluation of postfix expression:
|
| 1117 |
+
|
| 1118 |
+
The postfix expression is evaluated easily by the use of a stack. When a number is seen,
|
| 1119 |
+
it is pushed onto the stack; when an operator is seen, the operator is applied to the two
|
| 1120 |
+
numbers that are popped from the stack and the result is pushed onto the stack. When
|
| 1121 |
+
an expression is given in postfix notation, there is no need to know any precedence rules;
|
| 1122 |
+
this is our obvious advantage.
|
| 1123 |
+
|
| 1124 |
+
Example 1:
|
| 1125 |
+
|
| 1126 |
+
Evaluate the postfix expression: 6 5 2 3 + 8 * + 3 + *
|
| 1127 |
+
|
| 1128 |
+
SYMBOL OPERAND 1 OPERAND 2 VALUE STACK REMARKS
|
| 1129 |
+
|
| 1130 |
+
6 6
|
| 1131 |
+
5 6, 5
|
| 1132 |
+
2 6, 5, 2
|
| 1133 |
+
3 6, 5, 2, 3
|
| 1134 |
+
+ 2 3 5 6, 5, 5 The first four symbols are placed on
|
| 1135 |
+
the stack.
|
| 1136 |
+
Next a โ+โ is read, so 3 and 2 are
|
| 1137 |
+
popped from the stack and their sum
|
| 1138 |
+
5, is pushed
|
| 1139 |
+
|
| 1140 |
+
17
|
| 1141 |
+
|
| 1142 |
+
------------PAGE TALK ABOUT------------
|
| 1143 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุชูููู
ุงูุชุนุจูุฑ ุงููุงุญู (Postfix Expression) ุจุงุณุชุฎุฏุงู
ุงูู
ูุฏุณ Stack.
|
| 1144 |
+
ุชุดุฑุญ ุงูููุฑุฉ: ุนูุฏ ูุฑุงุกุฉ ุฑูู
ูุชู
ุฏูุนู ุฅูู ุงูู
ูุฏุณุ ูุนูุฏ ูุฑุงุกุฉ ุนุงู
ู ู
ุซู + ุฃู * ูุชู
ุณุญุจ ุขุฎุฑ ุฑูู
ูู ูุฅุฌุฑุงุก ุงูุนู
ููุฉ ุซู
ุฅุนุงุฏุฉ ุงููุชูุฌุฉ ุฅูู ุงูู
ูุฏุณ.
|
| 1145 |
+
ุชุญุชูู ุงูุตูุญุฉ ุนูู ู
ุซุงู ุนู
ูู ููุชุนุจูุฑ: 6 5 2 3 + 8 * + 3 + * ู
ุน ุฌุฏูู ููุถุญ ูู ุฎุทูุฉ.
|
| 1146 |
+
ููุฌุฏ ุฃูุถูุง ุฌุฒุก ุตุบูุฑ ู
ู ููุฏ ุณุงุจู ูู ุฃุนูู ุงูุตูุญุฉุ ุจุงูุฅุถุงูุฉ ุฅูู ุฌุฏูู ู
ูุธู
ุจุฃุนู
ุฏุฉ ู
ุซู ุงูุฑู
ุฒุ ุงูู
ุนุงู
ูุงุชุ ุงูููู
ุฉุ ุงูู
ูุฏุณุ ูุงูู
ูุงุญุธุงุช.
|
| 1147 |
+
|
| 1148 |
+
|
| 1149 |
+
================================================================================
|
| 1150 |
+
PAGE 18 - page_0018.png
|
| 1151 |
+
================================================================================
|
| 1152 |
+
|
| 1153 |
+
------------TEXT EXTRACTED------------
|
| 1154 |
+
ITGS 220
|
| 1155 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 1156 |
+
|
| 1157 |
+
8ย ย ย ย 2ย ย ย ย 3ย ย ย ย 5ย ย ย ย 6, 5, 5, 8ย ย ย ย Next 8 is pushed
|
| 1158 |
+
*ย ย ย ย 5ย ย ย ย 8ย ย ย ย 40ย ย ย ย 6, 5, 40ย ย ย ย Now a โ*โ is seen, so 8 and 5 are
|
| 1159 |
+
popped as 8 * 5 = 40 is pushed
|
| 1160 |
+
+ย ย ย ย 5ย ย ย ย 40ย ย ย ย 45ย ย ย ย 6, 45ย ย ย ย Next, a โ+โ is seen, so 40 and 5 are
|
| 1161 |
+
popped and 40 + 5 = 45 is pushed
|
| 1162 |
+
3ย ย ย ย 5ย ย ย ย 40ย ย ย ย 45ย ย ย ย 6, 45, 3ย ย ย ย Now, 3 is pushed
|
| 1163 |
+
+ย ย ย ย 45ย ย ย ย 3ย ย ย ย 48ย ย ย ย 6, 48ย ย ย ย Next, โ+โ pops 3 and 45 and pushes
|
| 1164 |
+
45 + 3 = 48 is pushed
|
| 1165 |
+
*ย ย ย ย 6ย ย ย ย 48ย ย ย ย 288ย ย ย ย 288ย ย ย ย Finally, a โ*โ is seen and 48 and 6
|
| 1166 |
+
are popped, the result 6 * 48 =
|
| 1167 |
+
288 is pushed
|
| 1168 |
+
|
| 1169 |
+
Example 2:
|
| 1170 |
+
|
| 1171 |
+
Evaluate the following postfix expression: 6 2 3 + - 3 8 2 / + * 2 โ 3 +
|
| 1172 |
+
|
| 1173 |
+
SYMBOLย ย ย ย OPERAND 1ย ย ย ย OPERAND 2ย ย ย ย VALUEย ย ย ย STACK
|
| 1174 |
+
6ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 6
|
| 1175 |
+
2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 6, 2
|
| 1176 |
+
3ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 6, 2, 3
|
| 1177 |
+
+ย ย ย ย ย ย ย ย ย 2ย ย ย ย ย ย ย ย ย ย ย ย ย 3ย ย ย ย ย ย ย ย ย ย ย ย 5ย ย ย ย ย ย ย ย 6, 5
|
| 1178 |
+
-ย ย ย ย ย ย ย ย ย 6ย ย ย ย ย ย ย ย ย ย ย ย ย 5ย ย ย ย ย ย ย ย ย ย ย ย 1ย ย ย ย ย ย ย ย 1
|
| 1179 |
+
3ย ย ย ย ย ย ย ย ย 6ย ย ย ย ย ย ย ย ย ย ย ย ย 5ย ย ย ย ย ย ย ย ย ย ย ย 1ย ย ย ย ย ย ย ย 1, 3
|
| 1180 |
+
8ย ย ย ย ย ย ย ย ย 6ย ย ย ย ย ย ย ย ย ย ย ย ย 5ย ย ย ย ย ย ย ย ย ย ย ย 1ย ย ย ย ย ย ย ย 1, 3, 8
|
| 1181 |
+
2ย ย ย ย ย ย ย ย ย 6ย ย ย ย ย ย ย ย ย ย ย ย ย 5ย ย ย ย ย ย ย ย ย ย ย ย 1ย ย ย ย ย ย ย ย 1, 3, 8, 2
|
| 1182 |
+
/ย ย ย ย ย ย ย ย ย 8ย ย ย ย ย ย ย ย ย ย ย ย ย 2ย ย ย ย ย ย ย ย ย ย ย ย 4ย ย ย ย ย ย ย ย 1, 3, 4
|
| 1183 |
+
+ย ย ย ย ย ย ย ย ย 3ย ย ย ย ย ย ย ย ย ย ย ย ย 4ย ย ย ย ย ย ย ย ย ย ย ย 7ย ย ย ย ย ย ย ย 1, 7
|
| 1184 |
+
*ย ย ย ย ย ย ย ย ย 1ย ย ย ย ย ย ย ย ย ย ย ย ย 7ย ย ย ย ย ย ย ย ย ย ย ย 7ย ย ย ย ย ย ย ย 7
|
| 1185 |
+
2ย ย ย ย ย ย ย ย ย 1ย ย ย ย ย ย ย ย ย ย ย ย ย 7ย ย ย ย ย ย ย ย ย ย ย ย 7ย ย ย ย ย ย ย ย 7, 2
|
| 1186 |
+
โย ย ย ย ย ย ย ย ย 7ย ย ย ย ย ย ย ย ย ย ย ย ย 2ย ย ย ย ย ย ย ย ย ย ย ย 49ย ย ย ย ย ย ย 49
|
| 1187 |
+
3ย ย ย ย ย ย ย ย ย 7ย ย ย ย ย ย ย ย ย ย ย ย ย 2ย ย ย ย ย ย ย ย ย ย ย ย 49ย ย ย ย ย ย ย 49, 3
|
| 1188 |
+
+ย ย ย ย ย ย ย ย ย 49ย ย ย ย ย ย ย ย ย ย ย ย 3ย ย ย ย ย ย ย ย ย ย ย ย 52ย ย ย ย ย ย ย 52
|
| 1189 |
+
|
| 1190 |
+
1.5.1 Program to evaluate a postfix expression:
|
| 1191 |
+
|
| 1192 |
+
#include <stdio.h>
|
| 1193 |
+
#include <stdlib.h>
|
| 1194 |
+
#include <ctype.h>
|
| 1195 |
+
#include <math.h>
|
| 1196 |
+
#include <string.h>
|
| 1197 |
+
|
| 1198 |
+
18
|
| 1199 |
+
|
| 1200 |
+
------------PAGE TALK ABOUT------------
|
| 1201 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู **ุชูููู
ุงูุชุนุจูุฑ postfix ุจุงุณุชุฎุฏุงู
ุงูู
ูุฏุณ (Stack)** ุฎุทูุฉ ุจุฎุทูุฉ.
|
| 1202 |
+
ุชุญุชูู ุนูู ู
ุซุงู ู
ุญููู ููุถุญ ููู ูุชู
ุฏูุน ุงูุฃุฑูุงู
ุฅูู ุงูู
ูุฏุณ ุซู
ุชูููุฐ ุงูุนู
ููุงุช ุงูุญุณุงุจูุฉ ุนูุฏ ุธููุฑ ุงูุฑู
ูุฒ ู
ุซู `+` ู `*` ู `/` ู `โ`.
|
| 1203 |
+
ููุฌุฏ ุฌุฏูู ุฑุฆูุณู ูู Example 2 ูุจูู ุงูุฃุนู
ุฏุฉ: ุงูุฑู
ุฒุ ุงูู
ุนุงู
ู ุงูุฃููุ ุงูู
ุนุงู
ู ุงูุซุงููุ ุงูููู
ุฉุ ูุญุงูุฉ ุงูู
ูุฏุณ ุจุนุฏ ูู ุฎุทูุฉ ุญุชู ุงููุตูู ุฅูู ุงููุงุชุฌ ุงูููุงุฆู **52**.
|
| 1204 |
+
ูู
ุง ุชุจุฏุฃ ุงูุตูุญุฉ ูุณู
ูุง ุฌุฏูุฏูุง ุจุนููุงู **Program to evaluate a postfix expression** ูุชุนุฑุถ ุชุฑููุณุงุช ู
ูุชุจุงุช ูุบุฉ C ุงููุงุฒู
ุฉ ููุชุงุจุฉ ุงูุจุฑูุงู
ุฌ.
|
| 1205 |
+
|
| 1206 |
+
|
| 1207 |
+
================================================================================
|
| 1208 |
+
PAGE 19 - page_0019.png
|
| 1209 |
+
================================================================================
|
| 1210 |
+
|
| 1211 |
+
------------TEXT EXTRACTED------------
|
| 1212 |
+
ITGS 220
|
| 1213 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 1214 |
+
|
| 1215 |
+
____________________________________________________________
|
| 1216 |
+
____
|
| 1217 |
+
|
| 1218 |
+
#define MAX_SIZE 100
|
| 1219 |
+
|
| 1220 |
+
// Stack implementation
|
| 1221 |
+
double stack[MAX_SIZE];
|
| 1222 |
+
int top = -1;
|
| 1223 |
+
|
| 1224 |
+
// Function prototypes
|
| 1225 |
+
void push(double num);
|
| 1226 |
+
double pop();
|
| 1227 |
+
int isEmpty();
|
| 1228 |
+
int isOperator(char ch);
|
| 1229 |
+
double evaluatePostfix(char* expression);
|
| 1230 |
+
|
| 1231 |
+
int main() {
|
| 1232 |
+
ย ย ย ย char expression[MAX_SIZE];
|
| 1233 |
+
|
| 1234 |
+
ย ย ย ย printf("Enter postfix expression (numbers and operators separated by spaces):\n");
|
| 1235 |
+
ย ย ย ย printf("Example: 5 3 2 * + 4 -\n");
|
| 1236 |
+
ย ย ย ย fgets(expression, MAX_SIZE, stdin);
|
| 1237 |
+
|
| 1238 |
+
ย ย ย ย // Remove newline character if present
|
| 1239 |
+
ย ย ย ย if (expression[strlen(expression)-1] == '\n') {
|
| 1240 |
+
ย ย ย ย ย ย ย ย expression[strlen(expression)-1] = '\0';
|
| 1241 |
+
ย ย ย ย }
|
| 1242 |
+
|
| 1243 |
+
ย ย ย ย double result = evaluatePostfix(expression);
|
| 1244 |
+
ย ย ย ย printf("Result: %.2f\n", result);
|
| 1245 |
+
|
| 1246 |
+
ย ย ย ย return 0;
|
| 1247 |
+
}
|
| 1248 |
+
|
| 1249 |
+
// Push a number onto the stack
|
| 1250 |
+
void push(double num) {
|
| 1251 |
+
ย ย ย ย if (top >= MAX_SIZE - 1) {
|
| 1252 |
+
ย ย ย ย ย ย ย ย printf("Stack overflow!\n");
|
| 1253 |
+
ย ย ย ย ย ย ย ย exit(1);
|
| 1254 |
+
ย ย ย ย }
|
| 1255 |
+
ย ย ย ย stack[++top] = num;
|
| 1256 |
+
}
|
| 1257 |
+
|
| 1258 |
+
// Pop a number from the stack
|
| 1259 |
+
double pop() {
|
| 1260 |
+
ย ย ย ย if (top < 0) {
|
| 1261 |
+
ย ย ย ย ย ย ย ย printf("Stack underflow!\n");
|
| 1262 |
+
ย ย ย ย ย ย ย ย exit(1);
|
| 1263 |
+
ย ย ย ย }
|
| 1264 |
+
ย ย ย ย return stack[top--];
|
| 1265 |
+
}
|
| 1266 |
+
|
| 1267 |
+
// Check if stack is empty
|
| 1268 |
+
int isEmpty() {
|
| 1269 |
+
ย ย ย ย return top == -1;
|
| 1270 |
+
}
|
| 1271 |
+
|
| 1272 |
+
19
|
| 1273 |
+
|
| 1274 |
+
------------PAGE TALK ABOUT------------
|
| 1275 |
+
ุงูุตูุญุฉ ุชุนุฑุถ ุฌุฒุกูุง ู
ู ููุฏ ุจูุบุฉ C ูุชูููู
ุงูุชุนุจูุฑ postfix ุจุงุณุชุฎุฏุงู
ุจููุฉ ุงูุจูุงูุงุช Stack.
|
| 1276 |
+
ุชุชุถู
ู ุชุนุฑูู ุญุฌู
ุงูู
ูุฏุณุ ูุงูู
ุตูููุฉ ุงูู
ุณุชุฎุฏู
ุฉ ูุชุฎุฒูู ุงูููู
ุ ูุงูู
ุชุบูุฑ top ูุชุชุจุน ุฃุนูู ุนูุตุฑ.
|
| 1277 |
+
ูู
ุง ุชุญุชูู ุนูู prototypes ููุฏูุงูุ ูุฏุงูุฉ main ููุฑุงุกุฉ ุงูุชุนุจูุฑ ู
ู ุงูู
ุณุชุฎุฏู
ุซู
ุญุณุงุจ ุงููุชูุฌุฉ ูุทุจุงุนุชูุง.
|
| 1278 |
+
ููุธูุฑ ุฃูุถูุง ุชูููุฐ ุงูุฏูุงู ุงูุฃุณุงุณูุฉ ููู
ูุฏุณ ู
ุซู push ู pop ู isEmpty ู
ุน ู
ุนุงูุฌุฉ ุญุงูุชู overflow ู underflow.
|
| 1279 |
+
|
| 1280 |
+
|
| 1281 |
+
================================================================================
|
| 1282 |
+
PAGE 20 - page_0020.png
|
| 1283 |
+
================================================================================
|
| 1284 |
+
|
| 1285 |
+
------------TEXT EXTRACTED------------
|
| 1286 |
+
ITGS 220
|
| 1287 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 1288 |
+
|
| 1289 |
+
____________________________________________________________________________
|
| 1290 |
+
____
|
| 1291 |
+
|
| 1292 |
+
// Check if character is an operator
|
| 1293 |
+
int isOperator(char ch) {
|
| 1294 |
+
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';
|
| 1295 |
+
}
|
| 1296 |
+
|
| 1297 |
+
// Evaluate postfix expression
|
| 1298 |
+
double evaluatePostfix(char* expression) {
|
| 1299 |
+
char* token = strtok(expression, " ");
|
| 1300 |
+
|
| 1301 |
+
while (token != NULL) {
|
| 1302 |
+
if (isdigit(token[0]) || (token[0] == '-' && isdigit(token[1]))) {
|
| 1303 |
+
// It's a number (including negative numbers)
|
| 1304 |
+
double num = atof(token);
|
| 1305 |
+
push(num);
|
| 1306 |
+
} else if (isOperator(token[0])) {
|
| 1307 |
+
// It's an operator
|
| 1308 |
+
double operand2 = pop();
|
| 1309 |
+
double operand1 = pop();
|
| 1310 |
+
double result;
|
| 1311 |
+
|
| 1312 |
+
switch (token[0]) {
|
| 1313 |
+
case '+':
|
| 1314 |
+
result = operand1 + operand2;
|
| 1315 |
+
break;
|
| 1316 |
+
case '-':
|
| 1317 |
+
result = operand1 - operand2;
|
| 1318 |
+
break;
|
| 1319 |
+
case '*':
|
| 1320 |
+
result = operand1 * operand2;
|
| 1321 |
+
break;
|
| 1322 |
+
case '/':
|
| 1323 |
+
if (operand2 == 0) {
|
| 1324 |
+
printf("Error: Division by zero\n");
|
| 1325 |
+
exit(1);
|
| 1326 |
+
}
|
| 1327 |
+
result = operand1 / operand2;
|
| 1328 |
+
break;
|
| 1329 |
+
case '^':
|
| 1330 |
+
result = pow(operand1, operand2);
|
| 1331 |
+
break;
|
| 1332 |
+
default:
|
| 1333 |
+
printf("Error: Unknown operator '%c'\n", token[0]);
|
| 1334 |
+
exit(1);
|
| 1335 |
+
}
|
| 1336 |
+
push(result);
|
| 1337 |
+
} else {
|
| 1338 |
+
printf("Error: Invalid token '%s'\n", token);
|
| 1339 |
+
exit(1);
|
| 1340 |
+
}
|
| 1341 |
+
|
| 1342 |
+
token = strtok(NULL, " ");
|
| 1343 |
+
}
|
| 1344 |
+
|
| 1345 |
+
if (top != 0) {
|
| 1346 |
+
|
| 1347 |
+
20
|
| 1348 |
+
|
| 1349 |
+
------------PAGE TALK ABOUT------------
|
| 1350 |
+
ุงูุตูุญุฉ ุชุนุฑุถ ุฌุฒุกูุง ู
ู ููุฏ ุจูุบุฉ C ูุชูููู
ุงูุชุนุจูุฑ ุงููุงุญู Postfix Expression ุจุงุณุชุฎุฏุงู
ุงูู
ูุฏุณ Stack.
|
| 1351 |
+
ููุฌุฏ ุฏุงูุฉ isOperator ููุชุญูู ูู ุงูุญุฑู ุนุงู
ู ุญุณุงุจู ู
ุซู + ู- ู* ู/ ู^.
|
| 1352 |
+
ุซู
ุฏุงูุฉ evaluatePostfix ุชูุฑุฃ ุงูุฑู
ูุฒ token ูุงุญุฏูุง ูุงุญุฏูุงุ ูุชุชุนุงู
ู ู
ุน ุงูุฃุฑูุงู
ุงูุณุงูุจุฉ ูุงูุนู
ููุงุช ุงูุญุณุงุจูุฉ ุงูู
ุฎุชููุฉ.
|
| 1353 |
+
ูู
ุง ุชุชุถู
ู ู
ุนุงูุฌุฉ ุฃุฎุทุงุก ู
ุซู ุงููุณู
ุฉ ุนูู ุตูุฑุ ุฃู ูุฌูุฏ ุนุงู
ู ุบูุฑ ู
ุนุฑููุ ุฃู ุฑู
ุฒ ุบูุฑ ุตุงูุญ.
|
| 1354 |
+
ุงูุนูุงุตุฑ ุงูุจุตุฑูุฉ ุนุจุงุฑุฉ ุนู ุตูุญุฉ ู
ุญุงุถุฑุฉ ุฃูุงุฏูู
ูุฉ ูููุง ุนููุงู ุงูู
ูุฑุฑ ูุงุณู
ุงูุฏูุชูุฑุฉ ูุงููุตู ุงูุฏุฑุงุณู ูุฑูู
ุงูุตูุญุฉ ููุทุ ุจุฏูู ุตูุฑ ุฃู ู
ุฎุทุทุงุช.
|
| 1355 |
+
|
| 1356 |
+
|
| 1357 |
+
================================================================================
|
| 1358 |
+
PAGE 21 - page_0021.png
|
| 1359 |
+
================================================================================
|
| 1360 |
+
|
| 1361 |
+
------------TEXT EXTRACTED------------
|
| 1362 |
+
ITGS 220
|
| 1363 |
+
Dr. Amna Ali A Mohamed Fall 2025
|
| 1364 |
+
|
| 1365 |
+
____________________________________________________________
|
| 1366 |
+
____
|
| 1367 |
+
|
| 1368 |
+
printf("Error: Invalid expression format\n");
|
| 1369 |
+
exit(1);
|
| 1370 |
+
}
|
| 1371 |
+
|
| 1372 |
+
return pop();
|
| 1373 |
+
|
| 1374 |
+
}
|
| 1375 |
+
|
| 1376 |
+
21
|
| 1377 |
+
|
| 1378 |
+
------------PAGE TALK ABOUT------------
|
| 1379 |
+
ุชูุถุญ ุงูุตูุญุฉ ุฌุฒุกูุง ู
ู ููุฏ ุจูุบุฉ C ูู
ุนุงูุฌุฉ ุงูุฃุฎุทุงุก ูู ุชูุณูู ุงูุชุนุจูุฑ.
|
| 1380 |
+
ูุธูุฑ ูููุง ุทุจุงุนุฉ ุฑุณุงูุฉ ุฎุทุฃ: "Invalid expression format" ุซู
ุฅููุงุก ุงูุจุฑูุงู
ุฌ ุจุงุณุชุฎุฏุงู
`exit(1)`.
|
| 1381 |
+
ุจุนุฏ ุฐูู ูุนูุฏ ุงูุจุฑูุงู
ุฌ ููู
ุฉ ู
ู ุงูู
ูุฏุณ ุจุงุณุชุฎุฏุงู
`return pop();`.
|
| 1382 |
+
ุจุตุฑููุงุ ุงูุตูุญุฉ ุชุญุชูู ุนูู ุชุฑููุณุฉ ุงูู
ูุฑุฑ ูุงุณู
ุงูุฏูุชูุฑุฉ ูุงููุตู ุงูุฏุฑุงุณูุ ู
ุน ุฑูู
ุงูุตูุญุฉ 21 ุฃุณูู ุงููู
ูู.
|
data_structure/p6.txt
ADDED
|
@@ -0,0 +1,802 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"ุฃูุช ู
ุฏุฑุณ ุฎุจูุฑ ูู
ุญุชุฑู ูู ู
ุงุฏุฉ ุชุฑุงููุจ ุจูุงูุงุช. ุฃูุช ุชุฏุฑูุณ ุทุงูุจุงู ูุชุณุงุนุฏู ุนูู ุงูููู
.
|
| 2 |
+
|
| 3 |
+
=== ู
ุญุชูู ุงููุตู (ุงูู
ุฑุฌุน ุงูุนูู
ู) ===
|
| 4 |
+
|
| 5 |
+
=== ููุงูุฉ ุงูุณูุงู ===
|
| 6 |
+
|
| 7 |
+
**ุชุนููู
ุงุช ู
ูู
ุฉ:**
|
| 8 |
+
- ุงุณุชุฎุฏู
ุงูู
ุญุชูู ุงูุนูู
ู ุฃุนูุงู ูู
ุฑุฌุน ุฃุณุงุณู ููุฅุฌุงุจุฉ
|
| 9 |
+
- ุฑุงุนู ุณูุงู ุงูู
ุญุงุฏุซุฉ ุงูุณุงุจูุฉ ูุงูู
ุณุชูู ุงูุชุนููู
ู ููุทุงูุจ
|
| 10 |
+
- ุฃุฌุจ ุจุดูู ูุงุถุญ ูู
ูุตู ุจุงููุบุฉ ุงูุนุฑุจูุฉ
|
| 11 |
+
- ุงุณุชุฎุฏู
ุงูุฃู
ุซูุฉ ูุงูุชูุถูุญุงุช ุนูุฏ ุงูุญุงุฌุฉ
|
| 12 |
+
- ุฅุฐุง ุณุฃู ุงูุทุงูุจ ุนู ุดูุก ุฎุงุฑุฌ ุงูู
ุญุชูู ุงูู
ุชููุฑุ ุฃุฎุจุฑู ุจุฐูู ุจูุทู
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
================================================================================
|
| 16 |
+
SUBJECT: L6_Queue_20260417_203720
|
| 17 |
+
TOTAL PAGES: 11
|
| 18 |
+
EXTRACTED ON: 2026-04-17 19:59:23
|
| 19 |
+
================================================================================
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
================================================================================
|
| 23 |
+
PAGE 1 - page_0001.png
|
| 24 |
+
================================================================================
|
| 25 |
+
|
| 26 |
+
------------TEXT EXTRACTED------------
|
| 27 |
+
ITGS 220
|
| 28 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 29 |
+
|
| 30 |
+
Queue
|
| 31 |
+
|
| 32 |
+
A queue is another special kind of list, where items are inserted at one end called the
|
| 33 |
+
rear and deleted at the other end called the front. Another name for a queue is a โFIFOโ
|
| 34 |
+
or โFirst-in-first-outโ list.
|
| 35 |
+
|
| 36 |
+
The operations for a queue are analogues to those for a stack, the difference is that the
|
| 37 |
+
insertions go at the end of the list, rather than the beginning. We are using the following
|
| 38 |
+
operations on queues:
|
| 39 |
+
|
| 40 |
+
โขย ย ย ย ย ย ย ย enqueue: which inserts an element at the end of the queue.
|
| 41 |
+
โขย ย ย ย ย ย ย ย dequeue: which deletes an element at the start of the queue.
|
| 42 |
+
|
| 43 |
+
1.ย ย Representation of Queue:
|
| 44 |
+
|
| 45 |
+
Let us consider a queue, which can hold maximum of five elements. Initially the queue
|
| 46 |
+
is empty.
|
| 47 |
+
|
| 48 |
+
0ย ย ย ย ย 1ย ย ย ย ย 2ย ย ย ย ย 3ย ย ย ย ย 4
|
| 49 |
+
|
| 50 |
+
Queue Empty
|
| 51 |
+
FRONT = REAR = 0
|
| 52 |
+
|
| 53 |
+
Fย R
|
| 54 |
+
|
| 55 |
+
Now, insert 11 to the queue. Then queue status will be:
|
| 56 |
+
|
| 57 |
+
0ย ย ย ย ย 1ย ย ย ย ย 2ย ย ย ย ย 3ย ย ย ย ย 4
|
| 58 |
+
|
| 59 |
+
11
|
| 60 |
+
|
| 61 |
+
REAR = REAR + 1 = 1
|
| 62 |
+
FRONT = 0
|
| 63 |
+
|
| 64 |
+
Fย ย ย ย ย R
|
| 65 |
+
|
| 66 |
+
Next, insert 22 to the queue. Then the queue status is:
|
| 67 |
+
|
| 68 |
+
0ย ย ย ย ย 1ย ย ย ย ย 2ย ย ย ย ย 3ย ย ย ย ย 4
|
| 69 |
+
|
| 70 |
+
11ย ย ย ย ย 22
|
| 71 |
+
|
| 72 |
+
REAR = REAR + 1 = 2
|
| 73 |
+
FRONT = 0
|
| 74 |
+
|
| 75 |
+
Fย ย ย ย ย R
|
| 76 |
+
|
| 77 |
+
Again insert another element 33 to the queue. The status of the queue is:
|
| 78 |
+
|
| 79 |
+
0ย ย ย ย ย 1ย ย ย ย ย 2ย ย ย ย ย 3ย ย ย ย ย 4
|
| 80 |
+
|
| 81 |
+
11ย ย ย ย ย 22ย ย ย ย ย 33
|
| 82 |
+
|
| 83 |
+
REAR = REAR + 1 = 3
|
| 84 |
+
FRONT = 0
|
| 85 |
+
|
| 86 |
+
Fย ย ย ย ย R
|
| 87 |
+
|
| 88 |
+
1
|
| 89 |
+
|
| 90 |
+
------------PAGE TALK ABOUT------------
|
| 91 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ุจููุฉ ุงูุจูุงูุงุช Queue ูููู ุชุนู
ู ุจู
ุจุฏุฃ FIFO ุฃู ุฃูู ุฏุงุฎู ุฃูู ุฎุงุฑุฌ.
|
| 92 |
+
ุชูุถุญ ุนู
ููุชู enqueue ููุฅุฏุฎุงู ู
ู ุงูุฎูู rear ูdequeue ููุญุฐู ู
ู ุงูุฃู
ุงู
front.
|
| 93 |
+
ููุฌุฏ ุชู
ุซูู ุจุตุฑู ูุทุงุจูุฑ ุณุนุชู 5 ุฎุงูุงุช ู
ุฑูู
ุฉ ู
ู 0 ุฅูู 4ุ ู
ุน ุชุชุจุน ู
ููุนู FRONT ูREAR.
|
| 94 |
+
ูู
ุง ุชุนุฑุถ ุฃู
ุซูุฉ ู
ุชุชุงุจุนุฉ ูุฅุฏุฎุงู ุงูุนูุงุตุฑ 11 ุซู
22 ุซู
33ุ ู
ุน ุชุญุฏูุซ ููู
ุฉ REAR ูุจูุงุก FRONT ุนูุฏ 0.
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
================================================================================
|
| 98 |
+
PAGE 2 - page_0002.png
|
| 99 |
+
================================================================================
|
| 100 |
+
|
| 101 |
+
------------TEXT EXTRACTED------------
|
| 102 |
+
ITGS 220
|
| 103 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 104 |
+
|
| 105 |
+
Now, delete an element. The element deleted is the element at the front of the queue.
|
| 106 |
+
So the status of the queue is:
|
| 107 |
+
|
| 108 |
+
0ย ย ย 1ย ย ย 2ย ย ย 3ย ย ย 4
|
| 109 |
+
22ย ย ย 33
|
| 110 |
+
|
| 111 |
+
Fย ย ย ย ย ย ย ย ย ย ย ย ย R
|
| 112 |
+
|
| 113 |
+
REAR = 3
|
| 114 |
+
FRONT = FRONT + 1 = 1
|
| 115 |
+
|
| 116 |
+
Again, delete an element. The element to be deleted is always pointed to by the FRONT
|
| 117 |
+
pointer. So, 22 is deleted. The queue status is as follows:
|
| 118 |
+
|
| 119 |
+
0ย ย ย 1ย ย ย 2ย ย ย 3ย ย ย 4
|
| 120 |
+
33
|
| 121 |
+
|
| 122 |
+
Fย ย ย ย ย ย ย ย ย ย ย ย ย R
|
| 123 |
+
|
| 124 |
+
REAR = 3
|
| 125 |
+
FRONT = FRONT + 1 = 2
|
| 126 |
+
|
| 127 |
+
Now, insert new elements 44 and 55 into the queue. The queue status is:
|
| 128 |
+
|
| 129 |
+
0ย ย ย 1ย ย ย 2ย ย ย 3ย ย ย 4
|
| 130 |
+
33ย ย ย 44ย ย ย 55
|
| 131 |
+
|
| 132 |
+
Fย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย R
|
| 133 |
+
|
| 134 |
+
REAR = 5
|
| 135 |
+
FRONT = 2
|
| 136 |
+
|
| 137 |
+
Next insert another element, say 66 to the queue. We cannot insert 66 to the queue as
|
| 138 |
+
the rear crossed the maximum size of the queue (i.e., 5). There will be queue full signal.
|
| 139 |
+
The queue status is as follows:
|
| 140 |
+
|
| 141 |
+
0ย ย ย 1ย ย ย 2ย ย ย 3ย ย ย 4
|
| 142 |
+
33ย ย ย 44ย ย ย 55
|
| 143 |
+
|
| 144 |
+
Fย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย R
|
| 145 |
+
|
| 146 |
+
REAR = 5
|
| 147 |
+
FRONT = 2
|
| 148 |
+
|
| 149 |
+
Now it is not possible to insert an element 66 even though there are two vacant positions
|
| 150 |
+
in the linear queue. To over come this problem the elements of the queue are to be
|
| 151 |
+
shifted towards the beginning of the queue so that it creates vacant position at the rear
|
| 152 |
+
end. Then the FRONT and REAR are to be adjusted properly. The element 66 can be
|
| 153 |
+
inserted at the rear end. After this operation, the queue status is as follows:
|
| 154 |
+
|
| 155 |
+
0ย ย ย 1ย ย ย 2ย ย ย 3ย ย ย 4
|
| 156 |
+
33ย ย ย 44ย ย ย 55ย ย ย 66
|
| 157 |
+
|
| 158 |
+
Fย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย R
|
| 159 |
+
|
| 160 |
+
REAR = 4
|
| 161 |
+
FRONT = 0
|
| 162 |
+
|
| 163 |
+
This difficulty can overcome if we treat queue position with index 0 as a position that
|
| 164 |
+
comes after position with index 4 i.e., we treat the queue as a circular queue.
|
| 165 |
+
|
| 166 |
+
2
|
| 167 |
+
|
| 168 |
+
------------PAGE TALK ABOUT------------
|
| 169 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ุนู
ููุงุช ุงูุญุฐู ูุงูุฅุฏุฎุงู ูู **ุงูู linear queue** ุจุงุณุชุฎุฏุงู
ู
ุคุดุฑู **FRONT** ู **REAR**.
|
| 170 |
+
ุชูุถุญ ุจุงูุฑุณูู
ููู ูุชู
ุญุฐู ุงูุนูุงุตุฑ ู
ู ุงูู
ูุฏู
ุฉุ ุซู
ุฅุฏุฎุงู ุนูุงุตุฑ ุฌุฏูุฏุฉ ุญุชู ูุตู **REAR** ุฅูู ุงูุญุฏ ุงูุฃูุตู ูุชุธูุฑ ู
ุดููุฉ ุงูุงู
ุชูุงุก ุฑุบู
ูุฌูุฏ ูุฑุงุบุงุช ูู ุงูุจุฏุงูุฉ.
|
| 171 |
+
ูู
ุง ุชุนุฑุถ ุญูุงู ู
ุคูุชูุง ููู **ุฅุฒุงุญุฉ ุงูุนูุงุตุฑ ูุญู ุงูุจุฏุงูุฉ** ุซู
ุชุนุฏูู FRONT ู REAR ูุฅุฏุฎุงู ุงูุนูุตุฑ 66.
|
| 172 |
+
ููู ุงูููุงูุฉ ุชู
ููุฏ ูููุฑุฉ **ุงูู circular queue** ุจุงุนุชุจุงุฑ ุฃู ุงูู
ููุน 0 ูุฃุชู ุจุนุฏ ุงูู
ููุน 4 ูุชุฌูุจ ูุฐู ุงูู
ุดููุฉ.
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
================================================================================
|
| 176 |
+
PAGE 3 - page_0003.png
|
| 177 |
+
================================================================================
|
| 178 |
+
|
| 179 |
+
------------TEXT EXTRACTED------------
|
| 180 |
+
ITGS 220
|
| 181 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 182 |
+
|
| 183 |
+
1.2.ย ย ย ย Source code for Queue operations using array:
|
| 184 |
+
|
| 185 |
+
In order to create a queue we require a one dimensional array Q(1:n) and two variables
|
| 186 |
+
front and rear. The conventions we shall adopt for these two variables are that front is
|
| 187 |
+
always 1 less than the actual front of the queue and rear always points to the last element
|
| 188 |
+
in the queue. Thus, front = rear if and only if there are no elements in the queue. The
|
| 189 |
+
initial condition then is front = rear = 0. The various queue operations to perform
|
| 190 |
+
creation, deletion and display the elements in a queue are as follows:
|
| 191 |
+
|
| 192 |
+
1.ย ย ย ย ย ย ย ย insertQ(): inserts an element at the end of queue Q.
|
| 193 |
+
2.ย ย ย ย ย ย ย ย deleteQ(): deletes the first element of Q.
|
| 194 |
+
3.ย ย ย ย ย ย ย ย displayQ(): displays the elements in the queue.
|
| 195 |
+
|
| 196 |
+
#include <stdio.h>
|
| 197 |
+
#include <stdlib.h>
|
| 198 |
+
|
| 199 |
+
#define MAX_SIZE 6
|
| 200 |
+
|
| 201 |
+
int queue[MAX_SIZE];
|
| 202 |
+
int front = 0;
|
| 203 |
+
int rear = 0;
|
| 204 |
+
|
| 205 |
+
// Function prototypes
|
| 206 |
+
void displayMenu();
|
| 207 |
+
void enqueue();
|
| 208 |
+
void dequeue();
|
| 209 |
+
void displayQueue();
|
| 210 |
+
|
| 211 |
+
int main() {
|
| 212 |
+
ย ย ย ย int choice;
|
| 213 |
+
|
| 214 |
+
ย ย ย ย do {
|
| 215 |
+
|
| 216 |
+
ย ย ย ย ย ย ย ย // screen clearing
|
| 217 |
+
ย ย ย ย ย ย ย ย system("cls");
|
| 218 |
+
|
| 219 |
+
ย ย ย ย ย ย ย ย displayMenu();
|
| 220 |
+
ย ย ย ย ย ย ย ย printf("Enter your choice: ");
|
| 221 |
+
ย ย ย ย ย ย ย ย scanf("%d", &choice);
|
| 222 |
+
|
| 223 |
+
ย ย ย ย ย ย ย ย switch(choice) {
|
| 224 |
+
ย ย ย ย ย ย ย ย ย ย ย ย case 1:
|
| 225 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย enqueue();
|
| 226 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย break;
|
| 227 |
+
ย ย ย ย ย ย ย ย ย ย ย ย case 2:
|
| 228 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย dequeue();
|
| 229 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย break;
|
| 230 |
+
ย ย ย ย ย ย ย ย ย ย ย ย case 3:
|
| 231 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย displayQueue();
|
| 232 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย break;
|
| 233 |
+
ย ย ย ย ย ย ย ย ย ย ย ย case 4:
|
| 234 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย printf("\nExiting program...\n");
|
| 235 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย exit(0);
|
| 236 |
+
ย ย ย ย ย ย ย ย ย ย ย ย default:
|
| 237 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย printf("\nInvalid choice! Please try again.\n");
|
| 238 |
+
ย ย ย ย ย ย ย ย }
|
| 239 |
+
|
| 240 |
+
ย ย ย ย ย ย ย ย printf("\nPress Enter to continue...");
|
| 241 |
+
ย ย ย ย ย ย ย ย while(getchar() != '\n'); // Clear input buffer
|
| 242 |
+
ย ย ย ย ย ย ย ย getchar(); // Wait for Enter key
|
| 243 |
+
|
| 244 |
+
ย ย ย ย } while(1);
|
| 245 |
+
|
| 246 |
+
3
|
| 247 |
+
|
| 248 |
+
------------PAGE TALK ABOUT------------
|
| 249 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ู
ูููู
ุชูููุฐ ุจููุฉ ุงูุจูุงูุงุช Queue ุจุงุณุชุฎุฏุงู
ู
ุตูููุฉ ุฃุญุงุฏูุฉ ุงูุจุนุฏ ูู ูุบุฉ C.
|
| 250 |
+
ุชูุถุญ ุฏูุฑ ุงูู
ุชุบูุฑูู front ู rearุ ูุฃู ุงูุญุงูุฉ ุงูุงุจุชุฏุงุฆูุฉ ุชููู front = rear = 0ุ ู
ุน ุดุฑุญ ุนู
ููุงุช ุงูุฅุฏุฎุงู ูุงูุญุฐู ูุงูุนุฑุถ.
|
| 251 |
+
ูู
ุง ุชุนุฑุถ ุจุฏุงูุฉ ุงูุดููุฑุฉ ุงูู
ุตุฏุฑูุฉุ ูุชุดู
ู ุชุนุฑูู ุงูู
ุตูููุฉ ูุงูุซูุงุจุช ูุงูู
ุชุบูุฑุงุช ุงูุนุงู
ุฉ ูุชุตุฑูุญุงุช ุงูุฏูุงู.
|
| 252 |
+
ููุฌุฏ ุฃูุถูุง ุฌุฒุก ู
ู ุงูุฏุงูุฉ main ูุญุชูู ุนูู ูุงุฆู
ุฉ ุฎูุงุฑุงุช ููู
ุณุชุฎุฏู
ูุชูููุฐ enqueue ู dequeue ู displayQueue ุฃู ุงูุฎุฑูุฌ ู
ู ุงูุจุฑูุงู
ุฌ.
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
================================================================================
|
| 256 |
+
PAGE 4 - page_0004.png
|
| 257 |
+
================================================================================
|
| 258 |
+
|
| 259 |
+
------------TEXT EXTRACTED------------
|
| 260 |
+
ITGS 220
|
| 261 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ๏ฟฝ๏ฟฝย Fall 2025
|
| 262 |
+
|
| 263 |
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 264 |
+
|
| 265 |
+
ย ย ย ย ย ย ย ย return 0;
|
| 266 |
+
ย ย ย ย }
|
| 267 |
+
ย ย ย ย
|
| 268 |
+
ย ย ย ย void displayMenu() {
|
| 269 |
+
ย ย ย ย ย ย ย ย printf("\n===== Queue Operations =====\n");
|
| 270 |
+
ย ย ย ย ย ย ย ย printf("1. Enqueue (Add element)\n");
|
| 271 |
+
ย ย ย ย ย ย ย ย printf("2. Dequeue (Remove element)\n");
|
| 272 |
+
ย ย ย ย ย ย ย ย printf("3. Display Queue\n");
|
| 273 |
+
ย ย ย ย ย ย ย ย printf("4. Exit\n");
|
| 274 |
+
ย ย ย ย }
|
| 275 |
+
ย ย ย ย
|
| 276 |
+
ย ย ย ย void enqueue() {
|
| 277 |
+
ย ย ย ย ย ย ย ย if(rear == MAX_SIZE) {
|
| 278 |
+
ย ย ย ย ย ย ย ย ย ย ย ย printf("\nQueue is full! Cannot enqueue.\n");
|
| 279 |
+
ย ย ย ย ย ย ย ย ย ย ย ย return;
|
| 280 |
+
ย ย ย ย ย ย ย ย }
|
| 281 |
+
ย ย ย ย
|
| 282 |
+
ย ย ย ย ย ย ย ย int data;
|
| 283 |
+
ย ย ย ย ย ย ย ย printf("\nEnter data to enqueue: ");
|
| 284 |
+
ย ย ย ย ย ย ย ย scanf("%d", &data);
|
| 285 |
+
ย ย ย ย
|
| 286 |
+
ย ย ย ย ย ย ย ย queue[rear] = data;
|
| 287 |
+
ย ย ย ย ย ย ย ย rear++;
|
| 288 |
+
ย ย ย ย ย ย ย ย printf("\n%d has been enqueued.\n", data);
|
| 289 |
+
ย ย ย ย }
|
| 290 |
+
ย ย ย ย
|
| 291 |
+
ย ย ย ย void dequeue() {
|
| 292 |
+
ย ย ย ย ย ย ย ย if(front == rear) {
|
| 293 |
+
ย ย ย ย ย ย ย ย ย ย ย ย printf("\nQueue is empty! Cannot dequeue.\n");
|
| 294 |
+
ย ย ย ย ย ย ย ย ย ย ย ย return;
|
| 295 |
+
ย ย ย ย ย ย ย ย }
|
| 296 |
+
ย ย ย ย
|
| 297 |
+
ย ย ย ย ย ย ย ย printf("\n%d has been dequeued.\n", queue[front]);
|
| 298 |
+
ย ย ย ย ย ย ย ย front++;
|
| 299 |
+
ย ย ย ย
|
| 300 |
+
ย ย ย ย ย ย ย ย // Reset queue if empty
|
| 301 |
+
ย ย ย ย ย ย ย ย if(front == rear) {
|
| 302 |
+
ย ย ย ย ย ย ย ย ย ย ย ย front = rear = 0;
|
| 303 |
+
ย ย ย ย ย ย ย ย }
|
| 304 |
+
ย ย ย ย }
|
| 305 |
+
ย ย ย ย
|
| 306 |
+
ย ย ย ย void displayQueue() {
|
| 307 |
+
ย ย ย ย ย ย ย ย if(front == rear) {
|
| 308 |
+
ย ย ย ย ย ย ย ย ย ย ย ย printf("\nQueue is empty!\n");
|
| 309 |
+
ย ย ย ย ย ย ย ย ย ย ย ย return;
|
| 310 |
+
ย ย ย ย ย ย ย ย }
|
| 311 |
+
ย ย ย ย
|
| 312 |
+
ย ย ย ย ย ย ย ย printf("\nCurrent Queue: ");
|
| 313 |
+
ย ย ย ย ย ย ย ย for(int i = front; i < rear; i++) {
|
| 314 |
+
ย ย ย ย ย ย ย ย ย ย ย ย printf("%d ", queue[i]);
|
| 315 |
+
ย ย ย ย ย ย ย ย }
|
| 316 |
+
ย ย ย ย ย ย ย ย printf("\n");
|
| 317 |
+
ย ย ย ย }
|
| 318 |
+
|
| 319 |
+
1.3.ย ย ย ย Applications of Queue:
|
| 320 |
+
|
| 321 |
+
1.ย ย ย ย ย ย It is used to schedule the jobs to be processed by the CPU.
|
| 322 |
+
|
| 323 |
+
2.ย ย ย ย ย ย When multiple users send print jobs to a printer, each printing job is kept in
|
| 324 |
+
ย ย ย ย ย ย ย ย the printing queue. Then the printer prints those jobs according to first in first
|
| 325 |
+
ย ย ย ย ย ย ย ย out (FIFO) basis.
|
| 326 |
+
|
| 327 |
+
3.ย ย ย ย ย ย Breadth first search uses a queue data structure to find an element from a
|
| 328 |
+
ย ย ย ย ย ย ย ย graph.
|
| 329 |
+
|
| 330 |
+
4
|
| 331 |
+
|
| 332 |
+
------------PAGE TALK ABOUT------------
|
| 333 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ุจููุฉ ุงูุจูุงูุงุช Queue ูู ูุบุฉ Cุ ูุชุนุฑุถ ุฏูุงู ุฃุณุงุณูุฉ ู
ุซู ุนุฑุถ ุงููุงุฆู
ุฉุ ุงูุฅุถุงูุฉ ุฅูู ุงูุทุงุจูุฑ enqueueุ ุงูุญุฐู dequeueุ ูุนุฑุถ ุนูุงุตุฑ ุงูุทุงุจูุฑ.
|
| 334 |
+
ููุถุญ ุงูููุฏ ููููุฉ ุงูุชุญูู ู
ู ุงู
ุชูุงุก ุงูุทุงุจูุฑ ุฃู ูุฑุงุบูุ ู
ุน ุฅุนุงุฏุฉ ุถุจุท ุงูู
ุคุดุฑูู front ู rear ุนูุฏ ุชูุฑูุบ ุงูุทุงุจูุฑ.
|
| 335 |
+
ูู
ุง ุชุนุฑุถ ุงูุตูุญุฉ ุชุทุจููุงุช ุงูู Queue ู
ุซู ุฌุฏููุฉ ู
ูุงู
ุงูู
ุนุงูุฌ CPUุ ูุทูุงุจูุฑ ุงูุทุจุงุนุฉุ ูุฎูุงุฑุฒู
ูุฉ ุงูุจุญุซ ุจุนุฑุถ ุงูุดุฌุฑุฉ ุฃู ุงูุฑุณู
BFS.
|
| 336 |
+
ุงูุนูุงุตุฑ ุงูุจุตุฑูุฉ ุงูู
ูุฌูุฏุฉ ูู ุชุฑููุณุฉ ุงูู
ูุฑุฑ ITGS 220 ูุงุณู
ุงูุฏูุชูุฑุฉ ูุงููุตู ุงูุฏุฑุงุณู ูุฑูู
ุงูุตูุญุฉ ููุทุ ููุง ุชูุฌุฏ ุตูุฑ ุฃู ู
ุฎุทุทุงุช.
|
| 337 |
+
|
| 338 |
+
|
| 339 |
+
================================================================================
|
| 340 |
+
PAGE 5 - page_0005.png
|
| 341 |
+
================================================================================
|
| 342 |
+
|
| 343 |
+
------------TEXT EXTRACTED------------
|
| 344 |
+
ITGS 220
|
| 345 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 346 |
+
|
| 347 |
+
1.4.ย ย ย ย Circular Queue:
|
| 348 |
+
|
| 349 |
+
A more efficient queue representation is obtained by regarding the array Q[MAX] as circular. Any number of items could be placed on the queue. This implementation of a queue is called a circular queue because it uses its storage array as if it were a circle instead of a linear list.
|
| 350 |
+
|
| 351 |
+
There are two problems associated with linear queue. They are:
|
| 352 |
+
|
| 353 |
+
โขย ย ย ย ย ย ย ย Time consuming: linear time to be spent in shifting the elements to the beginning of the queue.
|
| 354 |
+
|
| 355 |
+
โขย ย ย ย ย ย ย ย Signaling queue full: even if the queue is having vacant position.
|
| 356 |
+
|
| 357 |
+
For example, let us consider a linear queue status as follows:
|
| 358 |
+
|
| 359 |
+
0ย ย ย ย ย 1ย ย ย ย ย 2ย ย ย ย ย 3ย ย ย ย ย 4
|
| 360 |
+
|
| 361 |
+
33ย ย ย ย ย 44ย ย ย ย ย 55
|
| 362 |
+
|
| 363 |
+
REAR = 5
|
| 364 |
+
FRONT = 2
|
| 365 |
+
|
| 366 |
+
Fย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย R
|
| 367 |
+
|
| 368 |
+
Next insert another element, say 66 to the queue. We cannot insert 66 to the queue as the rear crossed the maximum size of the queue (i.e., 5). There will be queue full signal. The queue status is as follows:
|
| 369 |
+
|
| 370 |
+
0ย ย ย ย ย 1ย ย ย ย ย 2ย ย ย ย ย 3ย ย ย ย ย 4
|
| 371 |
+
|
| 372 |
+
33ย ย ย ย ย 44ย ย ย ย ย 55
|
| 373 |
+
|
| 374 |
+
REAR = 5
|
| 375 |
+
FRONT = 2
|
| 376 |
+
|
| 377 |
+
Fย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย R
|
| 378 |
+
|
| 379 |
+
This difficulty can be overcome if we treat queue position with index zero as a position that comes after position with index four then we treat the queue as a circular queue.
|
| 380 |
+
|
| 381 |
+
In circular queue if we reach the end for inserting elements to it, it is possible to insert new elements if the slots at the beginning of the circular queue are empty.
|
| 382 |
+
|
| 383 |
+
5
|
| 384 |
+
|
| 385 |
+
------------PAGE TALK ABOUT------------
|
| 386 |
+
ุชุชุญุฏุซ ุงูุตูุญุฉ ุนู ู
ูููู
**ุงูุทุงุจูุฑ ุงูุฏุงุฆุฑู (Circular Queue)** ูุญู ุฃูุซุฑ ููุงุกุฉ ู
ู ุงูุทุงุจูุฑ ุงูุฎุทู.
|
| 387 |
+
ุชูุถุญ ู
ุดููุชูู ูู ุงูุทุงุจูุฑ ุงูุฎุทู: ุงูุญุงุฌุฉ ุฅูู ุชุญุฑูู ุงูุนูุงุตุฑ ู
ู
ุง ูุณุชููู ููุชูุงุ ูุธููุฑ ุญุงูุฉ ุงูุงู
ุชูุงุก ุฑุบู
ูุฌูุฏ ุฃู
ุงูู ูุงุฑุบุฉ ูู ุจุฏุงูุฉ ุงูู
ุตูููุฉ.
|
| 388 |
+
ููุฌุฏ ุดููุงู ููุถุญุงู ู
ุตูููุฉ ุจุญุฌู
5 ูุฎุงูุงุชูุง ู
ุฑูู
ุฉ ู
ู 0 ุฅูู 4ุ ูุงูุนูุงุตุฑ ุงูู
ูุฌูุฏุฉ ูู 33 ู44 ู55ุ ู
ุน FRONT = 2 ูREAR = 5.
|
| 389 |
+
ุงูุฑุณูู
ุชุจูู ุฃู ุงูุฅุฏุฎุงู ููุดู ูู ุงูุทุงุจูุฑ ุงูุฎุทู ุนูุฏ ุงููุตูู ููููุงูุฉุ ุจููู
ุง ูู ุงูุทุงุจูุฑ ุงูุฏุงุฆุฑู ูู
ูู ุงูุฑุฌูุน ููุจุฏุงูุฉ ูุงุณุชุบูุงู ุงูุฎุงูุงุช ุงููุงุฑุบุฉ.
|
| 390 |
+
|
| 391 |
+
|
| 392 |
+
================================================================================
|
| 393 |
+
PAGE 6 - page_0006.png
|
| 394 |
+
================================================================================
|
| 395 |
+
|
| 396 |
+
------------TEXT EXTRACTED------------
|
| 397 |
+
ITGS 220
|
| 398 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 399 |
+
|
| 400 |
+
1.5.ย ย ย ย Representation of Circular Queue:
|
| 401 |
+
|
| 402 |
+
Let us consider a circular queue, which can hold maximum (MAX) of six elements.
|
| 403 |
+
Initially the queue is empty.
|
| 404 |
+
|
| 405 |
+
F R
|
| 406 |
+
|
| 407 |
+
5ย ย ย ย ย 0
|
| 408 |
+
4ย ย ย ย ย 1
|
| 409 |
+
3ย ย ย ย ย 2
|
| 410 |
+
|
| 411 |
+
Queue Empty
|
| 412 |
+
MAX = 6
|
| 413 |
+
FRONT = REAR = 0
|
| 414 |
+
COUNT = 0
|
| 415 |
+
|
| 416 |
+
Circular Queue
|
| 417 |
+
|
| 418 |
+
Now, insert 11 to the circular queue. Then circular queue status will be:
|
| 419 |
+
|
| 420 |
+
F
|
| 421 |
+
|
| 422 |
+
5ย ย ย ย ย 0
|
| 423 |
+
4ย ย ย ย ย 1
|
| 424 |
+
3ย ย ย ย ย 2
|
| 425 |
+
|
| 426 |
+
11
|
| 427 |
+
|
| 428 |
+
โฒ | R
|
| 429 |
+
|
| 430 |
+
FRONT = 0
|
| 431 |
+
REAR = (REAR + 1) % 6 = 1
|
| 432 |
+
COUNT = 1
|
| 433 |
+
|
| 434 |
+
Circular Queue
|
| 435 |
+
|
| 436 |
+
Insert new elements 22, 33, 44 and 55 into the circular queue. The circular queue status is:
|
| 437 |
+
|
| 438 |
+
Rย ย ย ย ย ย ย ย ย ย ย ย ย F
|
| 439 |
+
|
| 440 |
+
5ย ย ย ย ย 0
|
| 441 |
+
4ย ย ย ย ย 1
|
| 442 |
+
3ย ย ย ย ย 2
|
| 443 |
+
|
| 444 |
+
11
|
| 445 |
+
22
|
| 446 |
+
33
|
| 447 |
+
44
|
| 448 |
+
55
|
| 449 |
+
|
| 450 |
+
FRONT = 0
|
| 451 |
+
REAR = (REAR + 1) % 6 = 5
|
| 452 |
+
COUNT = 5
|
| 453 |
+
|
| 454 |
+
Circular Queue
|
| 455 |
+
|
| 456 |
+
6
|
| 457 |
+
|
| 458 |
+
------------PAGE TALK ABOUT------------
|
| 459 |
+
ุชุดุฑุญ ุงูุตูุญุฉ ุชู
ุซูู **ุงูุทุงุจูุฑ ุงูุฏุงุฆุฑู (Circular Queue)** ุจุณุนุฉ ูุตูู **6 ุนูุงุตุฑ**ุ ู
ุน ุชูุถูุญ ุญุงูุชู ูู ุงูุจุฏุงูุฉ ููู ูุงุฑุบ.
|
| 460 |
+
ุชุญุชูู ุนูู ุฑุณูู
ุงุช ุฏุงุฆุฑูุฉ ู
ูุณู
ุฉ ุฅูู ุงูู
ูุงูุน **0 ุฅูู 5** ูุชูุธูุฑ ู
ูุถุนู **FRONT** ู **REAR** ุจุงูุฃุณูู
.
|
| 461 |
+
ุซู
ุชุนุฑุถ ู
ุซุงู ุฅุฏุฎุงู ุงูุนูุตุฑ **11** ูููู ุชุชุบูุฑ ุงูููู
ุฅูู **FRONT = 0** ู **REAR = 1** ู **COUNT = 1**.
|
| 462 |
+
ุจุนุฏ ุฐูู ุชุถูู ุงูุนูุงุตุฑ **22ุ 33ุ 44ุ 55** ูุชูุถุญ ูู ุงูุฑุณู
ุฃู
ุงูููุง ุฏุงุฎู ุงูุทุงุจูุฑุ ู
ุน ุงูุญุงูุฉ ุงูููุงุฆูุฉ **COUNT = 5** ู **REAR = 5**.
|
| 463 |
+
|
| 464 |
+
|
| 465 |
+
================================================================================
|
| 466 |
+
PAGE 7 - page_0007.png
|
| 467 |
+
================================================================================
|
| 468 |
+
|
| 469 |
+
------------TEXT EXTRACTED------------
|
| 470 |
+
ITGS 220
|
| 471 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 472 |
+
|
| 473 |
+
Now, delete an element. The element deleted is the element at the front of the circular
|
| 474 |
+
queue. So, 11 is deleted. The circular queue status is as follows:
|
| 475 |
+
|
| 476 |
+
R
|
| 477 |
+
5ย ย ย ย ย ย ย ย ย 0
|
| 478 |
+
4ย ย ย ย ย ย ย ย ย 1
|
| 479 |
+
3ย ย ย ย ย ย ย ย ย 2
|
| 480 |
+
|
| 481 |
+
22
|
| 482 |
+
33
|
| 483 |
+
44
|
| 484 |
+
55
|
| 485 |
+
|
| 486 |
+
F
|
| 487 |
+
|
| 488 |
+
FRONT = (FRONT + 1) % 6 = 1
|
| 489 |
+
REAR = 5
|
| 490 |
+
COUNT = COUNT - 1 = 4
|
| 491 |
+
|
| 492 |
+
Circular Queue
|
| 493 |
+
|
| 494 |
+
Again, delete an element. The element to be deleted is always pointed to by the FRONT
|
| 495 |
+
pointer. So, 22 is deleted. The circular queue status is as follows:
|
| 496 |
+
|
| 497 |
+
R
|
| 498 |
+
5ย ย ย ย ย ย ย ย ย 0
|
| 499 |
+
4ย ย ย ย ย ย ย ย ย 1
|
| 500 |
+
3ย ย ย ย ย ย ย ย ย 2
|
| 501 |
+
|
| 502 |
+
33
|
| 503 |
+
44
|
| 504 |
+
55
|
| 505 |
+
|
| 506 |
+
F
|
| 507 |
+
|
| 508 |
+
FRONT = (FRONT + 1) % 6 = 2
|
| 509 |
+
REAR = 5
|
| 510 |
+
COUNT = COUNT - 1 = 3
|
| 511 |
+
|
| 512 |
+
Circular Queue
|
| 513 |
+
|
| 514 |
+
Again, insert another element 66 to the circular queue. The status of the circular queue
|
| 515 |
+
is:
|
| 516 |
+
|
| 517 |
+
R
|
| 518 |
+
5ย ย ย ย ย ย ย ย ย 0
|
| 519 |
+
4ย ย ย ย ย ย ย ย ย 1
|
| 520 |
+
3ย ย ย ย ย ย ย ย ย 2
|
| 521 |
+
|
| 522 |
+
66
|
| 523 |
+
33
|
| 524 |
+
44
|
| 525 |
+
55
|
| 526 |
+
|
| 527 |
+
F
|
| 528 |
+
|
| 529 |
+
FRONT = 2
|
| 530 |
+
REAR = (REAR + 1) % 6 = 0
|
| 531 |
+
COUNT = COUNT + 1 = 4
|
| 532 |
+
|
| 533 |
+
Circular Queue
|
| 534 |
+
|
| 535 |
+
Now, insert new elements 77 and 88 into the circular queue. The circular queue status
|
| 536 |
+
is:
|
| 537 |
+
|
| 538 |
+
7
|
| 539 |
+
|
| 540 |
+
------------PAGE TALK ABOUT------------
|
| 541 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ุนู
ููุงุช ุงูุญุฐู ูุงูุฅุฏุฎุงู ูู **ุงูุทุงุจูุฑ ุงูุฏุงุฆุฑู (Circular Queue)** ุฎุทูุฉ ุจุฎุทูุฉ.
|
| 542 |
+
ุชูุถุญ ุฃูููุง ุญุฐู ุงูุนูุตุฑ ุงูุฃู
ุงู
ู 11 ุซู
ุชุญุฏูุซ ุงูู
ุคุดุฑุงุช ุจุญูุซ ูุตุจุญ `FRONT = 1` ู`COUNT = 4`ุ ุซู
ุญุฐู 22 ููุตุจุญ `FRONT = 2` ู`COUNT = 3`.
|
| 543 |
+
ุจุนุฏ ุฐูู ุชุดุฑุญ ุฅุฏุฎุงู ุงูุนูุตุฑ 66 ูู ููุงูุฉ ุงูุทุงุจูุฑ ู
ุน ุงูุชูุงู ุงูู
ุคุด๏ฟฝ๏ฟฝ ุงูุฎูููุ ููุตุจุญ `REAR = 0` ู`COUNT = 4`.
|
| 544 |
+
ุชุชุถู
ู ุงูุตูุญุฉ ุซูุงุซุฉ ุฑุณูู
ุงุช ุฏุงุฆุฑูุฉ ุชู
ุซู ู
ูุงูุน ุงูุฎุงูุงุช ู
ู 0 ุฅูู 5ุ ู
ุน ุฃุณูู
ุชูุถุญ ู
ูุถุนู `F` ู`R` ูุงูุนูุงุตุฑ ุงูู
ูุฌูุฏุฉ ู
ุซู 33 ู44 ู55 ู66 ุฏุงุฎู ุงูุทุงุจูุฑ.
|
| 545 |
+
|
| 546 |
+
|
| 547 |
+
================================================================================
|
| 548 |
+
PAGE 8 - page_0008.png
|
| 549 |
+
================================================================================
|
| 550 |
+
|
| 551 |
+
------------TEXT EXTRACTED------------
|
| 552 |
+
ITGS 220
|
| 553 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 554 |
+
|
| 555 |
+
5ย ย ย ย ย ย ย ย ย ย ย ย ย 0
|
| 556 |
+
66ย ย ย ย ย ย ย ย ย ย ย ย 77
|
| 557 |
+
4ย ย ย ย ย 55ย ย ย ย ย ย ย ย ย ย ย ย ย 88ย ย ย ย ย 1
|
| 558 |
+
44ย ย ย ย ย ย ย ย ย ย ย ย 33
|
| 559 |
+
3ย ย ย ย ย ย ย ย ย ย ย ย ย 2
|
| 560 |
+
|
| 561 |
+
R
|
| 562 |
+
F
|
| 563 |
+
|
| 564 |
+
FRONT = 2, REAR = 2
|
| 565 |
+
REAR = REAR % 6 = 2
|
| 566 |
+
COUNT = 6
|
| 567 |
+
|
| 568 |
+
Circular Q u e u e
|
| 569 |
+
|
| 570 |
+
Now, if we insert an element to the circular queue, as COUNT = MAX we cannot add
|
| 571 |
+
the element to circular queue. So, the circular queue is full.
|
| 572 |
+
|
| 573 |
+
1.6.ย ย ย ย Source code for Circular Queue operations, using array:
|
| 574 |
+
|
| 575 |
+
#include <stdio.h>
|
| 576 |
+
#include <stdlib.h> // For system() clearScreen
|
| 577 |
+
#define MAX 6
|
| 578 |
+
|
| 579 |
+
// Global variables
|
| 580 |
+
int CQ[MAX];
|
| 581 |
+
int front = 0;
|
| 582 |
+
int rear = 0;
|
| 583 |
+
int count = 0;
|
| 584 |
+
|
| 585 |
+
// Function prototypes
|
| 586 |
+
|
| 587 |
+
int menu();
|
| 588 |
+
void insertCQ();
|
| 589 |
+
void deleteCQ();
|
| 590 |
+
void displayCQ();
|
| 591 |
+
|
| 592 |
+
int main() {
|
| 593 |
+
int ch;
|
| 594 |
+
do {
|
| 595 |
+
system("cls");
|
| 596 |
+
ch = menu();
|
| 597 |
+
|
| 598 |
+
switch(ch) {
|
| 599 |
+
case 1:
|
| 600 |
+
insertCQ();
|
| 601 |
+
break;
|
| 602 |
+
case 2:
|
| 603 |
+
deleteCQ();
|
| 604 |
+
break;
|
| 605 |
+
case 3:
|
| 606 |
+
displayCQ();
|
| 607 |
+
break;
|
| 608 |
+
case 4:
|
| 609 |
+
return 0;
|
| 610 |
+
default:
|
| 611 |
+
printf("\n Invalid Choice ");
|
| 612 |
+
}
|
| 613 |
+
|
| 614 |
+
printf("\nPress any key to continue...");
|
| 615 |
+
getchar(); // Clear input buffer
|
| 616 |
+
getchar(); // Wait for key press
|
| 617 |
+
|
| 618 |
+
} while(1);
|
| 619 |
+
}
|
| 620 |
+
|
| 621 |
+
8
|
| 622 |
+
|
| 623 |
+
------------PAGE TALK ABOUT------------
|
| 624 |
+
ุงูุตูุญุฉ ุชุดุฑุญ ุจููุฉ **ุงูู Circular Queue** ุจุงุณุชุฎุฏุงู
ู
ุตูููุฉ ุจุญุฌู
**MAX = 6**ุ ูุชูุถุญ ุญุงูุฉ ุงูุงู
ุชูุงุก ุนูุฏู
ุง ูุตุจุญ **COUNT = 6** ููุง ูู
ูู ุฅุฏุฎุงู ุนูุตุฑ ุฌุฏูุฏ.
|
| 625 |
+
ููุฌุฏ ุฑุณู
ุฏุงุฆุฑู ู
ูุณู
ุฅูู 6 ุฎุงูุงุช ู
ุฑูู
ุฉ ู
ู 0 ุฅูู 5ุ ูุชุญุชูู ุงูููู
: **77, 88, 33, 44, 55, 66**ุ ู
ุน ุชูุถูุญ ุฃู **FRONT = 2** ู **REAR = 2** ุจุนุฏ ุงูุงูุชูุงู.
|
| 626 |
+
ูู
ุง ุชุนุฑุถ ุงูุตูุญุฉ ุจุฏุงูุฉ ููุฏ ูุบุฉ **C** ูุชูููุฐ ุนู
ููุงุช ุงูุทุงุจูุฑ ุงูุฏุงุฆุฑู: **insert**, **delete**, **display**ุ ู
ุน ุชุนุฑูู ุงูู
ุชุบูุฑุงุช ุงูุนุงู
ุฉ ูุงูุฏุงูุฉ **main** ููุงุฆู
ุฉ ุงูุฎูุงุฑุงุช.
|
| 627 |
+
ุงูุฑุณู
ููุถุญ ููุฑุฉ ุงูุงูุชูุงู ูู ุงูุทุงุจูุฑ ุงูุฏุงุฆุฑู ูุฃู ุงูู
ุคุดุฑ ุงูุฎููู ูุนูุฏ ููุจุฏุงูุฉ ุจุงุณุชุฎุฏุงู
ุงูุนู
ููุฉ **REAR % 6**.
|
| 628 |
+
|
| 629 |
+
|
| 630 |
+
================================================================================
|
| 631 |
+
PAGE 9 - page_0009.png
|
| 632 |
+
================================================================================
|
| 633 |
+
|
| 634 |
+
------------TEXT EXTRACTED------------
|
| 635 |
+
ITGS 220
|
| 636 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 637 |
+
|
| 638 |
+
________________________________________________________________________________
|
| 639 |
+
|
| 640 |
+
int menu() {
|
| 641 |
+
ย ย ย ย int ch;
|
| 642 |
+
ย ย ย ย printf("\n \t Circular Queue Operations using ARRAY..");
|
| 643 |
+
ย ย ย ย printf("\n ----------------*******\n");
|
| 644 |
+
ย ย ย ย printf("\n 1. Insert ");
|
| 645 |
+
ย ย ย ย printf("\n 2. Delete ");
|
| 646 |
+
ย ย ย ย printf("\n 3. Display");
|
| 647 |
+
ย ย ย ย printf("\n 4. Quit ");
|
| 648 |
+
ย ย ย ย printf("\n Enter Your Choice: ");
|
| 649 |
+
ย ย ย ย scanf("%d", &ch);
|
| 650 |
+
ย ย ย ย return ch;
|
| 651 |
+
}
|
| 652 |
+
|
| 653 |
+
void insertCQ() {
|
| 654 |
+
ย ย ย ย if(count == MAX) {
|
| 655 |
+
ย ย ย ย ย ย ย ย printf("\n Circular Queue is Full");
|
| 656 |
+
ย ย ย ย }
|
| 657 |
+
ย ย ย ย else {
|
| 658 |
+
ย ย ย ย ย ย ย ย int data;
|
| 659 |
+
ย ย ย ย ย ย ย ย printf("\n Enter data: ");
|
| 660 |
+
ย ย ย ย ย ย ย ย scanf("%d", &data);
|
| 661 |
+
ย ย ย ย ย ย ย ย CQ[rear] = data;
|
| 662 |
+
ย ย ย ย ย ย ย ย rear = (rear + 1) % MAX;
|
| 663 |
+
ย ย ย ย ย ย ย ย count++;
|
| 664 |
+
ย ย ย ย ย ย ย ย printf("\n Data Inserted in the Circular Queue ");
|
| 665 |
+
ย ย ย ย }
|
| 666 |
+
}
|
| 667 |
+
|
| 668 |
+
void deleteCQ() {
|
| 669 |
+
ย ย ย ย if(count == 0) {
|
| 670 |
+
ย ย ย ย ย ย ย ย printf("\n\nCircular Queue is Empty..");
|
| 671 |
+
ย ย ย ย }
|
| 672 |
+
ย ย ย ย else {
|
| 673 |
+
ย ย ย ย ย ย ย ย printf("\n Deleted element from Circular Queue is %d ", CQ[front]);
|
| 674 |
+
ย ย ย ย ย ย ย ย front = (front + 1) % MAX;
|
| 675 |
+
ย ย ย ย ย ย ย ย count--;
|
| 676 |
+
ย ย ย ย }
|
| 677 |
+
}
|
| 678 |
+
|
| 679 |
+
void displayCQ() {
|
| 680 |
+
ย ย ย ย int i, j;
|
| 681 |
+
ย ย ย ย if(count == 0) {
|
| 682 |
+
ย ย ย ย ย ย ย ย printf("\n\n\t Circular Queue is Empty ");
|
| 683 |
+
ย ย ย ย }
|
| 684 |
+
ย ย ย ย else {
|
| 685 |
+
ย ย ย ย ย ย ย ย printf("\n Elements in Circular Queue are: ");
|
| 686 |
+
ย ย ย ย ย ย ย ย j = count;
|
| 687 |
+
ย ย ย ย ย ย ย ย for(i = front; j != 0; j--) {
|
| 688 |
+
ย ย ย ย ย ย ย ย ย ย ย ย printf("%d\t", CQ[i]);
|
| 689 |
+
ย ย ย ย ย ย ย ย ย ย ย ย i = (i + 1) % MAX;
|
| 690 |
+
ย ย ย ย ย ย ย ย }
|
| 691 |
+
ย ย ย ย }
|
| 692 |
+
}
|
| 693 |
+
|
| 694 |
+
9
|
| 695 |
+
|
| 696 |
+
------------PAGE TALK ABOUT------------
|
| 697 |
+
ุงูุตูุญุฉ ุชุนุฑุถ ููุฏ ุจูุบุฉ C ูุชูููุฐ ุนู
ููุงุช **ุงูุทุงุจูุฑ ุงูุฏุงุฆุฑู (Circular Queue)** ุจุงุณุชุฎุฏุงู
**ู
ุตูููุฉ**.
|
| 698 |
+
ุชุชุถู
ู ุฏุงูุฉ ูุงุฆู
ุฉ `menu()` ุชุนุฑุถ ุงูุฎูุงุฑุงุช: ุงูุฅุฏุฎุงูุ ุงูุญุฐูุ ุงูุนุฑุถุ ูุงูุฎุฑูุฌ.
|
| 699 |
+
ูู
ุง ุชุญุชูู ุนูู ุฏูุงู `insertCQ()` ู`deleteCQ()` ู`displayCQ()` ูุฅุฏุงุฑุฉ ุนูุงุตุฑ ุงูุทุงุจูุฑ ู
ุน ุงุณุชุฎุฏุงู
`front` ู`rear` ู`count` ู`MAX`.
|
| 700 |
+
ูุง ุชูุฌุฏ ุตูุฑ ุฃู ู
ุฎุทุทุงุชุ ููุท ุชูุณูู ุตูุญุฉ ู
ุญุงุถุฑุฉ ู
ุน ุนููุงู ุงูู
ูุฑุฑ ูุงุณู
ุงูุฏูุชูุฑุฉ ูุงููุตู ุงูุฏุฑุงุณู ูุฑูู
ุงูุตูุญุฉ.
|
| 701 |
+
|
| 702 |
+
|
| 703 |
+
================================================================================
|
| 704 |
+
PAGE 10 - page_0010.png
|
| 705 |
+
================================================================================
|
| 706 |
+
|
| 707 |
+
------------TEXT EXTRACTED------------
|
| 708 |
+
ITGS 220
|
| 709 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 710 |
+
|
| 711 |
+
Questions
|
| 712 |
+
|
| 713 |
+
1.ย ย ย ย ย Is it possible to have two designs for the same data structure that provide the
|
| 714 |
+
ย ย ย ย ย ย ย ย same functionality but are implemented differently?
|
| 715 |
+
|
| 716 |
+
2.ย ย ย ย ย What is the difference between the logical representation of a data structure and
|
| 717 |
+
ย ย ย ย ย ย ย ย the physical representation?
|
| 718 |
+
|
| 719 |
+
3.ย ย ย ย ย Transform the following infix expressions to reverse polish notation (prefix):
|
| 720 |
+
ย ย ย ย ย ย ย ย a) A โ B * C โ D + E / F / (G +H)
|
| 721 |
+
ย ย ย ย ย ย ย ย b) ((A + B) * C โ (D โ E)) โ (F + G)
|
| 722 |
+
ย ย ย ย ย ย ย ย c) A โ B / (C * D โE)
|
| 723 |
+
ย ย ย ย ย ย ย ย d) (a + b โ c โ d) * (e + f / d))
|
| 724 |
+
ย ย ย ย ย ย ย ย f) 3 โ 6 * 7 + 2 / 4 * 5 โ 8
|
| 725 |
+
ย ย ย ย ย ย ย ย g) (A โ B) / ((D + E) * F)
|
| 726 |
+
ย ย ย ย ย ย ย ย h) ((A + B) / D) โ ((E โ F) * G)
|
| 727 |
+
|
| 728 |
+
4.ย ย ย ย ย Evaluate the following postfix expressions:
|
| 729 |
+
ย ย ย ย ย ย ย ย a) Pโ: 5, 3, +, 2, *, 6, 9, 7, -, /, -
|
| 730 |
+
ย ย ย ย ย ย ย ย b) Pโ: 3, 5, +, 6, 4, -, *, 4, 1, -, 2, โ, +
|
| 731 |
+
ย ย ย ย ย ย ย ย c) Pโ : 3, 1, +, 2, โ, 7, 4, -, 2, *, +, 5,-
|
| 732 |
+
|
| 733 |
+
5.ย ย ย ย ย Consider the usual algorithm to convert an infix expression to a postfix expression.
|
| 734 |
+
ย ย ย ย ย ย ย ย Suppose that you have read 10 input characters during a conversion and that the
|
| 735 |
+
ย ย ย ย ย ย ย ย stack now contains these symbols:
|
| 736 |
+
|
| 737 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย +
|
| 738 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย (
|
| 739 |
+
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย *
|
| 740 |
+
|
| 741 |
+
ย ย ย ย ย ย ย ย bottom
|
| 742 |
+
|
| 743 |
+
ย ย ย ย ย ย ย ย Now, suppose that you read and process the 11th symbol of the input. Draw the
|
| 744 |
+
ย ย ย ย ย ย ย ย stack for the case where the 11th symbol is:
|
| 745 |
+
ย ย ย ย ย ย ย ย A. A number:
|
| 746 |
+
ย ย ย ย ย ย ย ย B. A left parenthesis:
|
| 747 |
+
ย ย ย ย ย ย ย ย C. A right parenthesis:
|
| 748 |
+
ย ย ย ย ย ย ย ย D. A minus sign:
|
| 749 |
+
ย ย ย ย ย ย ย ย E. A division sign:
|
| 750 |
+
|
| 751 |
+
10
|
| 752 |
+
|
| 753 |
+
------------PAGE TALK ABOUT------------
|
| 754 |
+
ุงูุตูุญุฉ ุชุญุชูู ุนูู ุฃุณุฆูุฉ ูู
ุงุฏุฉ ููุงูู ุงูุจูุงูุงุช/ุงูุชุนุงุจูุฑ ุงูุฑูุงุถูุฉ ูู ู
ูุฑุฑ ITGS 220 ููุตู Fall 2025.
|
| 755 |
+
ุชุฑููุฒ ุงูุฃุณุฆูุฉ ุนูู ู
ูุงููู
ุชุตู
ูู
ููุงูู ุงูุจูุงูุงุชุ ูุงููุฑู ุจูู ุงูุชู
ุซูู ุงูู
ูุทูู ูุงูููุฒูุงุฆูุ ูุชุญููู ุงูุชุนุงุจูุฑ ุจูู infix ูprefix/postfix ูุชูููู
postfix.
|
| 756 |
+
ููุฌุฏ ุฃูุถูุง ุณุคุงู ุนู ุฎูุงุฑุฒู
ูุฉ ุชุญููู infix ุฅูู postfix ุจุงุณุชุฎุฏุงู
ุงูู
ูุฏุณ (stack).
|
| 757 |
+
ุงูุนูุตุฑ ุงูุจุตุฑู ุงููุญูุฏ ูู ุฑุณู
ุจุณูุท ูู
ูุฏุณ ููุถูุญ ุฃู ู
ุญุชููุงุชู ุงูุญุงููุฉ ู
ู ุงูุฃุนูู ุฅูู ุงูุฃุณูู: + ุซู
( ุซู
* ู
ุน ููู
ุฉ bottom ุจุฌุงูุจ ุงููุงุน.
|
| 758 |
+
|
| 759 |
+
|
| 760 |
+
================================================================================
|
| 761 |
+
PAGE 11 - page_0011.png
|
| 762 |
+
================================================================================
|
| 763 |
+
|
| 764 |
+
------------TEXT EXTRACTED------------
|
| 765 |
+
ITGS 220
|
| 766 |
+
Dr. Amna Ali A Mohamedย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Fall 2025
|
| 767 |
+
|
| 768 |
+
6.ย ย ย ย Write a program using stack for parenthesis matching. Explain what modifications
|
| 769 |
+
ย ย ย ย ย ย would be needed to make the parenthesis matching algorithm check expressions
|
| 770 |
+
ย ย ย ย ย ย with different kinds of parentheses such as (), [] and {}'s.
|
| 771 |
+
|
| 772 |
+
7.ย ย ย ย Evaluate the following prefix expressions:
|
| 773 |
+
ย ย ย ย ย ย a) + * 2 + / 14 2 5 1
|
| 774 |
+
ย ย ย ย ย ย b) - * 6 3 - 4 1
|
| 775 |
+
ย ย ย ย ย ย c) + + 2 6 + - 13 2 4
|
| 776 |
+
|
| 777 |
+
8.ย ย ย ย Convert the following infix expressions to prefix notation:
|
| 778 |
+
ย ย ย ย ย ย a) ((A + 2) * (B + 4)) -1
|
| 779 |
+
ย ย ย ย ย ย b) Z - (((X + 1) * 2) - 5) / Y)
|
| 780 |
+
ย ย ย ย ย ย c) (((c * 2) + 1) / (A + B)
|
| 781 |
+
ย ย ย ย ย ย d) ((A + B) * C โ (D - E)) โ (F + G)
|
| 782 |
+
ย ย ย ย ย ย e) A โ B / (C * D โ E)
|
| 783 |
+
|
| 784 |
+
9.ย ย ย ย Write a โCโ function to copy one stack to another assuming
|
| 785 |
+
ย ย ย ย ย ย a) The stack is implemented using array.
|
| 786 |
+
|
| 787 |
+
10.ย ย Write an algorithm to construct a fully parenthesized infix expression from its
|
| 788 |
+
ย ย ย ย ย ย postfix equivalent. Write a โCโ function for your algorithm.
|
| 789 |
+
|
| 790 |
+
11.ย ย How can one convert a postfix expression to its prefix equivalent and vice-versa?
|
| 791 |
+
|
| 792 |
+
12.ย ย In a circular queue represented by an array, how can one specify the number of
|
| 793 |
+
ย ย ย ย ย ย elements in the queue in terms of โfrontโ, โrearโ and MAX-QUEUE-SIZE? Write a
|
| 794 |
+
ย ย ย ย ย ย โCโ function to delete the K-th element from the โfrontโ of a circular queue.
|
| 795 |
+
|
| 796 |
+
11
|
| 797 |
+
|
| 798 |
+
------------PAGE TALK ABOUT------------
|
| 799 |
+
ุงูุตูุญุฉ ุชุนุฑุถ ุฃุณุฆูุฉ ูุชู
ุงุฑูู ู
ู ู
ูุฑุฑ ITGS 220 ููุตู Fall 2025 ู
ุน ุงุณู
ุงูุฏูุชูุฑุฉ Amna Ali A Mohamed.
|
| 800 |
+
ุชุฑููุฒ ุงูุฃุณุฆูุฉ ุนูู ููุงูู ุงูุจูุงูุงุชุ ุฎุงุตุฉ ุงูู
ูุฏุณ (Stack) ูุงูุทุงุจูุฑ ุงูุฏุงุฆุฑู (Circular Queue).
|
| 801 |
+
ุชุชุถู
ู ุชู
ุงุฑูู ุนู ู
ุทุงุจูุฉ ุงูุฃููุงุณุ ุชูููู
ูุชุญููู ุงูุชุนุจูุฑุงุช ุจูู Prefix ูInfix ูPostfixุ ููุชุงุจุฉ ุฏูุงู ุจูุบุฉ C.
|
| 802 |
+
ูุง ุชูุฌุฏ ุตูุฑ ุฃู ู
ุฎุทุทุงุชุ ุงูุตูุญุฉ ุนุจุงุฑุฉ ุนู ูุต ู
ูุณู ุฃูุงุฏูู
ู ู
ุน ุชุฑููู
ููุฃุณุฆูุฉ ู
ู 6 ุฅูู 12 ูุฑูู
ุงูุตูุญุฉ 11.
|
data_structure/structure.txt
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"ุฃูุช ูุธุงู
ุชูุฌูู ุฐูู ูู
ุณุงุนุฏ ุชุนููู
ู ูู ู
ุงุฏุฉ ุงูุฑูุงุถูุงุช.
|
| 2 |
+
|
| 3 |
+
ู
ูู
ุชู: ุชุญููู ุฑุณุงูุฉ ุงูุทุงูุจ ูุงุฎุชูุงุฑ ุงูู
ูู ุงูู
ูุงุณุจ ููุฑุฏ.
|
| 4 |
+
|
| 5 |
+
**ุงูู
ููุงุช ุงูู
ุชุงุญุฉ:**
|
| 6 |
+
- main.txt: ููุชุญูุงุชุ ุงูุฃุณุฆูุฉ ุงูุนุงู
ุฉุ ุงูุชุนุฑูู ุจุงูููุณุ ุฃู ุฃู ุดูุก ูุง ูุชุนูู ุจูุตูู ุงููุชุงุจ ู
ุจุงุดุฑุฉ
|
| 7 |
+
- p1.txt: ุงููุตู ุงูุฃูู
|
| 8 |
+
- p2.txt: ุงููุตู ุงูุซุงูู
|
| 9 |
+
- p3.txt: ุงููุตู ุงูุซุงูุซ
|
| 10 |
+
- p4.txt: ุงููุตู ุงูุฑุงุจุน
|
| 11 |
+
- p5.txt: ุงููุตู ุงูุฎุงู
ุณ
|
| 12 |
+
etc.....
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
C & Data Structures
|
| 16 |
+
|
| 17 |
+
Chapter 1: Address, Pointers, Arrays, and Structures
|
| 18 |
+
Chapter 2: Address, Pointers, Arrays, and Structures (part 2)
|
| 19 |
+
Chapter 3: function & recursion
|
| 20 |
+
Chapter 4: stack
|
| 21 |
+
Chapter 5: stack (part 2)
|
| 22 |
+
Chapter 6: Queue
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
**ุชุนููู
ุงุช ุงูุชูุฌูู:**
|
| 27 |
+
1. ุฅุฐุง ูุงูุช ุงูุฑุณุงูุฉ ุชุญูุฉ ุฃู ุณุคุงู ุนุงู
ูุง ูุชุนูู ุจุชุฑุงููุจ ุจูุงูุงุช โ ุงุฎุชุฑ main.txt
|
| 28 |
+
2. ุฅุฐุง ูุงูุช ุงูุฑุณุงูุฉ ุชุณุฃู ุนู ู
ูุถูุน ู
ุญุฏุฏ ูู ุชุฑุงููุจ ุจูุงูุงุช โ ุงุฎุชุฑ ุงูู
ูู ุงูู
ูุงุณุจ ุญุณุจ ุงูููุฑุณ
|
| 29 |
+
3. ุฅุฐุง ูุงูุช ุงูุฑุณุงูุฉ ู
ุชุงุจุนุฉ ูู
ูุถูุน ุณุงุจู (ู
ุซู "ุงุดุฑุญ ุฃูุซุฑ" ุฃู "ู
ุง ุงูู
ูุตูุฏ") โ ุงุณุชุฎุฏู
ููุณ ุงูู
ูู ุงูุฃุฎูุฑ: {last_file}
|
| 30 |
+
4. ุฅุฐุง ูุงู ุงูุทุงูุจ "ุงุณุชู
ุฑ" ุฃู "ุฃูู
ู" โ ุงุณุชุฎุฏู
ููุณ ุงูู
ูู ุงูุฃุฎูุฑ
|
| 31 |
+
5. ุฃุฌุจ **ููุท** ุจุงุณู
ุงูู
ูู (ู
ุซุงู: p1.txt ุฃู main.txt)
|
front_end_road.md
ADDED
|
@@ -0,0 +1,530 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ๐จ Frontend Update Road Map
|
| 2 |
+
## Guide for Frontend Developer
|
| 3 |
+
|
| 4 |
+
This document explains all backend changes that require frontend updates.
|
| 5 |
+
The backend developer cannot see HTML files, and the frontend developer
|
| 6 |
+
cannot see backend code. This file is the bridge.
|
| 7 |
+
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
## ๐ CRITICAL CHANGE #1: Board Now Supports Multiple Subjects
|
| 11 |
+
|
| 12 |
+
### What Changed (Backend)
|
| 13 |
+
- The board is NO LONGER hardcoded to `physics_mechanic`
|
| 14 |
+
- Board now works with ANY subject that is in the `BOARD_ENABLED_SUBJECTS` list
|
| 15 |
+
- Each subject has its own folder with its own data files
|
| 16 |
+
- The board page URL changed from `/` to `/board/<subject_id>`
|
| 17 |
+
|
| 18 |
+
### What Frontend Must Do
|
| 19 |
+
|
| 20 |
+
#### 1. Dashboard (dashboard.html)
|
| 21 |
+
- Each subject card in "my subjects" now has a property `board_enabled` (true/false)
|
| 22 |
+
- For subjects where `board_enabled` is true, show TWO buttons:
|
| 23 |
+
- **"ู
ุญุงุฏุซุฉ"** (Chat) โ links to `/chat_page` (existing)
|
| 24 |
+
- **"ุณุจูุฑุฉ"** (Board) โ links to `/board/{subject_id}` (NEW)
|
| 25 |
+
- For subjects where `board_enabled` is false, show only the Chat button
|
| 26 |
+
- Example:
|
| 27 |
+
```html
|
| 28 |
+
{% if subject.board_enabled %}
|
| 29 |
+
<a href="/board/{{ subject.id }}" class="btn btn-board">๐ ุณุจูุฑุฉ</a>
|
| 30 |
+
{% endif %}
|
| 31 |
+
<a href="/chat_page" class="btn btn-chat"
|
| 32 |
+
onclick="selectSubject('{{ subject.id }}')">๐ฌ ู
ุญุงุฏุซุฉ</a>
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
#### 2. Board Page (board.html)
|
| 36 |
+
- The board page now receives these template variables:
|
| 37 |
+
- `{{ username }}` - current user
|
| 38 |
+
- `{{ subject_id }}` - e.g. "physics_mechanic"
|
| 39 |
+
- `{{ subject_name }}` - e.g. "ุงูููุฒูุงุก ุงูู
ููุงููููุฉ"
|
| 40 |
+
- Display the subject name in the board header/title
|
| 41 |
+
- The board page URL is now `/board/physics_mechanic` instead of `/`
|
| 42 |
+
|
| 43 |
+
---
|
| 44 |
+
|
| 45 |
+
## ๐ CRITICAL CHANGE #2: Board API Endpoints Changed
|
| 46 |
+
|
| 47 |
+
### Old Endpoints (REMOVE)
|
| 48 |
+
```
|
| 49 |
+
POST /api/message โ REMOVED
|
| 50 |
+
POST /api/replay โ REMOVED
|
| 51 |
+
GET /api/board/state โ REMOVED
|
| 52 |
+
POST /api/board/clear โ REMOVED
|
| 53 |
+
POST /api/chat/clear โ REMOVED
|
| 54 |
+
POST /api/board/sync โ REMOVED
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
### New Endpoints (USE THESE)
|
| 58 |
+
```
|
| 59 |
+
POST /api/board/message โ Send message to board AI
|
| 60 |
+
POST /api/board/replay โ Replay last response (voice only)
|
| 61 |
+
GET /api/board/state โ Get board state
|
| 62 |
+
POST /api/board/clear โ Clear board
|
| 63 |
+
POST /api/board/chat/clear โ Clear board chat history
|
| 64 |
+
POST /api/board/sync โ Sync board state
|
| 65 |
+
POST /api/board/set-subject โ Set/switch board subject
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
### New Request Format for /api/board/message
|
| 69 |
+
```json
|
| 70 |
+
{
|
| 71 |
+
"message": "student text here",
|
| 72 |
+
"board_state": [... current board items ...],
|
| 73 |
+
"subject_id": "physics_mechanic"
|
| 74 |
+
}
|
| 75 |
+
```
|
| 76 |
+
- `subject_id` is OPTIONAL if already set via `/board/<subject_id>` page
|
| 77 |
+
- `board_state` is the current board items array from frontend (source of truth)
|
| 78 |
+
- Response format is UNCHANGED (same sequence array)
|
| 79 |
+
|
| 80 |
+
### JavaScript Update Example
|
| 81 |
+
```javascript
|
| 82 |
+
// OLD CODE (remove this):
|
| 83 |
+
fetch('/api/message', {
|
| 84 |
+
method: 'POST',
|
| 85 |
+
headers: {'Content-Type': 'application/json'},
|
| 86 |
+
body: JSON.stringify({
|
| 87 |
+
message: userMessage,
|
| 88 |
+
board_state: currentBoardState
|
| 89 |
+
})
|
| 90 |
+
})
|
| 91 |
+
|
| 92 |
+
// NEW CODE (use this):
|
| 93 |
+
fetch('/api/board/message', {
|
| 94 |
+
method: 'POST',
|
| 95 |
+
headers: {'Content-Type': 'application/json'},
|
| 96 |
+
body: JSON.stringify({
|
| 97 |
+
message: userMessage,
|
| 98 |
+
board_state: currentBoardState,
|
| 99 |
+
subject_id: CURRENT_SUBJECT_ID // from template: "{{ subject_id }}"
|
| 100 |
+
})
|
| 101 |
+
})
|
| 102 |
+
```
|
| 103 |
+
|
| 104 |
+
---
|
| 105 |
+
|
| 106 |
+
## ๐ CRITICAL CHANGE #3: Board Page Gets Subject Info
|
| 107 |
+
|
| 108 |
+
### Template Variables Available in board.html
|
| 109 |
+
```html
|
| 110 |
+
<!-- These are injected by the backend -->
|
| 111 |
+
<script>
|
| 112 |
+
const CURRENT_USER = "{{ username }}";
|
| 113 |
+
const CURRENT_SUBJECT_ID = "{{ subject_id }}";
|
| 114 |
+
const CURRENT_SUBJECT_NAME = "{{ subject_name }}";
|
| 115 |
+
</script>
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
### Use them in:
|
| 119 |
+
1. Page title: `<title>{{ subject_name }} - ุงูุณุจูุฑุฉ</title>`
|
| 120 |
+
2. Header: Show subject name prominently
|
| 121 |
+
3. API calls: Include `subject_id` in board message requests
|
| 122 |
+
4. Back button: Link back to `/dashboard`
|
| 123 |
+
|
| 124 |
+
---
|
| 125 |
+
|
| 126 |
+
## ๐ CHANGE #4: Exam Page Per Subject
|
| 127 |
+
|
| 128 |
+
### What Changed
|
| 129 |
+
- Exam page URL: `/exam/<subject_id>` (unchanged)
|
| 130 |
+
- No changes needed in exam.html if it already uses `{{ subject_id }}`
|
| 131 |
+
|
| 132 |
+
---
|
| 133 |
+
|
| 134 |
+
## ๐ CHANGE #5: Chat Page Subject Selection
|
| 135 |
+
|
| 136 |
+
### What Changed
|
| 137 |
+
- No changes to chat API endpoints
|
| 138 |
+
- Chat still uses `/select-subject`, `/switch-subject`, `/chat`, `/stream`
|
| 139 |
+
- All unchanged
|
| 140 |
+
|
| 141 |
+
---
|
| 142 |
+
|
| 143 |
+
## ๐ CHANGE #6: Static Files
|
| 144 |
+
|
| 145 |
+
### What Changed
|
| 146 |
+
- Audio files still served from `/static/audio/`
|
| 147 |
+
- No changes needed
|
| 148 |
+
|
| 149 |
+
---
|
| 150 |
+
|
| 151 |
+
## ๐ NEW: Subject Folder Structure
|
| 152 |
+
|
| 153 |
+
Each subject that supports the board must have a folder like:
|
| 154 |
+
```
|
| 155 |
+
physics_mechanic/
|
| 156 |
+
โโโ main.txt # General info + AI persona
|
| 157 |
+
โโโ structure.txt # Book index/table of contents
|
| 158 |
+
โโโ p1.txt - pN.txt # Chapter content files
|
| 159 |
+
โโโ pages_base_url.txt # NEW: URL template for book page images
|
| 160 |
+
```
|
| 161 |
+
|
| 162 |
+
### pages_base_url.txt Content Example
|
| 163 |
+
```
|
| 164 |
+
https://huggingface.co/spaces/CORVO-AI/pages/resolve/main/ู
ููุงูููุง/page_{page_num}.png
|
| 165 |
+
```
|
| 166 |
+
- Must contain `{page_num}` placeholder
|
| 167 |
+
- Each subject has its own URL pattern
|
| 168 |
+
- If this file is missing, `<page>` tags won't resolve to images
|
| 169 |
+
|
| 170 |
+
---
|
| 171 |
+
|
| 172 |
+
## ๐ SUMMARY OF FRONTEND FILES TO UPDATE
|
| 173 |
+
|
| 174 |
+
| File | Priority | Changes |
|
| 175 |
+
|------|----------|---------|
|
| 176 |
+
| `dashboard.html` | ๐ด HIGH | Add board button per subject, use `board_enabled` flag |
|
| 177 |
+
| `board.html` | ๐ด HIGH | Update API URLs, add subject_id to requests, show subject name |
|
| 178 |
+
| `chat.html` | ๐ข NONE | No changes needed |
|
| 179 |
+
| `exam.html` | ๐ข NONE | No changes needed |
|
| 180 |
+
| `market.html` | ๐ข NONE | No changes needed |
|
| 181 |
+
| `login.html` | ๐ข NONE | No changes needed |
|
| 182 |
+
| `signup.html` | ๐ข NONE | No changes needed |
|
| 183 |
+
|
| 184 |
+
---
|
| 185 |
+
|
| 186 |
+
## ๐ TESTING CHECKLIST
|
| 187 |
+
|
| 188 |
+
After frontend updates, test these flows:
|
| 189 |
+
|
| 190 |
+
1. โ
Login โ Dashboard shows subjects with board/chat buttons
|
| 191 |
+
2. โ
Click "ุณุจูุฑุฉ" on physics_mechanic โ Opens `/board/physics_mechanic`
|
| 192 |
+
3. โ
Board shows subject name in header
|
| 193 |
+
4. โ
Send message on board โ AI responds with voice + board elements
|
| 194 |
+
5. โ
Board elements appear correctly (notes, text, shapes, icons, page images)
|
| 195 |
+
6. โ
Replay works (voice only, no board duplication)
|
| 196 |
+
7. โ
Clear board works
|
| 197 |
+
8. โ
Navigate back to dashboard
|
| 198 |
+
9. โ
Click "ุณุจูุฑุฉ" on different subject โ Opens that subject's board
|
| 199 |
+
10. โ
Chat page still works independently
|
| 200 |
+
11. โ
Exam page still works independently
|
| 201 |
+
12. โ
Subjects without board_enabled don't show board button
|
| 202 |
+
|
| 203 |
+
---
|
| 204 |
+
|
| 205 |
+
## ๐ API RESPONSE FORMAT REFERENCE
|
| 206 |
+
|
| 207 |
+
### /api/board/message Response (UNCHANGED)
|
| 208 |
+
```json
|
| 209 |
+
{
|
| 210 |
+
"success": true,
|
| 211 |
+
"chosen_file": "p1.txt",
|
| 212 |
+
"sequence": [
|
| 213 |
+
{
|
| 214 |
+
"type": "board_update",
|
| 215 |
+
"action": "add",
|
| 216 |
+
"items": [
|
| 217 |
+
{"type": "note", "content": "...", "x": 100, "y": 100, ...},
|
| 218 |
+
{"type": "text", "content": "...", "x": 300, "y": 100, ...}
|
| 219 |
+
]
|
| 220 |
+
},
|
| 221 |
+
{
|
| 222 |
+
"type": "voice",
|
| 223 |
+
"text": "ุฃููุงู ุจู ูุง ุทุงูุจู...",
|
| 224 |
+
"audio_url": "/static/audio/voice_abc123.mp3"
|
| 225 |
+
},
|
| 226 |
+
{
|
| 227 |
+
"type": "board_update",
|
| 228 |
+
"action": "add",
|
| 229 |
+
"items": [...]
|
| 230 |
+
},
|
| 231 |
+
{
|
| 232 |
+
"type": "voice",
|
| 233 |
+
"text": "...",
|
| 234 |
+
"audio_url": "/static/audio/voice_def456.mp3"
|
| 235 |
+
}
|
| 236 |
+
],
|
| 237 |
+
"board_state": [... full updated board state ...]
|
| 238 |
+
}
|
| 239 |
+
```
|
| 240 |
+
|
| 241 |
+
### /api/board/replay Response
|
| 242 |
+
```json
|
| 243 |
+
{
|
| 244 |
+
"success": true,
|
| 245 |
+
"sequence": [
|
| 246 |
+
{
|
| 247 |
+
"type": "voice",
|
| 248 |
+
"text": "ุฃููุงู ุจู ูุง ุทุงูุจู...",
|
| 249 |
+
"audio_url": "/static/audio/voice_abc123.mp3"
|
| 250 |
+
},
|
| 251 |
+
{
|
| 252 |
+
"type": "voice",
|
| 253 |
+
"text": "ูุงุญุธ ุงูู
ุนุงุฏูุฉ ุนูู ุงูุณุจูุฑุฉ...",
|
| 254 |
+
"audio_url": "/static/audio/voice_def456.mp3"
|
| 255 |
+
}
|
| 256 |
+
]
|
| 257 |
+
}
|
| 258 |
+
```
|
| 259 |
+
Note: Replay returns ONLY voice segments. Board items are NOT included
|
| 260 |
+
because they are already on the board from the original response.
|
| 261 |
+
|
| 262 |
+
### /api/board/set-subject Response
|
| 263 |
+
```json
|
| 264 |
+
{
|
| 265 |
+
"success": true,
|
| 266 |
+
"subject_id": "physics_mechanic",
|
| 267 |
+
"subject_name": "ุงูููุฒูุงุก ุงูู
ููุงููููุฉ",
|
| 268 |
+
"message": "ุชู
ุชุญุฏูุฏ ู
ุงุฏุฉ 'ุงูููุฒูุงุก ุงูู
ููุงููููุฉ' ููุณุจูุฑุฉ"
|
| 269 |
+
}
|
| 270 |
+
```
|
| 271 |
+
|
| 272 |
+
### /api/board/clear Response
|
| 273 |
+
```json
|
| 274 |
+
{
|
| 275 |
+
"success": true,
|
| 276 |
+
"board_state": []
|
| 277 |
+
}
|
| 278 |
+
```
|
| 279 |
+
|
| 280 |
+
### /api/board/chat/clear Response
|
| 281 |
+
```json
|
| 282 |
+
{
|
| 283 |
+
"success": true
|
| 284 |
+
}
|
| 285 |
+
```
|
| 286 |
+
|
| 287 |
+
---
|
| 288 |
+
|
| 289 |
+
## ๐ DASHBOARD SUBJECT CARD DATA FORMAT
|
| 290 |
+
|
| 291 |
+
Each subject in `my_subjects` now has these fields available in the template:
|
| 292 |
+
|
| 293 |
+
```python
|
| 294 |
+
{
|
| 295 |
+
"id": "physics_mechanic", # Subject ID
|
| 296 |
+
"name": "ุงูููุฒูุงุก ุงูู
ููุงููููุฉ", # Display name
|
| 297 |
+
"price": 5, # Price (0 = free)
|
| 298 |
+
"icon": "gear", # FontAwesome icon name
|
| 299 |
+
"free": False, # Is it free?
|
| 300 |
+
"board_enabled": True # NEW: Does it support the board?
|
| 301 |
+
}
|
| 302 |
+
```
|
| 303 |
+
|
| 304 |
+
### Dashboard Template Example
|
| 305 |
+
```html
|
| 306 |
+
{% for subject in my_subjects %}
|
| 307 |
+
<div class="subject-card">
|
| 308 |
+
<div class="subject-icon">
|
| 309 |
+
<i class="fas fa-{{ subject.icon }}"></i>
|
| 310 |
+
</div>
|
| 311 |
+
<h3>{{ subject.name }}</h3>
|
| 312 |
+
|
| 313 |
+
<div class="subject-actions">
|
| 314 |
+
<!-- Chat button - always shown -->
|
| 315 |
+
<a href="/chat_page"
|
| 316 |
+
class="btn btn-chat"
|
| 317 |
+
onclick="localStorage.setItem('selected_subject', '{{ subject.id }}')">
|
| 318 |
+
๐ฌ ู
ุญุงุฏุซุฉ
|
| 319 |
+
</a>
|
| 320 |
+
|
| 321 |
+
<!-- Board button - only if board_enabled -->
|
| 322 |
+
{% if subject.board_enabled %}
|
| 323 |
+
<a href="/board/{{ subject.id }}" class="btn btn-board">
|
| 324 |
+
๐ ุณุจูุฑุฉ ุชูุงุนููุฉ
|
| 325 |
+
</a>
|
| 326 |
+
{% endif %}
|
| 327 |
+
|
| 328 |
+
<!-- Exam button - always shown -->
|
| 329 |
+
<a href="/exam/{{ subject.id }}" class="btn btn-exam">
|
| 330 |
+
๐ ุงุฎุชุจุงุฑ
|
| 331 |
+
</a>
|
| 332 |
+
</div>
|
| 333 |
+
</div>
|
| 334 |
+
{% endfor %}
|
| 335 |
+
```
|
| 336 |
+
|
| 337 |
+
---
|
| 338 |
+
|
| 339 |
+
## ๐ BOARD.HTML FULL JAVASCRIPT UPDATE GUIDE
|
| 340 |
+
|
| 341 |
+
### Step 1: Add Subject Variables (top of script)
|
| 342 |
+
```javascript
|
| 343 |
+
// These come from the Jinja template
|
| 344 |
+
const CURRENT_SUBJECT_ID = "{{ subject_id }}";
|
| 345 |
+
const CURRENT_SUBJECT_NAME = "{{ subject_name }}";
|
| 346 |
+
const CURRENT_USER = "{{ username }}";
|
| 347 |
+
```
|
| 348 |
+
|
| 349 |
+
### Step 2: Update sendMessage Function
|
| 350 |
+
```javascript
|
| 351 |
+
async function sendMessage() {
|
| 352 |
+
const message = document.getElementById('messageInput').value.trim();
|
| 353 |
+
if (!message) return;
|
| 354 |
+
|
| 355 |
+
// Disable input while processing
|
| 356 |
+
setLoading(true);
|
| 357 |
+
|
| 358 |
+
try {
|
| 359 |
+
const response = await fetch('/api/board/message', {
|
| 360 |
+
method: 'POST',
|
| 361 |
+
headers: {'Content-Type': 'application/json'},
|
| 362 |
+
body: JSON.stringify({
|
| 363 |
+
message: message,
|
| 364 |
+
board_state: getCurrentBoardState(), // Your existing function
|
| 365 |
+
subject_id: CURRENT_SUBJECT_ID // NEW: include subject
|
| 366 |
+
})
|
| 367 |
+
});
|
| 368 |
+
|
| 369 |
+
const data = await response.json();
|
| 370 |
+
|
| 371 |
+
if (data.success) {
|
| 372 |
+
processSequence(data.sequence); // Your existing function
|
| 373 |
+
} else {
|
| 374 |
+
showError(data.error || 'ุญุฏุซ ุฎุทุฃ');
|
| 375 |
+
}
|
| 376 |
+
} catch (err) {
|
| 377 |
+
showError('ุฎุทุฃ ูู ุงูุงุชุตุงู ุจุงูุฎุงุฏู
');
|
| 378 |
+
console.error(err);
|
| 379 |
+
} finally {
|
| 380 |
+
setLoading(false);
|
| 381 |
+
}
|
| 382 |
+
}
|
| 383 |
+
```
|
| 384 |
+
|
| 385 |
+
### Step 3: Update Replay Function
|
| 386 |
+
```javascript
|
| 387 |
+
async function replayLast() {
|
| 388 |
+
try {
|
| 389 |
+
const response = await fetch('/api/board/replay', {
|
| 390 |
+
method: 'POST',
|
| 391 |
+
headers: {'Content-Type': 'application/json'}
|
| 392 |
+
});
|
| 393 |
+
|
| 394 |
+
const data = await response.json();
|
| 395 |
+
|
| 396 |
+
if (data.success) {
|
| 397 |
+
// Play ONLY voice segments - no board updates
|
| 398 |
+
processVoiceSequence(data.sequence);
|
| 399 |
+
} else {
|
| 400 |
+
showError(data.error || 'ูุง ููุฌุฏ ุฑุฏ ุณุงุจู');
|
| 401 |
+
}
|
| 402 |
+
} catch (err) {
|
| 403 |
+
showError('ุฎุทุฃ ูู ุงูุงุชุตุงู');
|
| 404 |
+
}
|
| 405 |
+
}
|
| 406 |
+
```
|
| 407 |
+
|
| 408 |
+
### Step 4: Update Clear Functions
|
| 409 |
+
```javascript
|
| 410 |
+
async function clearBoard() {
|
| 411 |
+
try {
|
| 412 |
+
await fetch('/api/board/clear', {
|
| 413 |
+
method: 'POST',
|
| 414 |
+
headers: {'Content-Type': 'application/json'}
|
| 415 |
+
});
|
| 416 |
+
// Clear board visually on frontend
|
| 417 |
+
clearBoardVisual();
|
| 418 |
+
} catch (err) {
|
| 419 |
+
console.error(err);
|
| 420 |
+
}
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
async function clearChatHistory() {
|
| 424 |
+
try {
|
| 425 |
+
await fetch('/api/board/chat/clear', {
|
| 426 |
+
method: 'POST',
|
| 427 |
+
headers: {'Content-Type': 'application/json'}
|
| 428 |
+
});
|
| 429 |
+
} catch (err) {
|
| 430 |
+
console.error(err);
|
| 431 |
+
}
|
| 432 |
+
}
|
| 433 |
+
```
|
| 434 |
+
|
| 435 |
+
### Step 5: Update Page Title and Header
|
| 436 |
+
```html
|
| 437 |
+
<title>{{ subject_name }} - ุงูุณุจูุฑุฉ ุงูุชูุงุนููุฉ</title>
|
| 438 |
+
|
| 439 |
+
<!-- In the header/navbar -->
|
| 440 |
+
<div class="board-header">
|
| 441 |
+
<a href="/dashboard" class="back-btn">โ ุงูุนูุฏุฉ</a>
|
| 442 |
+
<h1>๐ {{ subject_name }}</h1>
|
| 443 |
+
<span class="user-badge">{{ username }}</span>
|
| 444 |
+
</div>
|
| 445 |
+
```
|
| 446 |
+
|
| 447 |
+
---
|
| 448 |
+
|
| 449 |
+
## ๐ BOARD ENABLED SUBJECTS LIST
|
| 450 |
+
|
| 451 |
+
Currently these subjects support the interactive board:
|
| 452 |
+
- `physics_mechanic` - ุงูููุฒูุงุก ุงูู
ููุงููููุฉ
|
| 453 |
+
- `physics_electric` - ุงูููุฒูุงุก ุงูููุฑุจุงุฆูุฉ
|
| 454 |
+
- `chemistry` - ุงูููู
ูุงุก
|
| 455 |
+
- `math` - ุงูุฑูุงุถูุงุช
|
| 456 |
+
- `biology` - ุงูุฃุญูุงุก
|
| 457 |
+
- `statistics` - ุฃุณุงุณ ุงูุฅุญุตุงุก
|
| 458 |
+
|
| 459 |
+
To add more subjects to the board, the backend developer adds the subject ID
|
| 460 |
+
to `BOARD_ENABLED_SUBJECTS` in `subjects/definitions.py` and creates the
|
| 461 |
+
subject folder with the required files.
|
| 462 |
+
|
| 463 |
+
The frontend does NOT need to change when new subjects are added - it just
|
| 464 |
+
checks the `board_enabled` flag.
|
| 465 |
+
|
| 466 |
+
---
|
| 467 |
+
|
| 468 |
+
## ๐ ERROR HANDLING
|
| 469 |
+
|
| 470 |
+
### Board API Errors
|
| 471 |
+
All board API endpoints return errors in this format:
|
| 472 |
+
```json
|
| 473 |
+
{
|
| 474 |
+
"success": false,
|
| 475 |
+
"error": "Error message in Arabic",
|
| 476 |
+
"sequence": [
|
| 477 |
+
{
|
| 478 |
+
"type": "voice",
|
| 479 |
+
"text": "ุนุฐุฑุงูุ ุญุฏุซ ุฎุทุฃ...",
|
| 480 |
+
"audio_url": null
|
| 481 |
+
}
|
| 482 |
+
],
|
| 483 |
+
"board_state": [... unchanged board state ...]
|
| 484 |
+
}
|
| 485 |
+
```
|
| 486 |
+
|
| 487 |
+
### HTTP Status Codes
|
| 488 |
+
- `200` - Success
|
| 489 |
+
- `400` - Bad request (missing data, invalid subject)
|
| 490 |
+
- `401` - Unauthorized (session expired)
|
| 491 |
+
- `403` - Forbidden (no access to subject)
|
| 492 |
+
- `404` - Not found (subject folder missing)
|
| 493 |
+
- `500` - Server error
|
| 494 |
+
|
| 495 |
+
### Frontend Should Handle
|
| 496 |
+
1. `401` โ Redirect to login page
|
| 497 |
+
2. `403` with `"code": "no_access"` โ Show "buy subject" prompt
|
| 498 |
+
3. `400` with `"need_subject": true` โ Show subject selection
|
| 499 |
+
4. `500` โ Show generic error message
|
| 500 |
+
5. Voice with `audio_url: null` โ Show text only, skip audio playback
|
| 501 |
+
|
| 502 |
+
---
|
| 503 |
+
|
| 504 |
+
## ๐ MIGRATION STEPS (Order of Operations)
|
| 505 |
+
|
| 506 |
+
1. **Backend deploys new code** (all the new files)
|
| 507 |
+
2. **Frontend updates dashboard.html** (add board buttons)
|
| 508 |
+
3. **Frontend updates board.html** (new API URLs + subject support)
|
| 509 |
+
4. **Test board with physics_mechanic** (existing subject)
|
| 510 |
+
5. **Test board with another subject** (e.g. chemistry)
|
| 511 |
+
6. **Test chat still works** (should be unchanged)
|
| 512 |
+
7. **Test exam still works** (should be unchanged)
|
| 513 |
+
8. **Done!**
|
| 514 |
+
|
| 515 |
+
---
|
| 516 |
+
|
| 517 |
+
## ๐ CONTACT
|
| 518 |
+
|
| 519 |
+
If anything is unclear or you need more details about a specific
|
| 520 |
+
endpoint or data format, create a question in the shared document
|
| 521 |
+
or contact the backend developer.
|
| 522 |
+
|
| 523 |
+
Backend files you should NEVER modify:
|
| 524 |
+
- Everything in `database/`, `auth/`, `chat/`, `board/`, `exam/`,
|
| 525 |
+
`media/`, `market/`, `websocket/`, `subjects/`
|
| 526 |
+
- `app.py`, `config.py`, `json_processor.py`
|
| 527 |
+
|
| 528 |
+
Frontend files the backend will NEVER modify:
|
| 529 |
+
- Everything in `templates/`
|
| 530 |
+
- Everything in `static/` (except `static/audio/` which is auto-managed)
|
json_processor.py
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
import re
|
| 4 |
+
from typing import Optional, Dict, Any, List
|
| 5 |
+
|
| 6 |
+
class BoardProcessor:
|
| 7 |
+
def __init__(self, api_url: str = "https://corvo-ai-claude-4-6-opus.hf.space/chat"):
|
| 8 |
+
self.api_url = api_url
|
| 9 |
+
self.system_prompt = self._load_system_prompt()
|
| 10 |
+
|
| 11 |
+
def _load_system_prompt(self) -> str:
|
| 12 |
+
"""Load system prompt from system.txt file"""
|
| 13 |
+
try:
|
| 14 |
+
with open('system.txt', 'r', encoding='utf-8') as f:
|
| 15 |
+
return f.read()
|
| 16 |
+
except FileNotFoundError:
|
| 17 |
+
# Provide a default system prompt if file not found
|
| 18 |
+
return """You are a converter that transforms XML board data into JSON format.
|
| 19 |
+
|
| 20 |
+
Convert the given XML to JSON following these rules:
|
| 21 |
+
1. Extract all elements from the XML
|
| 22 |
+
2. Convert them to proper JSON structure
|
| 23 |
+
3. Wrap your JSON output in <json></json> tags
|
| 24 |
+
|
| 25 |
+
Example:
|
| 26 |
+
Input: <board><note color="yellow" x="100" y="200">Hello</note></board>
|
| 27 |
+
Output: <json>{"board": {"notes": [{"color": "yellow", "x": 100, "y": 200, "text": "Hello"}]}}</json>
|
| 28 |
+
|
| 29 |
+
Always wrap your final JSON output in <json></json> tags."""
|
| 30 |
+
|
| 31 |
+
def _call_ai_api(
|
| 32 |
+
self,
|
| 33 |
+
user_input: Optional[str] = None,
|
| 34 |
+
chat_history: Optional[List[Dict[str, Any]]] = None,
|
| 35 |
+
temperature: float = 0.9,
|
| 36 |
+
top_p: float = 0.95,
|
| 37 |
+
max_tokens: Optional[int] = None
|
| 38 |
+
) -> str:
|
| 39 |
+
"""Call the AI API and return the assistant's response"""
|
| 40 |
+
|
| 41 |
+
payload = {
|
| 42 |
+
"user_input": user_input,
|
| 43 |
+
"chat_history": chat_history or [],
|
| 44 |
+
"temperature": temperature,
|
| 45 |
+
"top_p": top_p,
|
| 46 |
+
"max_tokens": max_tokens
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
response = requests.post(self.api_url, json=payload, timeout=120)
|
| 51 |
+
response.raise_for_status()
|
| 52 |
+
result = response.json()
|
| 53 |
+
return result.get("assistant_response", "")
|
| 54 |
+
except requests.exceptions.RequestException as e:
|
| 55 |
+
raise Exception(f"API request failed: {str(e)}")
|
| 56 |
+
|
| 57 |
+
def _extract_json_from_response(self, response: str) -> str:
|
| 58 |
+
"""Extract JSON from <json></json> tags in the AI response and return as text"""
|
| 59 |
+
|
| 60 |
+
# Print the raw response for debugging
|
| 61 |
+
print("\n๐ DEBUG - Raw AI Response:")
|
| 62 |
+
print("=" * 80)
|
| 63 |
+
print(response)
|
| 64 |
+
print("=" * 80)
|
| 65 |
+
|
| 66 |
+
# Try multiple patterns to find JSON
|
| 67 |
+
patterns = [
|
| 68 |
+
r'<json>(.*?)</json>', # Standard <json></json> tags
|
| 69 |
+
r'```json\s*(.*?)\s*```', # Markdown code blocks
|
| 70 |
+
r'```\s*(.*?)\s*```', # Generic code blocks
|
| 71 |
+
]
|
| 72 |
+
|
| 73 |
+
for pattern in patterns:
|
| 74 |
+
json_match = re.search(pattern, response, re.DOTALL)
|
| 75 |
+
if json_match:
|
| 76 |
+
json_str = json_match.group(1).strip()
|
| 77 |
+
try:
|
| 78 |
+
json.loads(json_str) # Validate
|
| 79 |
+
return json_str
|
| 80 |
+
except json.JSONDecodeError:
|
| 81 |
+
continue
|
| 82 |
+
|
| 83 |
+
# If no tags found, try to find JSON directly in the response
|
| 84 |
+
try:
|
| 85 |
+
# Look for JSON object pattern
|
| 86 |
+
json_match = re.search(r'\{.*\}', response, re.DOTALL)
|
| 87 |
+
if json_match:
|
| 88 |
+
json_str = json_match.group(0).strip()
|
| 89 |
+
json.loads(json_str) # Validate
|
| 90 |
+
return json_str
|
| 91 |
+
except json.JSONDecodeError:
|
| 92 |
+
pass
|
| 93 |
+
|
| 94 |
+
raise ValueError("No valid JSON found in AI response. Check the debug output above.")
|
| 95 |
+
|
| 96 |
+
def convert_xml_to_json(
|
| 97 |
+
self,
|
| 98 |
+
xml_text: str,
|
| 99 |
+
temperature: float = 0.9,
|
| 100 |
+
top_p: float = 0.95,
|
| 101 |
+
max_tokens: Optional[int] = None
|
| 102 |
+
) -> str:
|
| 103 |
+
"""
|
| 104 |
+
Convert XML text to Board JSON text
|
| 105 |
+
|
| 106 |
+
Args:
|
| 107 |
+
xml_text: The XML string to convert
|
| 108 |
+
temperature: AI temperature parameter (default: 0.9)
|
| 109 |
+
top_p: AI top_p parameter (default: 0.95)
|
| 110 |
+
max_tokens: Maximum tokens for AI response (default: None)
|
| 111 |
+
|
| 112 |
+
Returns:
|
| 113 |
+
JSON string (as text)
|
| 114 |
+
"""
|
| 115 |
+
|
| 116 |
+
# Prepare chat history with system prompt
|
| 117 |
+
chat_history = [
|
| 118 |
+
{"role": "system", "content": self.system_prompt}
|
| 119 |
+
]
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
# Call AI API with XML input
|
| 124 |
+
ai_response = self._call_ai_api(
|
| 125 |
+
user_input=xml_text,
|
| 126 |
+
chat_history=chat_history,
|
| 127 |
+
temperature=temperature,
|
| 128 |
+
top_p=top_p,
|
| 129 |
+
max_tokens=max_tokens
|
| 130 |
+
)
|
| 131 |
+
print(xml_text)
|
| 132 |
+
|
| 133 |
+
# Extract and return JSON as text
|
| 134 |
+
json_text = self._extract_json_from_response(ai_response)
|
| 135 |
+
|
| 136 |
+
return json_text
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
# Example usage
|
| 140 |
+
if __name__ == "__main__":
|
| 141 |
+
processor = BoardProcessor()
|
| 142 |
+
|
| 143 |
+
xml_input = """
|
| 144 |
+
<board>
|
| 145 |
+
<note color="yellow" x="100" y="200" w="280" h="140">ูุงููู ูููุชู ุงูุซุงูู:
|
| 146 |
+
ุงูููุฉ = ุงููุชูุฉ ร ุงูุชุณุงุฑุน
|
| 147 |
+
F = m ร a</note>
|
| 148 |
+
<svg>ball</svg>
|
| 149 |
+
</board>
|
| 150 |
+
"""
|
| 151 |
+
|
| 152 |
+
try:
|
| 153 |
+
json_output = processor.convert_xml_to_json(xml_input)
|
| 154 |
+
print("\nโ
Conversion successful!")
|
| 155 |
+
print("\nJSON Output (as text):")
|
| 156 |
+
print(json_output)
|
| 157 |
+
|
| 158 |
+
# Optionally pretty print
|
| 159 |
+
print("\n๐ Pretty printed:")
|
| 160 |
+
print(json.dumps(json.loads(json_output), indent=2, ensure_ascii=False))
|
| 161 |
+
except Exception as e:
|
| 162 |
+
print(f"\nโ Error: {str(e)}")
|
system.txt
ADDED
|
@@ -0,0 +1,826 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script>
|
| 2 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 3 |
+
// MICANICA AI BOARD โ COMPLETE ENGINE
|
| 4 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 5 |
+
|
| 6 |
+
const $ = id => document.getElementById(id);
|
| 7 |
+
const canvas = $('canvas');
|
| 8 |
+
const canvasArea = $('canvas-area');
|
| 9 |
+
const contextMenu = $('context-menu');
|
| 10 |
+
const emptyState = $('emptyState');
|
| 11 |
+
const zoomDisplay = $('zoomDisplay');
|
| 12 |
+
const minimapCanvas = $('minimap-canvas');
|
| 13 |
+
const minimapCtx = minimapCanvas.getContext('2d');
|
| 14 |
+
const modalOverlay = $('modal-overlay');
|
| 15 |
+
const modalTextarea = $('modalTextarea');
|
| 16 |
+
const modalTitle = $('modalTitle');
|
| 17 |
+
const imageInput = $('imageInput');
|
| 18 |
+
const importFileInput = $('importFileInput');
|
| 19 |
+
const chatInput = $('chatInput');
|
| 20 |
+
const btnSend = $('btnSend');
|
| 21 |
+
const aiStatus = $('ai-status');
|
| 22 |
+
const statusText = aiStatus.querySelector('.status-text');
|
| 23 |
+
const voicePlayer = $('voicePlayer');
|
| 24 |
+
const playbackControls = $('playback-controls');
|
| 25 |
+
const iconPlay = $('iconPlay');
|
| 26 |
+
const iconPause = $('iconPause');
|
| 27 |
+
const progressFill = $('progressFill');
|
| 28 |
+
const stepInfo = $('stepInfo');
|
| 29 |
+
|
| 30 |
+
// โโโ STATE โโโ
|
| 31 |
+
let items = [];
|
| 32 |
+
let itemIdCounter = 0;
|
| 33 |
+
let selectedItemId = null;
|
| 34 |
+
let contextItemId = null;
|
| 35 |
+
let scale = 1;
|
| 36 |
+
let panX = 0, panY = 0;
|
| 37 |
+
let isPanning = false;
|
| 38 |
+
let panStartX = 0, panStartY = 0;
|
| 39 |
+
let panStartPanX = 0, panStartPanY = 0;
|
| 40 |
+
let sidebarOpen = false;
|
| 41 |
+
let currentMaxZ = 10;
|
| 42 |
+
let lastTouchDist = 0;
|
| 43 |
+
let lastTouchMid = {x:0, y:0};
|
| 44 |
+
let lastTapTime = 0;
|
| 45 |
+
|
| 46 |
+
// AI Playback
|
| 47 |
+
let currentSequence = [];
|
| 48 |
+
let currentStepIndex = 0;
|
| 49 |
+
let isPlaying = false;
|
| 50 |
+
let playbackPaused = false;
|
| 51 |
+
let voiceResolve = null; // to resolve voice promise on skip
|
| 52 |
+
|
| 53 |
+
// โโโ UTILS โโโ
|
| 54 |
+
const WOBBLY = [
|
| 55 |
+
'255px 15px 225px 15px / 15px 225px 15px 255px',
|
| 56 |
+
'15px 225px 15px 255px / 225px 15px 255px 15px',
|
| 57 |
+
'225px 15px 255px 15px / 15px 255px 15px 225px',
|
| 58 |
+
'15px 255px 15px 225px / 255px 15px 225px 15px',
|
| 59 |
+
];
|
| 60 |
+
function randomWobbly() { return WOBBLY[Math.floor(Math.random() * WOBBLY.length)]; }
|
| 61 |
+
function randomRotation() { return (Math.random() * 4 - 2).toFixed(1); }
|
| 62 |
+
function nextZ() { return ++currentMaxZ; }
|
| 63 |
+
function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }
|
| 64 |
+
|
| 65 |
+
function updateCanvasTransform() {
|
| 66 |
+
canvas.style.transform = `translate(${panX}px, ${panY}px) scale(${scale})`;
|
| 67 |
+
zoomDisplay.textContent = Math.round(scale * 100) + '%';
|
| 68 |
+
updateMinimap();
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
function hideEmptyState() {
|
| 72 |
+
emptyState.style.display = items.length > 0 ? 'none' : '';
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
function canvasXYFromClient(cx, cy) {
|
| 76 |
+
const rect = canvasArea.getBoundingClientRect();
|
| 77 |
+
return { x: (cx - rect.left - panX) / scale, y: (cy - rect.top - panY) / scale };
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
function centerCanvasXY() {
|
| 81 |
+
const rect = canvasArea.getBoundingClientRect();
|
| 82 |
+
return canvasXYFromClient(rect.left + rect.width / 2, rect.top + rect.height / 2);
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
function setStatus(status, text) {
|
| 86 |
+
aiStatus.className = 'ai-status ' + status;
|
| 87 |
+
statusText.textContent = text;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
// โโโ SHAPE SVGs โโโ
|
| 91 |
+
function getShapeSVG(shape, size = 80) {
|
| 92 |
+
const s = size;
|
| 93 |
+
const m = {
|
| 94 |
+
'rectangle': `<svg width="${s}" height="${s*.7}" viewBox="0 0 48 34"><rect x="2" y="2" width="44" height="30" rx="2"/></svg>`,
|
| 95 |
+
'circle': `<svg width="${s}" height="${s}" viewBox="0 0 48 48"><circle cx="24" cy="24" r="20"/></svg>`,
|
| 96 |
+
'triangle': `<svg width="${s}" height="${s}" viewBox="0 0 48 48"><polygon points="24,4 44,44 4,44"/></svg>`,
|
| 97 |
+
'diamond': `<svg width="${s}" height="${s}" viewBox="0 0 48 48"><polygon points="24,4 44,24 24,44 4,24"/></svg>`,
|
| 98 |
+
'star': `<svg width="${s}" height="${s}" viewBox="0 0 48 48"><polygon points="24,4 29,18 44,18 32,27 36,42 24,33 12,42 16,27 4,18 19,18"/></svg>`,
|
| 99 |
+
'hexagon': `<svg width="${s}" height="${s}" viewBox="0 0 48 48"><polygon points="24,4 42,14 42,34 24,44 6,34 6,14"/></svg>`,
|
| 100 |
+
'pentagon': `<svg width="${s}" height="${s}" viewBox="0 0 48 48"><polygon points="24,4 44,18 36,42 12,42 4,18"/></svg>`,
|
| 101 |
+
'square': `<svg width="${s}" height="${s}" viewBox="0 0 48 48"><rect x="6" y="6" width="36" height="36" rx="2"/></svg>`,
|
| 102 |
+
'oval': `<svg width="${s}" height="${s*.7}" viewBox="0 0 48 34"><ellipse cx="24" cy="17" rx="22" ry="15"/></svg>`,
|
| 103 |
+
'heart': `<svg width="${s}" height="${s}" viewBox="0 0 48 48"><path d="M24,42 C16,34 4,26 4,16 A10,10 0 0 1 24,12 A10,10 0 0 1 44,16 C44,26 32,34 24,42z"/></svg>`,
|
| 104 |
+
'arrow-right': `<svg width="${s}" height="${s*.5}" viewBox="0 0 48 24" stroke-width="3"><line x1="4" y1="12" x2="38" y2="12"/><polyline points="30,4 38,12 30,20"/></svg>`,
|
| 105 |
+
'arrow-left': `<svg width="${s}" height="${s*.5}" viewBox="0 0 48 24" stroke-width="3"><line x1="44" y1="12" x2="10" y2="12"/><polyline points="18,4 10,12 18,20"/></svg>`,
|
| 106 |
+
'arrow-up': `<svg width="${s*.5}" height="${s}" viewBox="0 0 24 48" stroke-width="3"><line x1="12" y1="44" x2="12" y2="10"/><polyline points="4,18 12,10 20,18"/></svg>`,
|
| 107 |
+
'arrow-down': `<svg width="${s*.5}" height="${s}" viewBox="0 0 24 48" stroke-width="3"><line x1="12" y1="4" x2="12" y2="38"/><polyline points="4,30 12,38 20,30"/></svg>`,
|
| 108 |
+
'arrow-double-h': `<svg width="${s}" height="${s*.5}" viewBox="0 0 48 24" stroke-width="3"><line x1="10" y1="12" x2="38" y2="12"/><polyline points="18,4 10,12 18,20"/><polyline points="30,4 38,12 30,20"/></svg>`,
|
| 109 |
+
'process': `<svg width="${s}" height="${s*.6}" viewBox="0 0 48 28"><rect x="2" y="2" width="44" height="24" rx="2"/></svg>`,
|
| 110 |
+
'decision': `<svg width="${s}" height="${s*.7}" viewBox="0 0 48 34"><polygon points="24,2 46,17 24,32 2,17"/></svg>`,
|
| 111 |
+
'speech': `<svg width="${s}" height="${s}" viewBox="0 0 48 48"><path d="M6,8 h28 a4,4 0 0 1 4,4 v14 a4,4 0 0 1 -4,4 h-14 l-8,8 v-8 h-6 a4,4 0 0 1 -4,-4 v-14 a4,4 0 0 1 4,-4z"/></svg>`,
|
| 112 |
+
'cloud': `<svg width="${s}" height="${s*.75}" viewBox="0 0 48 36"><path d="M10,30 A8,8 0 0 1 8,14 A10,10 0 0 1 26,10 A8,8 0 0 1 38,18 A6,6 0 0 1 36,30z"/></svg>`,
|
| 113 |
+
'checkmark': `<svg width="${s}" height="${s}" viewBox="0 0 48 48" stroke-width="4"><polyline points="10,26 20,36 38,14"/></svg>`,
|
| 114 |
+
'cross': `<svg width="${s}" height="${s}" viewBox="0 0 48 48" stroke-width="3"><line x1="10" y1="10" x2="38" y2="38"/><line x1="38" y1="10" x2="10" y2="38"/></svg>`,
|
| 115 |
+
'lightning': `<svg width="${s*.6}" height="${s}" viewBox="0 0 30 48"><polygon points="18,4 8,22 16,22 12,44 26,18 18,18"/></svg>`,
|
| 116 |
+
};
|
| 117 |
+
return m[shape] || m['circle'];
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 121 |
+
// BOARD ITEM CREATORS
|
| 122 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 123 |
+
|
| 124 |
+
function addNote(color='yellow', x, y, text='', w=220, h=180, isAI=false) {
|
| 125 |
+
const id = ++itemIdCounter;
|
| 126 |
+
if (x === undefined || y === undefined) { const c = centerCanvasXY(); x = c.x - 110 + Math.random()*60-30; y = c.y - 90 + Math.random()*60-30; }
|
| 127 |
+
const rot = randomRotation(), wob = randomWobbly(), z = nextZ();
|
| 128 |
+
const deco = Math.random() > 0.5 ? 'tape' : (Math.random() > 0.5 ? 'pin' : 'none');
|
| 129 |
+
|
| 130 |
+
const el = document.createElement('div');
|
| 131 |
+
el.className = `workspace-item note-paper${color==='yellow'?'':' '+color}${isAI?' ai-placed':''}`;
|
| 132 |
+
el.id = `item-${id}`;
|
| 133 |
+
el.dataset.itemId = id;
|
| 134 |
+
el.style.cssText = `left:${x}px;top:${y}px;width:${w}px;height:${h}px;transform:rotate(${rot}deg);border-radius:${wob};z-index:${z};`;
|
| 135 |
+
|
| 136 |
+
let decoHTML = '';
|
| 137 |
+
if (deco==='tape') decoHTML = `<div class="note-tape" style="transform:translateX(-50%) rotate(${(Math.random()*6-3).toFixed(1)}deg)"></div>`;
|
| 138 |
+
else if (deco==='pin') decoHTML = `<div class="note-pin"></div>`;
|
| 139 |
+
|
| 140 |
+
el.innerHTML = `${decoHTML}
|
| 141 |
+
<div class="note-header" data-drag-handle="${id}">
|
| 142 |
+
<div style="display:flex;gap:4px;">
|
| 143 |
+
<div class="note-color-dot" style="background:var(--postit)" data-color-btn="yellow" data-for="${id}"></div>
|
| 144 |
+
<div class="note-color-dot" style="background:var(--postit-blue)" data-color-btn="blue" data-for="${id}"></div>
|
| 145 |
+
<div class="note-color-dot" style="background:var(--postit-pink)" data-color-btn="pink" data-for="${id}"></div>
|
| 146 |
+
<div class="note-color-dot" style="background:var(--postit-green)" data-color-btn="green" data-for="${id}"></div>
|
| 147 |
+
</div>
|
| 148 |
+
<button class="note-close" data-close-btn="${id}">โ</button>
|
| 149 |
+
</div>
|
| 150 |
+
<div class="note-body"><textarea placeholder="ุงูุชุจ ููุง...">${text}</textarea></div>
|
| 151 |
+
<div class="note-resize" data-resize-handle="${id}"><svg viewBox="0 0 16 16"><path d="M14,2 L2,14 M14,8 L8,14 M14,14 L14,14"/></svg></div>`;
|
| 152 |
+
|
| 153 |
+
canvas.appendChild(el);
|
| 154 |
+
items.push({id, type:'note', el, x, y, color, w, h, rotation:rot, text});
|
| 155 |
+
if(!isAI) selectItem(id);
|
| 156 |
+
hideEmptyState(); updateMinimap();
|
| 157 |
+
return id;
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
function addBoardText(content='', x, y, size=20, color='#2d2d2d', font='Kalam', isAI=false) {
|
| 161 |
+
const id = ++itemIdCounter;
|
| 162 |
+
if (x===undefined||y===undefined) { const c=centerCanvasXY(); x=c.x-100; y=c.y-15; }
|
| 163 |
+
const z = nextZ();
|
| 164 |
+
|
| 165 |
+
const el = document.createElement('div');
|
| 166 |
+
el.className = `workspace-item board-text${isAI?' ai-placed':''}`;
|
| 167 |
+
el.id = `item-${id}`;
|
| 168 |
+
el.dataset.itemId = id;
|
| 169 |
+
el.style.cssText = `left:${x}px;top:${y}px;z-index:${z};font-size:${size}px;color:${color};font-family:'${font}',cursive;`;
|
| 170 |
+
el.contentEditable = true;
|
| 171 |
+
el.textContent = content;
|
| 172 |
+
|
| 173 |
+
const closeBtn = document.createElement('button');
|
| 174 |
+
closeBtn.className = 'text-close-btn';
|
| 175 |
+
closeBtn.dataset.closeBtn = id;
|
| 176 |
+
closeBtn.textContent = 'โ';
|
| 177 |
+
el.appendChild(closeBtn);
|
| 178 |
+
|
| 179 |
+
canvas.appendChild(el);
|
| 180 |
+
items.push({id, type:'text', el, x, y, content, size, color, font, w:el.offsetWidth||200, h:size+20});
|
| 181 |
+
hideEmptyState(); updateMinimap();
|
| 182 |
+
return id;
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
function placeShape(shape, x, y, size=80, isAI=false) {
|
| 186 |
+
const id = ++itemIdCounter;
|
| 187 |
+
if (x===undefined||y===undefined) { const c=centerCanvasXY(); x=c.x-40+Math.random()*40-20; y=c.y-40+Math.random()*40-20; }
|
| 188 |
+
const rot = randomRotation(), z = nextZ();
|
| 189 |
+
|
| 190 |
+
const el = document.createElement('div');
|
| 191 |
+
el.className = `workspace-item canvas-shape${isAI?' ai-placed':''}`;
|
| 192 |
+
el.id = `item-${id}`;
|
| 193 |
+
el.dataset.itemId = id;
|
| 194 |
+
el.style.cssText = `left:${x}px;top:${y}px;transform:rotate(${rot}deg);z-index:${z};padding:4px;`;
|
| 195 |
+
el.innerHTML = getShapeSVG(shape, size);
|
| 196 |
+
|
| 197 |
+
canvas.appendChild(el);
|
| 198 |
+
items.push({id, type:'shape', shape, el, x, y, rotation:rot, size});
|
| 199 |
+
hideEmptyState(); updateMinimap();
|
| 200 |
+
return id;
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
function addSvgImage(url, x, y, w=120, h=120, name='', isAI=false) {
|
| 204 |
+
const id = ++itemIdCounter;
|
| 205 |
+
if (x===undefined||y===undefined) { const c=centerCanvasXY(); x=c.x-w/2; y=c.y-h/2; }
|
| 206 |
+
const z = nextZ();
|
| 207 |
+
|
| 208 |
+
const el = document.createElement('div');
|
| 209 |
+
el.className = `workspace-item canvas-svg-image${isAI?' ai-placed':''}`;
|
| 210 |
+
el.id = `item-${id}`;
|
| 211 |
+
el.dataset.itemId = id;
|
| 212 |
+
el.style.cssText = `left:${x}px;top:${y}px;width:${w}px;height:${h}px;z-index:${z};`;
|
| 213 |
+
el.innerHTML = `<button class="img-close" data-close-btn="${id}">โ</button>
|
| 214 |
+
<img src="${url}" alt="${name}" draggable="false">
|
| 215 |
+
<div class="note-resize" data-resize-handle="${id}"><svg viewBox="0 0 16 16"><path d="M14,2 L2,14 M14,8 L8,14 M14,14 L14,14"/></svg></div>`;
|
| 216 |
+
|
| 217 |
+
canvas.appendChild(el);
|
| 218 |
+
items.push({id, type:'image', subtype:'svg_icon', el, x, y, w, h, url, name, no_bg:true});
|
| 219 |
+
hideEmptyState(); updateMinimap();
|
| 220 |
+
return id;
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
function addPageImage(url, x, y, w=300, h=400, pageNumber='', isAI=false) {
|
| 224 |
+
const id = ++itemIdCounter;
|
| 225 |
+
if (x===undefined||y===undefined) { const c=centerCanvasXY(); x=c.x-w/2; y=c.y-h/2; }
|
| 226 |
+
const z = nextZ();
|
| 227 |
+
|
| 228 |
+
const el = document.createElement('div');
|
| 229 |
+
el.className = `workspace-item canvas-page-image${isAI?' ai-placed':''}`;
|
| 230 |
+
el.id = `item-${id}`;
|
| 231 |
+
el.dataset.itemId = id;
|
| 232 |
+
el.style.cssText = `left:${x}px;top:${y}px;width:${w}px;height:${h}px;z-index:${z};`;
|
| 233 |
+
el.innerHTML = `<div class="page-curl"></div>
|
| 234 |
+
<div class="page-label">๐ ุตูุญุฉ ${pageNumber}</div>
|
| 235 |
+
<button class="img-close" data-close-btn="${id}">โ</button>
|
| 236 |
+
<img src="${url}" alt="ุตูุญุฉ ${pageNumber}" draggable="false">
|
| 237 |
+
<div class="note-resize" data-resize-handle="${id}"><svg viewBox="0 0 16 16"><path d="M14,2 L2,14 M14,8 L8,14 M14,14 L14,14"/></svg></div>`;
|
| 238 |
+
|
| 239 |
+
canvas.appendChild(el);
|
| 240 |
+
items.push({id, type:'image', subtype:'page', el, x, y, w, h, url, pageNumber, is_page:true});
|
| 241 |
+
hideEmptyState(); updateMinimap();
|
| 242 |
+
return id;
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
function addImage(dataUrl, x, y, w, h) {
|
| 246 |
+
const id = ++itemIdCounter;
|
| 247 |
+
if (x===undefined||y===undefined) { const c=centerCanvasXY(); x=c.x-(w||200)/2; y=c.y-(h||150)/2; }
|
| 248 |
+
w=w||240; h=h||180;
|
| 249 |
+
const rot = randomRotation(), z = nextZ();
|
| 250 |
+
|
| 251 |
+
const el = document.createElement('div');
|
| 252 |
+
el.className = 'workspace-item canvas-image';
|
| 253 |
+
el.id = `item-${id}`;
|
| 254 |
+
el.dataset.itemId = id;
|
| 255 |
+
el.style.cssText = `left:${x}px;top:${y}px;width:${w}px;height:${h}px;transform:rotate(${rot}deg);z-index:${z};`;
|
| 256 |
+
el.innerHTML = `<button class="img-close" data-close-btn="${id}">โ</button>
|
| 257 |
+
<img src="${dataUrl}" alt="image" draggable="false">
|
| 258 |
+
<div class="note-resize" data-resize-handle="${id}"><svg viewBox="0 0 16 16"><path d="M14,2 L2,14 M14,8 L8,14 M14,14 L14,14"/></svg></div>`;
|
| 259 |
+
|
| 260 |
+
canvas.appendChild(el);
|
| 261 |
+
items.push({id, type:'image', subtype:'upload', el, x, y, w, h, rotation:rot, dataUrl});
|
| 262 |
+
hideEmptyState(); updateMinimap();
|
| 263 |
+
return id;
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
function addBoardItems(itemsData, isAI=true) {
|
| 267 |
+
itemsData.forEach(d => {
|
| 268 |
+
if (d.type==='note') addNote(d.color||'yellow', d.x, d.y, d.text||'', d.w||220, d.h||180, isAI);
|
| 269 |
+
else if (d.type==='text') addBoardText(d.content||'', d.x, d.y, d.size||20, d.color||'#2d2d2d', d.font||'Kalam', isAI);
|
| 270 |
+
else if (d.type==='shape') placeShape(d.shape||'circle', d.x, d.y, d.size||80, isAI);
|
| 271 |
+
else if (d.type==='image') {
|
| 272 |
+
if (d.is_page||d.subtype==='page') addPageImage(d.url, d.x, d.y, d.w||300, d.h||400, d.pageNumber||'', isAI);
|
| 273 |
+
else if (d.no_bg||d.subtype==='svg_icon') addSvgImage(d.url, d.x, d.y, d.w||120, d.h||120, d.name||'', isAI);
|
| 274 |
+
else addImage(d.dataUrl||d.url, d.x, d.y, d.w||240, d.h||180);
|
| 275 |
+
}
|
| 276 |
+
});
|
| 277 |
+
deselectAll(); hideEmptyState(); updateMinimap();
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
// โโโ SELECTION / REMOVE / COLOR โโโ
|
| 281 |
+
function changeNoteColor(id, color) {
|
| 282 |
+
const el = $(`item-${id}`); if(!el) return;
|
| 283 |
+
el.classList.remove('blue','pink','green','orange','white','purple');
|
| 284 |
+
if(color!=='yellow') el.classList.add(color);
|
| 285 |
+
const item = items.find(i=>i.id===id); if(item) item.color=color;
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
function selectItem(id) {
|
| 289 |
+
if (selectedItemId!==null) { const p=$(`item-${selectedItemId}`); if(p) p.classList.remove('selected'); }
|
| 290 |
+
selectedItemId = id;
|
| 291 |
+
const el = $(`item-${id}`);
|
| 292 |
+
if(el) { el.classList.add('selected'); el.style.zIndex = nextZ(); }
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
function deselectAll() {
|
| 296 |
+
if (selectedItemId!==null) { const el=$(`item-${selectedItemId}`); if(el) el.classList.remove('selected'); }
|
| 297 |
+
selectedItemId = null;
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
function removeItem(id) {
|
| 301 |
+
const el = $(`item-${id}`); if(el) el.remove();
|
| 302 |
+
items = items.filter(i=>i.id!==id);
|
| 303 |
+
if(selectedItemId===id) selectedItemId=null;
|
| 304 |
+
if(contextItemId===id) contextItemId=null;
|
| 305 |
+
hideEmptyState(); updateMinimap(); syncBoardState();
|
| 306 |
+
}
|
| 307 |
+
|
| 308 |
+
// โโโ DRAG / RESIZE โโโ
|
| 309 |
+
function startItemDrag(id, cx, cy) {
|
| 310 |
+
selectItem(id);
|
| 311 |
+
const el = $(`item-${id}`); if(!el) return;
|
| 312 |
+
const r = el.getBoundingClientRect();
|
| 313 |
+
const ox = cx-r.left, oy = cy-r.top;
|
| 314 |
+
|
| 315 |
+
function mv(ex,ey) { const p=canvasXYFromClient(ex,ey); const nx=p.x-ox/scale, ny=p.y-oy/scale; el.style.left=nx+'px'; el.style.top=ny+'px'; const it=items.find(i=>i.id===id); if(it){it.x=nx;it.y=ny;} }
|
| 316 |
+
function mm(e){mv(e.clientX,e.clientY);}
|
| 317 |
+
function tm(e){if(e.touches.length===1){e.preventDefault();mv(e.touches[0].clientX,e.touches[0].clientY);}}
|
| 318 |
+
function up(){window.removeEventListener('mousemove',mm);window.removeEventListener('mouseup',up);window.removeEventListener('touchmove',tm);window.removeEventListener('touchend',up);window.removeEventListener('touchcancel',up);updateMinimap();syncBoardState();}
|
| 319 |
+
window.addEventListener('mousemove',mm);window.addEventListener('mouseup',up);
|
| 320 |
+
window.addEventListener('touchmove',tm,{passive:false});window.addEventListener('touchend',up);window.addEventListener('touchcancel',up);
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
function startItemResize(id, cx, cy) {
|
| 324 |
+
const el = $(`item-${id}`); if(!el) return;
|
| 325 |
+
const sw=el.offsetWidth, sh=el.offsetHeight, sx=cx, sy=cy;
|
| 326 |
+
function mv(ex,ey){const dx=(ex-sx)/scale,dy=(ey-sy)/scale; el.style.width=Math.max(80,sw+dx)+'px'; el.style.height=Math.max(60,sh+dy)+'px'; const it=items.find(i=>i.id===id);if(it){it.w=Math.max(80,sw+dx);it.h=Math.max(60,sh+dy);}}
|
| 327 |
+
function mm(e){mv(e.clientX,e.clientY);}
|
| 328 |
+
function tm(e){if(e.touches.length===1){e.preventDefault();mv(e.touches[0].clientX,e.touches[0].clientY);}}
|
| 329 |
+
function up(){window.removeEventListener('mousemove',mm);window.removeEventListener('mouseup',up);window.removeEventListener('touchmove',tm);window.removeEventListener('touchend',up);updateMinimap();}
|
| 330 |
+
window.addEventListener('mousemove',mm);window.addEventListener('mouseup',up);
|
| 331 |
+
window.addEventListener('touchmove',tm,{passive:false});window.addEventListener('touchend',up);
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 335 |
+
// PAN / ZOOM
|
| 336 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 337 |
+
|
| 338 |
+
canvasArea.addEventListener('mousedown', e => {
|
| 339 |
+
if(e.target.closest('.workspace-item')||e.target.closest('#chat-bar')||e.button!==0) return;
|
| 340 |
+
deselectAll(); isPanning=true; panStartX=e.clientX; panStartY=e.clientY; panStartPanX=panX; panStartPanY=panY; canvasArea.style.cursor='grabbing';
|
| 341 |
+
});
|
| 342 |
+
window.addEventListener('mousemove', e => { if(!isPanning)return; panX=panStartPanX+(e.clientX-panStartX); panY=panStartPanY+(e.clientY-panStartY); updateCanvasTransform(); });
|
| 343 |
+
window.addEventListener('mouseup', () => { if(isPanning){isPanning=false;canvasArea.style.cursor='crosshair';} });
|
| 344 |
+
|
| 345 |
+
canvasArea.addEventListener('touchstart', e => {
|
| 346 |
+
const t=e.target;
|
| 347 |
+
if(t.closest('#chat-bar')||t.closest('#playback-controls')) return;
|
| 348 |
+
const ws=t.closest('.workspace-item');
|
| 349 |
+
|
| 350 |
+
const rh=t.closest('[data-resize-handle]');
|
| 351 |
+
if(rh){e.preventDefault();startItemResize(parseInt(rh.dataset.resizeHandle),e.touches[0].clientX,e.touches[0].clientY);return;}
|
| 352 |
+
const cb=t.closest('[data-close-btn]');
|
| 353 |
+
if(cb){e.preventDefault();removeItem(parseInt(cb.dataset.closeBtn));return;}
|
| 354 |
+
const co=t.closest('[data-color-btn]');
|
| 355 |
+
if(co){e.preventDefault();changeNoteColor(parseInt(co.dataset.for),co.dataset.colorBtn);return;}
|
| 356 |
+
if(t.tagName==='TEXTAREA'||t.contentEditable==='true') return;
|
| 357 |
+
const dh=t.closest('[data-drag-handle]');
|
| 358 |
+
if(dh&&e.touches.length===1){e.preventDefault();startItemDrag(parseInt(dh.dataset.dragHandle),e.touches[0].clientX,e.touches[0].clientY);return;}
|
| 359 |
+
if(ws&&!dh&&e.touches.length===1){e.preventDefault();startItemDrag(parseInt(ws.dataset.itemId),e.touches[0].clientX,e.touches[0].clientY);return;}
|
| 360 |
+
|
| 361 |
+
if(!ws){
|
| 362 |
+
e.preventDefault(); deselectAll();
|
| 363 |
+
if(e.touches.length===1){isPanning=true;panStartX=e.touches[0].clientX;panStartY=e.touches[0].clientY;panStartPanX=panX;panStartPanY=panY;}
|
| 364 |
+
else if(e.touches.length===2){isPanning=false;const dx=e.touches[1].clientX-e.touches[0].clientX,dy=e.touches[1].clientY-e.touches[0].clientY;lastTouchDist=Math.hypot(dx,dy);lastTouchMid={x:(e.touches[0].clientX+e.touches[1].clientX)/2,y:(e.touches[0].clientY+e.touches[1].clientY)/2};}
|
| 365 |
+
}
|
| 366 |
+
},{passive:false});
|
| 367 |
+
|
| 368 |
+
canvasArea.addEventListener('touchmove', e => {
|
| 369 |
+
if(e.target.tagName==='TEXTAREA'||e.target.contentEditable==='true'||e.target.closest('#chat-bar')) return;
|
| 370 |
+
if(e.touches.length===1&&isPanning){e.preventDefault();panX=panStartPanX+(e.touches[0].clientX-panStartX);panY=panStartPanY+(e.touches[0].clientY-panStartY);updateCanvasTransform();}
|
| 371 |
+
else if(e.touches.length===2){
|
| 372 |
+
e.preventDefault();isPanning=false;
|
| 373 |
+
const dx=e.touches[1].clientX-e.touches[0].clientX,dy=e.touches[1].clientY-e.touches[0].clientY;
|
| 374 |
+
const dist=Math.hypot(dx,dy);
|
| 375 |
+
const mid={x:(e.touches[0].clientX+e.touches[1].clientX)/2,y:(e.touches[0].clientY+e.touches[1].clientY)/2};
|
| 376 |
+
if(lastTouchDist>0){const rect=canvasArea.getBoundingClientRect();const mx=mid.x-rect.left,my=mid.y-rect.top;const os=scale;scale=Math.min(3,Math.max(0.2,scale*(dist/lastTouchDist)));panX=mx-(mx-panX)*(scale/os)+(mid.x-lastTouchMid.x);panY=my-(my-panY)*(scale/os)+(mid.y-lastTouchMid.y);updateCanvasTransform();}
|
| 377 |
+
lastTouchDist=dist;lastTouchMid=mid;
|
| 378 |
+
}
|
| 379 |
+
},{passive:false});
|
| 380 |
+
|
| 381 |
+
canvasArea.addEventListener('touchend', e => {
|
| 382 |
+
if(e.touches.length===0){isPanning=false;lastTouchDist=0;}
|
| 383 |
+
else if(e.touches.length===1){isPanning=true;panStartX=e.touches[0].clientX;panStartY=e.touches[0].clientY;panStartPanX=panX;panStartPanY=panY;lastTouchDist=0;}
|
| 384 |
+
});
|
| 385 |
+
|
| 386 |
+
// Mouse on canvas items
|
| 387 |
+
canvas.addEventListener('mousedown', e => {
|
| 388 |
+
const t=e.target;
|
| 389 |
+
const rh=t.closest('[data-resize-handle]');if(rh){e.preventDefault();e.stopPropagation();startItemResize(parseInt(rh.dataset.resizeHandle),e.clientX,e.clientY);return;}
|
| 390 |
+
const cb=t.closest('[data-close-btn]');if(cb){e.preventDefault();e.stopPropagation();removeItem(parseInt(cb.dataset.closeBtn));return;}
|
| 391 |
+
const co=t.closest('[data-color-btn]');if(co){e.preventDefault();e.stopPropagation();changeNoteColor(parseInt(co.dataset.for),co.dataset.colorBtn);return;}
|
| 392 |
+
if(t.tagName==='TEXTAREA'||t.contentEditable==='true') return;
|
| 393 |
+
const dh=t.closest('[data-drag-handle]');if(dh&&e.button===0){e.preventDefault();e.stopPropagation();startItemDrag(parseInt(dh.dataset.dragHandle),e.clientX,e.clientY);return;}
|
| 394 |
+
const ws=t.closest('.workspace-item');if(ws&&e.button===0){e.preventDefault();e.stopPropagation();startItemDrag(parseInt(ws.dataset.itemId),e.clientX,e.clientY);return;}
|
| 395 |
+
});
|
| 396 |
+
|
| 397 |
+
// Wheel zoom
|
| 398 |
+
canvasArea.addEventListener('wheel', e => {
|
| 399 |
+
if(e.target.closest('#chat-bar')) return;
|
| 400 |
+
e.preventDefault();const rect=canvasArea.getBoundingClientRect();const mx=e.clientX-rect.left,my=e.clientY-rect.top;const os=scale;scale=Math.min(3,Math.max(0.2,scale+(e.deltaY>0?-0.08:0.08)));panX=mx-(mx-panX)*(scale/os);panY=my-(my-panY)*(scale/os);updateCanvasTransform();
|
| 401 |
+
},{passive:false});
|
| 402 |
+
|
| 403 |
+
// Zoom buttons
|
| 404 |
+
$('btnZoomIn').addEventListener('click',()=>{const r=canvasArea.getBoundingClientRect();const cx=r.width/2,cy=r.height/2;const os=scale;scale=Math.min(3,scale+0.15);panX=cx-(cx-panX)*(scale/os);panY=cy-(cy-panY)*(scale/os);updateCanvasTransform();});
|
| 405 |
+
$('btnZoomOut').addEventListener('click',()=>{const r=canvasArea.getBoundingClientRect();const cx=r.width/2,cy=r.height/2;const os=scale;scale=Math.max(0.2,scale-0.15);panX=cx-(cx-panX)*(scale/os);panY=cy-(cy-panY)*(scale/os);updateCanvasTransform();});
|
| 406 |
+
|
| 407 |
+
// Double click โ add note
|
| 408 |
+
canvasArea.addEventListener('dblclick', e => {
|
| 409 |
+
if(e.target.closest('.workspace-item')||e.target.closest('#chat-bar')) return;
|
| 410 |
+
const p=canvasXYFromClient(e.clientX,e.clientY); addNote('yellow',p.x-110,p.y-90);
|
| 411 |
+
});
|
| 412 |
+
|
| 413 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 414 |
+
// CONTEXT MENU
|
| 415 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 416 |
+
|
| 417 |
+
function showContextMenu(x,y){contextMenu.style.left=Math.min(x,window.innerWidth-180)+'px';contextMenu.style.top=Math.min(y,window.innerHeight-160)+'px';contextMenu.classList.add('show');}
|
| 418 |
+
|
| 419 |
+
document.addEventListener('contextmenu', e => {
|
| 420 |
+
const ws=e.target.closest('.workspace-item');
|
| 421 |
+
if(ws){e.preventDefault();contextItemId=parseInt(ws.dataset.itemId);selectItem(contextItemId);showContextMenu(e.clientX,e.clientY);}
|
| 422 |
+
else{contextMenu.classList.remove('show');contextItemId=null;}
|
| 423 |
+
});
|
| 424 |
+
document.addEventListener('click', e => {if(!e.target.closest('#context-menu'))contextMenu.classList.remove('show');});
|
| 425 |
+
document.addEventListener('touchstart', e => {if(!e.target.closest('#context-menu'))contextMenu.classList.remove('show');},{passive:true});
|
| 426 |
+
|
| 427 |
+
$('ctxDelete').addEventListener('click',()=>{if(contextItemId!==null)removeItem(contextItemId);contextMenu.classList.remove('show');});
|
| 428 |
+
$('ctxDuplicate').addEventListener('click',()=>{
|
| 429 |
+
if(contextItemId===null)return;const it=items.find(i=>i.id===contextItemId);if(!it)return;
|
| 430 |
+
if(it.type==='note'){const ta=it.el.querySelector('textarea');addNote(it.color||'yellow',it.x+30,it.y+30,ta?ta.value:'');}
|
| 431 |
+
else if(it.type==='text') addBoardText(it.el.textContent||'',it.x+30,it.y+30,it.size,it.color,it.font);
|
| 432 |
+
else if(it.type==='shape') placeShape(it.shape,it.x+30,it.y+30,it.size);
|
| 433 |
+
else if(it.type==='image'){
|
| 434 |
+
if(it.is_page||it.subtype==='page') addPageImage(it.url,it.x+30,it.y+30,it.w,it.h,it.pageNumber);
|
| 435 |
+
else if(it.no_bg||it.subtype==='svg_icon') addSvgImage(it.url,it.x+30,it.y+30,it.w,it.h,it.name);
|
| 436 |
+
else addImage(it.dataUrl||it.url,it.x+30,it.y+30,it.w,it.h);
|
| 437 |
+
}
|
| 438 |
+
contextMenu.classList.remove('show');
|
| 439 |
+
});
|
| 440 |
+
$('ctxFront').addEventListener('click',()=>{if(contextItemId!==null){const el=$(`item-${contextItemId}`);if(el)el.style.zIndex=nextZ();}contextMenu.classList.remove('show');});
|
| 441 |
+
$('ctxBack').addEventListener('click',()=>{if(contextItemId!==null){const el=$(`item-${contextItemId}`);if(el)el.style.zIndex=1;}contextMenu.classList.remove('show');});
|
| 442 |
+
|
| 443 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 444 |
+
// TOOLBAR BUTTONS
|
| 445 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 446 |
+
|
| 447 |
+
$('btnNote').addEventListener('click', () => addNote('yellow'));
|
| 448 |
+
|
| 449 |
+
$('btnShapes').addEventListener('click', () => {
|
| 450 |
+
const sb = $('sidebar');
|
| 451 |
+
sidebarOpen = !sidebarOpen;
|
| 452 |
+
if(sidebarOpen){sb.classList.remove('collapsed');canvasArea.classList.remove('sidebar-collapsed');canvasArea.classList.add('sidebar-open');}
|
| 453 |
+
else{sb.classList.add('collapsed');canvasArea.classList.add('sidebar-collapsed');canvasArea.classList.remove('sidebar-open');}
|
| 454 |
+
});
|
| 455 |
+
|
| 456 |
+
$('btnImage').addEventListener('click', () => imageInput.click());
|
| 457 |
+
|
| 458 |
+
$('btnClear').addEventListener('click', () => {
|
| 459 |
+
if(items.length===0)return;
|
| 460 |
+
if(confirm('ู
ุณุญ ูู ุดูุก ู
ู ุงูุณุจูุฑุฉุ')){items.forEach(i=>i.el.remove());items=[];selectedItemId=null;hideEmptyState();updateMinimap();fetch('/api/board/clear',{method:'POST'}).catch(()=>{});}
|
| 461 |
+
});
|
| 462 |
+
|
| 463 |
+
document.querySelectorAll('.shape-item').forEach(el=>{
|
| 464 |
+
el.addEventListener('click',()=>{placeShape(el.dataset.shape);if(window.innerWidth<=768){sidebarOpen=false;$('sidebar').classList.add('collapsed');canvasArea.classList.add('sidebar-collapsed');canvasArea.classList.remove('sidebar-open');}});
|
| 465 |
+
});
|
| 466 |
+
|
| 467 |
+
document.querySelectorAll('.color-swatch[data-note-color]').forEach(el=>{
|
| 468 |
+
el.addEventListener('click',()=>{addNote(el.dataset.noteColor);if(window.innerWidth<=768){sidebarOpen=false;$('sidebar').classList.add('collapsed');canvasArea.classList.add('sidebar-collapsed');canvasArea.classList.remove('sidebar-open');}});
|
| 469 |
+
});
|
| 470 |
+
|
| 471 |
+
imageInput.addEventListener('change', e => {
|
| 472 |
+
const files=e.target.files; if(!files||!files.length)return;
|
| 473 |
+
Array.from(files).forEach((file,idx)=>{if(!file.type.startsWith('image/'))return;const r=new FileReader();r.onload=ev=>{const img=new Image();img.onload=()=>{let w=img.naturalWidth,h=img.naturalHeight;const mx=400;if(w>mx||h>mx){const ra=Math.min(mx/w,mx/h);w=Math.round(w*ra);h=Math.round(h*ra);}const c=centerCanvasXY();addImage(ev.target.result,c.x-w/2+idx*30,c.y-h/2+idx*30,w,h);};img.src=ev.target.result;};r.readAsDataURL(file);});
|
| 474 |
+
imageInput.value='';
|
| 475 |
+
});
|
| 476 |
+
|
| 477 |
+
// Keyboard
|
| 478 |
+
document.addEventListener('keydown', e => {
|
| 479 |
+
if(e.target.tagName==='TEXTAREA'||e.target.tagName==='INPUT'||e.target.contentEditable==='true') return;
|
| 480 |
+
if(e.key==='Delete'||e.key==='Backspace'){if(selectedItemId!==null)removeItem(selectedItemId);}
|
| 481 |
+
if(e.key==='Escape'){deselectAll();modalOverlay.classList.remove('show');}
|
| 482 |
+
if((e.ctrlKey||e.metaKey)&&e.key==='d'){e.preventDefault();if(selectedItemId!==null){contextItemId=selectedItemId;$('ctxDuplicate').click();}}
|
| 483 |
+
});
|
| 484 |
+
|
| 485 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 486 |
+
// EXPORT / IMPORT
|
| 487 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 488 |
+
|
| 489 |
+
function getWorkspaceData() {
|
| 490 |
+
return items.map(it => {
|
| 491 |
+
const d = {type:it.type, x:parseFloat(it.el.style.left)||0, y:parseFloat(it.el.style.top)||0, w:it.el.offsetWidth, h:it.el.offsetHeight, zIndex:parseInt(it.el.style.zIndex)||10};
|
| 492 |
+
if(it.type==='note'){d.color=it.color||'yellow';const ta=it.el.querySelector('textarea');d.text=ta?ta.value:'';}
|
| 493 |
+
else if(it.type==='text'){d.content=it.el.textContent||'';d.size=it.size;d.color=it.color;d.font=it.font;}
|
| 494 |
+
else if(it.type==='shape'){d.shape=it.shape;d.size=it.size||80;}
|
| 495 |
+
else if(it.type==='image'){d.subtype=it.subtype;d.url=it.url||it.dataUrl;d.no_bg=it.no_bg||false;d.is_page=it.is_page||false;d.name=it.name||'';d.pageNumber=it.pageNumber||'';if(it.dataUrl)d.dataUrl=it.dataUrl;}
|
| 496 |
+
return d;
|
| 497 |
+
});
|
| 498 |
+
}
|
| 499 |
+
|
| 500 |
+
function syncBoardState() {
|
| 501 |
+
fetch('/api/board/sync',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({board_state:getWorkspaceData()})}).catch(()=>{});
|
| 502 |
+
}
|
| 503 |
+
|
| 504 |
+
let modalMode = 'export';
|
| 505 |
+
|
| 506 |
+
$('btnExport').addEventListener('click',()=>{
|
| 507 |
+
modalMode='export';modalTitle.textContent='๐ฆ ุชุตุฏูุฑ ุงูุณุจูุฑุฉ';
|
| 508 |
+
modalTextarea.value=JSON.stringify(getWorkspaceData(),null,2);
|
| 509 |
+
modalTextarea.readOnly=false;
|
| 510 |
+
$('modalCopy').style.display='';$('modalDownload').style.display='';
|
| 511 |
+
$('modalLoadFile').style.display='none';$('modalApply').style.display='none';
|
| 512 |
+
modalOverlay.classList.add('show');
|
| 513 |
+
});
|
| 514 |
+
|
| 515 |
+
$('btnImport').addEventListener('click',()=>{
|
| 516 |
+
modalMode='import';modalTitle.textContent='๐ฅ ุงุณุชูุฑุงุฏ ุงูุณุจูุฑุฉ';
|
| 517 |
+
modalTextarea.value='';modalTextarea.readOnly=false;modalTextarea.placeholder='ุงูุตู ุจูุงูุงุช JSON ููุง...';
|
| 518 |
+
$('modalCopy').style.display='none';$('modalDownload').style.display='none';
|
| 519 |
+
$('modalLoadFile').style.display='';$('modalApply').style.display='';
|
| 520 |
+
modalOverlay.classList.add('show');
|
| 521 |
+
});
|
| 522 |
+
|
| 523 |
+
$('modalClose').addEventListener('click',()=>modalOverlay.classList.remove('show'));
|
| 524 |
+
modalOverlay.addEventListener('click',e=>{if(e.target===modalOverlay)modalOverlay.classList.remove('show');});
|
| 525 |
+
|
| 526 |
+
$('modalCopy').addEventListener('click',()=>{
|
| 527 |
+
modalTextarea.select();navigator.clipboard.writeText(modalTextarea.value).then(()=>{$('modalCopy').textContent='โ
ุชู
!';setTimeout(()=>{$('modalCopy').textContent='๐ ูุณุฎ';},1500);}).catch(()=>{document.execCommand('copy');});
|
| 528 |
+
});
|
| 529 |
+
|
| 530 |
+
$('modalDownload').addEventListener('click',()=>{
|
| 531 |
+
const blob=new Blob([modalTextarea.value],{type:'application/json'});const url=URL.createObjectURL(blob);const a=document.createElement('a');a.href=url;a.download='micanica-board.json';document.body.appendChild(a);a.click();document.body.removeChild(a);URL.revokeObjectURL(url);
|
| 532 |
+
});
|
| 533 |
+
|
| 534 |
+
$('modalLoadFile').addEventListener('click',()=>importFileInput.click());
|
| 535 |
+
importFileInput.addEventListener('change',e=>{const file=e.target.files[0];if(!file)return;const r=new FileReader();r.onload=ev=>{modalTextarea.value=ev.target.result;};r.readAsText(file);importFileInput.value='';});
|
| 536 |
+
|
| 537 |
+
$('modalApply').addEventListener('click',()=>{
|
| 538 |
+
const txt=modalTextarea.value.trim();if(!txt){alert('ูุง ุชูุฌุฏ ุจูุงูุงุช!');return;}
|
| 539 |
+
try{const data=JSON.parse(txt);if(!Array.isArray(data))throw new Error('Expected array');
|
| 540 |
+
if(items.length>0&&!confirm('ุณูุชู
ุงุณุชุจุฏุงู ุงูู
ุญุชูู ุงูุญุงูู. ู
ุชุงุจุนุฉุ'))return;
|
| 541 |
+
items.forEach(i=>i.el.remove());items=[];selectedItemId=null;itemIdCounter=0;currentMaxZ=10;
|
| 542 |
+
addBoardItems(data,false);modalOverlay.classList.remove('show');
|
| 543 |
+
}catch(err){alert('ุจูุงูุงุช JSON ุบูุฑ ุตุงูุญุฉ: '+err.message);}
|
| 544 |
+
});
|
| 545 |
+
|
| 546 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 547 |
+
// MINIMAP
|
| 548 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 549 |
+
|
| 550 |
+
function updateMinimap() {
|
| 551 |
+
const ctx=minimapCtx, cw=140, ch=100;
|
| 552 |
+
ctx.clearRect(0,0,cw,ch);ctx.fillStyle='#fdfbf7';ctx.fillRect(0,0,cw,ch);
|
| 553 |
+
if(!items.length) return;
|
| 554 |
+
const CW=6000,CH=6000,s=Math.min(cw/CW,ch/CH);
|
| 555 |
+
const cols={blue:'#d4e4ff',pink:'#ffd4e4',green:'#d4ffd4',orange:'#ffe4c4',white:'#fff',yellow:'#fff9c4',purple:'#e4d4ff'};
|
| 556 |
+
items.forEach(it=>{
|
| 557 |
+
const ix=parseFloat(it.el.style.left)||0,iy=parseFloat(it.el.style.top)||0,iw=it.el.offsetWidth||80,ih=it.el.offsetHeight||80;
|
| 558 |
+
ctx.fillStyle=it.type==='note'?(cols[it.color]||'#fff9c4'):it.type==='text'?'rgba(100,100,200,0.3)':'rgba(45,45,45,0.15)';
|
| 559 |
+
ctx.fillRect(ix*s,iy*s,Math.max(iw*s,2),Math.max(ih*s,2));
|
| 560 |
+
ctx.strokeStyle='#2d2d2d';ctx.lineWidth=0.5;ctx.strokeRect(ix*s,iy*s,Math.max(iw*s,2),Math.max(ih*s,2));
|
| 561 |
+
});
|
| 562 |
+
const rect=canvasArea.getBoundingClientRect();
|
| 563 |
+
ctx.strokeStyle='#ff4d4d';ctx.lineWidth=1.5;ctx.setLineDash([3,2]);
|
| 564 |
+
ctx.strokeRect((-panX/scale)*s,(-panY/scale)*s,(rect.width/scale)*s,(rect.height/scale)*s);
|
| 565 |
+
ctx.setLineDash([]);
|
| 566 |
+
}
|
| 567 |
+
|
| 568 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 569 |
+
// AI CHAT & PLAYBACK
|
| 570 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 571 |
+
|
| 572 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 573 |
+
// FIX 1: sendMessage must include board_state from frontend
|
| 574 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 575 |
+
|
| 576 |
+
async function sendMessage() {
|
| 577 |
+
const msg = chatInput.value.trim();
|
| 578 |
+
if(!msg) return;
|
| 579 |
+
chatInput.disabled=true; btnSend.disabled=true; chatInput.value='';
|
| 580 |
+
setStatus('thinking','ุฌุงุฑู ุงูุชูููุฑ...');
|
| 581 |
+
|
| 582 |
+
try {
|
| 583 |
+
// โ
โ
โ
FIXED: Send board_state with message โ
โ
โ
|
| 584 |
+
const boardState = getCurrentBoardState(); // Get all items from canvas
|
| 585 |
+
|
| 586 |
+
const res = await fetch('/api/message', {
|
| 587 |
+
method: 'POST',
|
| 588 |
+
headers: {'Content-Type': 'application/json'},
|
| 589 |
+
body: JSON.stringify({
|
| 590 |
+
message: msg,
|
| 591 |
+
board_state: boardState // โ THIS IS THE FIX
|
| 592 |
+
})
|
| 593 |
+
});
|
| 594 |
+
|
| 595 |
+
if(!res.ok) throw new Error(`HTTP ${res.status}`);
|
| 596 |
+
const data = await res.json();
|
| 597 |
+
|
| 598 |
+
if(data.success && data.sequence && data.sequence.length > 0) {
|
| 599 |
+
currentSequence = data.sequence;
|
| 600 |
+
currentStepIndex = 0;
|
| 601 |
+
startPlayback();
|
| 602 |
+
} else {
|
| 603 |
+
setStatus('error','ุฎุทุฃ ูู ุงูุงุณุชุฌุงุจุฉ');
|
| 604 |
+
setTimeout(() => setStatus('idle','ุฌุงูุฒ'), 3000);
|
| 605 |
+
}
|
| 606 |
+
} catch(err) {
|
| 607 |
+
console.error('Message error:', err);
|
| 608 |
+
setStatus('error','ุฎุทุฃ ูู ุงูุงุชุตุงู');
|
| 609 |
+
setTimeout(() => setStatus('idle','ุฌุงูุฒ'), 3000);
|
| 610 |
+
} finally {
|
| 611 |
+
chatInput.disabled=false; btnSend.disabled=false; chatInput.focus();
|
| 612 |
+
}
|
| 613 |
+
}
|
| 614 |
+
|
| 615 |
+
|
| 616 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 617 |
+
// FIX 2: getCurrentBoardState - reads all items from canvas
|
| 618 |
+
// Returns array of JSON objects representing board items
|
| 619 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 620 |
+
|
| 621 |
+
function getCurrentBoardState() {
|
| 622 |
+
// โ
FIXED: Use correct class name and collect all item data
|
| 623 |
+
return items.map(it => {
|
| 624 |
+
const d = {
|
| 625 |
+
type: it.type,
|
| 626 |
+
x: parseFloat(it.el.style.left) || 0,
|
| 627 |
+
y: parseFloat(it.el.style.top) || 0,
|
| 628 |
+
w: it.el.offsetWidth,
|
| 629 |
+
h: it.el.offsetHeight
|
| 630 |
+
};
|
| 631 |
+
|
| 632 |
+
if(it.type === 'note') {
|
| 633 |
+
d.color = it.color || 'yellow';
|
| 634 |
+
const ta = it.el.querySelector('textarea');
|
| 635 |
+
d.text = ta ? ta.value : '';
|
| 636 |
+
}
|
| 637 |
+
else if(it.type === 'text') {
|
| 638 |
+
d.content = it.el.textContent || '';
|
| 639 |
+
d.size = it.size;
|
| 640 |
+
d.color = it.color;
|
| 641 |
+
d.font = it.font;
|
| 642 |
+
}
|
| 643 |
+
else if(it.type === 'shape') {
|
| 644 |
+
d.shape = it.shape;
|
| 645 |
+
d.size = it.size || 80;
|
| 646 |
+
}
|
| 647 |
+
else if(it.type === 'image') {
|
| 648 |
+
d.subtype = it.subtype;
|
| 649 |
+
d.url = it.url || it.dataUrl;
|
| 650 |
+
d.no_bg = it.no_bg || false;
|
| 651 |
+
d.is_page = it.is_page || false;
|
| 652 |
+
d.name = it.name || '';
|
| 653 |
+
d.pageNumber = it.pageNumber || '';
|
| 654 |
+
if(it.dataUrl) d.dataUrl = it.dataUrl;
|
| 655 |
+
}
|
| 656 |
+
|
| 657 |
+
return d;
|
| 658 |
+
});
|
| 659 |
+
}
|
| 660 |
+
|
| 661 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 662 |
+
// FIX 3: Replay - voice only, NO board duplication
|
| 663 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 664 |
+
|
| 665 |
+
async function replayResponse() {
|
| 666 |
+
try {
|
| 667 |
+
setStatus('speaking', 'ุฅุนุงุฏุฉ ุงูุชุดุบูู...');
|
| 668 |
+
|
| 669 |
+
// โ
โ
โ
FIXED: Call /api/replay which returns VOICE ONLY โ
โ
โ
|
| 670 |
+
const res = await fetch('/api/replay', { method: 'POST' });
|
| 671 |
+
const data = await res.json();
|
| 672 |
+
|
| 673 |
+
if(data.success && data.sequence && data.sequence.length > 0) {
|
| 674 |
+
// Play ONLY voice - no board updates in this sequence
|
| 675 |
+
currentSequence = data.sequence;
|
| 676 |
+
currentStepIndex = 0;
|
| 677 |
+
startPlayback(); // Will only encounter "voice" types
|
| 678 |
+
} else {
|
| 679 |
+
setStatus('idle', 'ูุง ุชูุฌุฏ ุงุณุชุฌุงุจุฉ ุณุงุจูุฉ');
|
| 680 |
+
}
|
| 681 |
+
} catch(err) {
|
| 682 |
+
console.error('Replay error:', err);
|
| 683 |
+
setStatus('error', 'ุฎุทุฃ ูู ุฅุนุงุฏุฉ ุงูุชุดุบูู');
|
| 684 |
+
setTimeout(() => setStatus('idle', 'ุฌุงูุฒ'), 2000);
|
| 685 |
+
}
|
| 686 |
+
}
|
| 687 |
+
|
| 688 |
+
chatInput.addEventListener('keydown', e => { if(e.key==='Enter'&&!e.shiftKey){e.preventDefault();sendMessage();} });
|
| 689 |
+
btnSend.addEventListener('click', sendMessage);
|
| 690 |
+
|
| 691 |
+
// โโโ PLAYBACK โโโ
|
| 692 |
+
function startPlayback() {
|
| 693 |
+
isPlaying=true; playbackPaused=false;
|
| 694 |
+
playbackControls.classList.remove('hidden');
|
| 695 |
+
updatePlaybackUI();
|
| 696 |
+
playNextStep();
|
| 697 |
+
}
|
| 698 |
+
|
| 699 |
+
function updatePlaybackUI() {
|
| 700 |
+
const total=currentSequence.length, cur=currentStepIndex+1;
|
| 701 |
+
stepInfo.textContent=`${Math.min(cur,total)}/${total}`;
|
| 702 |
+
progressFill.style.width=`${(currentStepIndex/Math.max(total-1,1))*100}%`;
|
| 703 |
+
if(isPlaying&&!playbackPaused){iconPlay.style.display='none';iconPause.style.display='block';setStatus('playing','ูุดุฑุญ ุงูู
ุฏุฑุณ...');}
|
| 704 |
+
else if(playbackPaused){iconPlay.style.display='block';iconPause.style.display='none';setStatus('paused','ู
ุคูุช');}
|
| 705 |
+
else{iconPlay.style.display='block';iconPause.style.display='none';setStatus('idle','ุฌุงูุฒ');}
|
| 706 |
+
}
|
| 707 |
+
|
| 708 |
+
async function playNextStep() {
|
| 709 |
+
if(currentStepIndex>=currentSequence.length){
|
| 710 |
+
isPlaying=false;playbackPaused=false;updatePlaybackUI();setStatus('idle','ุฌุงูุฒ');syncBoardState();
|
| 711 |
+
return;
|
| 712 |
+
}
|
| 713 |
+
if(playbackPaused) return;
|
| 714 |
+
|
| 715 |
+
const step=currentSequence[currentStepIndex];
|
| 716 |
+
updatePlaybackUI();
|
| 717 |
+
|
| 718 |
+
if(step.type==='voice'){setStatus('speaking','๐ ูุชุญุฏุซ...');await playVoice(step);}
|
| 719 |
+
else if(step.type==='board_update'){setStatus('drawing','โ๏ธ ูุฑุณู
...');await applyBoardUpdate(step);}
|
| 720 |
+
|
| 721 |
+
currentStepIndex++;
|
| 722 |
+
if(!playbackPaused){await sleep(300);playNextStep();}
|
| 723 |
+
}
|
| 724 |
+
|
| 725 |
+
function playVoice(step) {
|
| 726 |
+
return new Promise(resolve => {
|
| 727 |
+
voiceResolve = resolve;
|
| 728 |
+
if(step.audio_url){
|
| 729 |
+
voicePlayer.src=step.audio_url;
|
| 730 |
+
voicePlayer.onended=()=>{voiceResolve=null;resolve();};
|
| 731 |
+
voicePlayer.onerror=()=>{voiceResolve=null;resolve();};
|
| 732 |
+
voicePlayer.play().catch(()=>{
|
| 733 |
+
const t=Math.max(2000,(step.text||'').length*80);
|
| 734 |
+
setTimeout(()=>{voiceResolve=null;resolve();},t);
|
| 735 |
+
});
|
| 736 |
+
} else {
|
| 737 |
+
const t=Math.max(1500,(step.text||'').length*60);
|
| 738 |
+
setTimeout(()=>{voiceResolve=null;resolve();},t);
|
| 739 |
+
}
|
| 740 |
+
});
|
| 741 |
+
}
|
| 742 |
+
|
| 743 |
+
async function applyBoardUpdate(step) {
|
| 744 |
+
if(step.action==='add'&&step.items){
|
| 745 |
+
for(const item of step.items){
|
| 746 |
+
addBoardItems([item],true);
|
| 747 |
+
scrollToItem(item);
|
| 748 |
+
await sleep(400);
|
| 749 |
+
}
|
| 750 |
+
}
|
| 751 |
+
}
|
| 752 |
+
|
| 753 |
+
function scrollToItem(item) {
|
| 754 |
+
if(item.x!==undefined&&item.y!==undefined){
|
| 755 |
+
const rect=canvasArea.getBoundingClientRect();
|
| 756 |
+
const tx=rect.width/2-item.x*scale, ty=rect.height/2-item.y*scale;
|
| 757 |
+
const dx=tx-panX, dy=ty-panY;
|
| 758 |
+
if(Math.abs(dx)>200||Math.abs(dy)>200){panX+=dx*0.3;panY+=dy*0.3;updateCanvasTransform();}
|
| 759 |
+
}
|
| 760 |
+
}
|
| 761 |
+
|
| 762 |
+
$('btnPlayPause').addEventListener('click',()=>{
|
| 763 |
+
if(!isPlaying)return;
|
| 764 |
+
playbackPaused=!playbackPaused;
|
| 765 |
+
updatePlaybackUI();
|
| 766 |
+
if(!playbackPaused) playNextStep();
|
| 767 |
+
else voicePlayer.pause();
|
| 768 |
+
});
|
| 769 |
+
|
| 770 |
+
$('btnSkip').addEventListener('click',()=>{
|
| 771 |
+
if(!isPlaying)return;
|
| 772 |
+
voicePlayer.pause();voicePlayer.currentTime=0;
|
| 773 |
+
if(voiceResolve){voiceResolve();voiceResolve=null;}
|
| 774 |
+
});
|
| 775 |
+
|
| 776 |
+
$('btnReplay').addEventListener('click',()=>{
|
| 777 |
+
if(!currentSequence.length) return;
|
| 778 |
+
voicePlayer.pause();voicePlayer.currentTime=0;
|
| 779 |
+
if(voiceResolve){voiceResolve();voiceResolve=null;}
|
| 780 |
+
currentStepIndex=0;playbackPaused=false;isPlaying=true;
|
| 781 |
+
playNextStep();
|
| 782 |
+
});
|
| 783 |
+
|
| 784 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 785 |
+
// INIT
|
| 786 |
+
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 787 |
+
|
| 788 |
+
updateCanvasTransform();
|
| 789 |
+
hideEmptyState();
|
| 790 |
+
setStatus('idle','ุฌุงูุฒ');
|
| 791 |
+
|
| 792 |
+
// Start with sidebar collapsed
|
| 793 |
+
$('sidebar').classList.add('collapsed');
|
| 794 |
+
canvasArea.classList.add('sidebar-collapsed');
|
| 795 |
+
|
| 796 |
+
// Welcome
|
| 797 |
+
setTimeout(()=>{
|
| 798 |
+
addNote('yellow', 250, 200,
|
| 799 |
+
"๐ ู
ุฑุญุจุงู ูู ุงูุณุจูุฑุฉ ุงูุฐููุฉ!\n\nุงูุชุจ ุณุคุงูู ูู ุงูุฃุณูู\nูุณูุดุฑุญ ุงูู
ุฏุฑุณ ุนูู ุงูุณุจูุฑุฉ\nุจุงูุตูุช ูุงูุฑุณู
!");
|
| 800 |
+
addNote('blue', 550, 250,
|
| 801 |
+
"๐ก ูุตุงุฆุญ:\nโข ุงุณุฃู ุนู ุฃู ู
ูุถูุน ูู ุงูู
ููุงูููุง\nโข ูู
ููู ุฅุถุงูุฉ ู
ูุงุญุธุงุชู\nโข ุงููุฑ ู
ุฒุฏูุฌ ูุฅุถุงูุฉ ู
ูุงุญุธุฉ\nโข ุงุณุญุจ ูุญุฑู ูู ุดูุก!");
|
| 802 |
+
placeShape('star', 800, 200);
|
| 803 |
+
deselectAll();
|
| 804 |
+
updateMinimap();
|
| 805 |
+
},200);
|
| 806 |
+
|
| 807 |
+
</script>
|
| 808 |
+
|
| 809 |
+
|
| 810 |
+
You are a converter that transforms XML board data into JSON format.
|
| 811 |
+
|
| 812 |
+
Convert the given XML to JSON following these rules:
|
| 813 |
+
1. Extract all elements from the XML
|
| 814 |
+
2. Convert them to proper JSON structure
|
| 815 |
+
3. Wrap your JSON output in <json></json> tags
|
| 816 |
+
|
| 817 |
+
Example:
|
| 818 |
+
Input: <board><note color="yellow" x="100" y="200">Hello</note></board>
|
| 819 |
+
Output: <json>{"board": {"notes": [{"color": "yellow", "x": 100, "y": 200, "text": "Hello"}]}}</json>
|
| 820 |
+
|
| 821 |
+
Always wrap your final JSON output in <json></json> tags
|
| 822 |
+
|
| 823 |
+
|
| 824 |
+
NO any other words about explain what you did etc...... just the JSON
|
| 825 |
+
|
| 826 |
+
ALSO response just with the new board the history dont touch it because it is already added
|
users.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"ุณูู": {
|
| 3 |
+
"username": "user",
|
| 4 |
+
"password": "123456",
|
| 5 |
+
"student_type": "ุนูู
ู",
|
| 6 |
+
"telegram_user_id": "6481568318",
|
| 7 |
+
"session_id": "e88a786f-ebc1-4717-8381-35774604a24a",
|
| 8 |
+
"created_at": "2026-03-29T16:17:51.893566",
|
| 9 |
+
"verified": true,
|
| 10 |
+
"last_login": "2026-04-17T21:17:06.768894",
|
| 11 |
+
"balance": 0,
|
| 12 |
+
"purchased_subjects": [
|
| 13 |
+
"islamic",
|
| 14 |
+
"physics_mechanic",
|
| 15 |
+
"math",
|
| 16 |
+
"physics_electric",
|
| 17 |
+
"it",
|
| 18 |
+
"english",
|
| 19 |
+
"statistics",
|
| 20 |
+
"chemistry",
|
| 21 |
+
"biology",
|
| 22 |
+
"linguistic_studies",
|
| 23 |
+
"literary_studies",
|
| 24 |
+
"data_structure"
|
| 25 |
+
]
|
| 26 |
+
},
|
| 27 |
+
"omar": {
|
| 28 |
+
"username": "omar",
|
| 29 |
+
"password": "123456",
|
| 30 |
+
"student_type": "ุฃุฏุจู",
|
| 31 |
+
"telegram_user_id": "1475261130",
|
| 32 |
+
"session_id": null,
|
| 33 |
+
"created_at": "2026-04-05T09:27:15.630502",
|
| 34 |
+
"verified": true,
|
| 35 |
+
"last_login": "2026-04-05T09:27:15.630523",
|
| 36 |
+
"balance": 20,
|
| 37 |
+
"purchased_subjects": [
|
| 38 |
+
"islamic",
|
| 39 |
+
"statisticss",
|
| 40 |
+
"history"
|
| 41 |
+
]
|
| 42 |
+
}
|
| 43 |
+
}
|