本来有三个,全弄出来太无聊啦,
截取两个,是关于函数和数组基础知识的。
代码A:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int power(int m, int n); 5 int main(int argc, char *argv[]) 6 { 7 int i; 8 9 for (i = 0; i < 10; ++i) 10 printf("%2d %5d %7d\n", i, power(2,i), power(-3,i)); 11 12 system("PAUSE"); 13 return 0; 14 } 15 16 int power(int base, int n) 17 { 18 int i, p; 19 20 p = 1; 21 for (i = 1; i <= n; ++i) 22 p = p * base; 23 return p; 24 }
代码B(相对于书上源码,我加了几条PRINFT语句,以便能更深入的了解作者编程的思路及实现):
1 #include <stdio.h> 2 #define MAXLINE 1000 3 4 int getline(char line[], int maxline); 5 void copy(char to[], char from[]); 6 7 main() 8 { 9 int len; 10 int max; 11 char line[MAXLINE]; 12 char longest[MAXLINE]; 13 14 max = 0; 15 while ((len = getline(line, MAXLINE)) >0){ 16 printf("\nlen's value is : %d\n", len); 17 if (len > max) { 18 max = len; 19 copy(longest, line); 20 } 21 } 22 if (max > 0) 23 printf("%s\n", longest); 24 printf(" max is %d",max); 25 26 return 0; 27 } 28 29 int getline(char s[], int lim) 30 { 31 int c,i; 32 33 for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c !='\n'; i ++){ 34 s[i] = c; 35 } 36 37 if (c == '\n'){ 38 s[i] = c; 39 printf("after a new line,s array is %s \n",s); 40 ++i; 41 printf("after a new line,i value is %d \n",i); 42 } 43 44 s[i] = '\0'; 45 return i; 46 } 47 48 void copy(char to[], char from[]) 49 { 50 int i; 51 52 i = 0; 53 while ((to[i] = from[i]) != '\0') 54 ++i; 55 }
B截图(注意最长数组前面被填充的算法):
如果现实是无极,那内存就是太极,CPU的作用只是力图将线性化的空间还原为立体化的空间。其间当然要涉及映射运算。