HW5.pdf

‫ﺗﻤﺮﻳﻦ ﺳﺮﻱ ﭘﻨﺠﻢ ﻣﺒﺎﻧﻲ ﺑﺮﻧﺎﻣﻪ ﻧﻮﻳﺴﻲ ‪C++‬‬
‫‪-‬‬
‫ﻣﻬﻠﺖ ﺍﺭﺳﺎﻝ‪ :‬ﺷﻨﺒﻪ ‪ 8‬ﺩﻱ ‪1386‬‬
‫ﺗﻤﺮﻳﻦ ﻫﺎﻱ ﺧﻮﺩ ﺭﺍ ﺑﻪ ﻓﺮﻣﺖ ‪ zip‬ﻭ ﺑﺎ ﻧﺎﻡ ‪ HW5_StudentID‬ﮐﻪ ﺩﺭ ﺁﻥ ‪ StudentID‬ﺷﻤﺎﺭﻩ ﺩﺍﻧﺸﺠﻮﻳﻲ ﺷﻤﺎﺳﺖ ‪ ،‬ﺑﻪ ﺁﺩﺭﺱ‬
‫‪ [email protected]‬ﺑﻔﺮﺳﺘﻴﺪ‪.‬‬
‫‪-‬‬
‫ﺑﻪ ﺑﺮﻧﺎﻣﻪﻫﺎﻱ ﻣﺸﺎﺑﻪ ﻧﻤﺮﻩ ﻣﻨﻔﻲ ﺗﻌﻠﻖ ﺧﻮﺍﻫﺪ ﮔﺮﻓﺖ‪.‬‬
‫ﻣﺴﺎﻟﻪ ﺍﻭﻝ ‪. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .‬‬
‫ﺗﺒﺪﻳﻞ ﺭﺷﺘﻪ ﺑﻪ ﻋﺪﺩ‬
‫ﺩﺭ ﺍﻳﻦ ﺑﺮﻧﺎﻣﻪ ﻣﻲﺧﻮﺍﻫﻴﻢ ﺑﺎ ﮔﺮﻓﺘﻦ ﻳﮏ ﺭﺷﺘﻪ ﻋﺪﺩﻱ )ﺍﻋﺸﺎﺭﻱ ﻭ ﻳﺎ ﺻﺤﻴﺢ( ﺍﺯ ﮐﺎﺭﺑﺮ‪ ،‬ﺁﻥ ﺭﺍ ﺑﻪ ﻧﻮﻉ ‪ double‬ﺗﺒﺪﻳﻞ ﮐﻨﻴﻢ‪ .‬ﺗﺎﺑﻌﻲ‬
‫ﺑﺮﺍﻱ ﺧﻮﺍﻧﺪﻥ ﺩﻭ ﺭﺷﺘﻪ ﺍﺯ ﮐﺎﺭﺑﺮ ﻭ ﺗﺎﺑﻌﻲ ﺑﺮﺍﻱ ﺗﺒﺪﻳﻞ ﻳﮏ ﺭﺷﺘﻪ ﺑﻪ ﻋﺪﺩ ﺣﻘﻴﻘﻲ ﺑﻨﻮﻳﺴﻴﺪ‪ .‬ﭘﺎﺭﺍﻣﺘﺮﻫﺎﻱ ﺧﺮﻭﺟﻲ ﺭﺍ ﺑﺎﻳﺪ ﺍﺯ ﻃﺮﻳﻖ‬
‫ﻓﺮﺍﺧﻮﺍﻧﻲ ﺑﺎ ﺍﺭﺟﺎﻉ‪ 1‬ﺑﻪ ﺗﻮﺍﺑﻊ ﺍﺭﺳﺎﻝ ﮐﻨﻴﺪ‪.‬‬
‫ﺣﺎﻝ ﺑﻪ ﮐﻤﮏ ﺍﻳﻦ ﺩﻭ ﺗﺎﺑﻊ ﺑﺮﻧﺎﻣﻪ ﺍﻱ ﺑﻨﻮﻳﺴﻴﺪ ﮐﻪ ﺍﺯ ﮐﺎﺭﺑﺮ ﺩﻭ ﺭﺷﺘﻪ ﻋﺪﺩﻱ ﮔﺮﻓﺘﻪ ﻭ ﺣﺎﺻﻠﻀﺮﺏ ﺁﻥ ﻫﺎ ﺭﺍ ﺩﺭ ﺧﺮﻭﺟﻲ ﭼﺎﭖ ﮐﻨﺪ‪.‬‬
‫ﻣﺜﺎﻝ‪:‬‬
‫‪Enter String1: 12.3450‬‬
‫‪Enter string2: 430‬‬
‫‪The Product is: 5308.35‬‬
‫ﻣﺴﺎﻟﻪ ﺩﻭﻡ ‪. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .‬‬
‫ﻣﺤﺎﺳﺒﺎﺕ ﺑﺎ ﺍﻋﺪﺍﺩ ﺑﺰﺭﮒ‬
‫ﺩﺭ ﺍﻳﻦ ﺑﺮﻧﺎﻣﻪ ﻣﻲﺧﻮﺍﻫﻴﻢ ﺗﻮﺍﺑﻌﻲ ﺑﺮﺍﻱ ﺟﻤﻊ ﻭ ﺿﺮﺏ ﺍﻋﺪﺍﺩ ﺻﺤﻴﺢ ﻭ ﺑﺰﺭﮒ )ﮐﻪ ﺁﻥﻫﺎ ﺭﺍ ﻧﻤﻲﺗﻮﺍﻥ ﺩﺭ ﺍﻧﻮﺍﻉ ﺩﺍﺩﻩ ﺻﺤﻴﺢ ‪ c++‬ﺫﺧﻴﺮﻩ‬
‫ﮐﺮﺩ(‪ ،‬ﭘﻴﺎﺩﻩﺳﺎﺯﻱ ﮐﻨﻴﻢ‪ .‬ﺑﺮﺍﻱ ﺍﻳﻦ ﻣﻨﻈﻮﺭ ﺑﺎ ﻓﺮﺽ ﺍﻳﻨﮑﻪ ﻃﻮﻝ ﺍﻋﺪﺍﺩ ﺍﺯ ‪ 256‬ﺗﺠﺎﻭﺯ ﻧﻤﻲﮐﻨﻨﺪ‪ ،‬ﺍﺯ ﺭﺷﺘﻪﻫﺎ ﺑﺮﺍﻱ ﻧﮕﻬﺪﺍﺭﻱ ﺍﻳﻦ ﺍﻋﺪﺍﺩ‬
‫ﺍﺳﺘﻔﺎﺩﻩ ﮐﻨﻴﺪ‪ .‬ﺍﺑﺘﺪﺍ ﺗﺎﺑﻌﻲ ﺑﺮﺍﻱ ﺟﻤﻊ ﺩﻭ ﺭﺷﺘﻪ ﻋﺪﺩﻱ ﺑﻪ ﺻﻮﺭﺕ ﺯﻳﺮ ﺑﻨﻮﻳﺴﻴﺪ‪:‬‬
‫)‪char *sum(char *str1,char *str2‬‬
‫‪Call by reference‬‬
‫‪1‬‬
‫ﺳﭙﺲ ﺑﻪ ﮐﻤﮏ ﺍﻳﻦ ﺗﺎﺑﻊ‪ ،‬ﺗﺎﺑﻊ ﺯﻳﺮ ﺭﺍ ﺑﺮﺍﻱ ﺿﺮﺏ ﺩﻭ ﺭﺷﺘﻪ ﻋﺪﺩﻱ ﺗﮑﻤﻴﻞ ﮐﻨﻴﺪ‪:‬‬
‫)‪char *product(char *str1,char *str2‬‬
‫ﺣﺎﻝ ﺗﺎﺑﻌﻲ ﺑﺮﺍﻱ ﻣﺤﺎﺳﺒﻪ ﻓﺎﮐﺘﻮﺭﻳﻞ ﻋﺪﺩ ﺻﺤﻴﺢ ‪ n‬ﺑﻪ ﺻﻮﺭﺕ ﺑﺎﺯﮔﺸﺘﻲ ﺑﻨﻮﻳﺴﻴﺪ‪:‬‬
‫)‪char *factorial(int n‬‬
‫ﻭ ﺑﺎﻻﺧﺮﻩ ﺑﺎ ﮔﺮﻓﺘﻦ ‪ n‬ﺍﺯ ﮐﺎﺭﺑﺮ‪ ،‬ﻓﺎﮐﺘﻮﺭﻳﻞ ﺁﻥ ﺭﺍ ﺩﺭ ﺧﺮﻭﺟﻲ ﻧﻤﺎﻳﺶ ﺩﻫﻴﺪ‪.‬‬
‫ﻣﺜﺎﻝ‪:‬‬
‫‪Enter n: 28‬‬
‫‪28!= 304888344611713860501504000000‬‬
‫ﻣﺴﺎﻟﻪ ﺳﻮﻡ ‪. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .‬‬
‫ﺯﻳﺮ ﻣﺠﻤﻮﻋﻪ ﻫﺎ‬
‫ﺑﺮﻧﺎﻣﻪ ﺍﻱ ﺑﻨﻮﻳﺴﻴﺪ ﮐﻪ ﺑﺎ ﮔﺮﻓﺘﻦ ﻋﺪﺩ ‪ n‬ﺍﺯ ﮐﺎﺭﺑﺮ ﺗﻤﺎﻡ ‪ 2 n‬ﺯﻳﺮ ﻣﺠﻤﻮﻋﻪﻱ }‪ {1,2,…,n‬ﺭﺍ ﺩﺭ ﺧﺮﻭﺟﻲ ﭼﺎﭖ ﮐﻨﺪ‪ .‬ﺗﺮﺗﻴﺐ ﺍﻳﻦ‬
‫ﻣﺠﻤﻮﻋﻪ ﻫﺎ ﺩﺭ ﺧﺮﻭﺟﻲ ﺑﺎﻳﺪ ﺑﻪ ﺻﻮﺭﺕ ﺻﻌﻮﺩﻱ ﺑﺎﺷﺪ‪.‬‬
‫ﻧﻤﻮﻧﻪ ﺍﺟﺮﺍﻱ ﺑﺮﻧﺎﻣﻪ‪:‬‬
‫‪Enter n: 3‬‬
‫}{‬
‫}‪{1‬‬
‫}‪{1,2‬‬
‫}‪{1,2,3‬‬
‫}‪{1,3‬‬
‫}‪{2‬‬
‫}‪{2,3‬‬
‫}‪{3‬‬
‫ﺩﻗﺖ ﮐﻨﻴﺪ ﮐﻪ ﻣﻨﻈﻮﺭ ﺍﺯ ﺻﻌﻮﺩﻱ ﺑﻮﺩﻥ ﺍﻳﻦ ﺍﺳﺖ ﮐﻪ ﺩﺭ ﻫﺮ ﺩﻭ ﻣﺠﻤﻮﻋﻪ ﻣﺘﻮﺍﻟﻲ‪ ،‬ﺍﻭﻟﻴﻦ ﻋﻀﻮ ﻏﻴﺮ ﻳﮑﺴﺎﻥ ﺑﺮﺍﻱ ﻣﺠﻤﻮﻋﻪ ﭘﺎﻳﻴﻨﻲ‪ ،‬ﺑﺰﺭﮔﺘﺮ‬
‫ﺑﺎﺷﺪ )ﺭﺍﻫﻨﻤﺎﻳﻲ‪ :‬ﺍﺯ ﺍﻳﺪﻩ ﺑﺎﺯﮔﺸﺘﻲ ﺍﺳﺘﻔﺎﺩﻩ ﮐﻨﻴﺪ( ‪.‬‬
‫ﻣﺴﺎﻟﻪ ﭼﻬﺎﺭﻡ ‪. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .‬‬
‫ﻣﺮﺗﺐ ﺳﺎﺯﻱ ﻣﻌﺪﻝ ﺩﺍﻧﺶ ﺁﻣﻮﺯﺍﻥ‬
‫ﺩﺭ ﺍﻳﻦ ﺑﺮﻧﺎﻣﻪ ﺑﺎﻳﺪ ﺍﻃﻼﻋﺎﺕ ﻣﺮﺑﻮﻁ ﺑﻪ ﺩﺍﻧﺶ ﺁﻣﻮﺯﺍﻥ ﻳﮏ ﮐﻼﺱ ‪ n‬ﻧﻔﺮﻩ ﺭﺍ ﺍﺯ ﻳﮏ ﻓﺎﻳﻞ ﻭﺭﻭﺩﻱ ﺑﻪ ﺁﺩﺭﺱ ”‪ “c:\input.txt‬ﺧﻮﺍﻧﺪﻩ ﻭ‬
‫ﭘﺲ ﺍﺯ ﻣﺮﺗﺐ ﺳﺎﺯﻱ ﺩﺍﻧﺶ ﺁﻣﻮﺯﺍﻥ ﺑﺮ ﺣﺴﺐ ﻣﻌﺪﻝ‪ ،‬ﺍﻃﻼﻋﺎﺕ ﻣﺮﺗﺐ ﺷﺪﻩ ﺭﺍ ﺑﻪ ﺻﻮﺭﺗﻲ ﮐﻪ ﺩﺭ ﻧﻤﻮﻧﻪ ﺁﻭﺭﺩﻩ ﺷﺪﻩ ﺍﺳﺖ‪ ،‬ﺩﺭ ﻓﺎﻳﻞ ﺧﺮﻭﺟ ﻲ‬
‫ﺑﻪ ﺁﺩﺭﺱ‪ “c:\output.txt” :‬ﺫﺧﻴﺮﻩ ﻧﻤﺎﻳﻴﺪ‪.‬‬
‫ﺗﻌﺪﺍﺩ ﺩﺍﻧﺶ ﺁﻣﻮﺯﺍﻥ ﮐﻼﺱ)‪ (n‬ﺩﺭ ﺳﻄﺮ ﺍﻭﻝ ﻓﺎﻳﻞ ﻭﺭﻭﺩﻱ ﺫﺧﻴﺮﻩ ﺷﺪﻩ ﺍﺳﺖ ﻭ ﺳﭙﺲ ﺩﺭ ‪ 3*n‬ﺧﻂ ﻣﺘﻮﺍﻟﻲ ﺍﺑﺘﺪﺍ ﻧﺎﻡ‪ ،‬ﺳﭙﺲ ﻧﺎﻡ‬
‫ﺧﻮﺍﻧﻮﺍﺩﮔﻲ ﻭ ﺑﻌﺪ ﻣﻌﺪﻝ ﺩﺍﻧﺶ ﺁﻣﻮﺯﺍﻥ ﺫﺧﻴﺮﻩ ﺷﺪﻩ ﺍﺳﺖ‪.‬‬
‫ﻣﺜﻼ ﺑﺮﺍﻱ ﻓﺎﻳﻞ ﻭﺭﻭﺩﻱ ﻳﮏ ﮐﻼﺱ ‪ 2‬ﻧﻔﺮﻩ ﺑﻪ ﺻﻮﺭﺕ ‪:‬‬
‫‪2‬‬
‫‪Ali‬‬
‫‪Ahmadi‬‬
‫‪14.34‬‬
‫‪Reza‬‬
‫‪Sadegh Zadeh‬‬
‫‪18.57‬‬
‫ﻓﺎﻳﻞ ﺧﺮﻭﺟﻲ ﺑﻪ ﺻﻮﺭﺕ ﺯﻳﺮ ﺑﺎﻳﺪ ﺑﺎﺷﺪ ‪:‬‬
‫‪1-Reza Sadegh Zadeh 18.57‬‬
‫‪2-Ali Ahmadi 14.34‬‬
‫ﺑﺮﺍﻱ ﻫﺮ ﺩﺍﻧﺶ ﺁﻣﻮﺯ ﺍﺯ ‪ class‬ﻳﺎ ‪ struct‬ﺍﻱ ﺑﺎ ﺭﮐﻮﺭﺩﻫﺎﻱ ‪:‬‬
‫;]‪char first_name[20‬‬
‫;]‪char last_name[20‬‬
‫;‪float avarage‬‬
‫ﺩﺭ ﻧﻈﺮ ﺑﮕﻴﺮﻳﺪ‪ .‬ﻫﻤﭽﻨﻴﻦ ﻃﻮﻝ ﺁﺭﺍﻳﻪ ﺩﺍﻧﺶ ﺁﻣﻮﺯﺍﻥ ﻧﺒﺎﻳﺪ ﺍﺯ ‪ n‬ﺑﻴﺸﺘﺮ ﺑﺎﺷﺪ )ﺁﺭﺍﻳﻪ ﺩﺍﻧﺶ ﺁﻣﻮﺯﺍﻥ ﺭﺍ ﺑﻪ ﮐﻤﮏ ﺍﺷﺎﺭﻩ ﮔﺮﻫﺎ ﺗﻌﺮﻳﻒ ﮐﻨﻴﺪ(‪.‬‬