为什么i值会改变呢?(水题)

a542107840 2011-03-24 09:01:44
/*传说nit有个叫jiangjiang的短信群发男,他很喜欢发短信,但是今天他要发很多英文短信,
想找聪明的你帮帮他。
我们知道手机键盘上的数字和英文字母的对应是这样的。
1:(','或'.',即按一下为逗号,按两下为句号);
2:(a b c,同理按一下为a,两下为b,三下为c);
3:(d e f);
4:(g h i);
5:(j k l);
6:(m n o);
7:(p q r s);
8:(t u v);
9:(w x y z);
现在已经知道要你帮他发的短信,问你如果要打出这篇文章所需要的按键顺序。
打印的要求是每个字母对应的按键要用空格隔开,最后一个字母输出后面没有多余的空格。
例如要求打出的短信为“ahans”,那么输出应该是这样的“2 44 2 66 7777”。
很简单吧,那么请按要求为我们的jiangjiang编辑短信吧!

输入要求
输入的每条短信都是由英文小写字母和‘#’组成,不包含空格,每个输入以‘#’结束,
有多个输入,每个输入不超过1000个字母。
两个输入之间有空行隔开。

输出要求
如上面所描述的,注意最后一个‘#’不要求输出,每个输出一行。

Sample Input

ahans#

ahans,#

Sample Output

2 44 2 66 7777
2 44 2 66 7777 1
*/

