Step By Step C++ Plusplus

  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Step By Step C++ Plusplus as PDF for free.

More details

  • Words: 7,732
  • Pages: 59
‫ﺗﻢ ﺗﺤﻤﻴﻞ اﻟﻤﻠﻒ ﻣﻦ ﻣﻮﻗﻊ‬ ‫اﻟﺒﻮﺻﻠﺔ اﻟﺘﻘﻨﻴﺔ‬ ‫‪www.boosla.com‬‬

‫ﺑﺴﻢ ﺍﷲ ﺍﻟﺮﲪﻦ ﺍﻟﺮﺣﻴﻢ‬ ‫اﻟﺤﻤﺪ ﷲ رب اﻟﻌﺎﻟﻤﯿﻦ‪ ،‬اﻟﻘﺎﺋﻞ ﻓﻲ ﻣﺤﻜﻢ اﻟﺘﻨﺰﯾﻞ ) وﻋﻠﻤﻚ ﻣﺎ ﻟﻢ ﺗﻜﻦ ﺗﻌﻠﻢ ‪ ،‬وﻛﺎن ﻓﻀﻞ اﷲ ﻋﻠﯿﻚ‬ ‫ﻋﻈﯿﻤﺎً ( ‪ ،‬واﻟﺼﻼة و اﻟﺴﻼم ﻋﻠﻰ ﺳﯿﺪﻧﺎ ﻣﺤﻤﺪ ﺳﯿﺪ اﻟﻌﻠﻤﺎء و ﺳﯿﺪ اﻷوﻟﯿﻦ و اﻵﺧﺮﯾﻦ رﺳﻮل رب‬ ‫اﻟﻌﺎﻟﻤﯿﻦ ‪ ،‬وﻋﻠﻰ آﻟﮫ و ﺻﺤﺒﮫ أﺟﻤﻌﯿﻦ ‪.‬‬ ‫) ﺳﺒﺤﺎﻧﻚ ﻻ ﻋﻠﻢ ﻟﻨﺎ إﻻ ﻣﺎ ﻋﻠﻤﺘﻨﺎ إﻧﻚ أﻧﺖ اﻟﻌﻠﯿﻢ اﻟﺤﻜﯿﻢ (‬ ‫ھﺬا اﻟﻜﺘﺎب ﻣﻮﺟﮫ إﻟﻰ ﻛﻞ ﻣﻦ ﻣﺒﺘﺪئ ﻓﻲ اﻟﺒﺮﻣﺠﺔ وإﻟﻰ ﻃﻼﺑﻨﺎ ﻓﻲ اﻟﻤﻌﺎھﺪ و اﻟﺠﺎﻣﻌﺎت ﻟﺘﻜﻮن‬ ‫ﻋﻮﻧﺎً ﻟﮭﻢ إن ﺷﺎء اﷲ ‪.‬‬ ‫وأدﻋﻮ اﷲ أن ﯾﺘﻘﺒﻠﮫ ﺧﺎﻟﺼﺎً ﻟﻮﺟﮫ وأن ﯾﻜﺘﺒﮫ ﻓﻲ ﺻﺤﯿﻔﺘﻲ و ﺻﺤﯿﻔﺔ ﻣﻦ ﺳﯿﺘﺎﺑﻊ ﻓﻲ ﻧﺸﺮه‬ ‫وأن ﯾﺠﻌﻠﮫ ﻋﻤﻼً ﯾﻨﺘﻔﻊ ﺑﮫ إﻟﻰ ﯾﻮم اﻟﺪﯾﻦ‬ ‫ﻓﻤﻦ وﺟﺪ ﺧﻄﺄً ﻓﮭﻮ ﻣﻨﻲ وﻣﺎ ﻛﺎن ﻓﯿﮫ ﻣﻦ ﺻﻮاب ﻓﻤﻦ ﺗﻮﻓﯿﻖ اﷲ‬ ‫وآﺧﺮ دﻋﻮاﻧﺎ اﻟﺤﻤﺪ ﷲ رب اﻟﻌﺎﻟﻤﯿﻦ‬ ‫أﺧﻮﻛﻢ ﻓﻲ اﷲ‬ ‫ﻣﺤﻤﺪ ھﻨﺪاوي‬ ‫‪[email protected]‬‬

‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫اﻟﻤﺤﺘﻮﯾﺎت‬ ‫ﺗﺴﻤﯿﺔ اﻟﻤﺘﻐﯿﺮات ﻓﻲ ﻟﻐﺔ ‪C++‬‬ ‫ﻛﯿﻔﯿﺔ ﻛﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺞ ﺑﻠﻐﺔ ‪C++‬‬ ‫اﻹﻋﻼم ﻋﻦ اﻟﻤﺘﻐﯿﺮات‬ ‫أﻧﻮاع اﻟﻤﺘﻐﯿﺮات‬ ‫اﻟﻌﻤﻠﯿﺎت اﻟﺤﺴﺎﺑﯿﺔ و اﻷدوات اﻟﻤﺴﺘﺨﺪﻣﺔ ﻓﯿﮭﺎ‬ ‫أوﻟﻮﯾﺎت اﻟﻌﻤﻠﯿﺎت اﻟﺤﺴﺎﺑﯿﺔ‬ ‫اﻷدوات اﻟﻤﻨﻄﻘﯿﺔ‬ ‫اﻷدوات اﻟﺸﺮﻃﯿﺔ‬ ‫اﻷدوات اﻟﺸﺮﻃﯿﺔ اﻷوﻟﯿﺔ ‪conditional operator‬‬ ‫اﻟﺠﻤﻠﺔ اﻟﺸﺮﻃﯿﺔ ‪if – else‬‬ ‫ﺣﻠﻘﺔ )‪(switch – case‬‬ ‫ﺣﻠﻘﺔ ‪for‬‬ ‫ﺣﻠﻘﺎت ‪ for‬اﻟﻤﺘﺪاﺧﻠﺔ‬ ‫ﺣﻠﻘﺔ ‪while‬‬ ‫ﺣﻠﻘﺔ ‪do while‬‬ ‫ﺣﻠﻘﺔ اﻹﯾﻘﺎف ‪break‬‬ ‫ﺣﻠﻘﺔ اﻻﺳﺘﻤﺮار ‪continue‬‬ ‫ﺗﻌﻠﯿﻤﺔ اﻻﻧﺘﻘﺎل ‪goto‬‬ ‫ﺳﻠﺴﻠﺔ ﻓﯿﺒﻮﻧﺎﺷﻲ ‪Fibonacci Series‬‬ ‫اﻟﻤﺼﻔﻮﻓﺎت‪Arrays‬‬ ‫ﻛﯿﻔﯿﺔ إدﺧﺎل ﻋﻨﺎﺻﺮ ﻣﺼﻔﻮﻓﺔ‬ ‫اﻟﻤﺼﻔﻮﻓﺎت ﺛﻨﺎﺋﯿﺔ اﻟﺒﻌﺪ‬ ‫اﻟﺴﺠﻼت ) اﻟﺘﺮﻛﯿﺒﺎت ( ‪structure‬‬ ‫ﺗﻌﺮﯾﻒ ﺳﺠﻞ داﺧﻞ ﺳﺠﻞ آﺧﺮ‬ ‫اﻟﺘﻮاﺑﻊ ‪function‬‬ ‫اﻟﻤﻠﻔﺎت ‪files‬‬ ‫ﻛﯿﻔﯿﺔ اﻟﺘﻌﺎﻣﻞ ﺑﯿﻦ اﻟﻤﻠﻔﺎت و اﻟﺴﺠﻼت‬ ‫اﻟﻘﺮاءة واﻟﻜﺘﺎﺑﺔ ﻋﻠﻰ ﻣﻠﻒ ﺑﺒﺮﻧﺎﻣﺞ واﺣﺪ‬

‫‪٢‬‬ ‫‪٢‬‬ ‫‪٣‬‬ ‫‪٤‬‬ ‫‪٦‬‬ ‫‪٦‬‬ ‫‪٧‬‬ ‫‪٨‬‬ ‫‪٩‬‬ ‫‪٩‬‬ ‫‪١١‬‬ ‫‪١٦‬‬ ‫‪١٧‬‬ ‫‪١٨‬‬ ‫‪١٩‬‬ ‫‪٢١‬‬ ‫‪٢٢‬‬ ‫‪٢٢‬‬ ‫‪٢٩‬‬ ‫‪٣٢‬‬ ‫‪٣٣‬‬ ‫‪٣٨‬‬ ‫‪٤٠‬‬ ‫‪٤٤‬‬ ‫‪٤٥‬‬ ‫‪٤٩‬‬ ‫‪٥٣‬‬ ‫‪٥٥‬‬

‫‪١‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫ﻟﻐﺔ ‪C++‬‬ ‫ﺗﺴﻤﯿﺔ اﻟﻤﺘﻐﯿﺮات ﻓﻲ ﻟﻐﺔ ‪C++‬‬ ‫‪ -١‬ﯾﺠﺐ أن ﻻ ﺗﻜﻮن أﺳﻤﺎء اﻟﻤﺘﻐﯿﺮات ﻣﻦ‬ ‫اﻟﻜﻠﻤﺎت اﻟﻤﺤﺠﻮزة ) ‪(RESTRAINED WORDS‬‬ ‫أو اﻟﻜﻠﻤﺎت اﻟﺘﻲ ﺗﺤﻤﻞ ﻣﻌﻨﺎً ﺧﺎﺻﺎً ﻣﺜﻞ ‪main‬‬ ‫‪ -٢‬ﯾﻤﻜﻦ أن ﯾﺤﺘﻮي اﻻﺳﻢ ﻋﻠﻰ أي ﺣﺮف ﻣﻦ اﻟﺤﺮوف اﻷﺑﺠﺪﯾﺔ ﺻﻐﯿﺮة أو ﻛﺒﯿﺮة وﻋﻠﻰ أي رﻗﻢ‬ ‫ﻣﻦ اﻷرﻗﺎم ﻛﺬﻟﻚ ﯾﻤﻜﻦ أن ﺗﺤﻮي اﻹﺷﺎرة ) _ (‬ ‫‪ -٣‬ﻻ ﯾﺠﻮز أن ﯾﺒﺪأ اﻻﺳﻢ ﺑﺮﻗﻢ‬ ‫ﻣﺜﺎل‪:‬‬ ‫ﺧﻄﺄ ﺑﺴﺒﺐ اﻟﺒﺪاﯾﺔ ﺑﺮﻗﻢ‬ ‫‪7-up‬‬ ‫ﺧﻄﺄ ﺑﺴﺒﺐ اﺳﺘﺨﺪام )!( اﻟﻤﺤﺠﻮزة‬ ‫!‪Salim‬‬ ‫ﺧﻄﺄ ﺑﺴﺒﺐ اﺳﺘﺨﺪام ) ‪ ( #‬اﻟﻤﺤﺠﻮزة‬ ‫‪No#‬‬ ‫ﻋﺒﺎرة ﺻﺤﯿﺤﺔ‬ ‫‪A9‬‬ ‫ﺧﻄﺄ ﺑﺴﺒﺐ إﺷﺎرة اﻟﻄﺮح‬ ‫‪a-a‬‬ ‫‪-٤‬إن ھﺬه اﻟﻠﻐﺔ ﺣﺴﺎﺳﺔ ﻟﺤﺎﻟﺔ اﻟﺤﺮف أي اﻟﺤﺮف اﻟﺼﻐﯿﺮ ﻟﯿﺲ ھﻮ اﻟﺤﺮف اﻟﻜﺒﯿﺮ‬ ‫ﻓﺈن اﻟﻤﺘﺤﻮل ‪ a‬ﻓﮭﻮ ﻏﯿﺮ اﻟﻤﺘﺤﻮل ‪A‬‬

‫ﻛﯿﻔﯿﺔ ﻛﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺞ ﺑﻠﻐﺔ ‪C++‬‬ ‫ﻟﻜﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺞ ﯾﺠﺐ أن ﻧﺴﺘﮭﻞ اﻟﺒﺮﻧﺎﻣﺞ ﺑﺎﻟﻤﻜﺘﺒﺎت اﻟﺘﻲ ﺗﺪﻋﻢ اﻟﺘﻌﻠﯿﻤﺎت اﻟﻮاﺟﺐ اﺳﺘﺨﺪاﻣﮭﺎ و‬ ‫اﻟﻤﻜﺘﺒﺔ اﻟﺘﻲ ﺳﻨﺴﺘﺨﺪﻣﮭﺎ اﻵن ھﻲ ﻣﻜﺘﺒﺔ ‪iostream‬‬ ‫ﺑﺤﯿﺚ ﺗﻤﻜﻨﻨﺎ ھﺬه اﻟﻤﻜﺘﺒﺔ ﻣﻦ اﺳﺘﺨﺪام اﻷواﻣﺮ اﻟﺘﺎﻟﯿﺔ‬ ‫‪c in‬‬ ‫إدﺧﺎل وﺗﻘﺮأ‬ ‫>>‪cin‬‬ ‫‪c out‬‬ ‫<<‪ cout‬إﺧﺮاج و ﺗﻘﺮأ‬ ‫‪end line‬‬ ‫ﻧﮭﺎﯾﺔ ﺳﻄﺮ وﺗﻘﺮأ‬ ‫‪endl‬‬ ‫‪٢‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫ﺗﺘﻌﺎﻣﻞ ھﺬه ﻟﻐﺔ ‪ C++‬ﻣﻊ ﻋﺪد ﻛﺒﯿﺮ ﻣﻦ اﻟﻤﻜﺘﺒﺎت وﻛﻞ ﻣﻜﺘﺒﺔ ﻟﮭﺎ اﻛﻮاد ﺧﺎﺻﺔ ﺑﮭﺎ و ﯾﻤﻜﻦ اﻟﺘﻌﺎﻣﻞ‬ ‫ﻣﻊ ﻋﺪة ﻣﻜﺘﺒﺎت ﻓﻲ آن واﺣﺪ وﺳﻨﺘﺤﺪث ﻋﻦ ﻛﻞ ﻣﻜﺘﺒﺔ ﻋﻨﺪﻣﺎ ﯾﺘﻄﻠﺐ اﻟﺒﺮﻧﺎﻣﺞ ذﻟﻚ‬ ‫ﻛﯿﻔﯿﺔ ﻛﺘﺎﺑﺔ اﻟﺒﺮﻧﺎﻣﺞ‬ ‫ﯾﻜﺘﺐ أي ﺑﺮﻧﺎﻣﺞ ﻛﻤﺎ ﻓﻲ اﻟﺸﻜﻞ اﻟﺘﺎﻟﻲ‪:‬‬ ‫ﻣﻘﺪﻣﺔ ﻟﺬﻛﺮ اﺳﻢ اﻟﻤﻜﺘﺒﺔ‬ ‫ﻣﻘﺪﻣﺔ أﯾﻀﺎ ﺳﻨﺸﺮح ھﺬه اﻷواﻣﺮ ﻓﻲ اﻟﻔﺼﻮل اﻟﻼﺣﻘﺔ‬ ‫ﻗﻮس اﻟﺪﻻﻟﺔ ﻟﺒﺪاﯾﺔ اﻟﺒﺮﻧﺎﻣﺞ‬ ‫ﻗﻮس اﻟﺪﻻﻟﺔ ﻋﻠﻰ ﻧﮭﺎﯾﺔ اﻟﺒﺮﻧﺎﻣﺞ‬

‫>‪#include
‫اﻹﻋﻼم ﻋﻦ اﻟﻤﺘﻐﯿﺮات‬ ‫ﻟﯿﺘﻤﻜﻦ اﻟﻤﺴﺘﺨﺪم ﻣﻦ اﺳﺘﺨﺪام اﻟﻤﺘﻐﯿﺮات اﻟﺘﻲ ﯾﺮﯾﺪھﺎ ﯾﺘﻄﻠﺐ اﻟﺒﺮﻧﺎﻣﺞ اﻹﻋﻼم اﻟﻤﺴﺒﻖ ﻋﻦ أﺳﻤﺎء‬ ‫ھﺬه اﻟﻤﺘﻐﯿﺮات ﻓﻤﺜﻼً ﻟﻠﺘﻌﺎﻣﻞ ﻣﻊ ﻣﺘﻐﯿﺮﯾﻦ ﻣﻦ ﻧﻮع ) ﻋﺪد ﺻﺤﯿﺢ ( ﻧﻜﺘﺐ ﻣﺎ ﯾﻠﻲ‪:‬‬ ‫;‪int a‬‬ ‫;‪int b‬‬ ‫وﯾﻤﻜﻦ أن ﺗﻜﺘﺐ ﺑﺎﻟﺸﻜﻞ اﻟﺘﺎﻟﻲ ; ‪int a,b‬‬ ‫وﺳﻨﺴﺘﺨﺪم اﻟﺒﺮﻧﺎﻣﺞ ‪ Turbo‬ﻟﺘﻨﻔﯿﺬ اﻷﻣﺜﻠﮫ ﻛﻤﺎ ھﻮ ﻇﺎھﺮ ﻓﻲ اﻟﺸﻜﻞ ‪:‬‬

‫‪٣‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫وﯾﻜﻔﯿﻚ أن ﺗﻀﻐﻂ اﻟﻘﺎﺋﻤﺔ ‪ File‬وﺗﺨﺘﺎر اﻷﻣﺮ ‪ New‬وﺗﺒﺪأ ﺑﻜﺘﺎﺑﺔ اﻟﺒﺮﻧﺎﻣﺞ وﺗﻀﻐﻂ اﻷﻣﺮ‬ ‫‪Run‬‬ ‫ﻟﻜﻲ ﺗﻨﻔﺬ اﻟﺒﺮﻧﺎﻣﺞ‬ ‫ﻣﺜﺎل‪:‬‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﺎﻟﺘﺼﺮﯾﺢ ﻋﻦ ﻣﺘﺤﻮﻟﯿﻦ ﺻﺤﯿﺤﯿﻦ وﻣﻦ ﺛﻢ ﯾﻘﻮم ﺑﺠﻤﻊ ﻗﯿﻤﺔ اﻷول إﻟﻰ ﻋﺸﺮة‬ ‫وإﺳﻨﺎده إﻟﻰ ﻗﯿﻤﺔ اﻟﻤﺘﺤﻮل اﻟﺜﺎﻧﻲ وﻣﻦ ﺛﻢ ﯾﻘﻮم اﻟﺒﺮﻧﺎﻣﺞ ﺑﻄﺒﺎﻋﺔ ﻗﯿﻤﺔ اﻟﻨﺎﺗﺞ ؟‬ ‫>‪#include > var1‬‬ ‫;‪var2 = var1 + 10‬‬ ‫;‪cout<< " var1+ 10 is "<
‫اﻟﻌﺒﺎرة ;‪ cin>> var1‬ﺗﻌﻨﻲ أدﺧﻞ ﻗﯿﻤﺔ اﻟﻤﺘﺤﻮل ‪var1‬‬

‫أﻧﻮاع اﻟﻤﺘﻐﯿﺮات ‪:‬‬ ‫اﻟﻤﺘﻐﯿﺮات اﻟﺤﺮﻓﯿﺔ ‪char‬‬ ‫‪٤‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫وﺗﺘﻀﻤﻦ اﻟﺤﺮوف ﺑﻜﺎﻓﺔ أﺷﻜﺎﻟﮭﺎ و اﻟﺮﻣﻮز و اﻟﻔﺮاﻏﺎت ) ﻣﺴﺎﻓﺔ ﻓﺎرﻏﺔ ( ﻣﺜﻞ ‪:‬‬ ‫;‪char a,b‬‬ ‫; '‪a= 'a‬‬ ‫;''=‪b‬‬ ‫;‪char var1‬‬ ‫; '‪var1 = '.‬‬ ‫اﻟﻤﺘﻐﯿﺮات اﻟﺼﺤﯿﺤﺔ ‪integer‬‬ ‫ﺗﺘﻀﻤﻦ ﻗﯿﻢ ﻋﺪدﯾﺔ ﺻﺤﯿﺤﺔ ﯾﻤﻜﻦ أن ﺗﺄﺧﺬ ﻗﯿﻤﺔ ﺗﺼﻞ إﻟﻰ ‪٣٢٧٦٧‬‬ ‫وﺗﻜﺘﺐ ﻋﻠﻰ اﻟﺸﻜﻞ اﻟﺘﺎﻟﻲ ‪:‬‬ ‫;‪int a‬‬ ‫;‪a= 100‬‬ ‫اﻟﻤﺘﻐﯿﺮات اﻟﻌﺪدﯾﺔ اﻟﻌﺸﺮﯾﺔ ‪floating point‬‬ ‫ﺗﺘﻀﻤﻦ ﺟﻤﯿﻊ اﻷﻋﺪاد اﻟﺤﻘﯿﻘﯿﺔ‬ ‫وﺗﻜﺘﺐ ﻋﻠﻰ اﻟﺸﻜﻞ اﻟﺘﺎﻟﻲ‪:‬‬ ‫;‪float x‬‬ ‫;‪x = 5.2‬‬ ‫اﻟﻤﺘﻐﯿﺮات اﻟﻌﺪدﯾﺔ اﻟﻌﺸﺮﯾﺔ اﻟﻄﻮﯾﻠﺔ ‪double‬‬ ‫ھﻲ ﻧﻔﺲ اﻟﻤﺘﻐﯿﺮات اﻟﻌﺪدﯾﺔ اﻟﻌﺸﺮﯾﺔ وﻟﻜﻦ ﯾﻤﻜﻦ ﺗﻤﺜﯿﻠﮭﺎ إﻟﻰ ﺧﻤﺴﺔ ﻋﺸﺮ ﺧﺎﻧﺔ‬ ‫وﺗﻜﺘﺐ ﻋﻠﻰ اﻟﺸﻜﻞ اﻟﺘﺎﻟﻲ‪:‬‬ ‫;‪double x‬‬

‫ﻣﻼﺣﻈﺎت ھﺎﻣﺔ‬ ‫ﻗﺴــــﻤﺔ ﻋﺪد ﺻﺤﯿﺢ ﻋﻠﻰ ﻋﺪد ﺻﺤﯿﺢ ﯾﻜﻮن اﻟـﻨــﺎﺗﺞ ﺻﺤﯿﺢ أﯾﻀﺎً‬ ‫ﻗﺴــــﻤﺔ ﻋـﺪد ﺣﻘﯿﻘﻲ ﻋﻠﻰ ﻋﺪد ﺻﺤﯿﺢ ﯾﻜﻮن اﻟـــﻨــﺎﺗﺞ ﺣﻘﯿﻘﻲ أﯾﻀﺎً‬ ‫أﻣﺎ ﻗﺴﻤﺔ ﻋﺪد ﺣﻘﯿﻘﻲ ﻋﻠﻰ ﻋﺪد ﺻﺤﯿﺢ ﯾﻜﻮن اﻟــﻨﺎﺗــﺞ ﺣﻘﯿﻘﻲ‬ ‫ﻻ ﺗﻮﺟﺪ ﻋﻤﻠﯿﺔ ﻗﺴﻤﺔ ﻋﺪد ﺻﺤﯿﺢ ﻋﻠﻰ ﻋﺪد ﺣﻘﯿﻘﻲ‬ ‫ﻣﺜﺎل ‪:‬‬ ‫أوﺟﺪ ﻧﺘﯿﺠﺔ اﻟﺘﻤﺮﯾﻦ اﻟﺘﺎﻟﻲ‬ ‫>‪#include
‫;‪float x,y,z‬‬ ‫;‪i = 5/2‬‬ ‫; ‪x = 5/2‬‬ ‫;)‪y = (float ) (5/2‬‬ ‫;‪j = ( float) 5/2‬‬ ‫; ‪z = 5. / 2‬‬ ‫;‪cout<
‫اﻟﺠﻮاب‬ ‫‪j=2.5‬‬

‫‪y= 2.000‬‬

‫‪x= 2.000‬‬

‫‪I=2‬‬

‫اﻟﻌﻤﻠﯿﺎت اﻟﺤﺴﺎﺑﯿﺔ و اﻷدوات اﻟﻤﺴﺘﺨﺪﻣﺔ ﻓﯿﮭﺎ‬ ‫ﻟﻠﺠﻤﻊ ﺗﺴﺘﺨﺪم‬ ‫ﻟﻠﻄﺮح‬ ‫ﻟﻠﻘﺴﻤﺔ‬ ‫‪%‬‬ ‫ﺑﺎﻗﻲ اﻟﻘﺴﻤﺔ‬ ‫*‬ ‫ﻟﻠﻀﺮب‬ ‫ﻟﻠﺰﯾﺎدة ﺑﻤﻘﺪار واﺣﺪ ‪++‬‬ ‫ﻟﻠﻨﻘﺼﺎن ﺑﻤﻘﺪار واﺣﺪ ‪--‬‬ ‫‪+‬‬ ‫‪-‬‬

‫‪/‬‬

‫أوﻟﻮﯾﺎت اﻟﻌﻤﻠﯿﺎت اﻟﺤﺴﺎﺑﯿﺔ‬ ‫‪ -١‬اﻟﺰﯾﺎدة و اﻟﻨﻘﺼﺎن ﻋﻨﺪﻣﺎ ﺗﺄﺗﻲ ﻗﺒﻞ اﻟﻌﺪد‬ ‫‪ -٢‬اﻷﻗﻮاس‬ ‫‪ -٣‬إﺷﺎرة اﻟﺴﺎﻟﺐ‬ ‫‪ -٤‬اﻟﻘﺴﻤﺔ و ﺑﺎﻗﻲ اﻟﻘﺴﻤﺔ و اﻟﻀﺮب‬ ‫‪ -٥‬اﻟﺠﻤﻊ و اﻟﺼﺮح‬ ‫‪ -٦‬اﻟﻤﺴﺎواة‬ ‫‪ -٧‬اﻟﺰﯾﺎدة و اﻟﻨﻘﺼﺎن اﻟﻤﺘﺄﺧﺮة ﺑﻌﺪ اﻟﻌﺪد‬ ‫ﻣﻼﺣﻈﺔ‬ ‫ﻓﻲ ﺣﺎل وﺟﻮد ﻋﻤﻠﯿﺘﯿﻦ ﻟﮭﻤﺎ ﻧﻔﺲ اﻷوﻟﻮﯾﺔ ﻧﺒﺪأ ﺑﺘﻨﻔﯿﺬ اﻟﻌﻤﻠﯿﺔ اﻷﻗﺮب إﻟﻰ اﻟﯿﺴﺎر‪.‬‬ ‫‪٦‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫اﻷدوات اﻟﻌﻼﻗﯿﺔ ‪:‬‬ ‫وھﻲ ﺣﺴﺐ ھﺬا اﻟﺠﺪول‬ ‫اﻷﻛﺒﺮ‬ ‫اﻷﺻﻐﺮ‬ ‫أﻛﺒﺮ أو ﯾﺴﺎوي‬ ‫أﺻﻐﺮ أو‬ ‫ﯾﺴﺎوي‬ ‫إن ﻛﺎن ﯾﺴﺎوي‬ ‫إن ﻛﺎن ﻻ‬ ‫ﯾﺴﺎوي‬

‫<‬ ‫>‬ ‫=<‬ ‫=>‬ ‫==‬ ‫=!‬

‫أﻣﺎ ﺑﺎﻟﻨﺴﺒﺔ ﻟﻸوﻟﻮﯾﺎت ﻓﮭﻲ ﻋﻠﻰ ﻧﻔﺲ اﻟﺘﺮﺗﯿﺐ‬ ‫ﻣﺜﺎل‪:‬‬ ‫أﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﻹدﺧﺎل ﻋﺪد ﻣﺎ وﻣﻦ ﺛﻢ ﻃﺒﺎﻋﺔ ﺟﺪول ﺿﺮب ﻟﮫ‬ ‫اﻟﺤﻞ‪:‬‬ ‫>‪#include < iostream.h‬‬ ‫) ( ‪void main‬‬ ‫}‬ ‫;‪int x‬‬ ‫;‪cin>> x‬‬ ‫;‪cout<< x <<endl‬‬ ‫;‪cout<< x *2<<endl‬‬ ‫;‪cout<< x *3<<endl‬‬ ‫;‪cout<< x *4<<endl‬‬ ‫;‪cout<< x *5<<endl‬‬ ‫;‪cout<< x *6<<endl‬‬ ‫;‪cout<< x *7<<endl‬‬ ‫;‪cout<< x *8<<endl‬‬ ‫;‪cout<< x *9<<endl‬‬ ‫{‬

‫ﻣﺜﺎل آﺧﺮ‪:‬‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﻹدﺧﺎل ﺛﻼﺛﺔ ﻋﻼﻣﺎت ﻟﻄﺎﻟﺐ وﻣﻦ ﺛﻢ ﻃﺒﺎﻋﺔ ﻣﻌﺪل ھﺬه اﻟﻌﻼﻣﺎت‬ ‫‪٧‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

:‫اﻟﺤﻞ‬ #include void main () { int a,b,c; cin>> a >> b >> c ; cout<<"the averaje is"<< ( a+b+c )/3; }

‫اﻷدوات اﻟﻤﻨﻄﻘﯿﺔ‬ && || !

and or not :‫ﻣﺜﺎل‬

Int a=b=3; A<3 false A < = 3 true A>b false A != b false A = =b true : ‫اﻷدوات اﻟﺪﻗﯿﻘﺔ‬ ‫ وﺗﺴﺘﻌﻤﻞ ھﺬه اﻷدوات ﻣﻊ اﻟﻤﻌﻄﯿﺎت‬bit ‫ أﻧﮭﺎ ﺗﺴﺘﺨﺪم أدوات دﻗﯿﻘﺔ ﺗﺘﻌﺎﻣﻞ ﻣﻊ‬C++ ‫ﺗﺘﻤﯿﺰ ﻟﻐﻮ‬ .‫ وﻻ ﺗﺴﺘﺨﺪم ﻣﻊ ﻏﯿﺮھﺎ‬Int , char :‫وھﺬه اﻷدوات ھﻲ‬ ‫اﻟﺘﻌﻠﯿﻤﺔ‬ ~ >> << ^ & |

٨ PDF created with pdfFactory Pro trial version www.pdffactory.com

‫اﻟﺸﺮح‬ Not ‫إزاﺣﺔ اﻟﻰ اﻟﯿﺴﺎر‬ ‫إزاﺣﺔ اﻟﻰ اﻟﯿﻤﯿﻦ‬ Xor And Or

‫وﺗﻜﻮن اﻷوﻟﻮﯾﺔ ﺣﺴﺐ ﺗﺮﺗﯿﺐ اﻟﺠﺪول‪.‬‬

‫ﻣﺜﺎل‪:‬‬ ‫إذا ﻛﺎن ﻟﺪﯾﻨﺎ ‪ x‬ﻣﻤﺜﻞ ﺑﺎﻟﻨﻈﺎم اﻟﺜﻨﺎﺋﻲ‬ ‫‪0‬‬

‫‪1‬‬

‫‪0‬‬

‫‪1‬‬

‫‪1‬‬

‫‪0‬‬

‫‪0‬‬

‫‪0‬‬

‫اﻟﻤﻄﻠﻮب أوﺟﺪ ﻗﯿﻤﺔ ﻧﻔﻲ ‪ x‬أي ‪~x‬‬ ‫اﻟﺠﻮاب‪:‬‬ ‫‪0 1‬‬

‫‪1‬‬

‫‪0‬‬

‫‪0‬‬

‫‪1‬‬

‫‪1‬‬

‫‪1‬‬

‫اﻷدوات اﻟﺸﺮﻃﯿﺔ‪:‬‬ ‫وھﻲ ﻋﺒﺎرة ﻋﻦ ﺣﻠﻘﺎت ﺗﺘﻢ ﺿﻤﻦ ﺷﺮوط ﯾﺤﺪدھﺎ ﻛﺎﺗﺐ اﻟﺒﺮﻧﺎﻣﺞ‬ ‫اﻷدوات اﻟﺸﺮﻃﯿﺔ اﻷوﻟﯿﺔ ‪conditional operator‬‬ ‫اﻟﺸﻜﻞ اﻟﻨﻤﻮذﺟﻲ ھﻮ ‪:‬‬ ‫وﯾﻜﻮن اﻟﺠﻮاب‬

‫‪Expression ? Expression1 : Expression2‬‬ ‫‪(a>b)? c : d‬‬ ‫إن ﻛﺎن اﻟﺸﺮط ﺻﺤﯿﺢ ﻓﺎﺧﺘﺮ اﻟﻘﯿﻤﺔ ‪c‬‬ ‫وإن ﻛﺎن اﻟﺸﺮط ﻏﯿﺮ ﺻﺤﯿﺢ ﻓﺎﺧﺘﺮ ‪d‬‬

‫ﻣﺜﺎل ‪:‬‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﻟﺤﺴﺎب اﻟﻘﯿﻤﺔ اﻟﻤﻄﻠﻘﺔ ﻟﻠﻤﺘﺤﻮل ‪ y‬ﺑﺎﺳﺘﺨﺪام اﻟﻤﻌﺎﻣﻞ اﻟﺸﺮﻃﻲ ) اﻷداة اﻟﺸﺮﻃﯿﺔ (‬ ‫و اﻟﻤﻌﺮﻓﺔ ﺑﺎﻟﻌﻼﻗﺔ اﻟﺘﺎﻟﯿﺔ‬ ‫‪ y‬ﺗﺴﺎوي ‪ x‬ﺑﺎﻟﻘﯿﻤﺔ اﻟﻤﻄﻠﻘﺔ‬ ‫أي أن ‪ y = x‬ﻋﻨﺪﻣﺎ ‪x>0‬‬ ‫‪x>0‬‬ ‫‪y= -x‬ﻋﻨﺪﻣﺎ‬ ‫و‬ ‫اﻟﺤﻞ‪:‬‬ ‫>‪#include < iostream.h‬‬ ‫) ( ‪void main‬‬ ‫}‬ ‫‪٩‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

int y, x; cin>> x; y =(x >= 0)? x :-x; cout<< y; {

if – else ‫اﻟﺠﻤﻠﺔ اﻟﺸﺮﻃﯿﺔ‬ : ‫ﺷﻜﻠﮭﺎ اﻟﻌﺎم‬ if(Expression) Expression1 else Expression2 ‫ﻣﺜﺎل ﺗﻮﺿﯿﺤﻲ‬ if(b < c) a = b; else a = c; ‫ﻣﺜﺎل آﺧﺮ‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﻹدﺧﺎل ﻃﻮﻟﻚ و ﻃﻮل زﻣﯿﻠﻚ ﺑﺸﺮط‬ ‫إن ﻛﺎن ﻃﻮﻟﻚ أﻛﺒﺮ ﻣﻦ ﻃﻮل زﻣﯿﻠﻚ ﻋﻨﺪﺋﺬ ﻗﻢ ﺑﻄﺒﺎﻋﺔ ﻃﻮﻟﻚ وﻣﻦ ﺛﻢ ﻗﻢ ﺑﺤﺴﺎب ﻣﻌﺪل اﻷﻃﻮال‬ ‫ﺛﻢ أﻃﺒﻌﮫ وإﻻ ﻓﺄﻃﺒﻊ ﻃﻮل زﻣﯿﻠﻚ وأﻃﺒﻊ ﺿﻌﻒ ﻃﻮﻟﮫ وأﻃﺒﻊ ﻧﺼﻒ ﻃﻮﻟﮫ‬ :‫اﻟﺤﻞ‬ #include void main() { int z,y; cout<< "Inter your long"<<endl; cin>> z ; cout<< "Inter friend's long"<<endl; cin>> y; cout<< "--------------------"<<endl; if(z>y) { cout<< "You longer"<<endl; cout<< "Your long is "<
cout<< (z+y)/2; } else { cout<<" Your friend is longer"<<endl; cout<<" He's long is "<> z; cout<<"enter scanned number"<<endl; cin>>y; if(z>y) cout<<"the large number is first"<
(switch – case) ‫ﺣﻠﻘﺔ‬ ‫ﺷﻜﻠﮭﺎ اﻟﻌﺎم‬ switch(Expression) ١١ PDF created with pdfFactory Pro trial version www.pdffactory.com

{ case constant 1 : statement 1; break; case constant 2 : statement 2; break; . . . case constant x : statement x; break; default : statement; break; } : ‫ﻣﻼﺣﻈﺔ‬ ‫ ﻗﯿﻤﺔ ﺻﺤﯿﺤﺔ‬switch ‫ اﻟﻤﻌﺮف ﻓﻲ‬Expression ‫ﯾﺠﺐ أن ﯾﻜﻮن‬ :‫ﻣﺜﺎل‬ #include void main() { int x; cin>> x; switch (x) { case 1: cout<<" welcome"<<endl; break; case 2: cout<<" hello student"<<endl; break; case 3: cout<<" introdaction to c++"<<endl; break; default: cout<< "bye bye"; } }

:‫ﻣﺜﺎل آﺧﺮ‬

١٢ PDF created with pdfFactory Pro trial version www.pdffactory.com

‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻄﺒﻊ اﺳﻢ اﻟﻜﻮﻛﺐ ﻓﻲ اﻟﻤﺠﻤﻮﻋﺔ اﻟﺸﻤﺴﯿﺔ ﻣﺮﺗﺒﺎً ﻣﻦ اﻷﻗﺮب إﻟﻰ اﻟﺸﻤﺲ ﺑﺤﯿﺚ‬ ‫ﻧﺪﺧﻞ إﻟﻰ اﻟﺒﺮﻧﺎﻣﺞ رﻗﻢ اﻟﻜﻮﻛﺐ ﻓﯿﻄﺒﻊ اﻟﺒﺮﻧﺎﻣﺞ اﺳﻢ اﻟﻜﻮﻛﺐ‬ ‫اﻟﺤﻞ‬ #include void main() { int x; cout<< " inter number a planet "<< endl; cin>> x; switch (x) { case 1: cout<<" its a Mercury"<<endl; break; case 2: cout<<" its a Venus"<<endl; break; case 3: cout<<" its a earth"<<endl; break; case 4: cout<<" its a Mars"<<endl; break; case 5: cout<<" its a Jupiter"<<endl; break; case 6: cout<<" its a Saturn"<<endl; break; case 7: cout<<" its a URANUS"<<endl; break; case 8: cout<<" its a NEPTUNE"<<endl; break; case 9: cout<<" its a PLUTO"<<endl; break; case 10: cout<<" its a senda "<<endl; break; ١٣ PDF created with pdfFactory Pro trial version www.pdffactory.com

default: cout<< "err no plant to resemble this number"<<endl; break; } }

