加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C语言ASCII码转换程序

(2012-12-27 22:13:29)
标签:

c语言

ubuntu

学习日志

杂谈

ascii码

分类: 学习日志
         用一个小程序,来综合体现循环输入输出机制/范围识别机制/字符判断及反馈机制/字符串与整型转换机制/推出指令识别机制。
        注:不同编译器对ASCII码的识别范围不同。

 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define N 10
//Function declaration
int record(char c[]);//Judge inputed is string or not
 
int main(void)
{
    int asc;
    char letter,c[N];
    while (1)//Judge the number inputed correspond or not;
    {
        printf("Please enter ASCII (Nmuber from 000 to 255),press 'quit' to end program:\n");
        scanf("%s",c);
        switch(record(c))
        {
            case -1:
                printf("Input error!\n");
                continue;
            case 0:
                asc=atoi(c);//Convert string to Integer
                if(asc>255||asc<0)
                {
                    printf("Input error!\n");
                }
                else//Proceed ASCII correspond
                {
                    printf("The ASCII code %d corresponding:%c\n",asc,asc);
                    printf("Press 'Enter' to continue , press 'quit' to end program:\n");
                    fflush(stdin);
                    gets(c);
                    if(strcmp(c,"quit")==0)
                        return 0;
                }
                continue;
 
            case 1:
                return 0;
        }
    }
}
 
int record(char c[])
{
    int i;
    if(strcmp(c,"quit")==0)
        return 1;//1=quit
 
    for(i=0;i<strlen(c);i++)
        {
            if(isdigit(c[i])==0)
            {
                return -1;
            }
        }
    return 0;
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有