《C++捷径教程》读书笔记--Chapter 4--程序控制语句(第二部分)
来源:互联网 发布:淘宝街头篮球三无账号 编辑:程序博客网 时间:2024/03/28 19:07
//--《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++捷径教程》读书笔记--Chapter 4--程序控制语句(第二部分)
- 《C++捷径教程》读书笔记--Chapter 4--程序控制语句(第一部分)
- 《C++捷径教程》读书笔记--Chapter 6--指针(第二部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第二部分)
- 《C++捷径教程》读书笔记--Chapter 7--函数,第一部分:基础知识(第二部分)
- 《C++捷径教程》读书笔记--Chapter 5--数组和字符串(第二部分)
- 《C++捷径教程》读书笔记--Chapter 9--更多的数据类型与运算符(第二部分)
- 《C++捷径教程》读书笔记--Chapter 6--指针(第一部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第一部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第三部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第四部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第五部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第六部分)
- 《C++捷径教程》读书笔记--Chapter 8--函数,第二部分:引用,重载和默认参数
- 《C++捷径教程》读书笔记--Chapter 7--函数,第一部分:基础知识(第一部分)
- 《C++捷径教程》读书笔记--Chapter 5--数组和字符串(第一部分)
- 《C++捷径教程》读书笔记--Chapter 9--更多的数据类型与运算符(第一部分)
- 《C++捷径教程》读书笔记--Chapter 12--类的深入学习(第一部分)
- DNN框架初探
- SQL Profiler的找法
- 直接读取进程内存实现两个应用程序之间的通信
- QQ秀文字,很酷的!
- blog时钟、天气预报、搜索、Media播放器源码
- 《C++捷径教程》读书笔记--Chapter 4--程序控制语句(第二部分)
- 服了
- Crontab 使用
- SMS modem资料
- SUS client regedit
- IPX协议设置
- 网络最经典命令
- 时间和日历类的设计(Java的Date和Calendar的C++实现)
- 路由器的配置与调试