:‫ﻣﺜﺎل آﺧﺮ‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﺤﻮل ﻣﻦ اﻷرﻗﺎم اﻟﻌﺮﺑﯿﺔ اﻷﺻﻞ إﻟﻰ اﻷرﻗﺎم اﻟﻤﻮاﻓﻘﺔ ﻟﮭﺎ ﺑﺎﻟﺮوﻣﺎﻧﯿﺔ‬ :‫اﻟﺤﻞ‬ #include main() { int x; cout<<"enter the decimal number:"; cin>> x; while(x<=12) {switch(x) { case 1: {cout<<"I"<<endl; cout<<"enter the decimal number:";} break; case 2: {cout<<"II"<<endl; cout<<"enter the decimal number:";} break; case 3: {cout<<"III"<<endl; cout<<"enter the decimal number:";} break; case 4: {cout<<"IV"<<endl; cout <<"enter the decimal number:";} break; case 5: {cout<<"V"<<endl; cout<<"enter the decimal number:";} break; case 6: {cout<<"VI"<<endl; cout<<"enter the decimal number:";}

١٤ PDF created with pdfFactory Pro trial version www.pdffactory.com

break; case 7: {cout<<"VII"<<endl; cout<<"enter the decimal number:";} break; case 8: {cout<<"VIII"<<endl; cout<<"enter the decimal number";} break; case 9: {cout<<"IX"<<endl; cout<<"enter the decimal number";} break; case 10: {cout<<"XI"<<endl; cout<<"enter the decimal number";} break; case 11: {cout<<"XII"<<endl; cout<<"enter the decimal number";} break; case 12: {cout<<"XIII"<<endl; cout<<"enter the decimal number:";} break; default: cout<<"NOOOO"; break; } cin>>x; } }

١٥ PDF created with pdfFactory Pro trial version www.pdffactory.com

