//--《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;
}