zoukankan      html  css  js  c++  java
  • 字符变换(好像是二级的题)? Anthony

    //isalpha和isupper函数是标准库函数,其的简要功能如下:
    int islower(int c)
    {
        return 'a' <= c && c <= 'z';
    }
    int isupper(int c)
    {
        return 'A' <= c && c <= 'Z';
    }
    int isalpha(int c)
    {
        return islower(c) || isupper(c);
    }

    //testXXXX函数是测试用例.
    ////////////////////////////////////////////////////////////////
    #include <assert.h>
    #include <ctype.h>
    #include <stdio.h>

    char Postpone(char orig, int distance)
    {
        //不处理非字母
        if (isalpha(orig) == 0)
        {
            return orig;
        }

        int d = distance % 26;
        char top = (isupper(orig) ? 'Z' : 'z') - d;

        //隐含orig < base + 4, 因为isalpha(orig)
        if (orig > top)
        {
            d -= 26;
        }

    #ifndef NDEBUG
        printf("if '%c'[%3d] > '%c'[%3d] then put off %3d get %c[%3d]n", orig, orig, top, top, d, orig + d, orig + d);
    #endif
        return orig + d;
    }
    void testPostponeNoneAlpha()
    {
        assert(Postpone(' ', 4) == ' ');
        assert(Postpone('_', 4) == '_');
        assert(Postpone('~', 4) == '~');
    }

    void testPostpone4Letter()
    {
        assert(Postpone('A', 4) == 'E');
        assert(Postpone('a', 4) == 'e');

        assert(Postpone('V', 4) == 'Z');
        assert(Postpone('v', 4) == 'z');

        assert(Postpone('W', 4) == 'A');
        assert(Postpone('w', 4) == 'a');

        assert(Postpone('Z', 4) == 'D');
        assert(Postpone('z', 4) == 'd');
    }

    void testPostpone0Letter()
    {
        assert(Postpone('V', 0) == 'V');
        assert(Postpone('v', 0) == 'v');

        assert(Postpone('W', 0) == 'W');
        assert(Postpone('w', 0) == 'w');

        assert(Postpone('Z', 0) == 'Z');
        assert(Postpone('z', 0) == 'z');
    }

    int main()
    {
        testPostponeNoneAlpha()
        testPostpone4Letter();
        testPostpone0Letter();
        return 0;
    }

  • 相关阅读:
    Docker 安装NGINX
    Ant 使用setFieldsValue更新Form表单值
    SpringSecurity设置角色和权限
    java 获取昨天、今天、明天的时间
    Ubuntu 20.04 root ssh登录配置
    http: server gave HTTP response to HTTPS client解决方案:
    06-多线程笔记-2-锁-3-Lock
    05-多线程笔记-2-锁-2-Synchronized
    04-多线程笔记-2-锁-1-死锁
    volatile
  • 原文地址:https://www.cnblogs.com/ahuangliang/p/5309284.html
Copyright © 2011-2022 走看看