‫ﺣﻠﻘﺔ ‪for‬‬ ‫ﺷﻜﻠﮭﺎ اﻟﻌﺎم‬ ‫) ‪For ( initial value ; coalition ; increment‬‬ ‫;‪Statements‬‬ ‫اﻟﺸﺮح ‪ :‬ﻋﺒﺎرة ﻋﻦ ﺷﺮط ﯾﻘﯿﺪ ﺣﺮﻛﺔ ‪ for‬وﻏﺎﻟﺒﺎً ﻣﺎ ﯾﺤﻮي ﻗﯿﻤﺔ ﻧﮭﺎﺋﯿﺔ‬ ‫وھﺬا اﻟﺸﺮط ﯾﻜﺘﺐ ﻓﯿﮫ اﻟﻘﯿﻤﺔ اﻻﺑﺘﺪاﺋﯿﺔ و اﻟﻘﯿﻤﺔ اﻟﻨﮭﺎﺋﯿﺔ و ﻗﯿﻤﺔ اﻟﺰﯾﺎدة ﻟﻠﻤﺘﺤﻮل ‪.‬‬ ‫ﻣﺜﺎل‪:‬‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻄﺒﻊ ھﺬه اﻹﺷﺎرة * ﻋﺸﺮ ﻣﺮات ﻣﺘﺘﺎﻟﯿﺔ‬ ‫>‪#include
‫>‪#include ‪#include ‪#include
‫{‬ ‫;‪cout<
‫ﺣﻠﻘﺔ ‪while‬‬ ‫ﺷﻜﻠﮭﺎ اﻟﻌﺎم‬ ‫)‪while(condition‬‬ ‫; ‪Statement‬‬ ‫ﻣﻼﺣﻈﺔ ‪:‬‬ ‫ﯾﺠﺐ إﺿﺎﻓﺔ ﺗﻌﻠﯿﻤﺔ ﻓﻲ ﻧﮭﺎﯾﺔ ھﺬه اﻟﺤﻠﻘﺔ ﻟﺘﺴﻤﺢ اﻟﻌﻮدة إﻟﻰ ﺑﺪاﯾﺔ اﻟﺒﺮﻧﺎﻣﺞ ﺣﺘﻰ ﯾﺘﻢ ﻓﺤﺺ اﻟﺸﺮط‬ ‫ﻣﺮة أﺧﺮى‬ ‫ﻣﺜﺎل‪:‬‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم اﻟﻤﺴﺘﺨﺪم ﺑﺈدﺧﺎل ﻋﺪد ﻣﺎ وﻣﻦ ﺛﻢ ﯾﻔﺤﺺ اﻟﺒﺮﻧﺎﻣﺞ ھﺬا اﻟﻌﺪد ﻟﻤﻌﺮﻓﺔ ﻓﯿﻤﺎ إذا‬ ‫ﻛﺎن ھﺬا اﻟﻌﺪد أوﻟﻲ أم ﻻ‬ ‫اﻟﺤﻞ‪:‬‬ ‫>‪#include > x‬‬ ‫;‪i=2‬‬ ‫)‪while (x>i‬‬ ‫{‬ ‫)‪if (x%i==0‬‬ ‫{‬ ‫‪cout<<"the number in not prime";// make i right any thing‬‬ ‫‪١٨‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫;‪i=x+1‬‬ ‫}‬ ‫‪else‬‬ ‫)‪if(x % i !=0‬‬ ‫;‪i++‬‬ ‫}‬ ‫)‪if(x==i‬‬ ‫;"‪cout<<"the number is prime‬‬ ‫}‬

‫ﻣﺜﺎل آﺧﺮ‪:‬‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻄﻠﺐ ﻣﻦ اﻟﻤﺴﺘﺨﺪم إدﺧﺎل ﻗﯿﻤﺔ ﻋﺪدﯾﺔ ﻣﺎ‬ ‫وﻃﺎﻟﻤﺎ ﻛﺎﻧﺖ ھﺬه اﻟﻘﯿﻤﺔ ﻣﻮﺟﺒﺔ ﻓﯿﻄﺒﻊ اﻟﺒﺮﻧﺎﻣﺞ ھﺬه اﻹﺷﺎرة * ﻋﻠﻰ ﺳﻄﺮ ﺟﺪﯾﺪ ‪.‬‬ ‫اﻟﺤﻞ‬ ‫>‪#include > x‬‬ ‫)‪while (x>0‬‬ ‫{‬ ‫;‪cout<<"*"<<endl‬‬ ‫;‪cin>>x‬‬ ‫}‬ ‫}‬

‫وھﻨﺎ ﯾﺒﻘﻰ اﻟﺒﺮﻧﺎﻣﺞ ﻓﻲ اﻟﺤﻠﻘﺔ وﻻ ﯾﺨﺮج ﻣﻨﮭﺎ إﻻ ﺑﻜﺘﺎﺑﺔ ﻗﯿﻤﺔ ﺳﺎﻟﺒﺔ‬ ‫أﻣﺎ إن اردﻧﺎ أن ﯾﺨﺮج اﻟﺒﺮﻧﺎﻣﺞ ﺑﻌﺪ أول ﻣﺮة ﻓﻨﻜﺘﺐ ھﺬه اﻟﻌﺒﺎرة‬ ‫;‪x=x-x‬‬ ‫ﺑﺪﻻً ﻣﻦ اﻟﻌﺒﺎرة‬ ‫;‪Cin>>x‬‬

‫‪١٩‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫ﺣﻠﻘﺔ ‪do while‬‬ ‫اﻟﺸﻜﻞ اﻟﻌﺎم‬ ‫‪Do‬‬ ‫{‬ ‫‪Statement‬‬ ‫;)‪}while (condition‬‬ ‫اﻟﺸﺮح‪:‬‬ ‫اﺑﻖ ﺿﻤﻦ اﻟﺤﻠﻘﺔ ‪ do‬ﺣﺘﻰ ﯾﺘﻢ ﺗﻨﻔﯿﺬ اﻟﺸﺮط )‪while (condition‬‬ ‫ﻣﻼﺣﻈﺔ ھﺎﻣﺔ ﺟﺪًا‬ ‫اﻟﻔﺮق اﻟﺠﻮھﺮي ﺑﯿﻦ اﻟﺤﻠﻘﺔ ‪ do while‬و اﻟﺤﻠﻘﺔ ‪while‬‬ ‫ھﻮ أن اﻟﺒﺮﻧﺎﻣﺞ ﻓﻲ اﻟﺤﻠﻘﺔ ‪ do while‬ﯾﻨﻔﺬ ﻣﺮة واﺣﺪة ﻋﻠﻰ اﻷﻗﻞ‬

‫ﻣﺜﺎل‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﺠﻤﻊ اﻷﻋﺪاد اﻟﻤﺪﺧﻠﺔ وﻣﻦ ﺛﻢ ﯾﺘﻮﻗﻒ ﻋﻦ اﻟﻌﻤﻞ وﯾﻘﻮم ﺑﻄﺒﺎﻋﺘﮭﺎ ﺑﻤﺠﺮد إدﺧﺎل‬ ‫اﻟﺮﻗﻢ ﺻﻔﺮ‪.‬‬ ‫>‪#include >x‬‬ ‫;‪sum+=x‬‬ ‫;)‪}while (x!=0‬‬ ‫;‪cout<<"sum of number is ="<<sum‬‬ ‫}‬

‫ﻣﻼﺣﻈﺔ ‪:‬‬ ‫إن اﻟﻌﺒﺎرة ‪ sum+=x‬ﺗﻜﺎﻓﺊ و ﺗﺴﺎوي اﻟﻌﺒﺎرة ‪sum = sum + x‬‬ ‫ﻣﻼﺣﻈﺔ أﺧﺮى‬ ‫ﯾﻤﻜﻦ ﻛﺘﺎﺑﺔ ھﺬا اﻟﺒﺮﻧﺎﻣﺞ ﺑﺎﻟﺤﻠﻘﺔ ‪ while‬ﻓﻘﻂ ﻋﻠﻰ اﻟﻨﺤﻮ اﻟﺘﺎﻟﻲ‬ ‫‪٢٠‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫>‪#include >x‬‬ ‫)‪While ( x!=0‬‬ ‫{‬ ‫;‪Sum+=x‬‬ ‫;‪Cin>>x‬‬ ‫}‬

‫ﺣﻠﻘﺔ اﻹﯾﻘﺎف ‪break‬‬ ‫وﻇﯿﻔﺘﮭﺎ ‪ :‬إﯾﻘﺎف ﺑﻨﯿﺔ أو ﺣﻠﻘﺔ ﺗﻜﺮار ﻋﻨﺪ ﺗﺤﻘﻖ ﺷﺮط أو ﺷﺮوط ﻣﻌﯿﻨﺔ ‪.‬‬ ‫ﻋﻨﺪ ﺗﻨﻔﯿﺬ ھﺬه اﻟﺘﻌﻠﯿﻤﺔ ﯾﺘﻢ اﻟﻘﻔﺰ إﻟﻰ ﺳﻠﺴﻠﺔ اﻟﺠﻤﻞ اﻟﺘﺎﻟﯿﺔ ﻟﻠﺒﻨﯿﺔ أو ﺣﻠﻘﺔ اﻟﺘﻜﺮار‪.‬‬ ‫ﻣﺜﺎل‪:‬‬ ‫>‪#include
‫ﺣﻠﻘﺔ اﻻﺳﺘﻤﺮار ‪continue‬‬ ‫ﺗﻌﻤﻞ ھﺬه اﻟﺤﻠﻘﺔ ﻋﻠﻰ ﺗﺠﺎوز ﺗﻨﻔﯿﺬ ﺑﻘﯿﺔ اﻟﺘﻌﻠﯿﻤﺎت ﻓﻲ اﻟﺘﻜﺮار ﺧﻼل اﻟﺪورة اﻟﺤﺎﻟﯿﺔ و اﻻﻧﺘﻘﺎل إﻟﻰ‬ ‫اﻟﺪورة اﻟﺘﺎﻟﯿﺔ ‪.‬‬ ‫ﻣﺜﺎل‪:‬‬ ‫>‪#include
‫)‪for(int i=1; i<=100; i++‬‬ ‫{‬ ‫)‪if(i==10‬‬ ‫;‪continue‬‬ ‫;‪cout<< i <<endl‬‬ ‫}‬ ‫}‬

‫ﻣﺜﺎل آﺧﺮ‪:‬‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﻄﺒﺎﻋﺔ ﺟﻤﯿﻊ اﻷرﻗﺎم اﻟﻮاﻗﻌﺔ ﺑﯿﻦ )‪ (١‬إﻟﻰ )‪ (١٠٠‬اﻟﺘﻲ ﺗﻘﺒﻞ اﻟﻘﺴﻤﺔ ﻋﻠﻰ‬ ‫اﻷﻋﺪاد اﻟﺘﺎﻟﯿﺔ ) ‪٢‬و‪٤‬و‪. (٦‬‬ ‫>‪#include
‫ﻃﺮﯾﻘﺔ أﺧﺮى أﺳﮭﻞ‪:‬‬ ‫>‪#include
‫ﺗﻌﻠﯿﻤﺔ اﻻﻧﺘﻘﺎل ‪goto‬‬ ‫ﺗﻔﯿﺪ ھﺬه اﻟﻌﻤﻠﯿﺔ ﻓﻲ اﻻﻧﺘﻘﺎل ﻣﻦ ﺳﻄﺮ ﺑﺮﻣﺠﻲ إﻟﻰ آﺧﺮ ﻣﻊ ﺗﺠﺎوز ﻟﺒﻌﺾ اﻷﺳﻄﺮ اﻟﺒﺮﻣﺠﯿﺔ ﻣﻦ‬ ‫أﺟﻞ ﺗﻨﻔﯿﺬ أﻣﺮ ﻣﻌﯿﻦ‪.‬‬ ‫ﻣﺜﺎل‪:‬‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﺈدﺧﺎل رﻗﻢ ﻣﺎ ﻓﺈن ﻛﺎن اﻟﺮﻗﻢ أﺻﻐﺮ ﻣﻦ ﻋﺸﺮة ﻓﯿﻄﺒﻊ اﻟﺒﺮﻧﺎﻣﺞ اﻟﻌﺒﺎرة اﻟﺘﺎﻟﯿﺔ‬ ‫‪The number is less ten‬‬ ‫أﻣﺎ إن ﻛﺎن اﻟﺮﻗﻢ أﻛﺒﺮ أو ﯾﺴﺎوي اﻟﻌﺸﺮة ﻓﯿﻄﺒﻊ اﻟﺒﺮﻧﺎﻣﺞ اﻟﻌﺒﺎرة‬ ‫‪٢٢‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫‪The number is equal or beggar ten‬‬

‫>‪#include >x‬‬ ‫)‪if(x<10‬‬ ‫;‪goto one‬‬ ‫;‪else goto two‬‬ ‫;"‪one: cout<<"the number is less ten‬‬ ‫};"‪two: cout<<"the number is equal or beggar ten‬‬

‫وﻟﻜﻦ ﻧﺠﺪ أن اﻟﺒﺮﻧﺎﻣﺞ ﯾﻌﻄﻲ اﻟﻌﺒﺎرﺗﯿﻦ ﻣﻌﺎً إن ﻛﺎن اﻟﺮﻗﻢ اﻟﻤﺪﺧﻞ أﺻﻐﺮ ﻣﻦ ﻋﺸﺮة‬ ‫وھﺬا اﻟﺨﻄﺄ ﺳﺒﺒﮫ‪:‬‬ ‫اﻟﺒﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﺘﻨﻔﯿﺬ اﻟﺘﻌﻠﯿﻤﺎت ﺳﻄﺮ ﺳﻄﺮ وﺑﻤﺎ أن اﻟﻌﺒﺎرة اﻟﺜﺎﻧﯿﺔ ﺟﺎءت ﺗﻤﺎﻣﺎً ﺑﻌﺪ اﻟﻌﺒﺎرة اﻷوﻟﻰ‬ ‫) ﻓﻲ اﻟﺴﻄﺮ اﻟﺘﺎﻟﻲ ﻟﮭﺎ ( ﻓﻨﻔﺬت ﺑﻄﺒﯿﻌﺔ اﻟﺤﺎل‪.‬‬

‫ﺗﺪرﯾﺒﺎت‪:‬‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم اﻟﻤﺴﺘﺨﺪم ﺑﮫ ﺑﺈدﺧﺎل ﻋﺪة أﻋﺪاد ‪ / n /‬وﯾﻘﻮم ﺑﺠﻤﻌﮭﺎ ﺑﺤﯿﺚ ﯾﺤﺪد اﻟﻤﺴﺘﺨﺪم‬ ‫ﻋﺪد اﻷﻋﺪاد ‪ /n/‬ﻗﺒﻞ إدﺧﺎﻟﮭﺎ ؟‬ ‫اﺧﺘﺮ اﻟﻄﺮﯾﻘﺔ اﻟﻤﻨﺎﺳﺒﺔ ﻟﺤﻞ ھﺬا اﻟﻤﺜﺎل؟‬ ‫اﻟﺤﻞ اﻟﻄﺮﯾﻘﺔ اﻷوﻟﻰ ﺑﺎﺳﺘﺨﺪام اﻟﺤﻠﻘﺔ ‪do while‬‬ ‫>‪#include >n‬‬ ‫‪do‬‬ ‫{‬ ‫;‪cin>>x‬‬ ‫;‪sum+=x‬‬ ‫;‪n--‬‬ ‫}‬ ‫;)‪while(n!=0‬‬ ‫;‪cout<<"the sum of number is "<<sum‬‬ ‫‪٢٣‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

}

while ‫اﻟﻄﺮﯾﻘﺔ اﻟﺜﺎﻧﯿﺔ ﺑﺎﺳﺘﺨﺪام اﻟﺤﻠﻘﺔ‬ #include void main() { int n,count=1,num,sum=0; cout<<"enter n"; cin>>n; while(count<=n) { cin>>num; sum+=num; count++; } cout <<"the sum of number is"<<sum; }

for ‫اﻟﻄﺮﯾﻘﺔ اﻟﺜﺎﻟﺜﺔ ﺑﺎﺳﺘﺨﺪام اﻟﺤﻠﻘﺔ‬ #include void main() { int sum=0,x,i,n; cout<<"enter n"<<endl; cin>>n; for(i=1; i<=n ; i++) { cout<<"enter namber"<<endl; cin>>x; sum=sum+x; } cout<<" the sum of number is = "; cout<<sum<<endl; }

‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻤﺜﻞ آﻟﺔ ﺣﺎﺳﺒﺔ ﺑﺴﯿﻄﺔ ﺗﺪﺧﻞ ﻋﺪدﯾﻦ ﻓﻘﻂ ﺑﺤﯿﺚ ﻧﺨﺘﺎر ﻋﻤﻠﯿﺔ ﻣﻦ اﻟﻌﻤﻠﯿﺎت اﻟﺤﺴﺎﺑﯿﺔ‬ ‫ ﻓﯿﻌﻄﯿﻨﺎ اﻟﺒﺮﻧﺎﻣﺞ اﻟﺠﻮاب‬٢+١ ‫اﻷرﺑﻌﺔ ﻣﺜﻞ أن ﻧﻜﺘﺐ‬ ‫وﻧﺎﻗﺶ ﺣﺎﻟﺔ اﻟﻘﺴﻤﺔ ﻋﻠﻰ ﺻﻔﺮ؟‬ #include void main() { int x,y; ٢٤ PDF created with pdfFactory Pro trial version www.pdffactory.com

char r; cin>> x; cin>> r; cin>> y; switch(r) { case '+': cout<<x+y; break; case '-': cout<<x-y; break; case '*': cout<<x*y; break; case '/': if(y==0) cout<<"error"; else cout<<x/y; break; default: cout<<" chose the right operate";}}

‫ﻣﺜﺎل ﻣﻤﺎﺛﻞ وﻟﻜﻦ ﺑﺘﻄﻮﯾﺮ أﻛﺜﺮ‬ ‫اﻛﺘﺐ ﻧﻔﺲ اﻟﺒﺮﻧﺎﻣﺞ اﻟﺴﺎﺑﻖ وﻟﻜﻦ اﺳﻤﺢ ﻟﻠﺒﺮﻧﺎﻣﺞ ﺑﺄن ﯾﺪﺧﻞ اﻟﻌﻤﻠﯿﺎت أﻛﺜﺮ ﻣﻦ ﻣﺮة‬ #include void main() { int x,y,n,count; char r; cout<<" enter how many play program"<<endl; cin>> n; cin>> x; cin>> r; cin>> y; while(n!=0) { n--; switch(r) { case '+':

٢٥ PDF created with pdfFactory Pro trial version www.pdffactory.com

{ cout<<"the score is = "<<x+y<<endl; cout<<"----------------------"<<endl; } break; case '-': { cout<<"the score is = "<<x-y<<endl; cout<<"----------------------"<<endl; } break; case '*': { cout<<"the score is = "<<x*y<<endl; cout<<"----------------------"<<endl; } break; case '/': { cout<<"the score is = "; if(y==0) cout<<"error"<<endl; else cout<<x/y<<endl; cout<<"----------------------"<<endl; } break; default: { cout<<" chose the right operate"<<endl; cout<<"----------------------"<<endl; } } cin>> x; cin>> r; cin>> y; } } :‫ﻣﺜﺎل‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﻄﺒﺎﻋﺔ اﻟﺸﻜﻞ اﻟﺘﺎﻟﻲ‬ 1 12 123 ٢٦ PDF created with pdfFactory Pro trial version www.pdffactory.com

‫‪1234‬‬ ‫‪12345‬‬ ‫اﻟﺤﻞ‪:‬‬ ‫ﺑﺎﺳﺘﺨﺪام ﻋﻼﻗﺔ رﯾﺎﺿﯿﺔ أي ﯾﻜﻮن اﻟﺨﺮج رﻗﻤﺎً وﻟﯿﺲ ﺷﻜﻞ ھﻮ ﻛﺎﻵﺗﻲ‪:‬‬ ‫>‪#include
‫أﻣﺎ ﻛﺘﺎﺑﺔ اﻟﺒﺮﻧﺎﻣﺞ ﺑﻄﺮﯾﻘﺔ ﯾﻌﻄﯿﻨﺎ اﻟﺨﺮج ﺷﻜﻼً وﻟﯿﺲ رﻗﻢ ﻓﮭﻮ‪:‬‬ ‫>‪#include>x;cin>>y‬ﻋﻠﻰ ﺳﻄﺮ واﺣﺪ وھﻜﺬا ‪.‬‬

‫ﻣﺜﺎل‪:‬‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﺤﺴﺎب !‪ n‬اﻟﻌﺎﻣﻠﻲ ﻷي رﻗﻢ‬ ‫اﻟﺤﻞ ‪:‬‬ ‫‪٢٧‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

while ‫ﺑﺎﺳﺘﺨﺪام اﻟﺤﻠﻘﺔ‬ #include void main() { int x,y,fact=1; cout<<" this program is for factorial calculation"; cout<<"\n ------------------------------------------"; cout<<"\n enter x"<<endl ; cin>>x; y=1; cout<<x<<"! = "; while(y<=x) { fact=fact*y; y++; } cout< void main() { int i,n,fact=1; cout<<" this program is for factorial calculation"; cout<<"\n copy right 2006"; cout<<"\n designed by hindawi"; cout<<"\n ------------------------------------------"; cout<<"\n enter x"<<endl ; cin>>n; cout<
٢٨ PDF created with pdfFactory Pro trial version www.pdffactory.com

