为啥C语言编程时输入数字转化为了ASC码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为啥C语言编程时输入数字转化为了ASC码相关的知识,希望对你有一定的参考价值。
你用getch()来获得z[i]的值,getch返回的是字符不是数值,字符的值就是asc码,你强制转换后输出的不是字符是字符的值,不会变成你要的数值的,只能做减法。或者不用getch 参考技术A 1、直接将字符变量赋值给整型变量,即可实现字符到对应ASCII码的转换。2、具体实现方法可以参考如下程序段:
char str[]="abds%*&34dfs"; // 定义一个字符数组,存放待转换为ASCII码的字符串
int AsciiNum[20]; // 定义一个整型数组,存放字符所对应的ASCII码值,数组大小根据字符串长度进行设置
int i;
// 将字符串的每个字符逐个赋值给整型数组AsciiNum,即实现字符到ASCII码值的转换
for(i=0; i<strlen(str); i++)
AsciiNum[i] = str[i]; // 最后数组AsciiNum就是字符串每个字符所对应ASCII码值的数组
参考技术B 你的程序这样设计可以输入不全是数字的密码。
如果需要将z数组中的数字转换成整数,可以:
-z数组开设7个空间,可转换6位密码,开个整型变量k
-加#include<stdlib.h>
-while语句建议如下修改:
while
(
(z[i]=getch())!=13
)
//或者替换13为'\n'
i++;
if
(
i>=6
)
break;
z[i]=0;
k=atoi(z);
//可将输入的字符串转换成相应的整数
-需要查看是否6位十进制数超过整型范围,否则改成long
int,并用
atol
函数转换。
如何用C语言将输入的数字转化成英语
数字为1-10000之间,由于我刚开始学C,我用的一种比较传统的方法比较复杂要写很多语句,所以想请教一下高手有没有什么简单的方法,谢谢!
是将数字转化成英文啊 如;11:ELEVEN,135:ONE HUNDRED FIVE
和你的相比,不知是否复杂。
此程序的计算范围:0<=num<1000。如果还想要计算更大的数,可以在最后面加判断语句,方法类似。
#include<stdio.h>
void main()
char *Eng1[20]="zero","one","two","three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen",
"sixteen","seventeen","eighteen","nineteen";
char *Eng2[8]="twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety";
int num;
printf("请输入数字: ");
scanf("%d",&num);
printf("对应的英文为: ");
if(num>=0&&num<=19)
printf("%s\n",Eng1[num]);
else if(num<100)
int s,y;
s=num/10;
y=num%10;
printf("%s %s\n",Eng2[s-2],Eng1[y]);
else if(num<1000)
int b,s,y;
b=num/100;
y=num%100;
if(y>9)
s=(num%100)/10;
y=(num%100)%10;
if(y==0)
printf("%s hundred and %s\n",Eng1[b],Eng2[s-2]);
else
printf("%s hundred and %s %s\n",Eng1[b],Eng2[s-2],Eng1[y]);
else
printf("%s hundred and %s\n",Eng1[b],Eng1[y]);
参考技术A 只有按照ASCII码进行对应转换,把输入的数字转换为相应的英文字母,没有其他方法了 参考技术B #include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
char a;
while('\n'!=a)
a=getchar();
switch (a)
case '0':printf("zero ");break;
case '1':printf("one ");break;
case '2':printf("two ");break;
case '3':printf("three ");break;
case '4':printf("four ");break;
case '5':printf("five ");break;
case '6':printf("six ");break;
case '7':printf("seven ");break;
case '8':printf("eight ");break;
case '9':printf("nine ");break;
return 0;
参考技术C #include<stdio.h>
char *Eng1[20] = "zero", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen" ;
char *Eng2[8] = "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" ;
int num;
void fun(int num)
if (num >= 0 && num <= 19)
printf("%s\n", Eng1[num]);
else if (num<100)
int s, y;
s = num / 10;
y = num % 10;
printf("%s %s\n", Eng2[s - 2], Eng1[y]);
else if (num<1000)
int b, s, y;
b = num / 100;
y = num % 100;
if (y>9)
s = (num % 100) / 10;
y = (num % 100) % 10;
if (y == 0)
printf("%s hundred %s ", Eng1[b], Eng2[s - 2]);
else
printf("%s hundred %s %s", Eng1[b], Eng2[s - 2], Eng1[y]);
else
printf("%s hundred %s", Eng1[b], Eng1[y]);
void main()
scanf("%d", &num);
if (num > 0)
if (num > 1000)
if (num > 1000000)
if (num > 1000000000)
int numw = num / 1000000000;
num = num % 1000000000;
fun(numw);
printf(" billion ");
int numx = num / 1000000;
num = num % 1000000;
fun(numx);
printf(" million ");
int numy = num / 1000;
num = num % 1000;
fun(numy);
printf(" thousand ");
int numz = num;
fun(num);
根据我的题目中间不用and,要改可以自调,数据范围一直到100billon,在此谢谢mhy8946(不会是清洁工吧。。。同行么)的算法,要不然我也搞不出来hh 参考技术D 没有
以上是关于为啥C语言编程时输入数字转化为了ASC码的主要内容,如果未能解决你的问题,请参考以下文章