注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Mister.Hu(巷里人家)

Go abroad!

 
 
 

日志

 
 
关于我

A campus photograph palyer,an enthusiastic reader,a solitary writer,a future traffic engineer.

网易考拉推荐

C++强化【2】  

2015-07-23 11:43:47|  分类: Senior |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

实例17 打印ASCII

#include <iostream>

using namespace std;

 int main()

{

    int i =0;

    for(int rows = 0; rows < 16; rows++)

    {

        i = rows;

        while(i <= 127)

        {

                         switch(i)

                         {

                         case 7://震铃

                                  cout<<i<<"="<<"\\a"<<" | ";

                                  break;

                         case 8://退格

                                  cout<<i<<"="<<"\\b"<<" | ";

                                  break;

                         case 9://水平制表符

                                  cout<<i<<"="<<"\\t"<<" | ";

                                  break;

                         case 10://换行

                                  cout<<i<<"="<<"\\n"<<" | ";

                                  break;

                         case 11://竖直制表符

                                  cout<<i<<"="<<"\\v"<<" | ";

                                  break;

                         case 12://换页

                                  cout<<i<<"="<<"\\f"<<" | ";

                                  break;

                         case 13://回车

                                  cout<<i<<"="<<"\\r"<<" | ";

                                  break;

                         default:

                                  cout<<i<<"="<<char(i)<<" | ";

                                  break;

                         }

 

            i+=16;//每隔16个另起一列

        }

        cout<<endl;

    }

     return 0;

}

关键点有二,第一是其换行方法的思路,先以行数(16 rows)为控制进行第一次循环,然后进行列的控制,每16个换一列,所以又定义了新的控制i(i+16i<127),这样保证了每8列换一行。

 实例18 求完数

#include <iostream>

using namespace std;

int main()