‫ﺳﻠﺴﻠﺔ ﻓﯿﺒﻮﻧﺎﺷﻲ ‪Fibonacci Series‬‬ ‫ھﻲ ﻋﺒﺎرة ﻋﻦ ﺳﻠﺴﻠﺔ أﻋﺪاد ﯾﻮن أي رﻗﻢ ﻓﯿﮭﺎ ﯾﺴﺎوي ﻣﺠﻤﻮع اﻟﺮﻗﻤﯿﻦ اﻟﺴﺎﺑﻘﯿﻦ ﻟﮫ و ﯾﻜﻮن اﻟﺮﻗﻢ اﻷول ﻣﻨﮭﺎ و‬ ‫اﻟﺜﺎﻧﻲ ﯾﺴﺎوي اﻟﻮاﺣﺪ ‪.‬‬ ‫أي ﻛﻤﺎ ﻓﻲ ھﺬه اﻟﺴﻠﺴﻠﺔ‬ ‫‪1 1 2 3 5 8 13 21 ……………..n‬‬ ‫ﻣﺜﺎل‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﺤﺴﺎب اﻟﻌﻨﺎﺻﺮ اﻟﻌﺸﺮة اﻷوﻟﻰ ﻣﻦ ﺳﻠﺴﻠﺔ ﻓﯿﺒﻮﻧﺎﺷﻲ‬ ‫>‪#include
‫‪٢٩‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

} :‫ﻣﺜﺎل آﺧﺮ‬ n ‫أوﺟﺪ ﺣﺪود ﻓﯿﺒﻮﻧﺎﺷﻲ ﻣﻦ أﺟﻞ‬ :‫اﻟﺤﻞ‬ #include void main() { int a,b,c,i,n; char s=' '; a=b=1; cin>>n; cout< void main() { int a,b,c,d,e; char s= ' '; a=1; b=2; c=3; d=4; e=5; for(int i=1;i<=5;i++) { cout<
٣٠ PDF created with pdfFactory Pro trial version www.pdffactory.com

a=b; b=c; c=d; d=e; e=a; e--; } } :‫ﻣﺜﺎل‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﻄﺒﺎﻋﺔ اﻟﺸﻜﻞ اﻟﺘﺎﻟﻲ ﻋﻠﻰ اﻟﺸﺎﺷﺔ‬ A.1 A.2 A.3 A.4 A.5 B.1 B.2 B.3 B.4 B.5 C.1 C.2 C.3 C.4 C.5 D.1 D.2 D.3 D.4 D.5 E.1 E.2 E.3 E.4 E.5 ‫اﻟﺤﻞ‬ #include void main() { char A,B,C,D,E,y,s,n; s='.'; n=' '; for(y='A';y<='E';y++) { cout<<endl; for(int i=1;i<=5;i++) cout<2>4>8>16>32> >>>>>>>>>> ‫اﻟﺤﻞ‬ #include void main() { float a,b,n; char s=' '; a=b=1; cin>>n;

٣١ PDF created with pdfFactory Pro trial version www.pdffactory.com

‫;‪cout<
‫اﻟﻤﺼﻔﻮﻓﺎت‬

‫‪Arrays‬‬

‫اﻟﻤﺼﻔﻮﻓﺔ ھﻲ ﻋﺒﺎرة ﻋﻦ ﻣﺠﻤﻮﻋﺔ ﻣﻦ اﻟﺒﯿﺎﻧﺎت اﻟﺘﻲ ﺗﺘﺸﺎﺑﮫ ﻓﻲ اﻟﻨﻮع ‪double , int , char , float‬‬ ‫وﻟﮭﺎ اﺳﻢ ﻣﺸﺘﺮك ‪.‬‬ ‫ﺗﺘﺸﺎﺑﮫ ﻓﻲ اﻟﻨﻮع ‪ :‬ﯾﻌﻨﻲ أن ﺗﻜﻮن اﻟﺒﯿﺎﻧﺎت اﻟﺘﻲ ﺗﺨﺰﻧﮭﺎ اﻟﻤﺼﻔﻮﻓﺔ ﻣﺜﻼً ﻛﻠﮭﺎ أﻋﺪاد ﺻﺤﯿﺤﺔ‪.‬‬ ‫ﻟﮭﺎ اﺳﻢ ﻣﺸﺘﺮك ‪ :‬ﯾﻌﺒﺮ ﻋﻦ ھﺬه اﻟﺒﯿﺎﻧﺎت ﺑﺎﺳﻢ اﻟﻤﺼﻔﻮﻓﺔ‬

‫اﻟﺘﺼﺮﯾﺢ ﻋﻦ اﻟﻤﺼﻔﻮﻓﺔ‪:‬‬ ‫ھﻨﺎك ﺛﻼﺛﺔ أﺷﯿﺎء ﯾﺠﺐ أن ﺗﺄﺧﺬ ﺑﻌﯿﻦ اﻻﻋﺘﺒﺎر ﻋﻨﺪ اﻟﺘﺼﺮﯾﺢ ﻋﻦ اﻟﻤﺼﻔﻮﻓﺔ‬ ‫اﺳﻢ اﻟﻤﺼﻔﻮﻓﺔ ‪ :‬وھﻮ اﺳﻢ ﻧﺨﺘﺎره ﻣﺜﻠﻤﺎ ﻧﺨﺘﺎر اﺳﻢ أي ﻣﺘﻐﯿﺮ‪.‬‬ ‫ﻋﺪد اﻟﻌﻨﺎﺻﺮ اﻟﺘﻲ ﺑﺪاﺧﻠﮭﺎ‪.‬‬ ‫ﻧﻮع اﻟﺒﯿﺎﻧﺎت اﻟﻤﺴﺘﺨﺪﻣﺔ ﻓﯿﮭﺎ‪.‬‬ ‫وﯾﻤﻜﻦ أن ﺗﻜﺘﺐ ﻛﻤﺎ ﻓﻲ ھﺬه اﻷﻣﺜﻠﺔ‪:‬‬ ‫;}‪Int mark [5] ={1,3,2,4,6‬‬ ‫;}‪Int mark [ ] = {5,2,1‬‬

‫‪٣٢‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫;]‪Int mark [5‬‬ ‫ﺣﯿﺚ‪:‬‬ ‫ﻧﻮع اﻟﺒﯿﺎﻧﺎت اﻟﺘﻲ ﺑﺪاﺧﻞ اﻟﻤﺼﻔﻮﻓﺔ‬ ‫‪int‬‬ ‫اﺳﻢ اﻟﻤﺼﻔﻮﻓﺔ‬ ‫‪mark‬‬ ‫ﻋﺪد اﻟﻌﻨﺎﺻﺮ داﺧﻞ اﻟﻤﺼﻔﻮﻓﺔ‬ ‫]‪[5‬‬ ‫ﻋﻠﻤﺎً أﻧﮫ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺎت ﯾﺒﺪأ ﺗﺮﻗﯿﻢ اﻟﻌﻨﺎﺻﺮ ) ﺧﺎﻧﺎت اﻟﻤﺼﻔﻮﻓﺔ ( وﻟﯿﺲ ﻗﯿﻤﺘﮭﺎ ﺑﺎﻟﺮﻗﻢ ﺻﻔﺮ‬ ‫أي وﺑﺸﻜﻞ أوﺿﺢ‪:‬‬ ‫ﻣﺼﻔﻮﻓﺔ ﺗﺘﺄﻟﻒ ﻣﻦ ﺳﺘﺔ ﻋﻨﺎﺻﺮ ﻓﺈﻧﮭﺎ ﺗﺘﻤﺜﻞ ﺑﺎﻟﺸﻜﻞ‬ ‫اﻟﻌﻨﺼﺮ ‪٥‬‬

‫اﻟﻌﻨﺼﺮ ‪٤‬‬

‫اﻟﻌﻨﺼﺮ ‪٣‬‬

‫اﻟﻌﻨﺼﺮ ‪٢‬‬

‫اﻟﻌﻨﺼﺮ ‪١‬‬

‫اﻟﻌﻨﺼﺮ ‪٠‬‬

‫ﻗﯿﻤﺔ‬

‫ﻗﯿﻤﺔ‬

‫ﻗﯿﻤﺔ‬

‫ﻗﯿﻤﺔ‬

‫ﻗﯿﻤﺔ‬

‫ﻗﯿﻤﺔ‬

‫وﻣﻦ ھﺬا اﻟﺸﻜﻞ ﻧﺴﺘﻨﺘﺞ ﺑﺄن اﻟﻤﺼﻔﻮﻓﺔ ﯾﺒﺪأ ﺗﺮﻗﯿﻤﮭﺎ ﻣﻦ اﻟﺼﻔﺮ‬

‫ﻛﯿﻔﯿﺔ إدﺧﺎل ﻋﻨﺎﺻﺮ ﻣﺼﻔﻮﻓﺔ‬ ‫ﯾﺘﻢ ذﻛﺮ اﺳﻢ اﻟﻤﺼﻔﻮﻓﺔ ﺛﻢ رﻗﻢ اﻟﻌﻨﺼﺮ اﻟﺬي ﻧﺮﯾﺪ اﻟﺘﻌﺎﻣﻞ ﻣﻌﮫ ﺑﯿﻦ ﻗﻮﺳﯿﻦ ‪.‬‬ ‫ﻓﺈذا أردﻧﺎ ﻣﺜﻼً أن ﻧﻀﻊ اﻟﻘﯿﻤﺔ ‪ ٧٥‬ﻓﻲ اﻟﻌﻨﺼﺮ اﻟﺜﺎﻟﺚ ﻣﻦ اﻟﻤﺼﻔﻮﻓﺔ ﯾﺘﻢ ذﻟﻚ ﻛﻤﺎ ﯾﻠﻲ‬ ‫‪Mark[2] = 75‬‬ ‫ﻣﻼﺣﻈﺔ ھﺎﻣﺔ ﺟﺪًا‬ ‫اﻟﺒﺪاﯾﺔ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ ﺗﺒﺪأ ﻣﻦ اﻟﺼﻔﺮ ) أي اﻟﻤﺆﺷﺮ ( ﻛﻤﺎ ذﻛﺮﻧﺎ ﺳﺎﺑﻘﺎً‪.‬‬ ‫ﻛﻤﺎ ﻓﻲ اﻟﺸﻜﻞ اﻟﺘﺎﻟﻲ‬ ‫‪4‬‬

‫‪3‬‬

‫‪2‬‬

‫‪1‬‬

‫‪0‬‬

‫رﻗﻢ اﻟﻌﻨﺼﺮ‬

‫‪852‬‬

‫‪258‬‬

‫‪1‬‬

‫‪11‬‬

‫‪454‬‬

‫ﻗﯿﻤﺔ اﻟﻌﻨﺼﺮ‬

‫ﻓﮭﺬه اﻟﻤﺼﻔﻮﻓﺔ ﺗﺘﺄﻟﻒ ﻣﻦ ﺧﻤﺴﺔ ﻋﻨﺎﺻﺮ‪.‬‬ ‫ﻟﻨﺘﻤﻜﻦ ﻣﻦ إدﺧﺎل ﻗﯿﻤﺔ ﻋﻨﺼﺮ‪-‬ﺣﺮف أو رﻗﻢ ‪ -‬ﻓﻲ ﻣﺼﻔﻮﻓﺔ ﯾﺠﺐ ﻋﻠﯿﻨﺎ ﺗﺤﺪﯾﺪ رﻗﻢ اﻟﻌﻨﺼﺮ ﺣﺘﻰ ﻧﺘﻤﻜﻦ ﻣﻦ‬ ‫ذﻟﻚ ﻛﻤﺎ ﻓﻲ اﻟﻤﺜﺎل اﻟﺘﺎﻟﻲ‪:‬‬ ‫أدﺧﻞ اﻟﻘﯿﻤﺔ ‪ ٣‬ﻓﻲ اﻟﻤﻜﺎن اﻟﺜﺎﻧﻲ ﻟﻠﻤﺼﻔﻮﻓﺔ ]‪mark[4‬‬

‫‪٣٣‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫اﻟﺤﻞ‪:‬‬ ‫;]‪cin rel="nofollow">>mark[1‬‬ ‫ﻃﺒﻌﺎ ﻛﺘﺒﻨﺎ ]‪ [1‬ﻷن اﻟﻌﻨﺼﺮ اﻟﺜﺎﻧﻲ ﻣﻦ اﻟﻤﺼﻔﻮﻓﺔ ﯾﺸﺎر ﻟﮫ ﺑﺎﻟﺮﻗﻢ ‪١‬‬ ‫وﻷن اﻟﻌﻨﺼﺮ اﻷول ﻣﻨﮭﺎ ﯾﺄﺧﺬ اﻟﻤﻜﺎن )‪ (0‬ﻛﻤﺎ ﺗﻢ ذﻛﺮه ﺳﺎﺑﻘﺎً‬

‫ﻛﯿﻔﯿﺔ إدﺧﺎل ﻋﻨﺎﺻﺮ ﻣﺼﻔﻮﻓﺔ ﺑﺸﻜﻞ ﻣﺘﺘﺎﻟﻲ‪:‬‬ ‫ﻹدﺧﺎل ﻋﻨﺎﺻﺮ ﻣﺼﻔﻮﻓﺔ اﺳﻤﮭﺎ ‪ mark‬ﺗﺘﺄﻟﻒ ﻣﻦ ﺧﻤﺲ ﻋﻨﺎﺻﺮ ﻧﺘﺒﻊ ﺣﻠﻘﺔ ‪ for‬ﻛﻤﺎ ﯾﻠﻲ‪:‬‬ ‫)‪for(int i=0 ; i<5 ; i++‬‬ ‫{‬ ‫; "‪cout<<" please enter array elements‬‬ ‫; ]‪cin>> mark [i‬‬ ‫}‬

‫* ﻋﻠﻤﺎً أن رﻗﻢ اﻟﻌﻨﺼﺮ) وﻟﯿﺲ ﻗﯿﻤﺘﮫ( ﯾﺒﺪأ ﻣﻦ اﻟﺮﻗﻢ ﺻﻔـﺮ وﻟﮭﺬا ﻛﺘﺒﻨﺎ ‪i<5‬‬ ‫ﻓﺎﻟﻤﺼﻔﻮﻓﺔ اﻟﺘﻲ ﺗﺘﺄﻟﻒ ﻣﻦ ﺧﻤﺴﺔ ﻋﻨﺎﺻﺮ ﯾﻜﻮن رﻗﻢ اﻟﻌﻨﺼﺮ اﻷﺧﯿﺮ – وﻟﯿﺲ ﻗﯿﻤﺘﮫ – ﺗﻜﻮن أرﺑﻌﺔ‬

‫ﺗﻨﻘﺴﻢ اﻟﻤﺼﻔﻮﻓﺔ إﻟﻰ ﻧﻮﻋﯿﻦ ‪:‬‬ ‫‪ -١‬ﻣﺼﻔﻮﻓﺔ ذات اﻟﺒﻌﺪ اﻟﻮاﺣﺪ‬ ‫‪ -٢‬ﻣﺼﻔﻮﻓﺔ ذات ﺑﻌﺪﯾﻦ‬ ‫وﺗﺴﻤﻰ اﻟﻤﺼﻔﻮﻓﺔ }‪ A{ 2,5,4,6‬ﻣﺼﻔﻮﻓﺔ ذات اﻟﺒﻌﺪ اﻟﻮاﺣﺪ ﻷﻧﮭﺎ ﺗﺘﻜﻮن ﻣﻦ ﺻﻒ واﺣﺪ و إن ﻛﺎﻧﺖ‬ ‫ﺗﺘﻜﻮن ﻣﻦ ﻋﺎﻣﻮد واﺣﺪ ﻓﺘﺴﻤﻰ ﻧﻔﺲ اﻻﺳﻢ ‪.‬‬ ‫أﻣﺎ اﻟﻤﺼﻔﻮﻓﺔ ذات اﻟﺒﻌﺪﯾﻦ ﻓﮭﻲ اﻟﺘﻲ ﺗﺘﺄﻟﻒ ﻣﻦ أﻛﺜﺮ ﻣﻦ ﺻﻒ أو أﻛﺜﺮ ﻣﻦ ﻋﺎﻣﻮد‪.‬‬ ‫ﻣﻼﺣﻈﺔ ‪:‬‬ ‫ﻗﺪ ﻧﺤﺘﺎج ﻓﻲ اﻟﻤﺼﻔﻮﻓﺎت أو ﻏﯿﺮھﺎ ﻟﻠﺘﻌﺎﻣﻞ ﻣﻊ ﻗﯿﻤﺔ ﺛﺎﺑﺘﺔ ) ﻣﺜﻼً ‪ ( ١٠‬ﻟﻤﺘﺤﻮل ﻣﺎ) ‪ ( max‬ﻻ ﺗﺘﻐﯿﺮ أﺑﺪا‬ ‫أي ﻣﺘﺤﻮل ﺛﺎﺑﺖ اﻟﻘﯿﻤﺔً ﻓﺈﻧﻨﺎ ﻧﻌﺮف ھﺬا اﻟﻤﺘﺤﻮل ﻋﻠﻰ اﻟﺸﻜﻞ اﻟﺘﺎﻟﻲ‬ ‫‪#define max 10‬‬ ‫ﻓﻔﻲ ھﺬه اﻟﺘﻌﻠﯿﻤﺔ ﻓﺈن اﻟﻤﺘﺤﻮل ‪ max‬أﺧﺬ اﻟﻘﯿﻤﺔ )‪ (١٠‬ﻃﻮال ﻓﺘﺮة ﺗﻨﻔﯿﺬ اﻟﺒﺮﻧﺎﻣﺞ‬ ‫وﻻ ﯾﻤﻜﻦ أن ﯾﺪﺧﻞ ‪ max‬ﻓﻲ أي ﻣﻦ اﻟﻌﻤﻠﯿﺎت اﻟﺤﺴﺎﺑﯿﺔ أو اﻹﺳﻨﺎد‪.‬‬ ‫ﻣﺜﺎل ‪:‬اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﺈدﺧﺎل ﻣﺮﺗﺒﺎت ﻋﺸﺮة ﻣﻮﻇﻔﯿﻦ ﺛﻢ ﯾﻘﻮم ﺑﺤﺴﺎب وﻃﺒﺎﻋﺔ ﻣﺘﻮﺳﻂ ھﺬه اﻟﺮواﺗﺐ‬ ‫>‪#include
‫{‬ ‫;]‪float salary[max‬‬ ‫;‪float average ,sum‬‬ ‫;‪int count‬‬ ‫;‪sum=0.0‬‬ ‫)‪for(count=0;count<max;count++‬‬ ‫ﻛﯿﻔﯿﺔ اﺳﺘﺨﺪام اﻟﻤﺘﺤﻮل اﻟﺜﺎﺑﺖ ﺿﻤﻦ ھﺬه اﻟﺤﻠﻘﺔ‬ ‫{‬ ‫;"‪cout<<"please enter salary for employee\n‬‬ ‫;]‪cin>>salary[count‬‬ ‫;]‪sum=sum+salary[count‬‬ ‫}‬ ‫;‪average = sum / max‬‬ ‫اﺳﺘﺨﺪام اﻟﻤﺘﺤﻮل اﻟﺜﺎﺑﺖ ﺿﻤﻦ اﻟﻌﻤﻠﯿﺎت اﻟﺤﺴﺎﺑﯿﺔ‬ ‫;‪cout<<"\n salary average is = "<> mark‬‬ ‫وﻧﻜﺘﺐ اﻻﺳﻢ اﻟﻤﺮاد ادﺧﺎﻟﮫ ﺑﺎﻟﻜﺎﻣﻞ ﺛﻢ ﻧﻀﻐﻂ إﻧﺘﺮ‬ ‫ﺑﯿﻨﻤﺎ ﻟﯿﺲ ﺑﺎﻹﻣﻜﺎن ﻓﻌﻞ ذﻟﻚ ﻣﻊ اﻟﻤﺼﻔﻮﻓﺔ اﻟﺮﻗﻤﯿﺔ ‪ int float‬إﻻ رﻗﻢ رﻗﻢ ‪.‬‬ ‫ﻣﺜﺎل‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﺈدﺧﺎل أﺳﻤﺎء اﻟﻤﻮﻇﻔﯿﻦ و ﻋﻨﺎوﯾﻨﮭﻢ‬ ‫>‪#include >name‬‬ ‫;‪cin>>address‬‬ ‫;‪cout<
‫‪٣٥‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫ﻛﻤﺎ ﻧﻼﺣﻆ أﻧﻨﺎ ﻓﻲ ھﺬا اﻟﻤﺜﺎل أدﺧﻠﻨﺎ اﺳﻢ اﻟﻤﻮﻇﻒ ﻛﺎﻣﻼً و ﻋﻨﻮاﻧﮫ أﯾﻀﺎً ﻷن اﺳﻢ اﻟﻤﻮﻇﻒ ھﻮ ﻋﺒﺎرة ﻋﻦ‬ ‫ﻣﺼﻔﻮﻓﺔ ﺣﺮﻓﯿﺔ وﻛﺬﻟﻚ اﻟﻌﻨﻮان‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﻣﻦ أﺟﻞ إﯾﺠﺎد ﻣﺠﻤﻮع ﻋﻨﺎﺻﺮ ﻣﺼﻔﻮﻓﺔ أﺣﺎدﯾﺔ اﻟﺒﻌﺪ ﻋﺪد ﻋﻨﺎﺻﺮھﺎ ﺧﻤﺴﺔ ﻋﻨﺎﺻﺮ‬ ‫ rel="nofollow">‪#include>mark[i‬‬ ‫;]‪sum=sum+mark[i‬‬ ‫}‬ ‫;‪cout<<"sum = "<<sum‬‬ ‫}‬ ‫ﺷﺮح اﻟﺒﺮﻧﺎﻣﺞ‬ ‫ﻟﻘﺪ أدﺧﻠﻨﺎ ھﻨﺎ اﻟﻌﻨﺎﺻﺮ رﻗﻢ رﻗﻢ و اﻟﺬي ﻗﺎم ﺑﺬﻟﻚ ﺣﻠﻘﺔ ‪FOR‬‬ ‫وﻣﻦ ﺛﻢ ﻗﻤﻨﺎ ﺑﺠﻤﻊ ﻗﯿﻢ ﻋﻨﺎﺻﺮ ھﺬه اﻟﻤﺼﻔﻮﻓﺔ‬ ‫ﻣﻼﺣﻈﺔ‬ ‫ﻟﯿﺲ ﺑﺎﻹﻣﻜﺎن ﺗﺤﺪﯾﺪ ﻋﺪد ﻋﻨﺎﺻﺮ اﻟﻤﺼﻔﻮﻓﺔ ﺑﻤﺘﺤﻮل‬ ‫ﻓﺈﻧﮫ ﻣﻦ اﻟﻮاﺟﺐ ﺗﺤﺪﯾﺪ ﻋﺪد ﻋﻨﺎﺻﺮھﺎ وﻟﺬﻟﻚ ﻓﺈﻧﮫ ﻣﻦ اﻟﺨﻄﺄ ﻛﺘﺎﺑﺔ‬ ‫]‪float mark[x‬‬ ‫‪cin>>x‬‬ ‫ﻷﻧﮫ ﻻ ﯾﺠﻮز ﺗﺤﺪﯾﺪ ﻋﺪد ﻋﻨﺎﺻﺮ اﻟﻤﺼﻔﻮﻓﺔ ﺑﻤﺘﺤﻮل‬ ‫ﻣﻼﺣﻈﺔ‬ ‫ﻟﻘﺪ ﻛﺘﺒﻨﺎ ﻓﻲ اﻟﻤﺜﺎل اﻟﺴﺎﺑﻖ )‪for(int i=0;i<5;i++‬‬ ‫اﻟﻤﺼﻔﻮﻓﺔ ﺗﺒﺪأ ﻣﻦ اﻟﺼﻔﺮ‬

‫اﻟﻌﺒﺎرة ‪ i<5‬وﻟﻢ ﻧﻜﺘﺐ ‪ i<=5‬ﻷن ﻋﻨﺎﺻﺮ‬

‫ﻣﺜﺎل آﺧﺮ‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﺈدﺧﺎل ﺳﻠﺴﻠﺔ ﻣﺆﻟﻔﺔ ﻣﻦ ﻋﺸﺮة رﻣﻮز ﺛﻢ ﯾﻘﻮم ﺑﻄﺒﺎﻋﺘﮭﺎ وﻓﻖ ﺗﺮﺗﯿﺐ اﻹدﺧﺎل ﺛﻢ ﺑﻌﻜﺴﮫ‬ ‫>‪#include>s[i‬‬

‫‪٣٦‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫ﺣﻠﻘﺔ اﻹﺧﺮاج اﻟﺘﺼﺎﻋﺪﯾﺔ‬ ‫ﺣﻠﻘﺔ اﻹﺧﺮاج اﻟﺘﻨﺎزﻟﯿﺔ‬

‫)‪for(i=0;i<10;i++‬‬ ‫;]‪cout<<s[i‬‬ ‫)‪for(i=9;i>=0;i--‬‬ ‫;]‪cout<<s[i‬‬ ‫}‬

‫ﻣﻼﺣﻈﺔ ھﺎﻣﺔ‬ ‫* ﻓﻲ ھﺬا اﻟﻤﺜﺎل أدﺧﻠﻨﺎ ﻋﻨﺎﺻﺮ اﻟﻤﺼﻔﻮﻓﺔ ﺣﺮف ﺣﺮف‬ ‫* ﻟﻘﺪ ﻛﺘﺒﻨﺎ ﺣﻠﻘﺔ اﻹﺧﺮاج اﻟﺘﻨﺎزﻟﯿﺔ ﺑﮭﺬا اﻟﺸﻜﻞ‬ ‫)‪for(i=9;i>=0;i--‬‬ ‫ﻋﻨﺪﻣﺎ ‪ i=9‬ﺗﻨﻌﻲ ﺑﺄﻧﻨﺎ ﻧﺮﯾﺪ اﻟﻌﻨﺼﺮ اﻟﻌﺎﺷﺮ‬ ‫ﻟﻸﺳﺒﺎب اﻟﺘﺎﻟﯿﺔ‬ ‫ﻋﻨﺪﻣﺎ ‪ i=0‬ﻧﻌﻨﻲ ﺑﺄﻧﻨﺎ ﻧﺮﯾﺪ اﻟﻌﻨﺼﺮ اﻷول‬ ‫ﺗﻤﺎرﯾﻦ ﺣﻮل اﻟﻤﺼﻔﻮﻓﺔ أﺣﺎدﯾﺔ اﻟﺒﻌﺪ‬ ‫* اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾُﻤَﻜّﻦ اﻟﻤﺴﺘﺨﺪم ﻣﻦ إدﺧﺎل ﻋﺸﺮة رﻣﻮز ﺛﻢ ﯾﻘﻮم اﻟﺒﺮﻧﺎﻣﺞ ﺑﻄﺒﺎﻋﺔ ﻋﺪد ﻣﺮات ﺗﻜﺮار اﻟﺤﺮﻓﯿﻦ‬ ‫‪ A a‬ﻓﻲ ھﺬه اﻟﺴﻠﺴﻠﺔ‬ ‫>‪#include> s[i‬‬ ‫)'‪if(s[i]=='A'||s[i]=='a‬‬ ‫;‪count ++‬‬ ‫}‬ ‫;‪cout<
‫ﺷﺮح اﻟﺒﺮﻧﺎﻣﺞ‬ ‫ﻛﻤﺎ ﺗﻼﺣﻆ‬ ‫ﺑﻌﺪ أﻣﺮ اﻻدﺧﺎل ﻛﺘﺒﻨﺎ اﻟﻜﻮد اﻟﺘﺎﻟﻲ‬ ‫)'‪if(s[i]=='A'||s[i]=='a‬‬ ‫ﻣﺎ ﻣﻌﻨﻰ ھﺬه اﻟﺤﻠﻘﺔ ؟‬ ‫إن ﻛﺎن اﻟﻌﻨﺼﺮ اﻟﻤﺪﺧﻞ ﯾﺴﺎوي اﻟﻘﯿﻤﺔ ‪ A‬أو ‪ or‬ﯾﺴﺎوي اﻟﻘﯿﻤﺔ ‪ a‬ﻧﻔﺬ اﻟﺴﻄﺮ اﻟﺘﺎﻟﻲ‬

