c_第五次作业_2019.04.01

1.完成猜数字游戏。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int Menu(){
printf("*******************************\n");
printf("**************1.开始游戏*****************\n");
printf("**************2.退出游戏*****************\n");
printf("*******************************\n");
printf("请输入你的选择:\n");
int choice=0;
scanf("%d",&choice);
return choice;

 

}
void Game(){
    int to_guess=rand()%1000+1;//rand函数:使系统随机产生一个整数
    srand((unsigned int)time(0));//时间戳,确保每次游戏过后不是同一个数据
    while(1){
    printf("请输入一个整数(1-1000);");//系统随机生成一个整数
    int num=0;
    scanf("%d",&num);
    if(num>to_guess){
    printf("high\n");

    }
    else if(num<to_guess){
    
    printf("down\n");
    }
    else{
    printf("right\n");
    break;
    }
    
    
    }

 

 

 

 

}

int main(){
    while(1){
    int choice=Menu();
    if(choice==1){
    Game();
    }
    else if(choice==0){
    printf("goodbye\n");
    break;
    
    }
    else{
    printf("您的输入有误\n");
    
    }
    
    }

system("pause");
return 0;
}

 

2.写代码可以在整型有序数组中查找想要的数字, 
找到了返回下标,找不到返回-1.(折半查找) 

#include<stdio.h>
#include<stdlib.h>

int findArgs(int a[], int n, int f)
{
    int i;
    for (i = 0; i < n; i++)
    {
        if (a[i] == f)
        {
            return i;
        }
    }
    return -1;
}

int halfFindArgs(int a[], int n, int f)
{
    int left = 0, right = n - 1;
    int mid;

    while (left <= right)
    {
        mid = (left + right) / 2;
        if (a[mid] < f)
        {
            left = mid + 1;
        }
        else if (a[mid] > f)
        {
            right = mid - 1;
        }
        else
        {
            return mid;
        }
    }
    return -1;
}

int main()
{
    int a[10] = { 1, 3, 5,7,9};

    printf("%d\n", halfFindArgs(a, 5, 5));

    system("pause");
    return 0;
}

 


3.编写代码模拟三次密码输入的场景。 
最多能输入三次密码,密码正确,提示“登录成功”,密码错误, 
可以重新输入,最多输入三次。三次均错,则提示退出程序。 

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int pwdJudge(char pwd[]) {
 char input[21] = { 0 };
 int i;
 for (i = 0; i < 3; i++) {
  scanf("%s", input);
  if (strcmp(input, pwd) == 0) {
   return 1;
  }
  else {
   printf("密码错误,请重新输入\n");
  }
 }
  return 0; 
}
int main() {
 printf("请输入密码,最多有三次机会\n");
 char pwd[21] = "nizuishuai";
 if (pwdJudge(pwd)) {
  printf("登陆成功\n");
 }
 else {
  printf("尝试次数过多,登陆失败\n");
 }
 system("pause");
 return 0;
}


4.编写一个程序,可以一直接收键盘字符, 
如果是小写字符就输出对应的大写字符, 
如果接收的是大写字符,就输出对应的小写字符, 
如果是数字不输出。

#include<stdio.h>
#include<stdlib.h>
int main()
{
    char ch;
    while (1){
        ch = getchar();
        if (ch <= 'Z' && ch >= 'A'){          //如果是大写字母,输出为小写字母,' '在ASCII码其值为32,在ASCII码中,大写字母的值比小写字母的值小32
            putchar(ch + ' ');
        }
        else if (ch <= 'z' && ch >= 'a'){      //如果是小写字母,输出为大写字母
            putchar(ch - ' ');
        }
        else if (isalnum(ch))                //如果是数字什么也不输出
        {
            
        }
        else{                                //如果是其他字符,不做改变,直接输出
            putchar(ch);
        }
    }
    system("pause");
    return 0;

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值