{

        int shu;//变量数字

        cout<<"请输入一个任意的自然数:"<<endl;

        cin>>shu;//输入数字

        //求输入数字的所有真因子

        int *zhen_yinzi = new int[shu];//不知道真因子有多少个,所以用指针指向

        int index = 0;//个数索引变量,初始化为0

        int sum = 0;//所有真因子的总和

        zhen_yinzi[0] = 1;//所有数字都有一个真因子为1

        for (int i = 2; i < shu; i++)//循环找所有因子

        {

                 if(shu % i == 0)//表示该因子为真因子

                 {

                         index ++;

                         zhen_yinzi[index] = i;

                 }

        }

 

        for(int j = 0; j <= index; j++)//将所有真因子加和

        {

                 sum += zhen_yinzi[j];

        }

        delete[] zhen_yinzi;//释放内存

        zhen_yinzi = NULL;

 

        if(sum == shu)//如果真因子之和等于数字本身,即为完数

                 cout<<"数字"<<shu<<"是完数"<<endl;

        else

                 cout<<"数字"<<shu<<"不是完数"<<endl;

 

        return 0;

}

 int *zhen_yinzi = new int[shu];定义了一个长度为shu的数组zhen_yinzi[],对于此类数组长度未知的数组,在最后还需要用语句delete[] zhen_yinzi; zhen_yinzi = NULL;进行内存的释放。

 实例22获取变量的字节大小(sizeof

#include <iostream>

using namespace std;

 int main()

{

        int a = 6;//一个整型变量

        int int_size;//整型变量的字节大小

        int_size = sizeof(a);

         cout<<"变量a所占的字节大小为:"<<int_size<<endl;

         return 0;

}

关键在于sizeof()的作用。

 实例25命令响应

#include <iostream>

using namespace std;

 int main()

{

        bool switcher=false;//开关状态

        bool switcher_ex=false;//前一时刻的开关状态

        char ch;

         while(1)

        {

                 cout<<"是否要响应命令?是:输入Y,不是:输入N,退出:Q"<<endl;

                 cin>>ch;

                 if(ch=='Y')

                 {

                         switcher_ex=switcher;

                         switcher=true;

                         if(switcher_ex)

                                  cout<<"继续响应命令!"<<endl;

                         else                          

                                  cout<<"开始响应命令!"<<endl;

                 }

                 else if(ch=='N')

                 {

                         switcher_ex=switcher;

                         switcher=false;

                         if(switcher_ex)

                                  cout<<"暂停命令响应!"<<endl;

                         else

                                  cout<<"继续等待命令响应!"<<endl;

                 }

                 else if(ch=='Q')

                 {

                         cout<<"退出成功!"<<endl;

                         break;

                 }

                 else

                 {

                         cout<<"输入错误!"<<endl;

                 }

        }

         return 0;

}

While(1)循环是无限循环的经典结构,内部往往会含有break;只有达到了break之前的条件才会跳出循环。

实例26买水果

#include <iostream>

using namespace std;

 double capital=0.0;//金额

 double cal(int num, double quantity)

{

        switch(num)

        {

        case 0:

                 return quantity*1.50;

        case 1:

                 return quantity*1.70;

        case 2:

                 return quantity*4.60;

        case 3:

                 return quantity*0.90;

        case 4:

                 return quantity*2.50;

        case 5:

                 return quantity*3.70;

        case 6:

                 return quantity*7.60;

        case 7:

                 return quantity*3.90;

        }

}

int main()

{

        cout<<"提示:本店提供8种水果,如下(左边为水果编号及名称,右边为其单价)"<<endl;

        cout<<"0西瓜 1.50, 1木瓜 1.70, 2哈密瓜 4.60, 3苹果0.9\n"

                  <<"4 2.50, 5柚子 3.70, 6弥猴桃 7.60, 7山楂 3.9"<<endl;

        int num_choice=0;//几种选择

        while(1)

        {

                 int Y_N;

                 cout<<"请输入您要购买的种类数量:";

                 cin>>num_choice;

                 if(num_choice==0)//不买东西

                 {

                         cout<<"您确定只是看看吗?要不买点吧?继续请按1,否则请按0,直接退出"<<endl;

                         cin>>Y_N;

                         if(Y_N==0)

                         {

                                  cout<<"退出成功!"<<endl;

                                  break;

                         }

                         else if(Y_N==1)

                         {}

                         else

                                  cout<<"不要捣乱,按规定输,亲"<<endl;

                 }

                 else

                 {

                         cout<<"种类编号分别为:";

                         int *choice=new int[num_choice];

                         double *quantity_choice=new double[num_choice];

                          for(int i=0;i<num_choice;i++)

                                  cin>>choice[i];//种类编号

                         cout<<"请输入对应购买种类的重量:";

                         for(int i=0;i<num_choice;i++)

                                  cin>>quantity_choice[i];

                          for(int j=0;j<num_choice;j++)//计算金额

                         {

                                  capital += cal(choice[j], quantity_choice[j]);

                         }

                          cout<<"继续购物吗?继续请按1,否则请按0,开始结算"<<endl;

                         cin>>Y_N;

                         if(Y_N==0)

                         {

                                  cout<<"开始结算,请付"<<capital<<""<<endl;

                                  break;

                         }

                         else if(Y_N==1)

                         {}

                         else

                                  cout<<"不要捣乱,按规定输,亲"<<endl;

                 }

        }

         delete[] choice;

        choice=NULL;

        delete[] quantity_choice;

        quantity_choice=NULL;

         return 0;

}

本例子先定义了一个函数double cal(int num, double quantity),函数内有int num, double quantity两个变量,且其主体为一个switch函数,用于计算具体金额。下面来看主体,大结构为一个while(1)的循环结构,进入后依次cin种类编号、种类重量,然后调用cal函数即可计算出captial,同样定义了两个指针数组int *choice=new int[num_choice]double *quantity_choice=new double[num_choice];用于动态记录输入的值。

 实例27 常量

#include <iostream>

using namespace std;

 #define Pi 3.1415

#define code "1234"

 int main()

{

        cout<<"常量例子1"<<1<<endl;

        cout<<"常量例子2"<<true<<endl;

        cout<<"常量例子3"<<"3456"<<endl;

        cout<<"常量例子4"<<Pi<<endl;

        cout<<"常量例子5"<<code<<endl;

        const double Pi1 = 3.1415;

        cout<<"常量例子6"<<Pi1<<endl;

         return 0;

}

主要介绍了两种定义常量的方法:#define code "1234"以及   const double Pi1 = 3.1415;

注意#define code "1234",加上双引号后为一个字符串。

 实例29 寻找出口 

#include <iostream>

using namespace std;

int main()

{

        int migong[5][5]={{0,0,1,1,1},{1,0,0,1,1},{1,1,0,1,1},{1,1,0,0,1},{1,1,1,0,2}};//迷宫

        int row,column;//行列

        int path_row[25];//通行路径的行

        int path_column[25];//通行路径的列

        for(int i=0;i<25;i++)//初始化

                 path_row[i]=path_column[i]=0;

 

        row=0;

        column=0;

        int count=0;//次数

         do//按行循环,先处理,后判断

        {

                 column = 0;

                 do//按列循环,先处理,后判断

                 {

                         switch(migong[row][column])

                         {

                         case 0://可以通行

                                  path_row[count]=row;

                                  path_column[count]=column;

                                  cout<<"加油,快要找到出口了"<<endl;

                                  count++;

                                  break;

                         case 1:

                                  cout<<"不可通行"<<endl;

                                  break;

                         case 2:

                                  path_row[count]=row;

                                  path_column[count]=column;

                                  cout<<"到达出口"<<endl;

                                  count++;

                                  break;

                         }

                         column += 1;

                 }while(column<5);

                  row += 1;

        }while(row<5);

         cout<<"到达出口的路径为:"<<endl;

        for(int j=0;j<count;j++)//输出出口路径

        {

                 cout<<"("<<path_row[j]<<","<<path_column[j]<<")->";

        }

        cout<<endl;

         return 0;

}

从上往下从左往右进行扫描,将矩阵中为0的点的行列数记下来,分别储存在path_row[j]path_column[j]中,用来do while switch case 来完成判断和循环工作。由于需要记下行列数,所以还需要引入计数变量count.

 实例30 数据统计系统

#include <iostream>

#include <string>

using namespace std;

 int main()

{

        cout<<"请输入一串数字,不能有空格等非数字字符"<<endl;

        string str;

        int count_0=0;//0出现的次数

        cin>>str;//输入

        cout<<"数字0出现的次数为:";

        for(int i=0;i<str.length();i++)//判断

        {

                 if(str[i]=='0')//如果等于0

                         count_0++;

                 else

                 {}

        }

         cout<<count_0<<endl;

         return 0;

}

主要是字符串数据类型的引入,首先表头需要添加#include <string>,其次变量声明的时候是string str;定义的字符串数据str本身具有数组功能,故str[i]表示字符串中的第i位字符,可直接引用而不需要专门再定义数组。

 实例31 投色子游戏

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <iostream>

using namespace std;

 int  main()

{

        int i,result;

        srand((int)time(0));//利用系统时间产生随机序列的种子值

        int count[6]={0};//1-6的统计个数

        for(i=0;i<10000;i++)

        {

                 result=1+(int)(6.0*rand()/(RAND_MAX+1.0));//设置出现1-6之间的整数

                 switch(result)

                 {

                 case 1:

                         count[0]++;

                         break;

                 case 2:

                         count[1]++;

                         break;

                 case 3:

                         count[2]++;

                         break;

                 case 4:

                         count[3]++;

                         break;

                 case 5:

                         count[4]++;

                         break;

                 case 6:

                         count[5]++;

                         break;

                 }

        }

         cout<<"1-6点的出现概率依次为:";

        for(int k =0;k<6;k++)//打印每点出现的概率

                 cout<<count[k]/10000.0<<",";

         cout<<endl;

         return 0;

}

 实例31 产生随机数

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <iostream>

using namespace std;

 int  main()

{

        int i,result;

        srand((int)time(0));//利用系统时间产生随机序列的种子值

        int count[6]={0};//1-6的统计个数

        for(i=0;i<10000;i++)

        {

                 result=rand()%6+1;//设置出现1-6之间的整数

                 cout<<result<<endl;

                 switch(result)

                 {

                 case 1:

                         count[0]++;

                         break;

                 case 2:

                         count[1]++;

                         break;

                 case 3:

                         count[2]++;

                         break;

                 case 4:

                         count[3]++;

                         break;

                 case 5:

                         count[4]++;

                         break;

                 case 6:

                         count[5]++;

                         break;

                 }

        }

         cout<<"1-6点的出现概率依次为:";

        for(int k =0;k<6;k++)//打印每点出现的概率

                 cout<<count[k]/10000.0<<",";

         cout<<endl;

         return 0;

}

产生随机数的关键点为,第一先设定随机数种子,保证种子的随机,然后是如何产生一定范围内的随机数:

srand((inttimenull));设定随机数种子(注意要放在循环之外)
rand()%100
;产生0-99的随机数。

假如要产生16-59之间的数,可以这样写:rand()%44+16(这里4459-16+1得到)。

或者写成:result=1+(int)(10*rand()/(RAND_MAX+5))(产生5-10之间的随机数,RAND_MAX表示系统中rand()的上限)

注意,在调用srand time rand这些函数时,需要先写出函数头#include <stdio.h>  #include <stdlib.h> #include <time.h>

同时,注意此例中数组的定义:int count[6]={0}6为数组中包含的数值个数,0为对其所有值初始化。

如果是生成小数范围内的随机数,则先生成整数范围内的随机数,然后除以小数位数对应的10n次方。

  评论这张
 
阅读(41)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016