《C++捷径教程》读书笔记--Chapter 4--程序控制语句(第一部分)
来源:互联网 发布:知字的草书 编辑:程序博客网 时间:2024/05/17 04:04
//--《C++捷径教程》读书笔记--Chapter 4--程序控制语句(第一部分)
//--Chapter 4--程序控制语句
//--11/8/2005 Tues.
//--Computer Lab
//--Liwei
//--程序#1 魔术数字程序
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int magic;
int guess;
magic=rand();
cout<<"magic is: "<<magic<<endl;
cout<<"Enter your guess: "<<endl;
cin>>guess;
if(guess==magic) cout<<"** Right **"<<endl;
return 0;
}
//--程序#2 魔术数字程序
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int magic;
int guess;
magic=rand();
cout<<"magic is: "<<magic<<endl;
cout<<"Enter your guess: "<<endl;
cin>>guess;
if(guess==magic)
cout<<"** Right **"<<endl;
else
cout<<"**Sorry,you're wrong.**"<<endl;
return 0;
}
//--程序#3 第一个数除以第二个数
#include <iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter two numbers: ";
cin>>a>>b;
if(b) //0是假
cout<<a/b<<endl;
else
cout<<"cannot divide by zero. "<<endl;
return 0;
}
//--程序#4 魔术数字程序改进
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int magic;
int guess;
magic=rand();
cout<<"magic is: "<<magic<<endl;
cout<<"Enter your guess: "<<endl;
cin>>guess;
if(guess==magic)
{
cout<<"** Right **"<<endl;
cout<<magic<<" is the magic number."<<endl;
}
else
{
cout<<"**Sorry,you're wrong.**"<<endl;
if(guess>magic)
cout<<"Your guess is too high."<<endl;
else
cout<<"Your guess is too low."<<endl;
}
return 0;
}
//--程序#5 if-else-if
#include <iostream>
using namespace std;
int main()
{
int x;
for(x=0;x<6;x++)
{
if(x==1)
cout<<"x is one."<<endl;
else if(x==2)
cout<<"x is two."<<endl;
else if(x==3)
cout<<"x is three."<<endl;
else if(x==4)
cout<<"x is four."<<endl;
else
cout<<"x is not between 1--4."<<endl;
}
return 0;
}
//--程序#6 1--99的平方根
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int num;
//float num;//double num
double sq_root;
for(num=1;num<100;num++)
{
sq_root=sqrt((double) num);//sqrt的参数必须是 double类型
cout<<"num is:"<<num<<" sq_root is:"<<" "<<sq_root<<endl;
}
return 0;
}
//--程序#7 for的使用
#include <iostream>
using namespace std;
int main()
{
int i;
for(i=100;i>=-100; i-=5)
cout<<i<<" ";
return 0;
}
//--程序#8 for的使用
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i;
for(i=0; !kbhit();i++)
cout<<i<<' ';
cout<<"dddddd"<<endl;
getchar();
return 0;
}
//--程序#9 for的部分省略的使用
#include <iostream>
using namespace std;
int main()
{
int x;
for(x=0;x!=123;)
{
cout<<"Enter a number: ";
cin>>x;
}
//getchar();
return 0;
}
//--程序#10 switch的使用
#include <iostream>
using namespace std;
int main()
{
int choice;
cout<<"Help on:/n"<<endl;
cout<<"1.for/n";
cout<<"2.if/n";
cout<<"3.switch/n";
cout<<"Enter choice(1-3): ";
cin>>choice;
cout<<endl;
switch(choice)
{
case 1:
cout<<"for is c++'s most versatile loop./n";
break;
case 2:
cout<<"if is c++'s conditional branch statement./n";
break;
case 3:
cout<<"switch is c++'s multiway branch statement./n";
break;
default:
cout<<"You must enter a number between 1--3./n";
}
//getchar();
return 0;
}
//--程序#11 switch的使用
#include <iostream>
using namespace std;
int main()
{
int i;
for(i=0; i<5; i++)
{
switch(i)
{
case 0: cout<<"less than 1./n";
case 1: cout<<"less than 2./n";
case 2: cout<<"less than 3./n";
case 3: cout<<"less than 4./n";
case 4: cout<<"less than 5./n";
}
cout<<endl;
}
//getchar();
return 0;
}
//--程序#12 显示可打印字符
#include <iostream>
using namespace std;
int main()
{
unsigned char ch;
ch=32;
while(ch)//可能有问题
{
cout<<ch<<' ';
ch++;
}
cout<<endl;
//getchar();
return 0;
}
//--程序#13 while的用法
#include <iostream>
using namespace std;
int main()
{
int len;
cout<<"Enter length (1 to 79): ";
cin>>len;
while(len>0 && len<80)
{
cout<<'.';
len--;
}
cout<<endl;
//getchar();
return 0;
}
- 《C++捷径教程》读书笔记--Chapter 4--程序控制语句(第一部分)
- 《C++捷径教程》读书笔记--Chapter 4--程序控制语句(第二部分)
- 《C++捷径教程》读书笔记--Chapter 6--指针(第一部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第一部分)
- 《C++捷径教程》读书笔记--Chapter 7--函数,第一部分:基础知识(第一部分)
- 《C++捷径教程》读书笔记--Chapter 7--函数,第一部分:基础知识(第二部分)
- 《C++捷径教程》读书笔记--Chapter 5--数组和字符串(第一部分)
- 《C++捷径教程》读书笔记--Chapter 9--更多的数据类型与运算符(第一部分)
- 《C++捷径教程》读书笔记--Chapter 12--类的深入学习(第一部分)
- 《C++捷径教程》读书笔记--Chapter 6--指针(第二部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第二部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第三部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第四部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第五部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第六部分)
- 《C++捷径教程》读书笔记--Chapter 5--数组和字符串(第二部分)
- 《C++捷径教程》读书笔记--Chapter 9--更多的数据类型与运算符(第二部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(完结)
- 工作线程中弹出对话框
- 六世达赖仓央嘉措情诗六十六首
- 分手的理由
- 转载王垠的《完全用GNU/Linux工作》!!!
- test
- 《C++捷径教程》读书笔记--Chapter 4--程序控制语句(第一部分)
- 今天的课
- 对病毒Script.Adware.Yobous.d的一点分析和建议
- 解决浏览器被web.9983.com劫持及不定期弹出广告窗口的问题(第3版)
- 身份证号码检验函数
- 使用数组实现快速数据查找
- DoS和DDoS攻击为什么是Internet的灾难
- 使用SYGUI快速创建类似于Office XP/2003 界面风格的应用程序
- 用C#使用HttpWebRequest Post数据时如何保持Session