第 6 章 C控制语句:循环
Posted web1013
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第 6 章 C控制语句:循环相关的知识,希望对你有一定的参考价值。
6.16.3 使用嵌套循环,按下面格式打印字母:
F
FE
FED
FEDC
FEDCB
FEDCBA
1 #include <stdio.h> 2 3 int main() 4 { 5 const int ROWS = 6; 6 7 for (int row(0); row != ROWS; ++row) 8 { 9 for (char ch(\'F\'); ch >= \'F\' - row; --ch) 10 printf("%c",ch); 11 12 printf("\\n"); 13 } 14 15 return 0; 16 }
6.16.4 使用嵌套循环,按下面格式打印字母:
A
BC
DEF
GHIJ
KLMNO
PQRSTU
1 #include <stdio.h> 2 3 int main() 4 { 5 const int ROWS = 6; 6 const int COLS = 6; 7 char ch = \'A\'; 8 9 for (int row(0); row != ROWS; ++row) 10 { 11 for (int col(0); col <= row; ++col, ++ch) 12 printf("%c", ch); 13 14 printf("\\n"); 15 } 16 17 return 0; 18 }
6.16.5
1 #include <stdio.h> 2 3 int main() 4 { 5 printf("Input a letter: "); 6 7 char ch; 8 while (1 == scanf("%c", &ch)) 9 { 10 if (\'\\n\' == ch || \' \' == ch || \'\\t\' == ch) 11 continue; 12 13 if (ch < \'A\' || ch > \'z\') 14 printf("\\nIt\'s not a letter, try again: "); 15 else 16 break; 17 } 18 19 if (ch >= \'a\') ch -= \'a\' - \'A\'; 20 21 const int ROWS = ch - \'A\' + 1; 22 const char *SPACE = " "; 23 24 for (int row(0); row != ROWS; ++row) 25 { 26 for (int col(0); col != ROWS - row; ++col) 27 printf(SPACE); 28 29 char chTmp = \'A\'; 30 for (; chTmp != \'A\' + row; ++chTmp) 31 printf("%c", chTmp); 32 33 for (; chTmp != \'A\' - 1; --chTmp) 34 printf("%c", chTmp); 35 36 printf("\\n"); 37 } 38 39 return 0; 40 }
以上是关于第 6 章 C控制语句:循环的主要内容,如果未能解决你的问题,请参考以下文章