一个弱弱的问题

leijunyuncyuyan 2008-04-26 02:29:49
#include<stdio.h>
void main()
{
char str[]="SSSWILTECH1 \1 \11W\1WALLMP1";
int k;
char c;
for(k=2;(c=str[k])!='\0';k++){
switch(c){
case 'A':putchar('a');continue;
case '1':break;
case 1: while((c=str[++k]!='\1'&&c!='\0'));
case 9:putchar('#');
case 'E':
case 'L': continue;
default:putchar(c);continue;
}
putchar('*');
}
printf("\n");
}
输出的是SWITCH* #WAMP*
我知道输出当中英文字母,不过*和#还有空格就有点糊涂了.请大家指教.THANKS VERY MUCH
...全文
245 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
羽中漫步 2008-06-11
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 friedchicken2002 的回复:]
这种问题不会没其他的办法,用VC跟!
[/Quote]

最好的方法
friedchicken2002 2008-06-11
  • 打赏
  • 举报
回复
这种问题不会没其他的办法,用VC跟!
zjw6861982 2008-06-11
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 zlybyq 的回复:]
引用 9 楼 ForestDB 的回复:
C/C++ code
S
S
S    // k = 2, start from here, default: S
W    // default: W
I    // default: I
L    // case 'L': not printed
T    // default: T
E    // case 'E' then case 'L': not printed
C    // default: C
H    // default: H
1    // case '1': break switch, *
-    // default: - (- stands for blank space)
\1    // case 1:
// notice here: != is preceded t…
[/Quote]
zlybyq 2008-06-11
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ForestDB 的回复:]
C/C++ code
S
S
S // k = 2, start from here, default: S
W // default: W
I // default: I
L // case 'L': not printed
T // default: T
E // case 'E' then case 'L': not printed
C // default: C
H // default: H
1 // case '1': break switch, *
- // default: - (- stands for blank space)
\1 // case 1:
// notice here: != is preceded than =, and logical resul…
[/Quote]

高手!
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 yuewei1231 的回复:]
9楼的很牛
[/Quote]
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 leijunyuncyuyan 的回复:]
还有,case 9是什么意思啊,case 1和case '1'的区别,谢谢各位给我回贴,THANKS
[/Quote]
case 1表示:switch(表达式),表达式的值为1时的情况;
case '1'表示:表达式的值为字符1时的情况。
zjw6861982 2008-06-11
  • 打赏
  • 举报
回复
在VC环境下执行单步调试
Soulic 2008-06-11
  • 打赏
  • 举报
回复
学习
yuewei1231 2008-06-11
  • 打赏
  • 举报
回复
9楼的很牛
K行天下 2008-06-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 leijunyuncyuyan 的回复:]
还有,case 9是什么意思啊,case 1和case '1'的区别,谢谢各位给我回贴,THANKS
[/Quote]
case 1表示接收的是整数1,int型,但是char型会隐式转换为int,如上面遇到'\1',ascii码为1,则转换为int 1,正好匹配 case 1
case '1'则是字符‘1’,char型
NKLoveRene 2008-06-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lisheng053758 的回复:]
1,break和continue的区别;
2,对特殊字符的理解
[/Quote]hoho
ForestDB 2008-06-09
  • 打赏
  • 举报
回复

S
S
S // k = 2, start from here, default: S
W // default: W
I // default: I
L // case 'L': not printed
T // default: T
E // case 'E' then case 'L': not printed
C // default: C
H // default: H
1 // case '1': break switch, *
- // default: - (- stands for blank space)
\1 // case 1:
// notice here: != is preceded than =, and logical result 1 for true and 0 for false
// so the while loop by-pass the - \11 W \1
// after the while ends, pass-through the case 9: #
// then pass-through case 'E' the case 'L': continue
- // skipped by the while
\11 // skipped by the while (note: if not skipped it should case 9, \11 is oct of 9)
W // skipped by the while
\1 // while ends here, continue, k++ then skipped
W // default: W
A // case 'A': a
L // case 'L': not printed
L // case 'L': not printed
M // default: M
P // default: P
1 // case '1': break switch, so *

