C语言_综合实例

// 猜数字
/*
 该实例使用while和rand()函数实现一个猜数字的游戏,每次执行游戏会随机产生一个数字,然后让用户猜这个游戏,猜错重来,直到用户猜中为止
 */

#include <stdio.h>
#include <stdlib.h>  // 用到rand(),需要导入stdlib.h
#include <time.h>

int main()
{

    int iRand = 0;  // iRand存储随机数
    int iGuess = 0;  // iGuess存储猜的数字
    
    srand((unsigned)time(NULL));  // 把时间传给srand()函数,这样就会修改每次的种子都不同
    iRand = rand() % 100;  // 随机数对100取模,rand()函数会自动调用系统的随机种子
    iRand = iRand + 1;
    
    printf("猜数字游戏\n该数字在1到100之间!\n");
    while (iGuess != iRand)
    {
        printf("\n请输入你所猜的数字:");
        scanf("%d",&iGuess);
        
        if (iGuess < iRand)  // 对用户输入的数字进行提示
        {
            printf("你所猜的数字小了!\n");
        }
        else if (iGuess > iRand)
        {
            printf("你所猜的数字大了!\n");
        }
    }
    
    printf("恭喜你猜对了,这个数字就是:%d\n",iRand);  // 猜对数字,输出随机数,游戏结束
    
    return 0;
    
    /*
     如果只是使用rand()函数产生随机数,这样系统就默认只使用同一个种子。
     如果想每次产生的随机数都不同,那就应该每次使用不同的种子来产生随机数,使用srand()就可以产生不同的种子
     */
}

// 杨辉三角型
/*
 杨辉三角形,它的两腰上的数字都是1,中间的数字则是左上的数字加正上的数字。
 */

#include <stdio.h>
#define N 10

int main()
{
    int a[N][N];
    for (int i = 0; i < N; i ++)
    {
        for (int j = 0; j <= i; j ++)
        {
            if (j == 0 || j == i)
            {
                a[i][j] = 1;
            }
            else
            {
                a[i][j] = a[i-1][j-1] + a[i-1][j];
            }
        }
    }
    for (int i = 0; i < N; i ++)
    {
        printf("%*d",20-i*2,a[i][0]);
        for (int j = 1; j <= i; j ++)
        {
            printf("%4d",a[i][j]);
        }
        printf("\n");
    }
    
    return 0;
}
// strlen函数的实现思路
/*
 首先定义一个变量来保存字符串的长度,初始值为0
 然后循环遍历所有元素,每遍历一个元素该变量加1,遇到字符串结束标志,循环终止
 */

#include <stdio.h>

int getLenght(const char *ch)
{
    int iLen = 0;
    
    while (ch[iLen] != '\0')
    {
        iLen ++;
    }
    
    return iLen;
}

int main()
{
    char ch[1024] = {0};
    printf("请输入一段字符串:");
    gets(ch);  // 不适用scanf()输入函数,因为该函数对空格符和换行符敏感
    printf("字符串长度为:%d\n", getLenght(ch));
    
    return 0;
}
// strcat函数的实现思路
/*
 1、找到第1个字符串的结尾
 2、将第2个字符串的所有字符依次复制到第1个字符串的结尾处
 3、添加字符串结束标志
 */

#include <stdio.h>

int main()
{

    char ch1[1024] = "Hello World";
    char ch2[] = "help me";
    
    int i = 0;  // ch1遍历需要的变量
    int j = 0;  // ch2遍历需要的变量
    
    while (ch1[i] != '\0')
    {
        i ++;  // 找到ch1字符串的结尾处
    }
    
    while (ch2[j] != '\0')
    {
        ch1[i] = ch2[j];  // 在ch1字符串结束标志的位置进行复制
        i ++;
        j ++;  // 各自下标加1
    }
    ch1[i] = '\0';  // 复制完成,在最后添加字符串结束标志
    
    printf("拼接完成后的字符串:%s\n",ch1);
    
    return 0;
}
// strcpy函数的实现思路
/*
 1、找到第1个字符串的开头
 2、将第2个字符串的所有字符依次复制到第1个字符串的开始处
 3、添加字符串结束标志
 */

#include <stdio.h>

int main()
{

    char ch1[1024] = "Hello World";
    char ch2[] = "help me";
    
    int i;
    for (i = 0; ch2[i] != '\0'; i ++)
    {
        ch1[i] = ch2[i];
    }
    ch1[i] = '\0';  // 复制完成,在最后添加字符串结束标志
    
    printf("拼接完成后的字符串:%s\n",ch1);
    
    return 0;
}
// strcmp函数的实现思路
/*
 1、比较两个字符串中的第一个字符,如果相等,那么继续比较下一个字符,直到不相等为止
 2、如果不相等,则对两个字符执行相减操作,并返回差值
 */

#include <stdio.h>

