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\n",i,sqrt(i),i,pow(i,3));
}
printf("%lf\n",ceil(x));
printf("%lf\n",floor(x));

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

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


3.产生11-20的随机数


int i;
long l=time(NULL);
srand(l);
for(i=0;i<10;i++)
{
  printf("%d\n",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次猜的拳\n",i);
  scanf("%d",&user);
  srand((unsigned)time(NULL));
  computer=rand()%3;
  if(user<0&&user>2)
  {
    printf("输入错误\n");
  }
  else
  {
    if(user==0&&computer==2||user==1&&computer==0||user==2&&computer==1)
    {
      printf("第%d次,人胜\n",i);
      count1++;
    }
    else if(user==2&&computer==0||user==0&&computer==1||user==1&&computer==2)
    {
      printf("第%d次,电脑胜\n",i);
      count2++;
    }
    else if(user==computer)
    {
      printf("第%d次,平局\n",i);
    }

  } 
}
printf("**************************************\n\n\n");
if(count1>count2)
{
  printf("人胜,比分%d :%d\n",count1,count2);
}
else if(count1<count2)
{
  printf("电脑胜,比分%d :%d\n",count1,count2);
}
else if(count1==count2)
{
  printf("平局,比分%d :%d\n",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("验证码为:\n");
for(i=0;i<4;i++)
{
  index=rand()%62;
  printf("%c",num[index]);


printf("\n");






}

posted @ 2017-05-16 20:09  咫尺天涯是路人丶  阅读(858)  评论(0编辑  收藏  举报