• C语言基础:内置函数的调用


    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<time.h>
    void main()
    {

    1. 内置函数:
    sqrt(double x);计算x的平方根                                                            //头文件math.h
    pow(double x,double y):计算x的y次方                   //头文件math.h
    ceil(double x); 计算不小于x的最小整数 (大于等于)                                  //头文件math.h
    floor(double x); 计算不大于x的最大整数 (小于等于)                               //头文件math.h

    srand((unsigned)time(NULL));随机种子     //unsigned:无负的    //time(NULL):返回值距1970-1-1,00:00:00经历的秒数。

    rand(); //伪随机数。                                                                          //头文件stdlib.h

    toupper('x');小写变大写                                                                     //头文件ctype.h

    tolower('x');大写变小写                         //头文件ctype.h

    1.函数的调用 
    int i,z,c;
    double x=1.42;
    for(i=1;i<9;i++)
    {
      printf("%d的根2次方是%0.2f,%d的三次方是%f ",i,sqrt(i),i,pow(i,3));
    }
    printf("%lf ",ceil(x));
    printf("%lf ",floor(x));

     
    2.产生10个[0,9]随机数

    int i;
    long l=time(NULL);
    srand(l);
    for(i=0;i<10;i++)
    {
      printf("%d ",rand()%10);
    }


    3.产生11-20的随机数


    int i;
    long l=time(NULL);
    srand(l);
    for(i=0;i<10;i++)
    {
      printf("%d ",rand()%10+11);
    }



    4.实现"人机猜拳大战" 0代表剪刀, 1代表石头,2代表布

      规则采用7局4胜制
      1.获取用户输入的出拳(0,1,2)
      2.让电脑尝试一个[0,2]之间的随机数
      3.判断当局输赢: 
      人胜: 人==0&&电脑==2 人==1&&电脑==0 人==2&&电脑==1
      电脑胜: 电脑==0&&人==2 电脑==1&&人==0 电脑==2&&人==1
      4.评判最终结果:count1(人)>count2(电脑)
      
    int count1=0,count2=0;
    int i,user,computer;
    for(i=1;i<=7;i++)
    {
      printf("请用户输入第%d次猜的拳 ",i);
      scanf("%d",&user);
      srand((unsigned)time(NULL));
      computer=rand()%3;
      if(user<0&&user>2)
      {
        printf("输入错误 ");
      }
      else
      {
        if(user==0&&computer==2||user==1&&computer==0||user==2&&computer==1)
        {
          printf("第%d次,人胜 ",i);
          count1++;
        }
        else if(user==2&&computer==0||user==0&&computer==1||user==1&&computer==2)
        {
          printf("第%d次,电脑胜 ",i);
          count2++;
        }
        else if(user==computer)
        {
          printf("第%d次,平局 ",i);
        }

      } 
    }
    printf("************************************** ");
    if(count1>count2)
    {
      printf("人胜,比分%d :%d ",count1,count2);
    }
    else if(count1<count2)
    {
      printf("电脑胜,比分%d :%d ",count1,count2);
    }
    else if(count1==count2)
    {
      printf("平局,比分%d :%d ",count1,count2);
    }

      
    5.打印验证码 :

      
    int i,index;
    char num[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};
    srand(time(NULL));
    printf("验证码为: ");
    for(i=0;i<4;i++)
    {
      index=rand()%62;
      printf("%c",num[index]);


    printf(" ");






    }

  • 相关阅读:
    SqlServer 查看被锁的表和解除被锁的表
    Windows Server 2012 R2 或 2016 无法安装 .Net 3.5.1
    请求文件下载URL过长处理
    T4语法
    windows下 安装 rabbitMQ 及操作常用命令
    ubuntu系统启动qtceator时提示:Qt5.5.1/Tools/QtCreator/lib/qtcreator/plugins/libHelp.so: 无法加载库
    升级到VS2013常见问题
    Windowns 无法启动 Office Software Protection Platform 服务,系统找不到指定的文件
    SVN clean失败解决方法
    使用PostSharp在.NET平台上实现AOP
  • 原文地址:https://www.cnblogs.com/kuangzhisen/p/6863431.html
Copyright © 2020-2023  润新知