返回列表 發帖

[隨堂測驗] 函式的建立與執行 - 罰寫小幫手

試以自訂函式的方式, 完成該程式.
譬如: 自訂一個 print(string,int) 的函式
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. void test(string sentence,int times)
  5. {
  6.         for(int i=0;i<times;i++)
  7.         {
  8.                 cout<<sentence<<endl;
  9.         }
  10. }
  11. int main()
  12. {
  13.         string sentence;int times=0;
  14.         cout<<"請輸入罰寫內容"<<endl;
  15.         cin>>sentence;
  16.         cout<<"請輸入次數"<<endl;
  17.         cin>>times;
  18.         test(sentence,times);
  19.         system("pause");
  20.         return 0;
  21. }
複製代碼

TOP

  1. #include <cstdlib>
  2. #include <iostream>
  3. using namespace std;
  4. void print(string sentence,int times)
  5. {
  6. for(int i=1;i<=times;i++)
  7. {
  8. cout<<sentence<<endl;
  9. }
  10. }
  11. int main()
  12. {
  13.     string sentence;
  14.     int times=0;
  15.     cout<<"請輸入罰寫內容:";
  16.     cin>>sentence;
  17.     cout<<"請輸入次數:";
  18.     cin>>times;
  19.     print(sentence,times);
  20.     system("pause");
  21.     return 0;
  22. }
複製代碼

TOP

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. void print(string str,int num)
  5. {
  6.     for(int i=0;i<=num;i++)
  7.       {
  8.         cout<<str<<endl;        
  9.       }   
  10. }
  11. int main()
  12. {
  13.      string str;
  14.      int num=0;
  15.      cout<<"罰寫小幫手"<<endl;
  16.      cout<<"請輸入罰寫內容";
  17.      cin>>str;
  18.      cout<<"次數";
  19.      cin>>num;
  20.      print(str,num);
  21.      system("pause");
  22.      return 0;
  23. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. void print(string str,int g)
  5. {
  6.     for(int i=0;i<=z;i++)
  7.       {
  8.         cout<<str<<endl;        
  9.       }   
  10. }
  11. int main()
  12. {
  13.      string str;
  14.      int g=0;
  15.      cout<<"罰寫小幫手"<<endl;
  16.      cout<<"罰寫內容:";
  17.      cin>>str;
  18.      cout<<"次數";
  19.      cin>>g;
  20.      print(str,g);
  21.      system("pause");
  22.      return 0;
  23. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. void print(string str,int z)
  5. {
  6.     for(int i=0;i<=z;i++)
  7.       {
  8.         cout<<str<<endl;        
  9.       }   
  10. }
  11. int main()
  12. {
  13.      string str;
  14.      int z=0;
  15.      cout<<"罰寫小幫手"<<endl;
  16.      cout<<"請輸入罰寫內容";
  17.      cin>>str;
  18.      cout<<"次數";
  19.      cin>>z;
  20.      print(str,z);
  21.      system("pause");
  22.      return 0;
  23. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. void test(string w,int num)
  5. {
  6.          for(int i=1;i<=num;i++)
  7.           {
  8.               cout<<w<<endl;     
  9.           }
  10. }
  11. int main()
  12.   {
  13.     int num;
  14.     string w;
  15.     cout<<"罰寫內容:";
  16.     cin>>w;
  17.     cout<<"罰寫次數:";
  18.     cin>>num;
  19.     test(w,num);
  20.     system("pause");
  21.     return 0;
  22.   }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. void page(string sentence,int times)
  5. {
  6.     for(int i=0;i<times;i++)
  7.     {
  8.         cout<<sentence<<endl;
  9.     }
  10. }
  11. int main()
  12. {
  13.     string sentence;
  14.     int times=0;
  15.     cout<<"罰寫內容?"<<endl;
  16.     cin>>sentence;
  17.     cout<<"次數?"<<endl;
  18.     cin>>times;
  19.     page(sentence,times);
  20.     system("pause");
  21.     return 0;
  22. }
複製代碼

TOP

  1. #include <cstdlib>
  2. #include <iostream>
  3. using namespace std;
  4. void print(string sentence,int times)
  5. {
  6.      for(int i=1;i<=times;i++)
  7.      {
  8.              cout<<sentence<<endl;
  9.      }
  10. }
  11. int main()
  12. {
  13.     string sentence;
  14.     int times=0;
  15.     cout<<"請輸入罰寫內容:";
  16.     cin>>sentence;
  17.     cout<<"請輸入次數:";
  18.     cin>>times;
  19.     print(sentence,times);
  20.     system("pause");
  21.     return 0;
  22. }
複製代碼

TOP

返回列表