《C++捷徑教程》讀書筆記–Chapter 4–程式控制語句(第二部分)

來源:互聯網
上載者:User

//--《C++捷徑教程》讀書筆記--Chapter 4--程式控制語句(第二部分)
//--Chapter 4--程式控制語句
//--11/9/2005 Wed.
//--Computer Lab
//--Liwei

//--程式#14  do--while的用法
#include <iostream>
using namespace std;

int main()
{
   int num;
   do{
      cout<<"Enter a number (100 to stop): ";
   cin>>num;
   }while(num!=100);
   //getchar();
   return 0;
}

//--程式#15  魔術數字程式改進
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
   int magic;
   int guess;
   magic=rand();
   cout<<"magic is: "<<magic<<endl;

   do{
       cout<<"Enter your guess: ";
    cin>>guess;
    if(guess==magic)
    {
       cout<<"**Right**"<<endl;
    cout<<magic<<" is the magic number."<<endl;
    }
    else
    {
          cout<<"...sorry,you are wrong."<<endl;   
    if(guess>magic)
     cout<<"Your guess is too high."<<endl;
    else
     cout<<"Your guess is too low."<<endl;
    }  
   }while(guess!=magic);
   //getchar();
   return 0;
}

//--程式#16 continue的使用
#include <iostream>
using namespace std;

int main()
{
   int x;
   for(x=0; x<=100; x++)
   {
      if(x%2) continue;
   cout<<x<<' ';//只輸出偶數
   }
   cout<<endl;
   //getchar();
   return 0;
}

//--程式#17 breake的使用
#include <iostream>
using namespace std;

int main()
{
   int t;
   for(t=0; t<100; t++)
   {
      if(t==10)  break;
   cout<<t<<' ';
   }
   cout<<endl<<"=================================="<<endl;
   //getchar();
   return 0;
}

//--程式#18 breake的使用
#include <iostream>
using namespace std;

int main()
{
   int t,count;
   for(t=0; t<100; t++)
   {
       count=1;
    for(;;)
    {
        cout<<count<<' ';
     count++;
     if(count==10)
      break;
    }
       cout<<endl;  
   }
   cout<<endl<<"=================================="<<endl;
   //getchar();
   return 0;
}

//--程式#19  找2--1000之間的質數
#include <iostream>
using namespace std;

int main()
{
   int i,j;
   for(i=2; i<100; i++)
   {
       for(j=2; j<=(i/j); j++)
     if(!(i%j))
      break;
       if(j>(i/j))
      cout<<i<<" is prime./n";
   }
   cout<<endl<<"======================="<<endl;
   //getchar();
   return 0;
}

//--程式#19  找2--1000之間的質數
#include <iostream>
using namespace std;

int main()
{
   int i,j;
   for(i=2; i<100; i++)
   {
       for(j=2; j<=(i/j); j++)  //經典就在 j<=(i/j) 
        if(!(i%j))
      break;
       if(j>(i/j))
            cout<<i<<" is prime./n";
   }
   cout<<endl<<"======================="<<endl;
 
   for(i=2; i<100; i++)
   {
       for(j=1; j<=i; j++)
        if(!(i%j))
      break;
       if(j>=i)
            cout<<i<<" is prime./n";
   }
   cout<<endl<<"======================="<<endl;

   for(i=2; i<100; i++)
   {
       for(j=2; j<=i/2; j++)
        if(!(i%j))
      break;
       if(j>i/2)
            cout<<i<<" is prime./n";
   }
   cout<<endl<<"======================="<<endl;
   //getchar();
   return 0;
}

//--程式#20  魔術數字程式
#include <iostream>
#include <cstdlib>
using namespace std;

void play(int m);

int main()
{
   int option;
   int magic;
   magic=rand();
   cout<<"magic is :"<<magic<<endl;

   do{
      cout<<"1. Get a new magic number."<<endl;
   cout<<"2. Play."<<endl;
   cout<<"3. Quit."<<endl;
   do{
       cout<<"Enter your choice: "<<endl;
    cin>>option;
   }while(option<1 || option>3);

      switch(option)
   {
       case 1:  magic=rand(); break;
    case 2:  play(magic);  break;
    case 3:  cout<<"Goodbye."<<endl; break; 
   }
   }while(option!=3);
   cout<<endl<<"======================="<<endl;
   //getchar();
   return 0;
}

void play(int m)
{
    int t,x;
 for(t=0; t<100; t++)
 {
     cout<<"Guess the number: ";
  cin>>x;
  if(x==m)
  {
     cout<<"**Right**"<<endl;
     return;
  }
  else
  {
       if(x<m)  cout<<"Too low."<<endl;
    else cout<<"Too high."<<endl;
  }
 }
 cout<<"You've used up all your guesses. try again."<<endl;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.