‫‪٣٧‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫واﻟﺬي ھﻮ ‪ count++‬أي زﯾﺪ اﻟﻌﺪاد ﺑﻤﻘﺪار واﺣﺪ‬ ‫ﻓﻜﻠﻤﺎ ﺗﻨﻔﺬ ھﺬا اﻟﺸﺮط زادت ﻗﯿﻤﺔ اﻟﻌﺪاد ﺣﺘﻰ ﺗﻤﺎم إدﺧﺎل ﻋﻨﺎﺻﺮ اﻟﻤﺼﻔﻮﻓﺔ ﻓﺈن اﻟﺒﺮﻧﺎﻣﺞ ﯾﻄﺒﻊ ﻗﯿﻤﺔ اﻟﻌﺪاد‪.‬‬ ‫ﻣﺜﺎل‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾُﻤَﻜﻦ اﻟﻤﺴﺘﺨﺪم ﺑﺈدﺧﺎل ﻣﺼﻔﻮﻓﺔ أﺣﺎدﯾﺔ اﻟﺒﻌﺪ ﻣﺆﻟﻔﺔ ﻣﻦ ﻋﺸﺮة أرﻗﺎم‬ ‫وﻣﻦ ﺛﻢ ﯾﻘﻮم اﻟﺒﺮﻧﺎﻣﺞ ﺑﻄﺒﺎﻋﺔ اﻷرﻗﺎم اﻟﺘﻲ ھﻲ أﻛﺒﺮ ﻣﻦ ﺧﻤﺴﺔ‬ ‫ﻣﻊ ﻃﺒﺎﻋﺔ أﻣﺎﻛﻦ ﺗﻮاﺟﺪھﺎ ﺿﻤﻦ اﻟﻤﺼﻔﻮﻓﺔ‬ ‫اﻟﺤﻞ‪:‬‬ ‫>‪#include>s[i‬‬ ‫)‪if(s[i]>5‬‬ ‫{‬ ‫;‪cout<<"this number is begger of 5"<<endl‬‬ ‫"<<‪cout<
‫ﺷﺮح اﻟﺒﺮﻧﺎﻣﺞ‬ ‫إن اﻟﻘﯿﻤﺔ ‪ I‬ﺗﻤﺜﻞ ﻣﻜﺎن ﺗﻮاﺟﺪ اﻟﻌﻨﺼﺮ داﺧﻞ اﻟﻤﺼﻔﻮﻓﺔ‬ ‫أﻣﺎ اﻟﻘﯿﻤﺔ ]‪ S[I‬ﻓﺈﻧﮭﺎ ﺗﺸﻜﻞ ﻗﯿﻤﺔ ھﺬا اﻟﻌﻨﺼﺮ‬

‫اﻟﻤﺼﻔﻮﻓﺎت ﺛﻨﺎﺋﯿﺔ اﻟﺒﻌﺪ‬ ‫ھﻲ اﻟﻤﺼﻔﻮﻓﺎت اﻟﺘﻲ ﺗﺘﺄﻟﻒ ﻣﻦ أﻛﺜﺮ ﻣﻦ ﻋﺎﻣﻮد أو أﻛﺜﺮ ﻣﻦ ﺳﻄﺮ‬ ‫ﻣﺜﺎل ﻋﻠﯿﮭﺎ‪:‬‬

‫ﻓﻔﻲ ھﺬا اﻟﻤﺜﺎل ﺗﺘﺄﻟﻒ ھﺬه اﻟﻤﺼﻔﻮﻓﺔ ﻣﻦ ﻋﺎﻣﻮدﯾﻦ و ﺛﻼﺛﺔ أﺳﻄﺮ‬ ‫وﻹدﺧﺎل ھﺬه اﻟﻤﺼﻔﻮﻓﺔ ﯾﺠﺐ ﻋﻠﯿﻨﺎ ﺑﺪاﯾﺔ أن ﻧﻌﺮف اﻟﺒﺮﻧﺎﻣﺞ ﺑﺴﻄﺮ ﺛﻢ ﺑﻌﺎﻣﻮد اﻟﻌﻨﺼﺮ اﻟﻤﺮاد إدﺧﺎﻟﮫ‬ ‫وﺗﻜﻮن ﺑﺎﻟﻄﺮﯾﻘﺔ اﻟﺘﺎﻟﯿﺔ‪:‬‬ ‫;]‪cin>> a[i] [j‬‬ ‫ﺣﯿﺚ ‪ a :‬اﺳﻢ اﻟﻤﺼﻔﻮﻓﺔ‬ ‫‪٣٨‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫‪ i‬رﻗﻢ اﻟﺴﻄﺮ‬ ‫‪ j‬رﻗﻢ اﻟﻌﺎﻣﻮد‬ ‫وﯾﻜﻮن ﺗﺮﻗﯿﻢ اﻷﺳﻄﺮ و اﻷﻋﻤﺪة ﺣﺴﺐ اﻟﺸﻜﻞ‪:‬‬

‫‪ a[1][0]=1‬أن اﻟﻘﯿﻤﺔ ) ‪ ( ١‬ﻣﻮﺟﻮدة ﻓﻲ اﻟﺴﻄﺮ ‪ 1‬و اﻟﻌﺎﻣﻮد ‪ 0‬ﻛﻤﺎ ﻓﻲ اﻟﺸﻜﻞ‬

‫ﺣﯿﺚ ﯾﻌﺒﺮ اﻟﻜﻮد‬ ‫اﻟﺴﺎﺑﻖ‬ ‫وﻛﻤﺎ أﻧﻨﺎ ﻗﺪ اﺳﺘﺨﺪﻣﻨﺎ ﺣﻠﻘﺔ ‪ for‬ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ أﺣﺎدﯾﺔ اﻟﺒﻌﺪ ﻓﺈﻧﻨﺎ ﺳﻨﺴﺘﺨﺪم ھﻨﺎ ﺣﻠﻘﺘﻲ ‪ for‬ﻹدﺧﺎل اﻟﻤﺼﻔﻮﻓﺔ‬ ‫ﺛﻨﺎﺋﯿﺔ اﻟﺒﻌﺪ ﻛﺎﻟﺘﺎﻟﻲ‪:‬‬ ‫)‪for(i=0 ; i<3 ; i++‬‬ ‫)‪for(j=0 ; j<2; j++‬‬ ‫;]‪cin>>a[i][j‬‬

‫ﻣﻼﺣﻈﺔ‪:‬‬ ‫ﻧﺴﻤﻲ اﻟﻤﺼﻔﻮﻓﺔ ﺛﻨﺎﺋﯿﺔ اﻟﺒﻌﺪ ﺑﺎﻟﻤﺼﻔﻮﻓﺔ اﻟﻤﺮﺑﻌﺔ إذا ﺗﺴﺎوى ﻋﺪد اﻷﺳﻄﺮ ﻣﻊ ﻋﺪد اﻷﻋﻤﺪة‬

‫ﻣﺜﺎل ‪:‬‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﺈدﺧﺎل ﻋﻨﺎﺻﺮ ﻣﺼﻔﻮﻓﺔ ﺛﻨﺎﺋﯿﺔ اﻟﺒﻌﺪ‬ ‫ﺗﺘﺄﻟﻒ ﻣﻦ ﺳﻄﺮﯾﻦ وﺛﻼﺛﺔ أﻋﻤﺪة‬ ‫ﺑﺤﯿﺚ ﯾﻘﻮم اﻟﺒﺮﻧﺎﻣﺞ ﺑﺎﺳﺘﺒﺪال ﻋﻨﺎﺻﺮ اﻟﻤﺼﻔﻮﻓﺔ ذات اﻟﻘﯿﻤﺔ اﻟﻔﺮدﯾﺔ ﺑﺎﻟﻘﯿﻤﺔ )‪( ١٠‬‬ ‫ﺛﻢ ﯾﻘﻮم اﻟﺒﺮﻧﺎﻣﺞ ﺑﻄﺒﺎﻋﺔ ﺷﻜﻞ اﻟﻤﺼﻔﻮﻓﺔ اﻟﺠﺪﯾﺪ‪.‬‬ ‫اﻟﺤﻞ‪:‬‬

‫>‪#include
‫;‪int s[2][3],i,j‬‬ ‫)‪for(i=0;i<2;i++‬‬ ‫)‪for(j=0;j<3;j++‬‬ ‫{‬ ‫;]‪cin>>s[i][j‬‬ ‫)‪if(s[i][j]%2!=0‬‬ ‫;‪s[i][j]=10‬‬ ‫}‬ ‫)‪for(i=0;i<2;i++‬‬ ‫{‬ ‫)‪for(j=0;j<3;j++‬‬ ‫;" "<<]‪cout<<s[i][j‬‬ ‫;‪cout<<endl‬‬ ‫}}‬

