一次做ACM擴展出來的小程序

這個小程序就是自己學習用的
第一次實踐式的寫一個程序,紀念一下,哈哈~~

程序功能描述:
0 - “_”
1 - “a”
2 - “b”
……….
25 - “y”
26 - “z”
27 - “.”

可以互相進行查詢

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

int main()
{
    int i, j = 0, length, code[28];
    char str[1000];

    //對code數組按照要求賦值
    //"_" ASCII value is 95
    //"." ASCII value is 46
    //"a" ASCII valude is 97
    code[0] = '_';
    code[27] = '.';

    for(i = 1; i < 27; i++)
    {
        code[i] = 'a' + i - 1;
    }

    printf("PS: \"-1\" means END!!\n");
    printf("Please input the num or letter:<0-27 | a-z | \"_\"| \".\" only>\n");


    while(scanf("%s", str))
    {

        length = strlen(str);
        if(length == 1)
        {
            if((str[0] < 'a' || str[0] > 'z') && str[0] != '_' && str[0] != '.' && (str[0] < '0' || str[0] > '9'))
            {
                printf("Input Error! Input again:\n");
            }
            else
            {
                for(i = 0; ; i++)
                {
                    //char型數據處理
                    if((str[0] - 'a') >= 0)
                    {
                        if(code[i] == str[0])
                        {
                            printf("The %c is %d\n", str[0], i);
                            break;
                        }
                    }
                    else if(str[0] == '_' || str[0] == '.')
                    {
                        if(str[0] == '_')
                        {
                            printf("The _ is %d\n", 0);
                            break;
                        }
                        else if(str[0] == '.')
                        {
                            printf("The . is %d\n", 27);
                            break;
                        }
                    }

                    //int型數據處理
                    else if(i == atoi(str))
                    {
                        printf("The %d is %c\n", i, code[i]);
                        break;
                    }
                }

            }
        }
        else if(length == 2)
        {
            //先處理正確的
            //
            //
            //int型的兩位數處理
            if(((atoi(str) - 10) >= 0) && ((atoi(str) - 27) <= 0) && atoi(str) != -1)
            {
                for(i = 10; ; i++)
                {
                    if(i == atoi(str))
                    {
                        printf("The %d is %c\n", i, code[i]);
                        break;
                    }
                }
            }
            else if(atoi(str) == -1)
            {
                printf("The program ENDED!!\n");
                return 0;
            }
            else
            {
                printf("Input Error! Input again!\n");
            }

        }
        else
        {
            printf("Input Error! Input again!\n");
        }

    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章