请高手告诉我下面代码中的几行是什么意思(这是个完整的代码)C/C++ code#include stdio.h#include conio
请高手告诉我下面代码中的几行是什么意思(这是个完整的代码)
C/C++ code#include <stdio.h>#include <conio.h>void main(){ char ch,nch; int count; int k; printf("Please input a string with a # in the end.\n"); scanf("%c",&ch); [color=#FF0000]while(ch != '#') { if(ch >= '0' && ch <= '9') { count = ch-'0'+1; scanf("%c",&nch); for(k=0;k<count;k++) printf("%c",nch); } else printf("%c",ch); printf(" "); scanf("%c",&ch); }[/color] printf("#\n"); getch();}
红色字体(或者是while里面的东西)我不懂,请大侠解释!!!
[解决办法]就是先输入一个字符,如果这字符n在[0,9]之间,再输入一个字符,并且打印该字符n次。如果不在区间内就直接打印。
[解决办法]count = ch-'0'+1; ch 变量里的字符 ASCII值 - 字符'0'的ASCII值 + 1;
比如 :你输入为2,则 count = 50 - 48 + 1 = 3;
楼主去百度 下 ASCII码
[解决办法]#include <stdio.h>
#include <conio.h>
void main()
{
char ch,nch;
int count;
int k;
printf("Please input a string with a # in the end.\n");
scanf("%c",&ch);
while(ch != '#')
{
if(ch >= '0' && ch <= '9')//如果读入的字符是数字,处理的结果是不输出数字
{
count = ch-'0'+1; //将该数字加1,然后赋给count,计数循环标志的初始化
scanf("%c",&nch); //接收数字后面的首个字符
for(k=0;k<count;k++) //循环n+1次
printf("%c",nch); //将该字符输出n+1次
}
else //如果输出的是字符,立即输出
printf("%c",ch);
printf(" ");//在输出空格进行分割
scanf("%c",&ch);//接受下一个字符
} printf("#\n"); //最后打印#
getch();
}
//这个程序的作用,我猜可能是哈弗曼编码一类的翻译,将经过编码后的字符串还原为初始的情况
//