‫اﻟﺴﺠﻼت ) اﻟﺘﺮﻛﯿﺒﺎت ( ‪structure‬‬ ‫ﺗﻌﺮﯾﻒ اﻟﺴﺠﻞ‪:‬‬ ‫ھﻮ ﻣﺠﻤﻮﻋﺔ ﻣﻦ اﻟﺒﯿﺎﻧﺎت اﻟﻤﺨﺘﻠﻔﺔ ﻓﻲ اﻟﻨﻮع ﻣﻊ ﺑﻌﻀﮭﺎ اﻟﺒﻌﺾ ﺑﺤﯿﺚ ﯾﻤﻜﻦ اﻟﺘﻌﺎﻣﻞ ﻣﻌﮭﺎ ﻛﻮﺣﺪة واﺣﺪة ‪.‬‬ ‫اﻟﻨﻮع ‪ :‬أي أن ﯾﻜﻮن ﺑﻌﻀﮭﺎ ﻣﻦ ﻧﻮع ‪ int‬وﺑﻌﻀﮭﺎ ‪ char‬وﺑﻌﻀﮭﺎ ‪ float‬وﺑﻌﻀﮭﺎ ‪.......‬اﻟﺦ‪.‬‬ ‫ﻓﻤﺜﻼً ‪:‬‬ ‫ﻟﻜﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺞ ﻟﺘﺴﺠﯿﻞ ﺑﯿﺎﻧﺎت ﻣﻮﻇﻔﯿﻦ ﻓﻲ اﻟﺸﺮﻛﺔ ﻧﺤﺘﺎج إﻟﻰ ﺗﺨﺰﯾﻦ ‪:‬‬ ‫‪-١‬‬ ‫‪-٢‬‬ ‫‪-٣‬‬ ‫‪-٤‬‬

‫اﺳﻢ اﻟﻤﻮﻇﻒ وھﻮ ﻣﻦ ﻧﻮع ﻣﺼﻔﻮﻓﺔ ﺣﺮﻓﯿﺔ ]‪char name[40‬‬ ‫]‪char address[40‬‬ ‫ﻋﻨﻮاﻧﮫ وھﻮ ﻣﻦ ﻧﻮع ﻣﺼﻔﻮﻓﺔ ﺣﺮﻓﯿﺔ‬ ‫‪int age‬‬ ‫ﻋﻤﺮه ﻣﺘﺤﻮل ﻣﻦ ﻧﻮع ﻋﺪد ﺻﺤﯿﺢ‬ ‫‪float salary‬‬ ‫راﺗﺒﮫ ﻣﺘﺤﻮل ﻣﻦ ﻧﻮع ﻋﺪد ﺣﻘﯿﻘﻲ‬

‫وﻛﻤﺎ ﻧﻼﺣﻆ ﻓﺈن ﺟﻤﯿﻊ ھﺬه اﻟﺒﯿﺎﻧﺎت ﯾﺠﺐ اﻟﺘﻌﺎﻣﻞ ﻣﻌﮭﺎ ﻛﻮﺣﺪة واﺣﺪة ﻷﻧﮭﺎ ﻟﻤﻮﻇﻒ واﺣﺪ‬ ‫وﻟﺬﻟﻚ ﻓﺈﻧﻨﺎ ﺑﺤﺎﺟﺔ إﻟﻰ ﺳﺠﻞ ﺧﺎص ﻟﮭﺬا اﻟﻤﻮﻇﻒ‬ ‫ﻛﯿﻔﯿﺔ اﻹﻋﻼم ﻋﻦ اﻟﺴﺠﻞ ‪:‬‬ ‫ﻟﻠﺘﺼﺮﯾﺢ ﻋﻦ ﺳﺠﻞ ﻧﺴﺘﺨﺪم اﻟﻜﻠﻤﺔ اﻟﻤﺤﺠﻮزة ‪ struct‬وھﻲ اﺧﺘﺼﺎر ﻟﻜﻠﻤﺔ ‪ structure‬وﻣﻌﻨﺎھﺎ ﺗﺮﻛﯿﺐ‬

‫‪٤٠‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫وﻧﻀﻊ ﺟﻤﯿﻊ ﻣﻜﻮﻧﺎت ھﺬا اﻟﺘﺮﻛﯿﺐ ﺿﻤﻦ ﻗﻮﺳﯿﻦ وﻧﺨﺘﻢ اﻟﺘﺼﺮﯾﺢ ﺑﻔﺎﺻﻠﺔ ﻣﻨﻘﻮﻃﺔ ﺑﻌﺪ اﻟﻘﻮس اﻟﺜﺎﻧﻲ‬ ‫ﺛﻢ ﯾﺘﻢ اﻟﺘﺼﺮﯾﺢ ﻋﻦ ﻣﺘﺤﻮل ﺧﺎص ﻟﮭﺬا اﻟﺘﺮﻛﯿﺐ ) اﻟﺴﺠﻞ ( ﺿﻤﻦ اﻟﻘﺎﺋﻤﺔ اﻟﺮﺋﯿﺴﯿﺔ ﻟﻠﺒﺮﻧﺎﻣﺞ ‪.‬‬ ‫وﯾﺘﻢ اﻟﺘﺼﺮﯾﺢ ﻋﻦ اﻟﺴﺠﻞ ﺑﺄﺣﺪ اﻟﻄﺮق اﻟﺘﺎﻟﯿﺔ‬ ‫>‪#include
‫ﻛﯿﻒ ﻧﻜﺘﺐ اﻟﻤﺘﺤﻮﻻت داﺧﻞ اﻟﺴﺠﻞ ) ﻛﯿﻔﯿﺔ ﻛﺘﺎﺑﺔ ھﺬا اﻟﺴﺠﻞ (‬ ‫>‪#include
‫ﻛﯿﻔﯿﺔ إدﺧﺎل وإﺧﺮاج اﻟﻤﻌﻠﻮﻣﺎت داﺧﻞ ھﺬا اﻟﺴﺠﻞ‬ ‫ﻋﻨﺪﻣﺎ ﻧﺮﯾﺪ ﻣﺜﻼً إدﺧﺎل ﻋﻤﺮ اﻟﻤﻮﻇﻒ ﻓﺈﻧﻨﺎ ﻧﻜﺘﺐ‬ ‫; ‪Cin >> emp . age‬‬ ‫وﻋﻨﺪﻣﺎ ﻧﺮﯾﺪ إدﺧﺎل اﺳﻢ اﻟﻤﻮﻇﻒ ﻧﻜﺘﺐ‬

‫‪٤١‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫;‪Cin >> emp . name‬‬ ‫و ﻻ ﻧﻜﺘﺐ اﻷﻗﻮاس ﻋﻨﺪﻣﺎ ﻧﺮﯾﺪ إدﺧﺎل اﻻﺳﻢ ﻛﺎﻣﻼً ﻛﻤﺎ ﺗﻌﻠﻤﻨﺎ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺎت‬ ‫أﻣﺎ ﻋﻨﺪﻣﺎ ﻧﺮﯾﺪ إﺧﺮاج أي ﻗﯿﻤﺔ ﻓﺈﻧﻨﺎ ﻧﺴﺘﺒﺪل ‪ cin‬ﺑـ ‪ cout‬ﻛﻤﺎ ھﻮ ﻣﻌﺮوف‬ ‫وﯾﻤﻜﻨﻨﺎ أن ﻧﻌﻠﻢ ﻋﻦ اﻟﺘﺼﺮﯾﺢ ﺑﻄﺮﯾﻘﺔ أﺧﺮى‬ ‫) ( ‪Void main‬‬ ‫{‬ ‫‪Struct employee‬‬ ‫{‬ ‫;‪-----‬‬ ‫;‪-----‬‬ ‫;‪-----‬‬ ‫;‪-----‬‬ ‫;}‬ ‫وﻧﻜﻤﻞ اﻟﺒﺮﻧﺎﻣﺞ‬

‫ﻣﺜﺎل ﻋﻤﻠﻲ‪:‬‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﺈدﺧﺎل ﻣﻌﻠﻮﻣﺎت ﻋﻦ‬ ‫‪ -١‬اﺳﻢ اﻟﻤﻮﻇﻒ‬ ‫‪ -٢‬ﻋﻨﻮاﻧﮫ‬ ‫‪ -٣‬ﻋﻤﺮه‬ ‫‪ -٤‬راﺗﺒﮫ‬ ‫ﻃﺒﻌﺎ ﻻ ﻧﻀﻊ ھﻨﺎ ﻓﺎﺻﻠﺔ ﻣﻨﻘﻮﻃﺔ‬

‫>‪#include
‫;‪struct employee emp‬‬ ‫;‪cout<<"enter name"<<endl‬‬ ‫‪٤٢‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

cin>>emp.name; cout<<"enter address"<<endl; cin>>emp.address; cout<<"enter age"<<endl; cin>>emp.age; cout<<"enter salary"<<endl; cin>>emp.salary; } ‫وﻟﻜﻦ إن ﻛﺎن ﻟﺪﯾﻨﺎ أﻛﺜﺮ ﻣﻦ ﻣﻮﻇﻒ ﻣﺜﻼً ﺛﻼﺛﺔ ﻋﺸﺮ ﻣﻮﻇﻒ ﻓﺈﻧﻨﺎ ﺑﺤﺎﺟﺔ إﻟﻰ ﻣﺼﻔﻮﻓﺔ ﺳﺠﻼت‬

‫ﻛﯿﻔﯿﺔ اﻟﺘﺼﺮﯾﺢ ﻋﻦ ﻣﺼﻔﻮﻓﺔ ﺳﺠﻼت‬ ‫ﻧﻔﺲ اﻟﻄﺮﯾﻘﺔ اﻟﺴﺎﺑﻘﺔ وﻟﻜﻦ ﻧﺠﻌﻞ اﻟﻤﺘﺤﻮل ﻋﺒﺎرة ﻋﻦ ﻣﺼﻔﻮﻓﺔ وﻧﻀﯿﻒ ﺳﻄﺮ ﺑﺮﻣﺠﻲ‬ . ‫ ﻛﻤﺎ ﻛﻨﺎ ﻧﻜﺘﺒﮭﺎ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺎت‬for ‫وھﻮ ﺣﻠﻘﺔ‬

:‫ﻣﺜﺎل ﻋﻤﻠﻲ‬ ‫ﻟﻨﻌﺪ ﻛﺘﺎﺑﺔ اﻟﺒﺮﻧﺎﻣﺞ اﻟﺴﺎﺑﻖ وﻟﻜﻦ ﻟﺜﻼﺛﺔ ﻋﺸﺮ ﻣﻮﻇﻔﺎً ﻧﺪﺧﻞ اﻟﻤﻌﻠﻮﻣﺎت ﻋﻨﮭﻢ وﻣﻦ ﺛﻢ ﻧﺨﺮﺟﮭﺎ‬ #include struct employee { char name[40]; char address[40]; int age; float salary; }; void main() { struct employee emp[13]; for(int i=0;i<13;i++) { cout<<"enter name"<<endl; cin>>emp[i].name;

٤٣ PDF created with pdfFactory Pro trial version www.pdffactory.com

‫;‪cout<<"enter address"<<endl‬‬ ‫;‪cin>>emp[i].address‬‬ ‫;‪cout<<"enter age"<<endl‬‬ ‫;‪cin>>emp[i].age‬‬ ‫;‪cout<<"enter salary"<<endl‬‬ ‫;‪cin>>emp[i].salary‬‬ ‫}‬ ‫)‪for(i=0;i<13;i++‬‬ ‫;‪cout<<emp[i].name<<emp[i].address<<emp[i].age<<emp[i].salary<<endl‬‬ ‫}‬

‫ﺳﺆال‪:‬‬ ‫إن أردﻧﺎ ﺗﻮﺳﯿﻊ ھﺬه اﻟﻤﺴﺄﻟﺔ ﻣﺜﻼً أن ﺗﻜﻮن ﻣﺠﻤﻮﻋﺔ ﻣﻦ اﻟﻤﻮﻇﻔﯿﻦ ﺗﺎﺑﻌﺔ ﻟﻘﺴﻢ ﻣﺎ أو ﻟﺸﺮﻛﺔ ﻣﺎ‬ ‫ﻓﻤﺎذا ﻧﻔﻌﻞ؟‬ ‫ﻧﺤﻦ ھﻨﺎ ﺑﺤﺎﺟﺔ إﻟﻰ ﺗﻌﺮﯾﻒ ﺳﺠﻞ داﺧﻞ ﺳﺠﻞ‪.‬‬ ‫ﺗﻌﺮﯾﻒ ﺳﺠﻞ داﺧﻞ ﺳﺠﻞ آﺧﺮ‬ ‫ﻣﻦ أﺟﻞ ﺗﻌﺮﯾﻒ ﺳﺠﻞ داﺧﻞ ﺳﺠﻞ ﻣﻦ أﺟﻞ ارﺗﺒﺎط اﻟﺴﺠﻞ اﻟﺜﺎﻧﻮي ﺑﺎﻟﺴﺠﻞ اﻟﺮﺋﯿﺴﻲ ﻣﺜﻞ أن ﻧﻜﺘﺐ ﺑﺮﻧﺎﻣﺞ‬ ‫ﯾﺪﺧﻞ اﺳﻢ اﻟﻤﻮﻇﻒ وﻋﻨﻮاﻧﮫ و‪.....‬و‪ .....‬وﯾﺘﻢ إﺳﻨﺎد ھﺬا اﻟﺴﺠﻞ إﻟﻰ ﺳﺠﻞ آﺧﺮ وھﻮ اﻟﻘﺴﻢ أو اﻟﻤﺆﺳﺴﺔ اﻟﺘﻲ‬ ‫ﯾﻮﺟﺪ ﺑﮭﺎ ھﺬا اﻟﻤﻮﻇﻒ‬ ‫ﻓﯿﻜﻮن ھﻨﺎ ﺳﺠﻞ اﻟﻤﺆﺳﺴﺔ ھﻮ اﻟﺴﺠﻞ اﻟﺮﺋﯿﺴﻲ أﻣﺎ ﺳﺠﻞ اﻟﻤﻮﻇﻒ ﻓﮭﻮ اﻟﺴﺠﻞ اﻟﺜﺎﻧﻮي‬ ‫وﻟﯿﺲ ﻣﻦ اﻟﻀﺮوري ‪ -‬ﻓﻲ ﻛﺘﺎﺑﺔ اﻟﻜﻮد ‪ -‬ﻛﺘﺎﺑﺔ أي ﺳﺠﻞ ﻗﺒﻞ اﻵﺧﺮ‬ ‫ﻣﺜﺎل ﻋﻠﻰ ذﻟﻚ ‪:‬‬ ‫ﺳﺠﻞ اﻟﻤﻮﻇﻒ ﺛﺎﻧﻮي‬

‫‪Struct employee‬‬ ‫{‬ ‫;]‪Char name[20‬‬ ‫;]‪Char address[40‬‬ ‫;‪Int age‬‬ ‫;‪Float salary‬‬

‫‪٤٤‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫;}‬ ‫‪Struct dept‬‬ ‫ﺳﺠﻞ اﻟﻘﺴﻢ رﺋﯿﺴﻲ‬ ‫{‬ ‫;‪Int deptno‬‬ ‫ﻧﻌﺮف ﻣﺘﺤﻮل رﻗﻢ اﻟﻘﺴﻢ‬ ‫;‪Int projct‬‬ ‫ﻧﻌﺮف ﻣﺘﺤﻮل اﻟﻤﺸﺮوع‬ ‫;‪Struct employee emp‬‬ ‫ﻧﻌﺮف ھﻨﺎ ﻣﺘﺤﻮل اﻟﺴﺠﻞ اﻟﺜﺎﻧﻮي داﺧﻞ اﻟﺮﺋﯿﺴﻲ‬ ‫;}‬ ‫أﻣﺎ ﻋﻦ ﻛﯿﻔﯿﺔ اﻹدﺧﺎل و اﻹﺧﺮاج ﻓﺈﻧﻨﺎ ﻧﻜﺘﺐ‬ ‫ﻓﺈذا أردﻧﺎ إدﺧﺎل ﻣﺜﻼً ﻋﻤﺮ اﻟﻤﻮﻇﻒ ﻓﺈﻧﻨﺎ ﻧﻜﺘﺐ اﺳﻢ اﻟﺴﺠﻞ اﻟﺮﺋﯿﺴﻲ ﺛﻢ اﻟﻤﺘﺤﻮل ﻟﻠﺴﺠﻞ اﻟﺜﺎﻧﻮي ﺛﻢ ﻣﻜﺎن‬ ‫اﻹدﺧﺎل أﻣﺎ ﻓﻲ ﺣﺎل إﺿﺎﻗﺔ ﻣﻌﻠﻮﻣﺔ ﺿﻤﻦ اﻟﺴﺠﻞ اﻟﺮﺋﯿﺴﻲ ﻓﯿﻜﻔﻲ ﻛﺘﺎﺑﺔ اﺳﻢ اﻟﺴﺠﻞ اﻟﺮﺋﯿﺴﻲ ﺛﻢ ﻛﺘﺎﺑﺔ اﺳﻢ‬ ‫ﻣﻜﺎن اﻻدﺧﺎل‬ ‫ﻣﺜﺎل ‪:‬‬ ‫;‪Dept.emp.age‬‬ ‫‪Dept.deptno‬‬

