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

原创粉丝点击