int main()
{

    char ch1[] = "help me";
    char ch2[] = "help";
    
    int i = 0;
    int n = 0;
    
    while (ch1[i] == ch2[i] && ch1[i] != '\0')  // 找到不相等的字符
    {
        i ++;
    }

    if (ch1[i] == '\0' && ch2[i] == '\0')
    {
        n = 0;
    }
    else
    {
        n = ch1[i] - ch2[i];
    }
    
    if (n > 0)
    {
        printf("第1个字符串比第2个字符串大!\n");
    }
    else if (n < 0)
    {
        printf("第1个字符串比第2个字符串小!\n");
    }
    else
    {
        printf("第1个字符串与第2个字符串相等!\n");
    }
    
    return 0;
}
// strrev函数的实现思路
/*
 1、首先将字符串的最后1个字符与第1个字符交换
 2、然后将字符串的倒数第2个字符与第2个字符交换
 3、接着将字符串的倒数第3个字符与第3个字符交换,其他以此类推
 */

#include <stdio.h>
#include <string.h>  // 用到strlen函数,需要导入string.h

int main()
{

    char ch1[] = "Hello World";
    char ch;
    
    int i,j,n;
    j = (int)strlen(ch1) - 1;  // 最后一个字符的下标
    n = (int)strlen(ch1) / 2;  // 交换次数
    
    for (i = 0; i < n; i ++)
    {
        ch = ch1[i];
        ch1[i] = ch1[j];
        ch1[j] = ch;
        j --;
    }
    
    printf("反转后的字符串:%s\n",ch1);
    
    return 0;
}

// strlwr函数的实现思路
/*
 1、不断遍历字符串,判断字符是否是大写
 2、如果是大写,求出大小写差值,然后用差值加上大写字母
 */

#include <stdio.h>

char *StrLwr(char *ch)
{
    char *ptr = NULL;
    ptr = ch;
    
    while (*ptr != '\0')
    {
        if (*ptr >= 'A' && *ptr <= 'Z')  // 判断是否是大小写
        {
            *ptr += 'a' - 'A';  // 求出大小写差值,然后用差值加上大写字母
        }
        ptr ++;  // 指向下一个字符
    }
    
    return ch;  // 返回转换后的字符串的首地址
}

int main()
{
    char str[] = "ABCDEFGHIJK";
    printf("大写转成小写字符串:%s\n",StrLwr(str));
    
    return 0;
}

// 密码验证
/*
 利用gets函数输入密码,利用strcmp函数验证密码
 */

#include <stdio.h>
#include <string.h>  // 用到strcmp函数,需要导入string.h
#include <stdlib.h>  // 用到exit函数,需要导入stdlib.h
#define password "abc123"

int main()
{

    char pwd[100] = {0};  // 输入的密码
    int i = 0;  // 输入的次数
    
    while (1)
    {
        printf("请输入密码:");
        gets(pwd);
        i ++;
        
        if (strcmp(pwd, password) == 0)  // 输入正确
        {
            printf("恭喜你,输入密码正确!\n");
            break;
        }
        else  // 输入错误
        {
            printf("密码错误!你还有%d次机会\n",3-i);
        }
        
        if (i == 3)  // 判断是否连续输入错误3次
        {
            printf("\n密码连续三次输入错误,该账号已被锁定!\n");
            exit(0);
        }
    }
    
    return 0;
}
// 导航菜单
/*
switch语句配合永久循环,例如while(1)或者for(;;),可以做出让用户选择的导航菜单,该菜单永不停止,直到用户选择到某一菜单为止
*/

#include <stdio.h>
#include <string>

using namespace std;

void navMenu()
{
    bool quit = false;  // 用户选择退出标志,初始化为false
    char choice;  // 用户选择标志

    printf("各电视台即时节目预报...\n");

    while(1)  // for(;;)
    {
        printf("(0)江苏台 (1)湖北台 (2)安徽台 (3)浙江台 (4)山东台 (5)湖南台 (6)宁夏台 (q)退出:");
        scanf("%c",&choice);
        getchar();

        switch(choice)
        {
            case '0':
                printf("江苏台正在播放《画皮》!\n");
                break;
            case '1':
                printf("湖北台正在播放《蜗居》!\n");
                break;
            case '2':
                printf("安徽台正在播放广告!\n");
                break;
            case '3':
                printf("浙江台正在播放《今日证券》!\n");
                break;
            case '4':
                printf("山东台正在播放《大宋惊世奇案》!\n");
                break;
            case '5':
                printf("湖南台正在播放《超级女生》!\n");
                break;
            case '6':
                printf("宁夏台正在播放《小兵张嘎》!\n");
                break;
            case 'q':
                quit = true;
                break;
            default:
                printf("\n请选择正确的数字!\n");
                break;
        }
        if(quit)
        {
            break;
        }
    }

    printf("程序结束!\n");
}

int main()
{
    navMenu();
    return 0;
}


















评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值