‫‪-----------------------------------------‬‬‫ﻣﻼﺣﻈﺔ‪:‬‬ ‫ﻣﻦ أﺟﻞ إدﺧﺎل ﺳﻠﺴﻠﺔ ﺣﺮﻓﯿﺔ – ﻓﻘﻂ ‪ -‬ﯾﻤﻜﻦ اﺳﺘﺨﺪام اﻟﺘﻌﻠﯿﻤﺔ ‪ gets‬ﺑﺪل اﻟﺘﻌﻠﯿﻤﺔ >>‪ cin‬ﻓﻲ ﺣﺎل‬ ‫اﺳﺘﺨﺪام اﻟﻤﻜﺘﺒﺔ اﻟﺘﺎﺑﻌﺔ ﻟﮭﺬه اﻟﺘﻌﻠﯿﻤﺔ وھﻲ >‪ <stdio.h‬وﯾﻜﺘﺐ اﻟﻜﻮد ﻛﺎﻟﺘﺎﻟﻲ‪:‬‬ ‫;)‪gets (emp[i] .name‬‬

‫‪Function‬‬ ‫ھﻮ ﻋﺒﺎرة ﻋﻦ ﺑﺮﻧﺎﻣﺞ ﻓﺮﻋﻲ ﻣﮭﻤﺘﮫ ﺗﻨﻔﯿﺬ ﻣﮭﻤﺔ ﻣﻌﯿﻨﺔ ﺣﯿﻦ ﯾﺘﻢ اﺳﺘﺪﻋﺎؤه‬ ‫ﻣﻤﺎ ﯾﺘﺄﻟﻒ اﻟﺒﺮﻧﺎﻣﺞ اﻟﻔﺮﻋﻲ‬ ‫ﯾﺘﺄﻟﻒ ﻣﻦ ﺛﻼﺛﺔ أﻗﺴﺎم‬ ‫‪ -١‬اﻟﺘﺼﺮﯾﺢ ﻋﻦ اﻟﺒﺮﻧﺎﻣﺞ ) وﺳﯿﺄﺗﻲ ﺷﺮﺣﮫ ﺑﻌﺪ ﻗﻠﯿﻞ (وﯾﻨﺘﮭﻲ ﺑﻔﺎﺻﻠﺔ ﻣﻨﻘﻮﻃﺔ‬ ‫وﻟﻜﻦ إن ﻛﺎن اﻟﺘﺼﺮﯾﺢ ﺧﺎرج ) ﻗﺒﻞ ( اﻟﺘﻌﻠﯿﻤﺔ‬ ‫) ( ‪Void main‬‬ ‫ﻓﺈن اﻟﺒﺮﻧﺎﻣﺞ اﻟﻔﺮﻋﻲ ﯾﻜﻮن ﻣﺸﺎع )ﻟﻜﻞ اﻟﺒﺮاﻣﺞ اﻟﻤﺘﺎﺣﺔ ﻓﻲ اﻟﺒﺮﻧﺎﻣﺞ(‬ ‫أﻣﺎ إن ﻛﺘﺐ داﺧﻞ ﺟﺴﻢ‬ ‫) ( ‪Void main‬‬ ‫ﻓﺈن اﻟﺒﺮﻧﺎﻣﺞ اﻟﻔﺮﻋﻲ ﯾﻜﻮن ﺣﻜﺮاً ﻋﻠﻰ ھﺬه اﻟﺘﻌﻠﯿﻤﺔ ‪.‬‬ ‫‪ -٢‬ﻛﻮد ﻃﻠﺐ اﻟﺒﺮﻧﺎﻣﺞ وﯾﻨﺘﮭﻲ ﺑﻔﺎﺻﻠﺔ ﻣﻨﻘﻮﻃﺔ ) ﯾﺸﺮح ﻣﻦ ﺧﻼل اﻷﻣﺜﻠﺔ (‬ ‫‪ -٣‬ﺟﺴﻢ اﻟﺒﺮﻧﺎﻣﺞ وھﻮ ﯾﺸﺎﺑﮫ ﻃﺮﯾﻘﺔ ﻛﺘﺎﺑﺔ اﻟﺒﺮﻧﺎﻣﺞ اﻟﺮﺋﯿﺴﻲ وﻃﺒﻌﺎً ﺑﺪون ﻓﺎﺻﻠﺔ ﻣﻨﻘﻮﻃﺔ‬ ‫وﯾﻜﺘﺐ ﺟﺴﻢ اﻟﺘﺎﺑﻊ ﺑﻌﺪ ﻧﮭﺎﯾﺔ ﻛﺘﺎﺑﺔ ﺟﺴﻢ اﻟﺒﺮﻧﺎﻣﺞ اﻟﺮﺋﯿﺴﻲ‬ ‫ﻛﯿﻔﯿﺔ اﻟﺘﺼﺮﯾﺢ ﻋﻦ ھﺬا اﻟﺘﺎﺑﻊ‪:‬‬ ‫‪٤٥‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫وھﻮ ﺑﺄن ﻧﻜﺘﺐ اﺳﻢ ﺧﺮج اﻟﺘﺎﺑﻊ ﺛﻢ اﺳﻢ اﻟﺘﺎﺑﻊ وﻣﻦ ﺛﻢ ﻧﻜﺘﺐ ﻧﻮع اﻟﺒﺎراﻣﺘﺮات أي ﻧﻮع اﻟﺨﺮج‬ ‫اﻟﺒﺎراﻣﺘﺮات اﺳﻢ اﻟﺘﺎﺑﻊ‬ ‫; ) ‪positive (int‬‬

‫ﺧﺮج اﻟﺘﺎﺑﻊ‬ ‫‪Void‬‬

‫وﯾﻤﻜﻦ أن ﯾﻜﻮن ﺧﺮج اﻟﺘﺎﺑﻊ‬ ‫‪void‬‬ ‫ﻓﺎرغ‬ ‫ﻋﺪد ﺻﺤﺼﺢ ‪int‬‬ ‫‪void‬‬ ‫ﺑﺪون ﻗﯿﻤﺔ‬ ‫‪float‬‬ ‫ﻋﺪد ﺣﻘﯿﻘﻲ‬ ‫‪.......‬‬ ‫‪.......‬‬ ‫‪.....‬اﻟﺦ ‪.‬‬ ‫ﻣﺜﺎل‪):‬ﺑﺪون ﻋﻤﻠﯿﺔ إرﺟﺎع أي ﻗﯿﻤﺔ ( أي ﺑﺪون ﺣﺎﺟﺔ إﻟﻰ ﺧﺮج‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﻤﻌﺮﻓﺔ اﻟﻌﺪد ‪ -‬اﻟﻤُﺪﺧَﻞ ﻣﻦ ﻗﺒﻞ اﻟﻤﺴﺘﺨﺪم‪ -‬ﻣﻮﺟﺐ أم ﺳﺎﻟﺐ‬ ‫>‪#include>x‬‬ ‫اﺳﺘﺪﻋﺎء اﻟﺒﺮﻧﺎﻣﺞ اﻟﻔﺮﻋﻲ ﻛﻮد ﻃﻠﺐ اﻟﺒﺮﻧﺎﻣﺞ ;)‪positive (x‬‬ ‫}‬ ‫;)‪while(x !=0‬‬ ‫}‬ ‫اﻟﺒﺮﻧﺎﻣﺞ اﻟﻔﺮﻋﻲ )‪void positive (int a‬‬ ‫{‬ ‫)‪if(a<0‬‬ ‫;"‪cout<
‫‪٤٦‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫ﺷﺮح اﻟﺒﺮﻧﺎﻣﺞ‪:‬‬ ‫ﺗﻢ اﻟﺘﺼﺮﯾﺢ ﻋﻦ اﻟﺒﺮﻧﺎﻣﺞ ﺑﺎﻟﻜﻮد اﻟﺘﺎﻟﻲ‬ ‫;)‪void positive (int‬‬ ‫وﻣﻌﻨﺎه ﺑﺄن اﻟﺒﺮﻧﺎﻣﺞ ﻏﯿﺮ ﻣﻄﺎﻟﺐ ﺑﺈرﺟﺎع ) ﺧﺮج اﻟﺒﺮﻧﺎﻣﺞ اﻟﻔﺮﻋﻲ ( إﻟﻰ اﻟﺒﺮﻧﺎﻣﺞ اﻷﺳﺎﺳﻲ‬ ‫ﻣﻦ ﺧﻼل اﻟﺘﻌﻠﯿﻤﺔ ‪void‬‬ ‫ﺛﻢ ﻣﻦ ﺧﻼل ﺣﻠﻘﺔ ‪ do while‬ﻃﺎﻟﺒﻨﺎ اﻟﺒﺮﻧﺎﻣﺞ ﺑﺎﻟﻌﻤﻞ ﻃﺎﻟﻤﺎ أن اﻟﻤﺘﺤﻮل ‪ x‬ﻻ ﯾﺴﺎوي‬ ‫اﻟﺼﻔﺮ‬ ‫وﺗﻢ اﺳﺘﺪﻋﺎء اﻟﺒﺮﻧﺎﻣﺞ اﻟﻔﺮﻋﻲ ﻣﻦ ﺧﻼل اﻟﻜﻮد اﻟﺘﺎﻟﻲ‬ ‫;)‪positive (x‬‬ ‫ﻓﺒﻮاﺳﻄﺔ ھﺬه اﻟﺘﻌﻠﯿﻤﺔ ﻓﺈن اﻟﺒﺮﻧﺎﻣﺞ ﺳﯿﻘﻔﺰ ﻣﺒﺎﺷﺮة إﻟﻰ اﻟﺒﺮﻧﺎﻣﺞ اﻟﻔﺮﻋﻲ و اﻟﻤﺘﻤﺜﻞ ﺑـﺎﻟﺘﺎﻟﻲ‬ ‫)‪void positive (int a‬‬ ‫{‬ ‫)‪if(a<0‬‬ ‫;"‪cout<‪#include
‫;‪int a,b,c,d‬‬ ‫;‪cin>>a>>b>>c‬‬ ‫ﻛﻮد ﻃﻠﺐ اﻟﺒﺮﻧﺎﻣﺞ اﻟﻔﺮﻋﻲ ;)‪d=great(a,b,c‬‬ ‫;‪cout<max) max=y‬‬ ‫;‪if(z>max) max=z‬‬ ‫;‪return max‬‬ ‫ﺗﻌﯿﺪ ھﺬه اﻟﺘﻌﻠﯿﻤﺔ اﻟﺒﺮﻧﺎﻣﺞ إﻟﻰ آﺧﺮ ﻗﯿﻤﺔ ﻟﻠﻤﺘﺤﻮل اﻟﻤﻜﺘﻮب‬ ‫}‬ ‫ﺑﻌﺪھﺎ‬ ‫إذا ﻟﻢ ﺗﻜﻦ ﻟﺪﯾﻨﺎ ﻗﯿﻤﺔ ﻣﻌﺎدة ﻓﺈﻧﻨﺎ ﻧﺤﺬف اﻟﺴﻄﺮ ‪return max‬‬ ‫وﻧﻜﺘﺐ ﺑﺪﻻ ﻣﻦ ‪ int‬اﻟﻌﺒﺎرة ‪ void‬ﻓﻲ اﻟﺴﻄﺮﯾﻦ‬ ‫;)‪int great (int,int,int‬‬ ‫)‪int great(int x,int y,int z‬‬ ‫ﻣﻼﺣﻈﺔ ھﺎﻣﺔ ﺟﺪًا‬ ‫* ﻧﻼﺣﻆ أن اﻟﺘﺎﺑﻊ ﻓﻲ ھﺬا اﻟﺒﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﺈﻋﺎدة ﻗﯿﻤﺔ واﺣﺪة إﻟﻰ اﻟﺒﺮﻧﺎﻣﺞ اﻟﻤﺴﺘﺪﻋﻲ ﻓﻠﻼﺳﺘﻔﺎدة‬ ‫ﻣﻦ ھﺬه اﻟﻘﯿﻤﺔ اﻟﻤﺮﺟﻌﺔ ﯾﺠﺐ اﺳﺘﺨﺪام ﻣﺘﺤﻮل آﺧﺮ ﺗﺴﻨﺪ إﻟﯿﮫ ﻗﯿﻤﺔ ﺧﺮج ھﺬا اﻟﺘﺎﺑﻊ‬ ‫;)‪d=great(a,b,c‬‬ ‫* ﻋﻨﺪﻣﺎ ﯾﻜﻮن اﻟﺘﺎﺑﻊ ﯾﻌﯿﺪ ﻗﯿﻤﺔ ﻓﺈن ھﺬه اﻟﻘﯿﻤﺔ ﺳﻮف ﺗﻌﺎد ﺿﻤﻦ اﻷﻣﺮ ‪ retun‬ﺣﯿﺚ أن وﻇﯿﻔﺔ‬ ‫ھﺬه اﻟﺘﻌﻠﯿﻤﺔ ھﻲ ﺗﻤﺮﯾﺮ اﻟﻘﯿﻤﺔ اﻟﻤﻜﺘﻮﺑﺔ ﺑﻌﺪھﺎ إﻟﻰ اﻟﺒﺮﻧﺎﻣﺞ اﻟﻤﺴﺘﺪﻋﻲ‬ ‫ﻣﺜﺎل ‪:‬‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﺮﻓﻊ ﻋﺪد اﻟﻰ ﻗﻮة ﺑﺪون اﺳﺘﺨﺪام اﻷﻣﺮ )‪ pow(x,n‬اﻟﻤﻤﻜﻦ ﺿﻤﻦ‬ ‫اﻟﻤﻜﺘﺒﺔ‬ ‫>‪<math.h‬‬ ‫ﻣﻊ اﻟﻌﻠﻢ أن )‪ abs(b‬ﻋﺒﺎرة ﻋﻦ ﺗﺎﺑﻊ ﯾﻌﻄﻲ اﻟﻘﯿﻤﺔ اﻟﻤﻄﻠﻘﺔ ﻟﻠﻘﯿﻤﺔ )‪(b‬‬ ‫اﻟﺤﻞ‪:‬‬ ‫>‪#include ‪#include<math.h‬‬ ‫;)‪float power(float,int‬‬ ‫)(‪void main‬‬ ‫‪٤٨‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

{ float x; int n; cin>>x>>n; cout<<power (x,n); ‫اﻃﻠﺐ اﻟﺒﺮﻧﺎﻣﺞ اﻟﻔﺮﻋﻲ وﻃﺒﻊ اﻟﻘﯿﻤﺔ اﻟﻤﻌﺎدة‬ } float power (float a,int b) ‫اﻟﺒﺮﻧﺎﻣﺞ اﻟﻔﺮﻋﻲ‬ { float c; int i; c=1; for(i=abs(b); i>1;i--) c*=a; if(b<0) c=1/c; return c; } ‫ھﺎم ﺟﺪًا‬ return ‫ﻋﻨﺪ وﺟﻮد ﻗﯿﻤﺔ ﻣﻌﺎدة ﯾﺠﺐ اﺳﺘﺨﺪام اﻟﺘﻌﻠﯿﻤﺔ‬ ‫ إﻟﻰ اﻟﺒﺮﻧﺎﻣﺞ اﻟﺮﺋﯿﺴﻲ‬c ‫ﺗﻌﺎد ﻓﻲ ھﺬا اﻟﺒﺮﻧﺎﻣﺞ ﻗﯿﻤﺔ اﻟﻤﺘﺤﻮل‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﻣﻦ أﺟﻞ ﺣﺴﺎب و ﻃﺒﺎﻋﺔ ﻗﯿﻤﺔ اﻟﻌﻼﻗﺔ اﻟﺘﺎﻟﯿﺔ‬ Sum = 0! + 1! + 2! + 3! + 4! + 5! :‫اﻟﺤﻞ‬ #include int fact (int); void main () { int sum=0,i; for(i=0;i<=5;i++) sum+=fact(i); ‫ﻃﻠﺐ ﺧﺮج اﻟﺒﺮﻧﺎﻣﺞ اﻟﻔﺮﻋﻲ ﺧﻤﺲ ﻣﺮات وﺟﻤﻊ اﻟﻘﯿﻢ اﻟﻤﺮﺟﻌﺔ‬ cout<<"sum = "<<sum; } int fact (int n) { int i,s; s=1; for(i=n;i>=1;i--) s*=i; ٤٩ PDF created with pdfFactory Pro trial version www.pdffactory.com

‫;‪return s‬‬ ‫}‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﻣﻦ أﺟﻞ ﺣﺴﺎب وﻃﺒﺎﻋﺔ ﻗﯿﻤﺔ اﻟﺘﻌﺒﯿﺮ اﻟﺘﺎﻟﻲ‬ ‫‪Sum = 1 + x^1 + x^2 + x^3 + x^4 + x^5‬‬ ‫ﻣﻊ اﻟﻌﻠﻢ أن )‪ (x^n‬ﺗﻌﻨﻲ أن اﻟﺮﻗﻢ اﻛﺲ ﻣﺮﻓﻮع إﻟﻰ اﻟﻘﻮة ‪n‬‬ ‫ﺑﺎﺳﺘﺨﺪام اﻷﻣﺮ ;)‪pow(x,i‬‬ ‫>‪#include‪#include<math.h‬‬ ‫)(‪void main‬‬ ‫{‬ ‫;‪int sum=0,x,i‬‬ ‫;‪cin>>x‬‬ ‫)‪for(i=5;i>=1;i--‬‬ ‫;)‪sum+=pow(x,i‬‬ ‫;‪cout<<sum‬‬ ‫}‬

‫اﻟﻤﻠﻔﺎت ‪FILES‬‬ ‫اﻟﻤﻠﻔﺎت ھﻲ ﻋﺒﺎرة ﻋﻦ وﺳﺎﺋﻂ ﻟﺘﺨﺰﯾﻦ اﻟﺒﯿﺎﻧﺎت ﺑﺤﯿﺚ ﯾﺘﻢ اﻟﻜﺘﺎﺑﺔ ﻋﻠﯿﮭﺎ و ﺗﺤﻔﻆ ھﺬه اﻟﺒﯿﺎﻧﺎت‬ ‫ﺑﺪاﺧﻠﮭﺎ‬ ‫ﺳﻨﻘﻮم ﺑﮭﺬا اﻟﺪرس ﺑﺸﺮح ﻛﯿﻔﯿﺔ اﻟﺘﻌﺎﻣﻞ ﻣﻊ اﻟﻤﻠﻔﺎت ﻣﻦ ﺣﯿﺚ اﻟﻘﺮاءة و اﻟﻜﺘﺎﺑﺔ ﻋﻠﻰ أن ﻧﺴﺘﻌﯿﻦ‬ ‫ﺑﺒﻌﺾ ﻣﻜﺘﺒﺎت ﻟﻐﺔ ‪C‬‬ ‫ﺷﺮح ﺑﻌﺾ ﺗﻌﻠﯿﻤﺎت ﻟﻐﺔ ‪C‬‬ ‫أوﻻً ﯾﺠﺐ أن ﻧﻌﻠﻢ أن اﻟﻤﻜﺘﺒﺔ اﻟﺪاﻋﻤﺔ ﻟﮭﺬه اﻷواﻣﺮ ھﻲ > ‪<stdio.h‬‬ ‫ﺗﻌﻠﯿﻤـﺔ ﻓــﺘـﺢ ﻣﻠﻒ ) ( ‪fopen‬‬ ‫ﺗﻌﻠﯿﻤﺔ إﻏﻼق ﻣﻠﻒ ) ( ‪fclose‬‬ ‫ﺗﻌﻠﯿﻤﺔ ﻃﺒﺎﻋﺔ ﻣﻠﻒ ) ( ‪fprintf‬‬ ‫اﻟﺸﺮح‬ ‫ﻟﻔﺘﺢ ﻣﻠﻒ ﯾﺠﺐ ﻋﻠﯿﻨﺎ ﺗﺤﺪﯾﺪ ﻣﺴﺎر ھﺬا اﻟﻤﻠﻒ و اﻣﺘﺪاده‬

‫‪٥٠‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫وﯾﺠﺐ ﻋﻠﯿﻨﺎ أﯾﻀﺎً ﺗﺤﺪﯾﺪ ﻣﻜﺎن ﻓﻲ اﻟﺬاﻛﺮة ﻟﺤﺠﺰه ﻟﮭﺬا اﻟﻤﻠﻒ‪.‬ﺑﺎﻟﺘﻌﻠﯿﻤﺔ ‪FILE *f‬‬ ‫وﯾﺠﺐ ﻋﻠﯿﻨﺎ اﻻﻧﺘﺒﺎه ﺑﺄن اﻟﻜﻠﻤﺔ ‪ FILE‬ﯾﺠﺐ أن ﺗﻜﺘﺐ ﺑﺄﺣﺮف ﻛﺒﯿﺮة ﺣﺼﺮاً‬ ‫وﻋﻨﺪ ﻓﺘﺢ ھﺬا اﻟﻤﻠﻒ ﯾﺠﺐ ﻋﻠﯿﻨﺎ إﺧﺒﺎر اﻟﺒﺮﻧﺎﻣﺞ ﺑﺄﻧﻨﺎ ھﻞ ﻧﺮﯾﺪ اﻟﻘﺮاءة ﻣﻦ ھﺬا اﻟﻤﻠﻒ أم اﻟﻜﺘﺎﺑﺔ‬ ‫أم اﻻﺛﻨﺘﯿﻦ ﻣﻌﺎً ‪.‬‬ ‫;)"‪f = fopen("c:\\student.txt","w‬‬ ‫ﻓﻠﻜﺘﺎﺑﺔ ﻧﺴﺘﺨﺪم‬ ‫"‪"r‬‬ ‫أﻣﺎ ﻟﻠﻘﺮاءة ﻓﻨﺴﺘﺨﺪم‬ ‫وﻓﻲ ﺣﺎل اﻟﻘﺮاءة و اﻟﻜﺘﺎﺑﺔ ﻧﺴﺘﺨﺪم "‪ "r+‬أو "‪"w+‬‬ ‫ﻣﺜﺎل ‪:‬‬ ‫> ‪#include <stdio.h‬‬ ‫) ( ‪void main‬‬ ‫{‬ ‫‪FILE * f‬‬ ‫;)"‪f=fopen("c:\\stdudent.txt","w‬‬ ‫;)"‪fprintf(f," welcome to C++‬‬ ‫;)‪fclose(f‬‬ ‫}‬ ‫وﻟﻄﺒﺎﻋﺔ ‪ welcome to C++‬ﻓﻲ ھﺬا اﻟﻤﻠﻒ اﻟﺬي ﺗﻢ ﻓﺘﺤﮫ أو إﻧﺸﺎءه ﻧﺴﺘﺨﺪم اﻟﻜﻮد اﻟﺘﺎﻟﻲ‬ ‫;)"‪fprintf(f," welcome to C++‬‬ ‫وإن اﻟﺘﻌﻠﯿﻤﺔ ‪ fprint‬ﺗﻜﺎﻓﺊ اﻟﺘﻌﻠﯿﻤﺔ ‪ cout‬ﻓﻲ اﻟﻤﻜﺘﺒﺔ ‪iostream‬‬ ‫وﻣﻦ أﺟﻞ إﻏﻼق ھﺬا اﻟﻤﻠﻒ اﺳﺘﺨﺪﻣﻨﺎ ھﺬا اﻟﻜﻮد‬ ‫;)‪fclose(f‬‬ ‫وإن ھﺬه اﻟﺘﻌﻠﯿﻤﺔ ﺗﻘﻮم ﺑﺈﻏﻼق ھﺬا اﻟﻤﻠﻒ وﺗﻨﮭﻲ اﻟﺘﻌﺎﻣﻞ ﻣﻌﮫ وﻋﺪم ﻛﺘﺎﺑﺔ ھﺬه اﻟﺘﻌﻠﯿﻤﺔ ﯾﻤﻜﻦ أن‬ ‫ﯾﺆدي ﻓﻲ ﺑﻌﺾ اﻷﺣﯿﺎن إﻟﻰ ﺗﻠﻒ أو ﺿﯿﺎع اﻟﺒﯿﺎﻧﺎت ‪.‬‬ ‫ﻣﻼﺣﻈﺔ ‪:‬‬ ‫* ﻋﻨﺪ اﻟﻄﻠﺐ ﻣﻦ اﻟﺒﺮﻧﺎﻣﺞ أﻣﺮ ﻓﺘﺢ ﻣﻠﻒ ﻓﺈن اﻟﺒﺮﻧﺎﻣﺞ إن ﻟﻢ ﯾﺠﺪ ھﺬا اﻟﻤﻠﻒ ﻓﺈن اﻟﺒﺮﻧﺎﻣﺞ‬ ‫ﺳﯿﻨﺸﺊ ھﺬا اﻟﻤﻠﻒ‪.‬‬ ‫* ﻓﻲ ﺣﺎل ﻋﺪم ﻛﺘﺎﺑﺔ ﻣﺴﺎر اﻟﻤﻠﻒ اﻟﻤﻨﺸﺄ ﻣﻦ ﻗﺒﻞ اﻟﺒﺮﻧﺎﻣﺞ ﻓﺈن اﻟﺒﺮﻧﺎﻣﺞ ﺳﯿﻨﺸﺊ ھﺬا اﻟﻤﻠﻒ ﻓﻲ‬ ‫اﻟﻤﺴﺎر اﻻﻓﺘﺮاﺿﻲ ﻟﻠﺒﺮﻧﺎﻣﺞ ) اﻟﻤﺴﺎر اﻟﻤﻮﺟﻮد ﺑﮫ اﻟﺒﺮﻧﺎﻣﺞ (‪.‬‬ ‫ﻣﺜﺎل ‪:‬‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﺈﻧﺸﺎء ﻣﻠﻒ ﻧﺼﻲ ﻣﻦ أﺟﻞ ﺗﺨﺰﯾﻦ ﻋﻼﻣﺎت ﻃﺎﻟﺐ ﺿﻤﻦ ھﺬا اﻟﻤﻠﻒ‬ ‫>‪#include‪#include<stdio.h‬‬ ‫>‪#include<stdlib.h‬‬ ‫)(‪void main‬‬ ‫‪٥١‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫{‬ ‫ﺣﺠﺰ ﻣﻜﺎن ﻟﻠﻤﻠﻒ اﻟﻤﻨﺸﺄ ;‪FILE *out‬‬ ‫;‪int score‬‬ ‫)‪if((out=fopen("test.txt","w"))==NULL‬‬ ‫{‬ ‫;"‪cout<<"can not open file\n‬‬ ‫;)‪exit(-1‬‬ ‫ھﺬه اﻟﺘﻌﻠﯿﻤﺔ ﺗﻘﻮم ﺑﺈﻧﮭﺎء اﻟﺒﺮﻧﺎﻣﺞ‬ ‫}‬ ‫;")‪cout<<"enter a test score (0 terminate input‬‬ ‫;‪cin>>score‬‬ ‫)‪while(score !=0‬‬ ‫{‬ ‫;)‪fprintf(out,"%d\n",score‬‬ ‫;"‪cout <<"enter anather score‬‬ ‫;‪cin>>score‬‬ ‫}‬ ‫;)‪fclose(out‬‬ ‫}‬ ‫ﺷﺮح اﻟﺒﺮﻧﺎﻣﺞ‬ ‫ﺑﺪاﯾﺔ ﺗﻢ اﺳﺘﺨﺪام ﻣﻜﺘﺒﺔ ﺟﺪﯾﺪ ة ھﻲ ‪ stdlip‬ﻻﺳﺘﺨﺪام ﺑﻌﺾ اﻷواﻣﺮ ﻣﻨﮭﺎ‬ ‫ﺳﻨﻜﺘﺐ اﻷﻛﻮاد ﺑﺎﻟﺘﺮﺗﯿﺐ ﻣﻊ اﻟﺸﺮح‬ ‫;‪FILE *out‬‬ ‫ﺗﺤﺪﯾﺪ ﻣﻜﺎن ﻓﻲ اﻟﺬاﻛﺮة ﻟﻠﺘﻌﺎﻣﻞ ﻣﻊ اﻟﻤﻠﻒ وأﻃﻠﻘﻨﺎ ﻋﻠﯿﮫ اﺳﻢ ‪out‬‬ ‫وﻛﻤﺎ ذﻛﺮﻧﺎ ﺳﺎﺑﻘﺎ ﻛﻠﻤﺔ ‪ FILE‬ﺗﻜﺘﺐ ﺑﺄﺣﺮف ﻛﺒﯿﺮة ﺣﺼﺮاً‬ ‫;‪int score‬‬ ‫ﺗﻌﺮﯾﻒ ﻣﺘﺤﻮل ﻋﻠﻰ أﻧﮫ ﻋﺪد ﺻﺤﯿﺢ‬ ‫)‪if((out=fopen("test.txt","w"))==NULL‬‬ ‫ﻣﻌﻨﻰ ‪ ) NULL‬وﺗﻜﺘﺐ ﺑﺄﺣﺮف ﻛﺒﯿﺮة ﺣﺼﺮاً ( ﻓﺸﻞ ‪ ،‬إﺣﺒﺎط وھﻲ ﻛﻠﻤﺔ ﻣﺤﺠﻮزة‬ ‫ﻓﯿﻜﻮن اﻟﻜﻮد ‪:‬‬ ‫إذا ﻟﻢ ﯾﺘﻢ ﻓﺘﺢ ھﺬا اﻟﻤﻠﻒ ﻟﺴﺒﺐ ﻣﻦ اﻷﺳﺒﺎب ) ﻣﺜﻞ ‪ :‬اﻟﻘﺮص ﻣﺤﻤﻲ ﺿﺪ اﻟﻜﺘﺎﺑﺔ ( ﻧﻔﺬ اﻟﺴﻄﺮ‬ ‫اﻟﺘﺎﻟﻲ ﻓﻲ اﻟﺒﺮﻧﺎﻣﺞ وإﻻ ﻓﺘﺨﻄﺎه‬ ‫;)‪exit(-1‬‬ ‫ﻗﻢ ﺑﺈﻧﮭﺎء اﻟﺒﺮﻧﺎﻣﺞ وھﻲ ﻣﻦ اﻟﻤﻜﺘﺒﺔ ‪stdlib‬‬ ‫إن ﺣﻠﻘﺔ ‪ if‬ﻣﺘﻀﻤﻨﺔ اﻷﻣﺮﯾﻦ ﻣﻌﺎ ‪:‬‬ ‫إﻋﻄﺎء رﺳﺎﻟﺔ اﻟﺨﻄﺄ و اﻟﺨﺮوج ﻣﻦ اﻟﺒﺮﻧﺎﻣﺞ‬ ‫;‪cin>>score‬‬ ‫أﻣﺮ إدﺧﺎل ﻗﯿﻤﺔ اﻟﻤﺘﺤﻮل وھﻮ إدﺧﺎل ﻋﻼﻣﺔ اﻟﻄﺎﻟﺐ‬ ‫‪٥٢‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫)‪while(score !=0‬‬ ‫ﻃﺎﻟﻤﺎ أن اﻟﻌﻼﻣﺔ ﻻ ﺗﺴﺎوي اﻟﺼﻔﺮ ﻧﻔﺬ اﻷواﻣﺮ اﻟﺘﺎﻟﯿﺔ‬ ‫وﻗﺪ ﻛﺘﺒﺖ ﻣﻦ أﺟﻞ ﺗﻀﻤﯿﻦ اﻟﺒﺮﻧﺎﻣﺞ أﻣﺮ ﻟﯿﺘﺴﻨﻰ ﻟﻨﺎ اﻟﺨﺮوج ﻣﻦ اﻟﺒﺮﻧﺎﻣﺞ‬ ‫;)‪fprintf(out,"%d\n",score‬‬ ‫ﺗﻔﯿﺪ ھﺬه اﻟﺘﻌﻠﯿﻤﺔ أن ﻣﺎ ﯾﻜﺘﺐ ﻓﻲ اﻟﻤﻠﻒ ھﻮ ﻣﻦ ﻧﻮع ‪int‬‬ ‫;)‪fclose(out‬‬ ‫وھﻮ أﻣﺮ ﻹﻏﻼق اﻟﻤﻠﻒ‪.‬‬

