這裡的第一篇博文《C++ primer plus 第7、8章》部分

來源:互聯網
上載者:User

</p><p>////////////////////////////////////////第7、8章 部分題目////////////////////////////////////////////////</p><p>1/*求中獎機率*/</p><p>#include<iostream></p><p>long double probability(unsigned number_1 , unsigned number_2 , unsigned picks_1 , unsigned picks_2) ; //因為號碼只有正數,所以</p><p>int main(void)<br />{<br /> using namespace std ;</p><p> double total_1 , total_2 ;<br /> double choice_1 , choice_2 ;</p><p> cout << "Enter the two total number of the choice on the game card and/n"<br /> "the two number of pick allowed :(any q to quit) /n" ;</p><p> while(cin >> total_1 >> total_2 >> choice_1 >> choice_2 && choice_2 < total_2 && choice_1 < total_1)<br /> {<br /> cout << "You have one chance in " ;<br /> cout << probability(total_1 , total_2 , choice_1 , choice_2) ;<br /> cout << " of winning./n" ;<br /> cout << "Next two number (q to quit) : " ;<br /> }</p><p> cout << "Bye Bye ./n" ;</p><p> return 0 ;<br />}</p><p>long double probability(unsigned number_1 , unsigned number_2 , unsigned picks_1 , unsigned picks_2)<br />{<br /> long double result_1 = 1.0 ;<br /> long double result_2 = 1.0 ;</p><p> long double n ;<br /> unsigned p ;</p><p> for(n = number_1 , p = picks_1 ; p > 0 ; p-- , n-- )<br /> result_1 = result_1 * n / p ;</p><p> for(n = number_2 , p = picks_2 ; p > 0 ; p-- , n--)<br /> result_2 = result_2 * n / p ;</p><p> return result_1 * result_2 ;<br />}</p><p>2/*函數對數組的操作*/</p><p>#include<iostream></p><p>#define num 6</p><p>using namespace std ;</p><p>int Fill_arrary(double ary[] , int n) ; //填充數組<br />void Show_arrary(double ary[] , int n) ; //顯示數組<br />void Reverse_arrary(double ary[] , int n) ; //倒置數組</p><p>int main(void)<br />{<br /> using namespace std ;</p><p> double arrary[num] ;<br /> int n ;</p><p> n = Fill_arrary(arrary , num) ;<br /> Show_arrary(arrary , n) ;<br /> Reverse_arrary(arrary , n) ;</p><p> cout << "After show :/n/n" ;<br /> Show_arrary(arrary , n);</p><p> cout << "/nBye Bye/n" ;</p><p> return 0 ;<br />}</p><p>int Fill_arrary(double ary[] , int n)<br />{<br /> cout << "Please enter the value(q to quit) : " ;</p><p> for(int i = 0 ; i < n && cin >> ary[i] <br /> if(++i < n)<br /> cout << "Next value: " ;</p><p> return i ;<br />}</p><p>void Show_arrary(double ary[] , int n)<br />{<br /> cout << "You have enter this number: /n";</p><p> for(int i = 0 ; i < n ; i++)<br /> cout << ary[i] << endl ;<br />}</p><p>void Reverse_arrary(double ary[] , int n)<br />{<br /> double temp ;</p><p> for(int i = 0 ; i < (n / 2) ; i++ )<br /> {<br /> if(i && i != (n-1))<br /> {<br /> temp = ary[i] ;<br /> ary[i] = ary[n-1-i] ;<br /> ary[n-1-i] = temp ;<br /> }<br /> else<br /> continue ;<br /> }</p><p> cout << endl ;<br />}</p><p>//////////////////////////////第八章 /////////////////////////////////</p><p>//函數重載</p><p>#include<iostream></p><p>using namespace std ;</p><p>void print_string(char *str ) ; //這裡面有兩個名字相同的函數,但是其參數不同,所以編譯器解釋他們時就不一樣。。所以。。<br />void print_string(char *str , int n) ; //</p><p>int main(void)<br />{<br /> char str[20] ;<br /> int n = 0 ;</p><p> cout << "Please enter the string : " ;<br /> cin.getline(str , 21) ; //一種讀取一行的輸入</p><p> cout << "Please enter the times : " ;<br /> cin >> n ;</p><p> cout << "if you don't use the /"n/" : " ;<br /> cout << "/nIt will print : " << endl ;</p><p> print_string(str) ;</p><p> cout << "/n/n/n/n" ;<br /> if(n)<br /> {<br /> cout << "if you use the /"n/" : " ;<br /> cout << "/nIt will print : " << endl ;<br /> print_string(str , n) ;<br /> }</p><p> cout << "/n/n/nByeBye/n" ;</p><p> return 0;<br />}</p><p>void print_string(char *str)<br />{<br /> cout << str << endl ;<br />}</p><p>void print_string(char *str , int n)<br />{<br /> int i = 0;</p><p> for ( ; i < n ; i++)<br /> {<br /> print_string(str) ;<br /> }</p><p>}</p><p>////////////////////////////////<br />/////糖果<br />#include<iostream><br />#include<cstring></p><p>typedef struct CandyBar //利用typedef定義<br /> {<br /> char name[20] ;<br /> float weight ;<br /> int hot ;<br /> } candybar ;</p><p>void set_candy( candybar &candy , char *str , float &weight , int &hot) ; //利用引用變數。。新知識。引用,其實就是變數的別名<br />void print_candy(const candybar &candy ) ; //作用與指標類似</p><p>int main(void)<br />{<br /> using namespace std ;</p><p> candybar cand_1 = { "make in china" ,<br /> 25.6f,<br /> 15<br /> }; </p><p> cout << "The candy : /n" ;<br /> print_candy(cand_1) ;</p><p> cout << "/nAfter set : /n" ;<br /> set_candy(cand_1 , cand_1.name , cand_1.weight , cand_1.hot) ;<br /> print_candy(cand_1) ;</p><p> cout << "BYe/n" ;</p><p> return 0;<br />}</p><p>void set_candy( candybar &candy , char *str , float &weight , int &hot)<br />{</p><p> strcpy(str , "make in japan") ;<br /> weight = 12.6f ;<br /> hot = 12 ;<br />}</p><p>void print_candy(const candybar &candy )<br />{<br /> using namespace std ;</p><p> cout << "candy company name : " << candy.name << endl ;<br /> cout << "candy weight : " << candy.weight << endl;<br /> cout << "candy hot : " << candy.hot << endl ;<br />}</p><p>///////轉換大小寫<br />#include<iostream><br />#include<string><br />#include<cctype></p><p>using namespace std ; //為什麼要放在這裡,因為string類放在std中</p><p>void tranuper(string &str) ;</p><p>int main(void)<br />{</p><p> string str ;</p><p> cout << "Enter a string (q to quit) : " ;<br /> while( getline(cin , str) && str != "q" ) //q判斷結束<br /> {<br /> tranuper(str) ;<br /> cout << "Next string (q to quit) : " ;<br /> }</p><p> cout << "Bye Bye./n/n" ;</p><p> return 0 ;<br />}</p><p>void tranuper(string &str)<br />{<br /> int i ;<br /> int end = str.size() ; //str.size()是string的大小<br /> for(i = 0 ; i < end ; i++)<br /> cout.put(toupper(str[i])) ; //這裡要用cout.put來輸出一個字元,而不是用cout<br /> cout << endl ;<br />}</p><p>///////////////////////</p><p>//函數重載與預設參數</p><p>#include<iostream></p><p>#include<cstring></p><p>using namespace std ;</p><p>struct stringy{<br /> char *str ;<br /> int ct ;<br /> } ;</p><p>void set(stringy &bea , const char *str ) ;</p><p>void show(const char *pstr , int n = 1) ; //注意這裡的n是有賦值的(預設參數),但是在下面的定義是沒有的,<br /> //這裡比較容易出錯。請注意</p><p>void show(const stringy &bea , int n = 1) ; //這一個是新知識,利用了函數重載的功能。</p><p>int main(void)<br />{<br /> stringy beany ;<br /> char testing[] = "Reality isn't what it used to be. :" ;</p><p> set(beany , testing) ;</p><p> show(beany) ;<br /> show(beany , 2) ;</p><p> testing[0] = 'D' ;<br /> testing[1] = 'u' ;</p><p> show(testing) ;<br /> show(testing , 3) ;</p><p> show("Done! ") ;</p><p> return 0 ;<br />}</p><p>void set(stringy &bea , const char *str )<br />{<br /> bea.ct = strlen(str) ;</p><p> bea.str = new char[bea.ct+1] ;</p><p> strcpy(bea.str , str) ;<br />}</p><p>void show(const char *pstr , int n )<br />{<br /> for(int i = 0 ; i < n ; i++)<br /> cout << pstr << endl ;<br />}</p><p>void show(const stringy &bea , int n )<br />{<br /> for(int i = 0 ; i < n ; i++)<br /> {<br /> cout << "String : " << bea.str << endl ;<br /> cout << "The lenght : " << bea.ct << endl ;<br /> cout << "/n/n" ;<br /> }<br />}</p><p>///////////////////</p><p>#include<iostream></p><p>template<class Any> //新知識:函數模板,當對不同類型,但是演算法相同時可用模板來建構函式。<br />Any max5(const Any *p) ;</p><p>int main(void)<br />{<br /> using namespace std ;</p><p> int ival_arrary[5] ;<br /> double dval_arrary[5] ;</p><p> cout << "Please enter 5 int value : " ;<br /> for(int i = 0 ; i < 5 ; i++)<br /> {<br /> cin >> ival_arrary[i] ;<br /> }</p><p> cout << "Please enter 5 double value : " ;<br /> for(i = 0 ; i < 5 ; i++)<br /> {<br /> cin >> dval_arrary[i] ;<br /> }</p><p> cout << "The max in the 5 int value is : " << max5(ival_arrary) << endl ;<br /> cout << "The max in the 5 double value is : " << max5(dval_arrary) << endl ;</p><p> cout << "Bye /n" ;</p><p> return 0 ;<br />}</p><p>template<class Any><br />Any max5(const Any *p)<br />{<br /> Any max = p[0] ;</p><p> for(int i = 0 ; i < 5 ; i++)<br /> {<br /> if(p[i] > max)<br /> max = p[i] ;<br /> }<br /> return max ;<br />}</p><p>/////////////////</p><p>//函數模板與具體化</p><p>#include<iostream><br />#include<cstring></p><p>template<class Any> //針對基本類型的模板<br />Any maxn( Any ary[] , const int n) ;</p><p>template<> char * maxn( char *ary[] , const int n) ; //具體化:針對字元數組的模板。</p><p>int main(void)<br />{<br /> using namespace std ;</p><p> int i_arrary[6] = {2,4,1,7,6,3} ;<br /> double d_arrary[4] = {1.2 , 4.6 ,3.2 ,1.1} ;<br /> char* str_arrary[5] = {<br /> "where there is will this is way ." ,<br /> "who is my next code ? " ,<br /> "you ? or you ?" ,<br /> "but I think you are't " ,<br /> "Only me ,the world"<br /> } ;</p><p> cout << "The max int value : " << maxn(i_arrary , 6) << endl ;<br /> cout << "The max double value : " << maxn(d_arrary , 4) << endl ;<br /> cout << "The longest string : " << maxn(str_arrary , 5) << endl ;</p><p> cout << "/nByeBye/n" ;</p><p> return 0 ;<br />}</p><p>template<class Any><br />Any maxn( Any ary[] , const int n)<br />{<br /> Any max = ary[0] ;</p><p> for(int i = 0 ; i < n ; i++)<br /> {<br /> if(ary[i] > max)<br /> max = ary[i] ;<br /> }</p><p> return max ;<br />}</p><p>template<> char * maxn( char* ary[] , const int n)<br />{<br /> char maxlen = strlen(ary[0]) ;<br /> char *maxpstr = ary[0] ;</p><p> for(int i = 0 ; i < n ; i++)<br /> {<br /> if(strlen(ary[i]) > maxlen)<br /> {<br /> maxlen = strlen(ary[i]) ;<br /> maxpstr = ary[i] ;<br /> }<br /> }</p><p> return maxpstr ;<br />}<br />////////////////////////////</p><p>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.