SWITCH* #WaMP*
That's all.

UltraBejing 2008-04-30
  • 打赏
  • 举报
回复
以后需再关注,现在先帮你顶一下
Yun0825 2008-04-27
  • 打赏
  • 举报
回复
标识位置错开了,LZ自己纠正吧,:)
Yun0825 2008-04-27
  • 打赏
  • 举报
回复
char str[]="SSSWILTECH1 \1 \11W\1WALLMP1";
| | |
'1' '\1' '\11'

建议LZ看看关于转义字符常量的内容,其他的只要了解switch语句和for语句的执行流程应该
可以解决了。
leijunyuncyuyan 2008-04-27
  • 打赏
  • 举报
回复
还有,case 9是什么意思啊,case 1和case '1'的区别,谢谢各位给我回贴,THANKS
fangfei_119 2008-04-26
  • 打赏
  • 举报
回复
楼主,这个问题不弱呀,每个问题我们都要重视它,不管它是大是小喔^_^!
我说下自己看了这个后的理解.大体上你的问题楼上两位帮你解决了,我补充下.
1.对于字符和整数的问题.在上面的字符串中,出现'\'的地方表示它是一个转义的符号.\1和'\1'不同,前者是整数1,后者是字符'1',所以str[]中\11表示的整 数9(8进制);
2.空格是一个字符,在输出第一个*后,扫描到空格' ',因此由default语句输出空格' ';
lisheng053758 2008-04-26
  • 打赏
  • 举报
回复
1,break和continue的区别;
2,对特殊字符的理解
bitxinhai 2008-04-26
  • 打赏
  • 举报
回复
void main()
{
char str[]="SSSWILTECH1 \1 \11W\1WALLMP1";
int k;
char c;
for(k=2;(c=str[k])!='\0';k++){
switch(c){
case 'A':putchar('a');continue;
case '1':break;
case 1: while((c=str[++k]!='\1'&&c!='\0'));
case 9:putchar('#');
case 'E':
case 'L': continue;
default:putchar(c);continue;
}
putchar('*');
}
printf("\n");
}
当 c = ‘1’式,执行 case '1':break;
跳出switch,执行putchar('*');,输出‘*’;
当c = ‘\1’时,执行while((c=str[++k]!='\1'&&c!='\0'));
一直到 才‘W’结束,然后执行putchar('#');
输出'#';
最后c= ‘1’;
执行 case '1':break;
跳出switch,执行putchar('*');,输出‘*’;
candy110 2008-04-26
  • 打赏
  • 举报
回复

#include <stdio.h>
void main()
{
char str[]="SSSWILTECH1 \1 \11W\1WALLMP1";
int k;
char c;
for(k=2;(c=str[k])!='\0';k++)
{
switch(c)
{
case 'A':putchar('a');continue;
case '1':break; ///关键在这步和下面一步
case 1: while((c=str[++k]!='\1'&&c!='\0')); //这步
case 9:putchar('#');
case 'E':
case 'L': continue;
default:putchar(c);continue;
}
putchar('*');
}
printf("\n");
}
//第一个"*"的输出是因为遇到第一个"1",执行break,跳转到putchar('*');
//"#"的输出是因为while((c=str[++k]!='\1'&&c!='\0'));循环3次以后继续向下执行(switch语句),输出"#".
//直到遇到"continue".
//第二个"*"和第一个道理一样;

把程序改成下面的在看输出结果更好理解

void main()
{
char str[]="SSSWILTECH1 \1 \11W\1WALLMP1";
int k;
char c;
for(k=2;(c=str[k])!='\0';k++)
{
switch(c)
{
case 'A':putchar('a');continue;
case '1':break;
case 1:
while((c=str[++k]!='\1'&&c!='\0'))
putchar('0');
case 9:putchar('#');
case 'E':
case 'L': continue;
default:putchar(c);continue;
}
putchar('*');
}
printf("\n");
}

69,371

社区成员

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

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