‫ﻛﯿﻔﯿﺔ اﻟﺘﻌﺎﻣﻞ ﺑﯿﻦ اﻟﻤﻠﻔﺎت و اﻟﺴﺠﻼت‬ ‫وﺗﻜﻮن ﺑﻨﻔﺲ ﻃﺮﯾﻘﺔ ﻃﺒﺎﻋﺔ اﻟﻤﻠﻒ ﻣﻊ ﻣﺮاﻋﺎة ﻛﯿﻔﯿﺔ ﻛﺘﺎﺑﺔ اﻷﻣﺮ ‪ fprintf‬و ﯾﻜﻮن ﻛﺘﺎﻟﻲ‬ ‫; ) اﺳﻢ اﻟﺴﺠﻞ ‪ .‬اﺳﻢ ﻣﺘﺤﻮل اﻟﺴﺠﻞ ‪ , " % s" ,‬اﺳﻢ اﻟﻤﻠﻒ اﻟﻤﺤﺠﻮز ﺑﺎﻟﺬاﻛﺮة (‪fprintf‬‬ ‫ﻓﻤﺜﻼً ‪ :‬ﻟﯿﻜﻦ ﻟﺪﯾﻨﺎ اﻟﺴﺠﻞ اﻟﺘﺎﻟﻲ ‪:‬‬ ‫‪struct employee‬‬ ‫{‬ ‫;‪int empno‬‬ ‫;]‪char name[20‬‬ ‫;‪float salary‬‬ ‫;}‬ ‫ﻓﺈذا أردﻧﺎ ﻃﺒﺎﻋﺔ ﻣﺼﻔﻮﻓﺔ اﻻﺳﻢ ]‪ name[20‬داﺧﻞ اﻟﻤﻠﻒ ﻓﺈﻧﻨﺎ ﻧﻜﺘﺐ اﻟﻜﻮد‬ ‫;)‪fprintf (myfile %s ", emp[i].name‬‬ ‫أﻣﺎ إن أردﻧﺎ ﻃﺒﺎﻋﺔ ھﺬا اﻟﺴﺠﻞ ﺑﺎﻟﻜﺎﻣﻞ ﻓﺈن اﻟﻜﻮد ﯾﻜﻮن ) ﻋﻠﻰ ﺳﻄﺮ واﺣﺪ ﯾﻜﺘﺐ ھﺬا اﻟﻜﻮد (‬ ‫‪fprintf‬‬ ‫;)‪(myfile,"%d\t%s\t%f\n",emp[i].empno,emp[i].name,emp[i].salary‬‬

‫‪٥٣‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

‫ﻣﻼﺣﻈﺔ‬ ‫ﺗﻜﺎﻓﺊ ﻓﻲ ﻟﻐﺔ ‪C++‬‬ ‫‪%d‬‬ ‫‪%s‬‬ ‫‪%f‬‬ ‫‪%c‬‬

‫‪int‬‬ ‫‪string‬‬ ‫‪float‬‬ ‫‪char‬‬

‫ﻛﻤﺎ ﻧﻼﺣﻆ ﺑﺄﻧﻨﺎ ﻋﻨﺪ اﻟﺘﻌﺎﻣﻞ ﻣﻊ اﻟﻤﻠﻔﺎت ﻧﺤﻦ ﺑﺤﺎﺟﺔ ﻟﻠﺘﻌﺎﻣﻞ ﻣﻊ اﻟﻤﻜﺘﺒﺔ > ‪<stdio.h‬‬ ‫ﻣﺜﺎل ‪:‬‬ ‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﻘﺮاءة ﺑﯿﺎﻧﺎت اﻟﻤﻮﻇﻔﯿﻦ وﻣﻦ ﺛﻢ ﯾﻘﻮم ﺑﺘﺨﺰﯾﻨﮭﺎ ﻓﻲ اﻟﻤﻠﻒ ‪ out.txt‬ﻋﻠﻤﺎً‬ ‫أن اﻟﺒﯿﺎﻧﺎت ﺗﺘﻀﻤﻦ رﻗﻢ اﻟﻤﻮﻇﻒ واﺳﻤﮫ وﻛﺬﻟﻚ راﺗﺒﮫ‬ ‫*ﺳﻨﺸﺮح اﻷﻛﻮاد داﺧﻞ اﻟﺒﺮﻧﺎﻣﺞ‬ ‫*ﻣﻼﺣﻈﺔ ‪:‬‬ ‫ﺑﻌﺾ اﻷﻛﻮاد ﻃﻮﯾﻠﺔ وﻟﻢ ﺗﺘﺴﻊ ﻣﻌﻨﺎ ﺑﺴﻄﺮ واﺣﺪ وﻟﻢ ﻧﺮﻏﺐ ﺑﺘﺼﻐﯿﺮ اﻟﺨﻂ وﻟﺬﻟﻚ ﯾﺮﺟﻰ‬ ‫اﻻﻧﺘﺒﺎه ﻣﻊ اﻟﻌﻠﻢ ﺑﺄﻧﻨﺎ ﻧﺒﮭﻨﺎ إﻟﻰ ذﻟﻚ ﻓﻲ ﻣﻜﺎن ﻛﺘﺎﺑﺔ اﻟﻜﻮد‬ ‫>‪#include‪#include<stdio.h‬‬ ‫>‪#include<stdlib.h‬‬ ‫)(‪void main‬‬ ‫{‬ ‫;‪FILE *myfile‬‬ ‫ﺣﺠﺰ ﻣﻜﺎن ﻓﻲ اﻟﺬاﻛﺮة ﺑﮭﺬا اﻻﺳﻢ‬ ‫ﻛﺘﺎﺑﺔ اﻟﺴﺠﻞ ‪struct employee‬‬ ‫{‬ ‫;‪int empno‬‬ ‫ﺗﻌﺮﯾﻒ رﻗﻢ اﻟﻤﻮﻇﻒ داﺧﻞ اﻟﺴﺠﻞ‬ ‫ﺗﻌﺮﯾﻒ ﻣﺼﻔﻮﻓﺔ اﺳﻢ اﻟﻤﻮﻇﻒ داﺧﻞ اﻟﺴﺠﻞ ;]‪char name[20‬‬ ‫;‪float salary‬‬ ‫ﺗﻌﺮﯾﻒ راﺗﺐ اﻟﻤﻮﻇﻒ داﺧﻞ اﻟﺴﺠﻞ‬ ‫;}‬ ‫ﺗﻌﺮﯾﻒ ﻣﺼﻔﻮﻓﺔ ﻣﺘﺤﻮل ﻟﻠﺴﺠﻞ ;]‪employee emp[100‬‬ ‫;‪int i,n‬‬ ‫)‪if((myfile=fopen("out.txt","w"))==NULL‬‬ ‫{‬ ‫;"‪cout<<"can not open file‬‬ ‫;)‪exit(-1‬‬ ‫;"‪cout<<"enter the number of employee<=100‬‬ ‫;‪cin>>n‬‬ ‫ﺗﺤﺪﯾﺪ ﻋﺪد اﻟﺴﺠﻼت اﻟﻤﺮاد إدﺧﺎﻟﮭﺎ )‪for(i=0;i
cout<<"enter the number of employee"<<<" : "; cin>>emp[i].empno; cout<<"enter the name of employee"<<<" : "; cin>>emp[i].name; cout<<"enter the salary of employee"<<<" : "; cin>>emp[i].salary; fprintf (myfile,"%d\t%s\t%f\n",emp[i].empno,emp[i].name,emp[i].salary); ‫ﯾﻜﺘﺐ ھﺬا اﻟﻜﻮد ﻓﻲ ﺳﻄﺮ واﺣﺪ ﻓﯿﺮﺟﻰ اﻻﻧﺘﺒﺎه‬ ‫وھﻮ أﻣﺮ ﻃﺒﺎﻋﺔ اﻟﺴﺠﻼت داﺧﻞ اﻟﻤﻠﻒ ﺑﻮاﺳﻄﺔ أﻣﺮ اﻟﻄﺒﺎﻋﺔ‬ } fclose(myfile); ‫أﻣﺮ إﻏﻼق اﻟﻤﻠﻒ‬ }

‫اﻟﻘﺮاءة واﻟﻜﺘﺎﺑﺔ ﻋﻠﻰ ﻣﻠﻒ ﺑﺒﺮﻧﺎﻣﺞ واﺣﺪ‬ ‫ﻣﺜﺎل‬ ‫ وﻣﻦ ﺛﻢ‬out.txt ً‫اﻛﺘﺐ ﺑﺮﻧﺎﻣﺞ ﯾﻘﻮم ﺑﻘﺮاءة ﺑﯿﺎﻧﺎت اﻟﻤﻮﻇﻔﯿﻦ وذﻟﻚ ﻣﻦ اﻟﻤﻠﻒ اﻟﻤﻨﺸﺄ ﺳﺎﺑﻘﺎ‬ ‫ﯾﻘﻮم اﻟﺒﺮﻧﺎﻣﺞ ﺑﺘﺮﺗﯿﺒﮭﺎ ﺣﺴﺐ اﻻﺳﻢ ﺛﻢ ﯾﻘﻮم ﺑﺈﻋﺎدة ﺗﺨﺰﯾﻨﮭﺎ ﺑﻨﻔﺲ اﻟﻤﻠﻒ‬ #include #include<stdio.h> #include<stdlib.h> #include<string.h> ‫ﻣﻜﺘﺒﺔ ﺟﺪﯾﺪة ﻻﺳﺘﺨﺪام أﻣﺮ اﻟﻤﻘﺎرﻧﺔ و أﻣﺮ اﻻﺳﺘﻔﺴﺎر‬ void main() { FILE *myfile; struct employee { int empno; char name[20]; float salary; ٥٥ PDF created with pdfFactory Pro trial version www.pdffactory.com

‫;}‬ ‫;‪employee emp[100],temp‬‬ ‫;‪int i=0,j,n=0‬‬ ‫)‪if((myfile=fopen("out.txt","r+"))==NULL‬‬ ‫ﺷﺮح ﺳﺎﺑﻘﺎً‬ ‫{‬ ‫;"‪cout<<"can not open file‬‬ ‫;)‪exit(-1‬‬ ‫}‬ ‫ﯾﺠﺐ اﻻﻧﺘﺒﺎه ﺑﺄن اﻟﻜﻮد اﻟﺘﺎﻟﻲ ﯾﻜﺘﺐ ﻋﻠﻰ ﺳﻄﺮ واﺣﺪ‬ ‫‪fscanf(myfile,"%d%s%f",&emp[i].empno,&emp[i].name,&emp[i].s‬‬ ‫;)‪alary‬‬ ‫أﻣﺮ ﻓﺘﺢ اﻟﻤﻠﻒ وﯾﺠﺐ اﻻﻧﺘﺒﺎه ﻟﻌﻼﻣﺔ اﻟﺮﺑﻂ & ﺑﯿﻦ اﻟﻤﺘﺤﻮﻻت‬ ‫))‪while(!feof (myfile‬‬ ‫‪ feof‬ﺗﻌﻨﻲ ‪ finch in of file‬وﻣﻌﻨﺎھﺎ ھﻞ اﻧﺘﮭﻰ اﻟﻤﻠﻒ وﯾﻜﻮن اﻟﺸﺮح‬ ‫ﻃﺎﻟﻤﺎ ﻟﻢ ﯾﺘﻢ اﻟﻮﺻﻮل ﻟﻨﮭﺎﯾﺔ اﻟﻤﻠﻒ ﺗﺎﺑﻊ‬ ‫{‬ ‫;‪i++‬‬ ‫زﯾﺎدة واﺣﺪ واﺣﺪ ﻟﻠﻮﺻﻮل ﻟﻠﺴﻄﺮ اﻟﺘﺎﻟﻲ ﻟﻨﻘﻞ اﻟﻤﻌﻠﻮﻣﺎت ﻣﻨﮫ‬ ‫ﻋﺪاد ﻟﻤﻌﺮﻓﺔ ﻋﺪد اﻻﺳﻄﺮ ;‪n++‬‬ ‫ﯾﻜﺘﺐ اﻟﻜﻮد ﻋﻠﻰ ﺳﻄﺮ واﺣﺪ‬ ‫‪fscanf(myfile,"%d%s%f",&emp[i].empno,&emp[i].name,&emp[i].s‬‬ ‫;)‪alary‬‬ ‫ﻓﺘﺢ ﺳﻄﺮ ﻛﻤﺎ ﺗﻢ ذﻛﺮه وﻟﻜﻦ ھﻨﺎ ﯾﻜﻮن ھﺬا اﻟﺴﻄﺮ ھﻮ اﻟﺴﻄﺮ اﻟﺘﺎﻟﻲ ﻛﻞ ﻣﺮة ﺿﻤﻦ اﻟﺤﻠﻘﺔ‬ ‫}‬ ‫)‪for(i=0;i0‬‬ ‫‪ strcmp‬ﺗﻌﻠﯿﻤﺔ ﻣﻘﺎرﻧﺔ وﺗﻜﻮن اﻟﻨﺘﯿﺠﺔ أﻛﺒﺮ ﻣﻦ اﻟﺼﻔﺮ ﻓﻲ ﺣﺎل ﻛﺎﻧﺖ اﻟﻘﯿﻤﺔ اﻷوﻟﻰ أﻛﺒﺮ‬ ‫ﻣﻦ اﻟﻘﯿﻤﺔ اﻟﺜﺎﻧﯿﺔ‬ ‫وﺗﻜﻮن ﻣﺴﺎوﯾﺔ ﻟﻠﺼﻔﺮ إذا ﺗﺴﺎوت اﻟﻘﯿﻤﺘﯿﻦ‬ ‫وأﺻﻐﺮ ﻣﻦ اﻟﺼﻔﺮ إذا ﻛﺎﻧﺖ اﻟﺜﺎﻧﯿﺔ أﻛﺒﺮ ﻣﻦ اﻷوﻟﻰ‬ ‫{‬ ‫;]‪temp=emp[i‬‬ ‫ﻛﻮد ذﻛﻲ ﻟﺘﺒﺪﯾﻞ ﻣﻜﺎن اﻟﺴﺠﻼت‬ ‫;]‪emp[i]=emp[j‬‬ ‫;‪emp[j]=temp‬‬ ‫}‬ ‫)‪for(i=0;i
‫‪fprintf(myfile,"%d\t%s\t%f\t",emp[i].empno,emp[i].name,emp[i].sal‬‬ ‫;)‪ary‬‬ ‫;)‪fclose(myfile‬‬ ‫}‬ ‫ﺷﺮح اﻟﻜﻮد اﻟﺬي اﺳﻤﯿﻨﺎه ذﻛﻲ‬ ‫ﻧﻀﻊ اﻟﺴﺠﻞ ]‪ emp [i‬داﺧﻞ اﻟﺴﺠﻞ ‪ temp‬ﻟﯿﺘﺴﻨﻰ ﻟﻨﺎ‬ ‫ﻧﻘﻞ اﻟﺴﺠﻞ ]‪ emp [j‬اﻟﻰ داﺧﻞ اﻟﺴﺠﻞ ]‪ emp [i‬وﻣﻦ ﺛﻢ‬ ‫]‪emp [j‬‬ ‫ﻧﻨﻘﻞ اﻟﺴﺠﻞ ‪ temp‬اﻟﻰ داﺧﻞ اﻟﺴﺠﻞ‬ ‫وﺑﮭﺬا ﻧﻜﻮن ﻗﺪ أﺑﺪﻟﻨﺎ ﻣﻜﺎﻧﺎ اﻟﺴﺠﻞ ]‪ emp [i‬ﺑﺎﻟﺴﺠﻞ ]‪emp [j‬‬ ‫ﺗﻢ ﺑﺤﻤﺪ اﷲ اﻻﻧﺘﮭﺎء ﻣﻦ ﻛﺘﺎﺑﺔ ھﺬه اﻟﻨﻮﺗﺔ وإن ﺷﺎء اﷲ ھﻲ ﺧﺎﻟﯿﺔ ﻣﻦ اﻷﺧﻄﺎء‬ ‫وﺟﻞ ﻣﻦ ﻻ ﯾﺴﮭﻰ‬ ‫وﻧﺮﺟﻮ ﻣﻨﻜﻢ اﻟﻤﺘﺎﺑﻌﺔ ﺑﮭﺎ وإﻋﺎدة ﻧﺸﺮھﺎ‬ ‫وﻧﺮﺟﻮ اﻟﺪﻋﺎء ﻟﻨﺎ ﻓﻲ ﻇﮭﺮ اﻟﻐﯿﺐ وﷲ اﻟﻤﺒﺘﻐﻰ ﻣﻦ وراء اﻟﻘﺼﺪ‬ ‫أﺧﻮﻛﻢ ﻓﻲ اﷲ‬

‫ﻣﺤﻤﺪ‬

‫ھﻨﺪاوي ‪٢٠٠٦ / ٨ / ٣٠‬‬ ‫‪[email protected]‬‬

‫‪٥٧‬‬ ‫‪PDF created with pdfFactory Pro trial version www.pdffactory.com‬‬

Related Documents

Step By Step
November 2019 33
Tunics Step By Step
November 2019 36
Step By Step
June 2020 27
Workflow Step By Step
December 2019 18
Step By Step Bf
November 2019 19