#include<string.h>//代码是很水的,凑合这看把,问题在下面
int main()
{
char message[1005],*p;
char letter_box[][6]={{',','.'},{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'},{'m','n','o'},{'p','q','r','s'},{'t','u','v'},{'w','x','y','z'}};
int v=0;
static int i;
char print[1];
while(scanf("%s",message)==1){
p=message;
strcpy(print,"\0");
if(v==1){
printf("\n");
v=1;
}
for(i=0;*(p+i)!='#';i++){
printf("%c\n",*(p+i));
if(*(p+i)==',')
strcat(print,"1 ");
else if(*(p+i)=='.')
strcat(print,"11 ");
else if(*(p+i)=='a')
strcat(print,"2 ");
else if(*(p+i)=='b')
strcat(print,"22 ");
else if(*(p+i)=='c')
strcat(print,"222 ");
else if(*(p+i)=='d')
strcat(print,"3 ");
else if(*(p+i)=='e')
strcat(print,"33 ");
else if(*(p+i)=='f')
strcat(print,"333 ");
else if(*(p+i)=='g')
strcat(print,"4 ");
else if(*(p+i)=='h') /*输入测试数据“ahans#”debug一下发现当指针走到h的时候从strcat函数出来的时候i值居然由原来的1变成了32,太奇怪了,但后来我把i定义成static int i;就没问题了,请问这是什么情况啊?????*/
strcat(print,"44 ");
else if(*(p+i)=='i')
strcat(print,"444 ");
else if(*(p+i)=='j')
strcat(print,"5 ");
else if(*(p+i)=='k')
strcat(print,"55 ");
else if(*(p+i)=='l')
strcat(print,"555 ");
else if(*(p+i)=='m')
strcat(print,"6 ");
else if(*(p+i)=='n')
strcat(print,"66 ");
else if(*(p+i)=='o')
strcat(print,"666 ");
else if(*(p+i)=='p')
strcat(print,"7 ");
else if(*(p+i)=='q')
strcat(print,"77 ");
else if(*(p+i)=='r')
strcat(print,"777 ");
else if(*(p+i)=='s')
strcat(print,"7777 ");
else if(*(p+i)=='t')
strcat(print,"8 ");
else if(*(p+i)=='u')
strcat(print,"88 ");
else if(*(p+i)=='v')
strcat(print,"888 ");
else if(*(p+i)=='w')
strcat(print,"9 ");
else if(*(p+i)=='x')
strcat(print,"99 ");
else if(*(p+i)=='y')
strcat(print,"999 ");
else if(*(p+i)=='z')
strcat(print,"9999 ");
}
printf("%s",print);
/* for(i=0;i<(strlen(print)-1);i++){
printf("%c",print[i]);
}*/
printf("\n");
}
return 0;
}
#include<stdio.h>
...全文
130 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
a542107840 2011-04-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 moorsf 的回复:]
char print[1];
这个定义太小了,当你用strcat函数的时候,数据就会覆盖i,改变i的值。
应该加大数组的长度,具体自己算。

另外strcat函数如果像你这样频繁使用的话,效率不高。
因为strcat函数内部肯定是去找寻结束符,然后再做cat的操作,串越大,这个操作就越费时。
[/Quote]
嗯,知道了,print[]长度太小了,谢谢
pathuang68 2011-03-25
  • 打赏
  • 举报
回复
好玩的题目啊
touda2 2011-03-24
  • 打赏
  • 举报
回复
这是修改后的代码:
#include<string.h>//代码是很水的,凑合这看把,问题在下面
#include<stdio.h>
int main()
{
char message[1005],*p;
// char letter_box[][6]={{',','.'},{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'},{'m','n','o'},{'p','q','r','s'},{'t','u','v'},{'w','x','y','z'}};
// int v=0;
int i;

//char print[1];//只有一个字符
char print[10000];
while(scanf("%s",message)==1){
p=message;
strcpy(print,"\0");
printf("%d\n",sizeof(print));
// if(v==1){
// printf("\n");
// v=1;
// }
for(i=0;*(p+i)!='#';i++){
printf("%c\n",*(p+i));
if(*(p+i)==',')
strcat(print,"1 ");
else if(*(p+i)=='.')
strcat(print,"11 ");
else if(*(p+i)=='a')
strcat(print,"2 ");
else if(*(p+i)=='b')
strcat(print,"22 ");
else if(*(p+i)=='c')
strcat(print,"222 ");
else if(*(p+i)=='d')
strcat(print,"3 ");
else if(*(p+i)=='e')
strcat(print,"33 ");
else if(*(p+i)=='f')
strcat(print,"333 ");
else if(*(p+i)=='g')
strcat(print,"4 ");
else if(*(p+i)=='h') /*输入测试数据“ahans#”debug一下发现当指针走到h的时候从strcat函数出来的时候i值居然由原来的1变成了32,太奇怪了,但后来我把i定义成static int i;就没问题了,请问这是什么情况啊?????*/
strcat(print,"44 ");
else if(*(p+i)=='i')
strcat(print,"444 ");
else if(*(p+i)=='j')
strcat(print,"5 ");
else if(*(p+i)=='k')
strcat(print,"55 ");
else if(*(p+i)=='l')
strcat(print,"555 ");
else if(*(p+i)=='m')
strcat(print,"6 ");
else if(*(p+i)=='n')
strcat(print,"66 ");
else if(*(p+i)=='o')
strcat(print,"666 ");
else if(*(p+i)=='p')
strcat(print,"7 ");
else if(*(p+i)=='q')
strcat(print,"77 ");
else if(*(p+i)=='r')
strcat(print,"777 ");
else if(*(p+i)=='s')
strcat(print,"7777 ");
else if(*(p+i)=='t')
strcat(print,"8 ");
else if(*(p+i)=='u')
strcat(print,"88 ");
else if(*(p+i)=='v')
strcat(print,"888 ");
else if(*(p+i)=='w')
strcat(print,"9 ");
else if(*(p+i)=='x')
strcat(print,"99 ");
else if(*(p+i)=='y')
strcat(print,"999 ");
else if(*(p+i)=='z')
strcat(print,"9999 ");
}
printf("%s",print);
/* for(i=0;i<(strlen(print)-1);i++){
printf("%c",print[i]);
}*/
printf("\n");
}
return 0;
}
moorsf 2011-03-24
  • 打赏
  • 举报
回复
char print[1];
这个定义太小了,当你用strcat函数的时候,数据就会覆盖i,改变i的值。
应该加大数组的长度,具体自己算。

另外strcat函数如果像你这样频繁使用的话,效率不高。
因为strcat函数内部肯定是去找寻结束符,然后再做cat的操作,串越大,这个操作就越费时。
qq120848369 2011-03-24
  • 打赏
  • 举报
回复
扫描一遍键盘,存储28个键值对(char,string).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char* keyBoard[]=
{
",.","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz",NULL
};

char table[28][5];

void initTable()
{
table[26][0]='1';
table[27][0]=table[27][1]='1';
table[26][1]=table[27][2]=NULL;

for(int i=1;keyBoard[i]!=NULL;++i)
{
for(int j=0;keyBoard[i][j]!=NULL;++j)
{
for(int k=0;k<j+1;++k)
{
table[keyBoard[i][j]-'a'][k]='0'+i+1;
}

table[keyBoard[i][j]-'a'][j+1]=NULL;
}
}
}

void sendMsg(const char *msg)
{
for(int i=0;msg[i]!='#';++i)
{
if(msg[i]==keyBoard[0][0]) //,
{
printf("%s ",table[26]);
}
else if(msg[i]==keyBoard[0][1]) //.
{
printf("%s ",table[27]);
}
else
{
printf("%s ",table[msg[i]-'a']);
}
}

printf("\n");
}

int main()
{
initTable();
sendMsg("ahans#");

return 0;
}



69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