//--《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;
}
此博客为《C++捷径教程》读书笔记,聚焦程序控制语句。包含do - while用法、魔术数字程序改进、continue和break使用示例,还有找质数程序及新魔术数字程序等,通过多个C++代码示例展示程序控制语句的应用。

被折叠的 条评论
为什么被折叠?



