Do语句
C语言中提供了 3 中循环执行程序语句。这里首先学习 do 语句。
do 语句
输入一个整数,显示出它是奇数还是偶数。然后询问是否重复同样的操作,并按照要求进行处理。
代码清单4-1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int retry; do { int no; printf ("请输入一个整数:" ); scanf ("%d" , &no); if (no % 2 ) puts ("这个整数是奇数。" ); else puts ("这个整数是偶数。" ); printf ("要重复执行一次吗? 【yes...0/No...9】:" ); scanf ("%d" , &retry); } while (retry == 0 ); return 0 ; }
反汇编
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 0061528F 68 28 7 E 61 00 push 617E28 h 00615294 E8 13 C1 FF FF call 006113 AC 00615299 83 C4 04 add esp,4 0061529 C 8 D 45 E8 lea eax,[ebp-18 h] 0061529F 50 push eax 006152 A0 68 30 7B 61 00 push 617B 30h 006152 A5 E8 48 C1 FF FF call 006113F 2 006152 AA 83 C4 08 add esp,8 006152 AD 8B 45 E8 mov eax,dword ptr [ebp-18 h] 006152B 0 25 01 00 00 80 and eax,80000001 h 006152B 5 79 05 jns 006152B C 006152B 7 48 dec eax 006152B 8 83 C8 FE or eax,0F FFFFFFEh 006152B B 40 inc eax 006152B C 85 C0 test eax,eax 006152B E 74 19 je 006152 D9 006152 C0 8B F4 mov esi,esp 006152 C2 68 F0 7 C 61 00 push 617 CF0h 006152 C7 FF 15 70 B1 61 00 call dword ptr ds:[0061B 170h] 006152 CD 83 C4 04 add esp,4 006152 D0 3B F4 cmp esi,esp 006152 D2 E8 59 BF FF FF call 00611230 006152 D7 EB 17 jmp 006152F 0 006152 D9 8B F4 mov esi,esp 006152 DB 68 48 7 E 61 00 push 617E48 h 006152E0 FF 15 70 B1 61 00 call dword ptr ds:[0061B 170h] 006152E6 83 C4 04 add esp,4 006152E9 3B F4 cmp esi,esp 006152 EB E8 40 BF FF FF call 00611230 006152F 0 68 B0 85 61 00 push 6185B 0h 006152F 5 E8 B2 C0 FF FF call 006113 AC 006152F A 83 C4 04 add esp,4 006152F D 8 D 45 F4 lea eax,[ebp-0 Ch] 00615300 50 push eax 00615301 68 30 7B 61 00 push 617B 30h 00615306 E8 E7 C0 FF FF call 006113F 2 0061530B 83 C4 08 add esp,8 0061530 E 83 7 D F4 00 cmp dword ptr [ebp-0 Ch],0 00615312 0F 84 77 FF FF FF je 0061528F 00615318 33 C0 xor eax,eax
do…while 语句架构
复合语句(程序块)中的声明
使用 do 语句的话,从键盘读取的数值是有限制的。
代码清单4-2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int hand; do { printf ("请选择出什么拳[0...石头/1...剪刀/2...布]:" ); scanf ("%d" , &hand); } while (hand < 0 || hand>2 ); printf ("你选择了" ); switch (hand) { case 0 : printf ("石头。\n" ); break ; case 1 : printf ("剪刀。\n" ); break ; case 2 : printf ("布。\n" ); break ; } return 0 ; }
反汇编
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 002B 528F 68 10 86 2B 00 push 2B 8610h 002B 5294 E8 13 C1 FF FF call 002B 13AC 002B 5299 83 C4 04 add esp,4 002B 529C 8 D 45 F4 lea eax,[ebp-0 Ch] 002B 529F 50 push eax 002B 52A0 68 30 7B 2B 00 push 2B 7B30h 002B 52A5 E8 48 C1 FF FF call 002B 13F2 002B 52AA 83 C4 08 add esp,8 002B 52AD 83 7 D F4 00 cmp dword ptr [ebp-0 Ch],0 002B 52B1 7 C DC jl 002B 528F 002B 52B3 83 7 D F4 02 cmp dword ptr [ebp-0 Ch],2 002B 52B7 7F D6 jg 002B 528F 002B 52B9 68 F0 7 C 2B 00 push 2B 7CF0h 002B 52BE E8 E9 C0 FF FF call 002B 13AC 002B 52C3 83 C4 04 add esp,4 002B 52C6 8B 45 F4 mov eax,dword ptr [ebp-0 Ch] 002B 52C9 89 85 2 C FF FF FF mov dword ptr [ebp+FFFFFF2Ch],eax 002B 52CF 83 BD 2 C FF FF FF 00 cmp dword ptr [ebp+FFFFFF2Ch],0 002B 52D6 74 14 je 002B 52EC 002B 52D8 83 BD 2 C FF FF FF 01 cmp dword ptr [ebp+FFFFFF2Ch],1 002B 52DF 74 1 A je 002B 52FB 002B 52E1 83 BD 2 C FF FF FF 02 cmp dword ptr [ebp+FFFFFF2Ch],2 002B 52E8 74 20 je 002B 530A 002B 52EA EB 2B jmp 002B 5317 002B 52EC 68 48 7 E 2B 00 push 2B 7E48h 002B 52F1 E8 B6 C0 FF FF call 002B 13AC 002B 52F6 83 C4 04 add esp,4 002B 52F9 EB 1 C jmp 002B 5317 002B 52FB 68 B0 85 2B 00 push 2B 85B0h 002B 5300 E8 A7 C0 FF FF call 002B 13AC 002B 5305 83 C4 04 add esp,4 002B 5308 EB 0 D jmp 002B 5317 002B 530A 68 34 7B 2B 00 push 2B 7B34h 002B 530F E8 98 C0 FF FF call 002B 13AC 002B 5314 83 C4 04 add esp,4 002B 5317 33 C0 xor eax,eax
逻辑非运算符.德摩根定律
德摩根定律
求多个整数的和及平均值
不停地输入整数,显示其和及平均值。
代码清单4-3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int sum = 0 ; int cnt = 0 ; int retry; do { int t; printf ("请输入一个整数:" ); scanf ("%d" , &t); sum = sum + t; cnt = cnt + 1 ; printf ("是否继续? <Yes...0/No...9>:" ); scanf ("%d" , &retry); } while (retry == 0 ); printf ("和为%d,平均值为%.2f。\n" , sum, (double )sum / cnt); return 0 ; }
反汇编
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 00F 4529D 68 10 86 F4 00 push 0F 48610h 00F 452A2 E8 05 C1 FF FF call 00F 413AC 00F 452A7 83 C4 04 add esp,4 00F 452AA 8 D 45 D0 lea eax,[ebp-30 h] 00F 452AD 50 push eax 00F 452AE 68 30 7B F4 00 push 0F 47B30h 00F 452B3 E8 3 A C1 FF FF call 00F 413F2 00F 452B8 83 C4 08 add esp,8 00F 452BB 8B 45 F4 mov eax,dword ptr [ebp-0 Ch] 00F 452BE 03 45 D0 add eax,dword ptr [ebp-30 h] 00F 452C1 89 45 F4 mov dword ptr [ebp-0 Ch],eax 00F 452C4 8B 45 E8 mov eax,dword ptr [ebp-18 h] 00F 452C7 83 C0 01 add eax,1 00F 452CA 89 45 E8 mov dword ptr [ebp-18 h],eax 00F 452CD 68 D8 7B F4 00 push 0F 47BD8h 00F 452D2 E8 D5 C0 FF FF call 00F 413AC 00F 452D7 83 C4 04 add esp,4 00F 452DA 8 D 45 DC lea eax,[ebp-24 h] 00F 452DD 50 push eax 00F 452DE 68 30 7B F4 00 push 0F 47B30h 00F 452E3 E8 0 A C1 FF FF call 00F 413F2 00F 452E8 83 C4 08 add esp,8 00F 452EB 83 7 D DC 00 cmp dword ptr [ebp-24 h],0 00F 452EF 74 AC je 00F 4529D 00F 452F1 F2 0F 2 A 45 F4 cvtsi2sd xmm0,dword ptr [ebp-0 Ch] 00F 452F6 F2 0F 2 A 4 D E8 cvtsi2sd xmm1,dword ptr [ebp-18 h] 00F 452FB F2 0F 5 E C1 divsd xmm0,xmm1 00F 452FF 83 EC 08 sub esp,8 00F 45302 F2 0F 11 04 24 movsd mmword ptr [esp],xmm0 00F 45307 8B 45 F4 mov eax,dword ptr [ebp-0 Ch] 00F 4530A 50 push eax 00F 4530B 68 48 7 E F4 00 push 0F 47E48h 00F 45310 E8 97 C0 FF FF call 00F 413AC 00F 45315 83 C4 10 add esp,10 h 00F 45318 33 C0 xor eax,eax
复合赋值运算
代码清单4-4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int sum = 0 ; int cnt = 0 ; int retry; do { int t; printf ("请输入一个整数:" ); scanf ("%d" , &t); sum += t; ++cnt; printf ("是否继续? <Yes...0/No...9>:" ); scanf ("%d" , &retry); } while (retry == 0 ); printf ("和为%d,平均值为%.2f。\n" , sum, (double )sum / cnt); return 0 ; }
反汇编
1 2 3 4 5 6 7 8 9 004052B B 8B 45 F4 mov eax,dword ptr [sum] 004052B E 03 45 D0 add eax,dword ptr [ebp-30 h] 004052 C1 89 45 F4 mov dword ptr [sum],eax 004052 C4 8B 45 E8 mov eax,dword ptr [cnt] 004052 C7 83 C0 01 add eax,1 004052 CA 89 45 E8 mov dword ptr [cnt],eax
1 2 3 4 5 6 7 8 9 000 A52BB 8B 45 F4 mov eax,dword ptr [sum] 000 A52BE 03 45 D0 add eax,dword ptr [ebp-30 h] 000 A52C1 89 45 F4 mov dword ptr [sum],eax 000 A52C4 8B 45 E8 mov eax,dword ptr [cnt] 000 A52C7 83 C0 01 add eax,1 000 A52CA 89 45 E8 mov dword ptr [cnt],eax
在 do…while 循环体中,++cnt 和 cnt++的反汇编没有区别。
While 语句
与 do 语句不同,在循环体执行之前对循环的继续执行条件进行判断的是 while 语句。
while 语句
输入一个整数值,显示出从它开始递减到0的每一个整数的程序。
代码清单4-5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int no; printf ("请输入一个正整数:" ); scanf ("%d" , &no); while (no >= 0 ) { printf ("%d\n" , no); no--; } return 0 ; }
反汇编
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 while (no >= 0 )006952 AD 83 7 D F4 00 cmp dword ptr [no],0 006952B 1 7 C 1 C jl __$EncStackInitStart+63 h (06952 CFh) { printf ("%d\n" , no); 006952B 3 8B 45 F4 mov eax,dword ptr [no] 006952B 6 50 push eax 006952B 7 68 34 7B 69 00 push offset string "%d\n" (0697B 34h) 006952B C E8 EB C0 FF FF call _printf (06913 ACh) 006952 C1 83 C4 08 add esp,8 no--; 006952 C4 8B 45 F4 mov eax,dword ptr [no] 006952 C7 83 E8 01 sub eax,1 006952 CA 89 45 F4 mov dword ptr [no],eax } 006952 CD EB DE jmp __$EncStackInitStart+41 h (06952 ADh)
循环体中 no–和–no
1 2 3 4 00E852 C4 8B 45 F4 mov eax,dword ptr [no] 00E852 C7 83 E8 01 sub eax,1 00E852 CA 89 45 F4 mov dword ptr [no],eax
1 2 3 4 008 D52C4 8B 45 F4 mov eax,dword ptr [no] 008 D52C7 83 E8 01 sub eax,1 008 D52CA 89 45 F4 mov dword ptr [no],eax
while 循环体中 no-- 和 --no 没有区别。
while 语句反汇编架构
1 2 3 4 5 6 7 start: cmp xxx JCC end 循环体 jmp start end: xxx
使用递减运算符简化程序代码
下面灵活使用后置递减运算符的特性进一步简化到计数的程序。
代码清单4-6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int no; printf ("请输入一个正整数:" ); scanf ("%d" , &no); while (no >= 0 ) { printf ("%d\n" , no--); } return 0 ; }
反汇编
1 2 3 4 5 6 7 8 9 10 11 12 13 14 00F 252B3 8B 45 F4 mov eax,dword ptr [no] 00F 252B6 89 85 2 C FF FF FF mov dword ptr [ebp-0 D4h],eax 00F 252BC 8B 4 D F4 mov ecx,dword ptr [no] 00F 252BF 83 E9 01 sub ecx,1 00F 252C2 89 4 D F4 mov dword ptr [no],ecx 00F 252C5 8B 95 2 C FF FF FF mov edx,dword ptr [ebp-0 D4h] 00F 252CB 52 push edx 00F 252CC 68 34 7B F2 00 push offset string "%d\n" (0F 27B34h) 00F 252D1 E8 D6 C0 FF FF call _printf (0F 213ACh) 00F 252D6 83 C4 08 add esp,8
1 2 3 4 5 6 7 8 9 000452B 3 8B 45 F4 mov eax,dword ptr [no] 000452B 6 83 E8 01 sub eax,1 000452B 9 89 45 F4 mov dword ptr [no],eax 000452B C 8B 4 D F4 mov ecx,dword ptr [no] 000452B F 51 push ecx 000452 C0 68 34 7B 04 00 push offset string "%d\n" (047B 34h) 000452 C5 E8 E2 C0 FF FF call _printf (0413 ACh) 000452 CA 83 C4 08 add esp,8
在printf函数中 no-- 和 --no 操作就不一样了。
no–:先将当前变量 no 的值做备份x,然后再将no值-1,最后将备份值 x 压入堆栈。
–no:表示直接将no的值减一,并压入堆栈中。
数据递增
这次编写一段跟之前相反的程序,显示出从0开始递增到输入的整数的各个整数。
代码清单4-7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int i,no; printf ("请输入一个正整数:" ); scanf ("%d" , &no); i = 0 ; while (i <= no) { printf ("%d\n" , i++); } return 0 ; }
反汇编
1 2 3 4 5 6 7 8 9 10 11 00 C052BC 8B 45 F4 mov eax,dword ptr [i] 00 C052BF 89 85 20 FF FF FF mov dword ptr [ebp-0E0 h],eax 00 C052C5 8B 4 D F4 mov ecx,dword ptr [i] 00 C052C8 83 C1 01 add ecx,1 00 C052CB 89 4 D F4 mov dword ptr [i],ecx 00 C052CE 8B 95 20 FF FF FF mov edx,dword ptr [ebp-0E0 h] 00 C052D4 52 push edx 00 C052D5 68 34 7B C0 00 push offset string "%d\n" (0 C07B34h) 00 C052DA E8 CD C0 FF FF call _printf (0 C013ACh) 00 C052DF 83 C4 08 add esp,8
1 2 3 4 5 6 7 8 9 printf ("%d\n" , ++i);008552B C 8B 45 F4 mov eax,dword ptr [i] 008552B F 83 C0 01 add eax,1 008552 C2 89 45 F4 mov dword ptr [i],eax 008552 C5 8B 4 D F4 mov ecx,dword ptr [i] 008552 C8 51 push ecx 008552 C9 68 34 7B 85 00 push offset string "%d\n" (0857B 34h) 008552 CE E8 D9 C0 FF FF call _printf (08513 ACh) 008552 D3 83 C4 08 add esp,8
这里的 “i++” 和 “++i” 的反汇编与4-6中的原理一样。i++:备份值,再对变量做加法操作,最后使用备份值做参数。++i:直接对变量使用加法操作,再将变量作为参数。
限定次数的循环操作
输入一个整数后,并排连续显示出该整数个 *,具体的程序代码如下。
代码清单 4-8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int no; printf ("请输入一个正整数:" ); scanf ("%d" , &no); while (no-- > 0 ) { putchar ('*' ); } putchar ('\n' ); return 0 ; }
反汇编
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 002 D52AD 8B 45 F4 mov eax,dword ptr [no] 002 D52B0 89 85 2 C FF FF FF mov dword ptr [ebp-0 D4h],eax 002 D52B6 8B 4 D F4 mov ecx,dword ptr [no] 002 D52B9 83 E9 01 sub ecx,1 002 D52BC 89 4 D F4 mov dword ptr [no],ecx 002 D52BF 83 BD 2 C FF FF FF 00 cmp dword ptr [ebp-0 D4h],0 002 D52C6 7 E 0 C jle __$EncStackInitStart+68 h (02 D52D4h) 002 D52C8 C7 85 28 FF FF FF 01 00 00 00 mov dword ptr [ebp-0 D8h],1 002 D52D2 EB 0 A jmp __$EncStackInitStart+72 h (02 D52DEh) 002 D52D4 C7 85 28 FF FF FF 00 00 00 00 mov dword ptr [ebp-0 D8h],0 002 D52DE 83 BD 28 FF FF FF 00 cmp dword ptr [ebp-0 D8h],0 002 D52E5 74 16 je __$EncStackInitStart+91 h (02 D52FDh) 002 D52E7 8B F4 mov esi,esp 002 D52E9 6 A 2 A push 2 Ah 002 D52EB FF 15 88 B1 2 D 00 call dword ptr [__imp__putchar (02 DB188h)] 002 D52F1 83 C4 04 add esp,4 002 D52F4 3B F4 cmp esi,esp 002 D52F6 E8 35 BF FF FF call __RTC_CheckEsp (02 D1230h) 002 D52FB EB B0 jmp __$EncStackInitStart+41 h (02 D52ADh)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 009152 AD 8B 45 F4 mov eax,dword ptr [no] 009152B 0 83 E8 01 sub eax,1 009152B 3 89 45 F4 mov dword ptr [no],eax 009152B 6 83 7 D F4 00 cmp dword ptr [no],0 009152B A 7 E 16 jle __$EncStackInitStart+66 h (09152 D2h) 009152B C 8B F4 mov esi,esp 009152B E 6 A 2 A push 2 Ah 009152 C0 FF 15 88 B1 91 00 call dword ptr [__imp__putchar (091B 188h)] 009152 C6 83 C4 04 add esp,4 009152 C9 3B F4 cmp esi,esp 009152 CB E8 60 BF FF FF call __RTC_CheckEsp (0911230 h) 009152 D0 EB DB jmp __$EncStackInitStart+41 h (09152 ADh)
while 中的 no-- 和 --no 也是不一样的。
前置递增和前置递减运算符
代码清单4-9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int i = 0 ; int sum = 0 ; int num, tmp; printf ("请输入一个正整数:" ); scanf ("%d" , &num); while (i < num) { printf ("No.%d" , ++i); scanf ("%d" , &tmp); sum += tmp; } printf ("合计值:%d\n" , sum); printf ("平均值:%2.f\n" , (double )sum / num); return 0 ; }
逆向显示整数
输入一个非负整数,并进行逆向显示。例如:输入 1963 的时候,显示出的结果是3691。
代码清单4-10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int no; do { printf ("请输入一个正整数:" ); scanf ("%d" , &no); if (no <= 0 ) puts ("\a请不要输入非正整数。" ); } while (no <= 0 ); printf ("该整数逆向显示的结果是" ); while (no > 0 ) { printf ("%d" , no % 10 ); no /= 10 ; } puts ("。" ); return 0 ; }
反汇编
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 00 C7528F 68 10 86 C7 00 push 0 C78610h 00 C75294 E8 13 C1 FF FF call 00 C713AC 00 C75299 83 C4 04 add esp,4 00 C7529C 8 D 45 F4 lea eax,[ebp-0 Ch] 00 C7529F 50 push eax 00 C752A0 68 30 7B C7 00 push 0 C77B30h 00 C752A5 E8 48 C1 FF FF call 00 C713F2 00 C752AA 83 C4 08 add esp,8 00 C752AD 83 7 D F4 00 cmp dword ptr [ebp-0 Ch],0 00 C752B1 7F 17 jg 00 C752CA 00 C752B3 8B F4 mov esi,esp 00 C752B5 68 D8 7 C C7 00 push 0 C77CD8h 00 C752BA FF 15 70 B1 C7 00 call dword ptr ds:[00 C7B170h] 00 C752C0 83 C4 04 add esp,4 00 C752C3 3B F4 cmp esi,esp 00 C752C5 E8 66 BF FF FF call 00 C71230 00 C752CA 83 7 D F4 00 cmp dword ptr [ebp-0 Ch],0 00 C752CE 7 E BF jle 00 C7528F 00 C752D0 68 F0 7 C C7 00 push 0 C77CF0h 00 C752D5 E8 D2 C0 FF FF call 00 C713AC 00 C752DA 83 C4 04 add esp,4 while (no > 0 ) 00 C752DD 83 7 D F4 00 cmp dword ptr [ebp-0 Ch],0 00 C752E1 7 E 29 jle 00 C7530C 00 C752E3 8B 45 F4 mov eax,dword ptr [ebp-0 Ch] 00 C752E6 99 cdq 00 C752E7 B9 0 A 00 00 00 mov ecx,0 Ah 00 C752EC F7 F9 idiv eax,ecx 00 C752EE 52 push edx 00 C752EF 68 30 7B C7 00 push 0 C77B30h 00 C752F4 E8 B3 C0 FF FF call 00 C713AC 00 C752F9 83 C4 08 add esp,8 00 C752FC 8B 45 F4 mov eax,dword ptr [ebp-0 Ch] 00 C752FF 99 cdq 00 C75300 B9 0 A 00 00 00 mov ecx,0 Ah 00 C75305 F7 F9 idiv eax,ecx 00 C75307 89 45 F4 mov dword ptr [ebp-0 Ch],eax 00 C7530A EB D1 jmp 00 C752DD 00 C7530C 8B F4 mov esi,esp 00 C7530E 68 E4 7B C7 00 push 0 C77BE4h 00 C75313 FF 15 70 B1 C7 00 call dword ptr ds:[00 C7B170h] 00 C75319 83 C4 04 add esp,4 00 C7531C 3B F4 cmp esi,esp 00 C7531E E8 0 D BF FF FF call 00 C71230 00 C75323 33 C0 xor eax,eax
for 语句
比起使用 while 语句,使用 for 语句实现循环会使得程序更加简洁、易读。
for 语句
下面使用 for 语句对代码清单4-7的递增程序进行修改。
代码清单4-11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int i,no; printf ("请输入一个整数:" ); scanf ("%d" , &no); for (i = 0 ; i <= no; i++) { printf ("%d\n" , i); } return 0 ; }
反汇编
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 0010528F 68 10 86 10 00 push offset string "\xc7\xeb\xca\xe4\xc8\xeb\xd2\xbb\xb8\xf6\xd5\xfb\xca\xfd\xa3\xba" (0108610 h) 00105294 E8 13 C1 FF FF call _printf (01013 ACh) 00105299 83 C4 04 add esp,4 0010529 C 8 D 45 E8 lea eax,[no] 0010529F 50 push eax 001052 A0 68 30 7B 10 00 push offset string "%d" (0107B 30h) 001052 A5 E8 48 C1 FF FF call _scanf (01013F 2h) 001052 AA 83 C4 08 add esp,8 001052 AD C7 45 F4 00 00 00 00 mov dword ptr [i],0 001052B 4 EB 09 jmp __$EncStackInitStart+53 h (01052B Fh) 001052B 6 8B 45 F4 mov eax,dword ptr [i] 001052B 9 83 C0 01 add eax,1 001052B C 89 45 F4 mov dword ptr [i],eax 001052B F 8B 45 F4 mov eax,dword ptr [i] 001052 C2 3B 45 E8 cmp eax,dword ptr [no] 001052 C5 7F 13 jg __$EncStackInitStart+6 Eh (01052 DAh) 001052 C7 8B 45 F4 mov eax,dword ptr [i] 001052 CA 50 push eax 001052 CB 68 34 7B 10 00 push offset string "%d\n" (0107B 34h) 001052 D0 E8 D7 C0 FF FF call _printf (01013 ACh) 001052 D5 83 C4 08 add esp,8 001052 D8 EB DC jmp __$EncStackInitStart+4 Ah (01052B 6h) 001052 DA 33 C0 xor eax,eax
for语句反汇编架构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 for1: mov xxx,idata jmp for2 for3: xxx for2: cmp xxx,xxx JCC end loop: 循环体 jmp for3 end: xor eax,eax
忽略个表达式的反汇编
一、忽略第一个表达式
反汇编
1 2 3 4 5 6 7 8 00 D652B4 EB 09 jmp __$EncStackInitStart+53 h (0 D652BFh) 00 D652B6 8B 45 F4 mov eax,dword ptr [i] 00 D652B9 83 C0 01 add eax,1 00 D652BC 89 45 F4 mov dword ptr [i],eax 00 D652BF 8B 45 F4 mov eax,dword ptr [i] 00 D652C2 3B 45 E8 cmp eax,dword ptr [no] 00 D652C5 7F 13 jg __$EncStackInitStart+6 Eh (0 D652DAh)
反汇编框架
1 2 3 4 5 6 7 8 9 10 11 jmp for2 for3: xxx for2: cmp xxx JCC end loop: 循环体 jmp for3 end: xxx
二、忽略第三个表达式
反汇编
1 2 3 4 004252B 4 8B 45 F4 mov eax,dword ptr [i] 004252B 7 3B 45 E8 cmp eax,dword ptr [no] 004252B A 7F 13 jg __$EncStackInitStart+63 h (04252 CFh)
反汇编框架
1 2 3 4 5 6 7 8 for2: cmp xxx JCC end loop: 循环体 jmp for2 end: xxx
三、无表达式
反汇编
1 2 3 4 5 6 7 8 9 10 002052B 4 8B 45 F4 mov eax,dword ptr [i] 002052B 7 50 push eax 002052B 8 68 34 7B 20 00 push offset string "%d\n" (0207B 34h) 002052B D E8 EA C0 FF FF call _printf (02013 ACh) 002052 C2 83 C4 08 add esp,8 002052 C5 EB ED jmp __$EncStackInitStart+48 h (02052B 4h)
反汇编框架
1 2 3 4 5 loop: 循环体 jmp loop end: xxx
如果for语句无表达式就是一个死循环。
for 语句实现固定次数的循环
使用 for 语句对代码清单4-8中的程序进行修改,实现同样的功能。
代码清单4-12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int i,no; printf ("请输入一个整数:" ); scanf ("%d" , &no); for (i=1 ;i<=no;i++) { putchar ('*' ); } putchar ('\n' ); return 0 ; }
代码清单4-13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int i = 0 ; int sum = 0 ; int num, tmp; printf ("输入多少个整数:" ); scanf ("%d" , &num); for (i=0 ;i<num;i++) { printf ("No.%d:" , i + 1 ); scanf ("%d" , &tmp); sum += tmp; } printf ("合计值:%d\n" , sum); printf ("平均值:%.2f\n" , (double )sum / sum); return 0 ; }
偶数的枚举
下面的程序,实现宿儒一个整数值,显示该整数值一下的正偶数。
代码清单4-14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int i, n; printf ("整数值:" ); scanf ("%d" , &n); for (i = 2 ; i <= n; i += 2 ) { printf ("%d" , i); } putchar ('\n' ); return 0 ; }
约数的枚举
实现输入一个整数值,显示该整数值的所有约数。
代码清单4-15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int i, n; printf ("请输入一个值:" ); scanf ("%d" , &n); printf ("整数%d的约数是:" ,n); for (i = 1 ; i <= n; i ++) { if (n % i == 0 ) { printf ("%d " , i); } } putchar ('\n' ); return 0 ; }
表达式语句和空语句
在while和for循环语句表达式后面加上分号,表示该循环体为空。
多重循环
在循环体中继续写循环语句,就可以进行二重循环、三重循环。这样的循环称为多重循环。
二重循环
之前的循环结构都比较简单。实际上,在一个循环中还可以嵌套另一个循环。根据所嵌套的循环的多少,有二重循环、三重循环。它们统称为多重循环。
代码清单4-16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int i, j; for (i = 1 ; i <= 9 ; i++) { for (j = 1 ; j <= 9 ; j++) { printf ("%5d" , i * j); } putchar ('\n' ); } return 0 ; }
反汇编
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 002 A43A5 C7 45 F8 01 00 00 00 mov dword ptr [i],1 002 A43AC EB 09 jmp __$EncStackInitStart+2B h (02 A43B7h) 002 A43AE 8B 45 F8 mov eax,dword ptr [i] 002 A43B1 83 C0 01 add eax,1 002 A43B4 89 45 F8 mov dword ptr [i],eax 002 A43B7 83 7 D F8 09 cmp dword ptr [i],9 002 A43BB 7F 45 jg __$EncStackInitStart+76 h (02 A4402h) 002 A43BD C7 45 EC 01 00 00 00 mov dword ptr [j],1 002 A43C4 EB 09 jmp __$EncStackInitStart+43 h (02 A43CFh) 002 A43C6 8B 45 EC mov eax,dword ptr [j] 002 A43C9 83 C0 01 add eax,1 002 A43CC 89 45 EC mov dword ptr [j],eax 002 A43CF 83 7 D EC 09 cmp dword ptr [j],9 002 A43D3 7F 17 jg __$EncStackInitStart+60 h (02 A43ECh) 002 A43D5 8B 45 F8 mov eax,dword ptr [i] 002 A43D8 0F AF 45 EC imul eax,dword ptr [j] 002 A43DC 50 push eax 002 A43DD 68 10 86 2 A 00 push offset string "%5d" (02 A8610h) 002 A43E2 E8 C5 CF FF FF call _printf (02 A13ACh) 002 A43E7 83 C4 08 add esp,8 002 A43EA EB DA jmp __$EncStackInitStart+3 Ah (02 A43C6h) 002 A43EC 8B F4 mov esi,esp 002 A43EE 6 A 0 A push 0 Ah 002 A43F0 FF 15 88 B1 2 A 00 call dword ptr [__imp__putchar (02 AB188h)] 002 A43F6 83 C4 04 add esp,4 002 A43F9 3B F4 cmp esi,esp 002 A43FB E8 30 CE FF FF call __RTC_CheckEsp (02 A1230h) 002 A4400 EB AC jmp __$EncStackInitStart+22 h (02 A43AEh) 002 A4402 33 C0 xor eax,eax
用break语句强制结束循环
代码清单4-16a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int i, j, eski=0 ; for (i = 1 ; i <= 9 ; i++) { for (j = 1 ; j <= 9 ; j++) { int eski = i * j; if (eski > 40 ) break ; printf ("%5d" , eski); } } return 0 ; }
反汇编
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 00F 143AC C7 45 F8 01 00 00 00 mov dword ptr [i],1 00F 143B3 EB 09 jmp __$EncStackInitStart+32 h (0F 143BEh) 00F 143B5 8B 45 F8 mov eax,dword ptr [i] 00F 143B8 83 C0 01 add eax,1 00F 143BB 89 45 F8 mov dword ptr [i],eax 00F 143BE 83 7 D F8 09 cmp dword ptr [i],9 00F 143C2 7F 3F jg __$EncStackInitStart+77 h (0F 14403h) 00F 143C4 C7 45 EC 01 00 00 00 mov dword ptr [j],1 00F 143CB EB 09 jmp __$EncStackInitStart+4 Ah (0F 143D6h) 00F 143CD 8B 45 EC mov eax,dword ptr [j] 00F 143D0 83 C0 01 add eax,1 00F 143D3 89 45 EC mov dword ptr [j],eax 00F 143D6 83 7 D EC 09 cmp dword ptr [j],9 00F 143DA 7F 25 jg __$EncStackInitStart+75 h (0F 14401h) 00F 143DC 8B 45 F8 mov eax,dword ptr [i] 00F 143DF 0F AF 45 EC imul eax,dword ptr [j] 00F 143E3 89 45 D4 mov dword ptr [ebp-2 Ch],eax 00F 143E6 83 7 D D4 28 cmp dword ptr [ebp-2 Ch],28 h 00F 143EA 7 E 02 jle __$EncStackInitStart+62 h (0F 143EEh) 00F 143EC EB 13 jmp __$EncStackInitStart+75 h (0F 14401h) 00F 143EE 8B 45 D4 mov eax,dword ptr [ebp-2 Ch] 00F 143F1 50 push eax 00F 143F2 68 30 7B F1 00 push offset string "%5d" (0F 17B30h) 00F 143F7 E8 05 D0 FF FF call _printf (0F 11401h) 00F 143FC 83 C4 08 add esp,8 00F 143FF EB CC jmp __$EncStackInitStart+41 h (0F 143CDh) 00F 14401 EB B2 jmp __$EncStackInitStart+29 h (0F 143B5h) 00F 14403 33 C0 xor eax,eax
break 语句使用 jmp 指令跳出当前的循环语句。
图形显示
用程序显示长方形。
代码清单4-17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int i, j; int height, width; puts ("画一个长方形。" ); printf ("高:" ); scanf ("%d" , &height); printf ("宽:" ); scanf ("%d" , &width); for (i = 1 ; i <= height; i++) { for (j = 1 ; j <= width; j++) putchar ('*' ); putchar ('\n' ); } return 0 ; }
代码清单4-18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int i, j, len; puts ("生成一个直角在左边的等腰三角形。" ); printf ("短边:" ); scanf ("%d" , &len); for (i = 1 ; i <= len; i++) { for (j = 1 ; j <= i; j++) putchar ('*' ); putchar ('\n' ); } return 0 ; }
代码清单4-19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int i, j, k, len; puts ("生成一个直角在右边的等腰三角形。" ); printf ("短边:" ); scanf ("%d" , &len); for (i = 1 ; i <= len; i++) { for (j = 1 ; j <= len-i; j++) { putchar (' ' ); } for (j = 1 ; j <= i; j++) { putchar ('*' ); } putchar ('\n' ); } return 0 ; }
代码清单4-19a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int i, j, k, len; puts ("生成一个直角在右边的等腰三角形。" ); printf ("短边:" ); scanf ("%d" , &len); for (i = 1 ; i <= len; i++) { for (j = len; j > i; j--) { putchar (' ' ); } for (k = 1 ; k <= i; k++) { putchar ('*' ); } putchar ('\n' ); } return 0 ; }
多重循环
do语句、while语句和for语句,都可以通过嵌套结构实现多重循环。
代码清单4-20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int retry; do { int i, no; do { printf ("请输入一个正整数:" ); scanf ("%d" , &no); if (no <= 0 ) puts ("\a请不要输入非正整数。" ); } while (no <= 0 ); for (i = 1 ; i <= no; i++) putchar ('*' ); putchar ('\n' ); printf ("是否继续执行? [Yes....0/Np....9]: " ); scanf ("%d" , &retry); } while (retry == 0 ); return 0 ; }
程序的组成元素和格式
关键字
运算符
标识符
分隔符
常量和字符串常量
自由的书写格式
C语言原则上允许开发人员以自由的格式编写程序。它并不像其他编程语言那样,规定了程序中必须从第几个字符开始写,或者每条语句必须写在一行之内等。
代码清单4-21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main ( void ) {int i, j; for (i = 1 ; i <= 9 ; i ++) { for (j = 1 ; j <= 9 ; j++) printf ("%3d" , i * j); putchar ('\n' ); } return 0 ; }
连接相邻的字符串常量
缩进
总结
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main (void ) { int i, j; int x, y, z; do { printf ("0-100的数值:" ); scanf ("%d" , &x); } while (x < 0 || x>100 ); y = x; z = x; while (y >= 0 ) printf ("%d %d\n" , y--, ++z); printf ("宽和高为整数面积为 %d" "的长方形的边长是:\n" , x); for (i = 1 ; i < x; i++) { if (i * i > x) break ; if (x % i != 0 ) continue ; printf ("%d x %d\n" , i, x / i); } puts ("5行7列的星号" ); for (i = 1 ; i <= 5 ; i++) { for (j = 1 ; j <= 7 ; j++) putchar ('*' ); putchar ('\n' ); } return 0 ; }
课后练习
练习4-1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int y; do { int x; printf ("请输入一个数字:" ); scanf ("%d" , &x); if (x > 0 ) puts ("输入的值是正数\n" ); else if (x < 0 ) puts ("输入的值是负数\n" ); else puts ("输入的值是0.\n" ); printf ("是否继续输入<YES......0/NO......9>:\n" ); scanf ("%d" , &y); } while (y == 0 ); system("pause" ); return 0 ; }
练习4-2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int y, sum=0 , min = 0 , max = 0 ; puts ("请输入两个整数。" ); do { int a, b; printf ("整数a:" ); scanf ("%d" , &a); printf ("整数b:" ); scanf ("%d" , &b); if (min == 0 && max == 0 ) { (a > b) ? (max = a )&&( min = b ) : (max = b)&&(min = a); } else { if (max < a || max < b) { max = a; if (max < b) max = b; } else if (min > a || min > b) { min = a; if (min > b) min = b; } } sum += a+b; printf ("是否继续输入<YES......0/NO......9>:" ); scanf ("%d" , &y); } while (y == 0 ); printf ("\n大于等于%d小于等于%d的所有整数的和是%d。" , min, max, sum); system("pause" ); return 0 ; }
练习4-3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x; printf ("请输入一个整数:" ); scanf ("%d" , &x); while (x >= 0 ) { printf ("%d " , x); x--; } if (x >= 0 ) { printf (" \n" ); } system("pause" ); return 0 ; }
练习4-4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x; printf ("请输入一个整数:" ); scanf ("%d" , &x); while (x >= 1 ) { printf ("%d " , x--); } if (x >= 0 ) printf ("\n" ); system("pause" ); return 0 ; }
练习4-5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x; int i = 1 ; printf ("请输入一个整数:" ); scanf ("%d" , &x); while (i <= x) { printf ("%d " , i++); } if (x>=0 ) printf (" \n" ); system("pause" ); return 0 ; }
练习4-6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x, i = 2 ; printf ("输入值:" ); scanf ("%d" , &x); while (x > i) { if (i % 2 == 0 ) printf ("%d " , i); i += 2 ; } printf ("\n" ); system("pause" ); return 0 ; }
练习4-7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x, i = 2 ; printf ("输入值:" ); scanf ("%d" , &x); while (x > i) { if (i % 2 == 0 ) printf ("%d " , i); i *= 2 ; } printf ("\n" ); system("pause" ); return 0 ; }
练习4-8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x; printf ("请输入一个整数:" ); scanf ("%d" , &x); while (--x >= 1 ) { putchar ('*' ); } x++; if (x >= 1 ) printf ("*\n" ); system("pause" ); return 0 ; }
练习4-9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x; printf ("正整数:" ); scanf ("%d" , &x); while (x >= 1 ) { if (x % 2 == 0 ) printf ("-" ); else printf ("+" ); x--; } printf ("\n" ); system("pause" ); return 0 ; }
练习4-10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x; printf ("正整数:" ); scanf ("%d" , &x); while (--x >= 0 ) { printf ("*\n" ); } system("pause" ); return 0 ; }
练习4-11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x; printf ("请输入一个正整数:" ); scanf ("%d" , &x); printf ("%d逆向显示的结果是" , x); while (x > 0 ) { printf ("%d" , x % 10 ); x /= 10 ; } printf ("\n" ); system("pause" ); return 0 ; }
练习4-12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x, tmp, i = 0 ; printf ("请输入一个正整数:" ); scanf ("%d" , &x); tmp = x; while (tmp > 0 ) { tmp /= 10 ; i++; } printf ("%d的位数是%d\n" , x,i); printf ("\n" ); system("pause" ); return 0 ; }
练习4-13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x, sum = 0 , num = 0 ; printf ("整数值:" ); scanf ("%d" , &x); for (int i = 1 ; i <= x; ++i) { sum += i; } printf ("1到%d的和为%d\n" , x, sum); system("pause" ); return 0 ; }
练习4-14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int num[11 ] = { 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,0 }; int x, flag = 0 ; printf ("整数值:" ); scanf ("%d" , &x); for (int i = 0 ; i < x; ++i&&flag++) { if (i % 10 == 0 ) flag = 0 ; printf ("%d" , num[flag]); } system("pause" ); return 0 ; }
练习4-15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int start, end, x; puts ("身高和标准体重对照表" ); printf ("\n开始数值(cm):" ); scanf ("%d" , &start); printf ("结束数值(cm):" ); scanf ("%d" , &end); printf ("间隔数值(cm):" ); scanf ("%d" , &x); printf ("\n" ); for (; start < end; start += x) { printf ("%dcm %.2fkg\n" , start, ((double )start - 100 )*0.9 ); } system("pause" ); return 0 ; }
练习4-16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x; printf ("整数值:" ); scanf ("%d" , &x); for (int i = 1 ; i <= x; i++) { if (i % 2 != 0 ) printf ("%d " , i); } printf ("\n" ); system("pause" ); return 0 ; }
练习4-17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x; printf ("n的值是:" ); scanf ("%d" , &x); for (int i = 1 ; i <= x; i++) { printf ("%d的二次方是%d\n" , i, i*i); } printf ("\n" ); system("pause" ); return 0 ; }
练习4-18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x; printf ("显示多少个*:" ); scanf ("%d" , &x); for (int i =0 ; i < x; i++) { if (i % 5 == 0 && i != 0 ) printf ("\n" ); printf ("*" ); } printf ("\n" ); system("pause" ); return 0 ; }
练习4-19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x, sum = 0 , num = 0 ; printf ("整数值:" ); scanf ("%d" , &x); for (int i = 1 ; i <= x; ++i) { if (x % i == 0 ) { printf ("%d\n" , i); num++; } } printf ("约数的个数是%d个\n\n" ,num); system("pause" ); return 0 ; }
练习4-20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { for (int j = 0 ; j <= 9 ; j++){ if (j == 1 ) printf ("%3d" , j); else printf ("%4d" , j); } printf ("\n----|--------------------------------------\n" );for (int k = 1 ; k <= 9 ; k++){ printf ("%4d|" , k); for (int n = 1 ; n <= 9 ; n++) { printf ("%2d " , k * n); } printf ("\n" ); } system("pause" ); return 0 ; }
练习4-21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x,b=0 ; puts ("生成一个正方形。" ); printf ("正方形有几层:" ); scanf ("%d" , &x); b = x; do { int a, b=0 ; for (a = 0 ; a < x; ++a) { putchar ('*' ); } putchar ('\n' ); } while (--b>0 ); system("pause" ); return 0 ; }
练习4-22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x, y, tmp; puts ("让我们来画一个长方形。" ); printf ("一边:" ); scanf ("%d" , &x); printf ("另一边:" ); scanf ("%d" , &y); if (x < y) { tmp = y; y = x; x = tmp; } for (int i = 1 ; i <= y; ++i) { for (int j = 1 ; j <= x; j++) { printf ("*" ); } printf ("\n" ); } system("pause" ); return 0 ; }
练习4-23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x, y, space; printf ("短边:" ); scanf ("%d" , &x); for (int i = 1 ; i <= x; ++i) { for (int j = 1 ; j <= i; j++) { printf ("* " ); } space = x * 4 ; y =(x+space) - (i*4 ); for (int k = 1 ; k <= y; k++) { printf (" " ); } for (int n = 1 ; n <= i; n++) { printf ("* " ); } printf ("\n" ); } system("pause" ); return 0 ; }
练习4-24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x=0 , y=1 , space; puts ("让我们来画一个金字塔。" ); printf ("金字塔有几层:" ); scanf ("%d" , &x); space = (x - 1 ) * 1 + x; space = (space - 1 ) / 2 ; for (int i = 1 ; i <= x; i++) { if (i >1 ) y = y + 2 ; for (int k = 1 ; k <= space; k++) { printf (" " ); } space--; for (int j=1 ; j <= y; j++) { printf ("*" ); } putchar ('\n' ); } system("pause" ); return 0 ; }
练习4-25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int x=0 , y, space; puts ("让我们来画一个金字塔。" ); printf ("金字塔有几层:" ); scanf ("%d" , &x); y = (x - 1 ) * 1 + x; space = (y - 1 ) / 2 ; for (int i = 1 ; i <= x; i++) { if (i >1 ) y = y - 2 ; for (int k = 1 ; k <= space; k++) { printf (" " ); } space++; for (int j=1 ; j <= y; j++) { printf ("%d" ,i); } putchar ('\n' ); } system("pause" ); return 0 ; }