|
|
|
|
|
吴裕雄--天生自然C语言开发:约瑟夫生者死者小游戏
摘要:30 个人在一条船上,超载,需要 15 人下船。 于是人们排成一队,排队的位置即为他们的编号。 报数,从 1 开始,数到 9 的人下船。 如此循环,直到船上仅剩 15 人为止,问都有哪些编号的人下船了呢? #include int c = 0; int i = 1; int j = 0; int a[30] = { 0 }; int b[30] = { 0 }; int mai...
阅读全文
吴裕雄--天生自然C语言开发:排序算法
摘要:#include void bubble_sort(int arr[], int len) { int i, j, temp; for (i = 0; i arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = ...
阅读全文
吴裕雄--天生自然C语言开发:内存管理
摘要:#include #include #include int main() { char name[100]; char *description; strcpy(name, "Zara Ali"); /* 动态分配内存 */ description = (char *)malloc( 200 * sizeof(char) ); if( de...
阅读全文
吴裕雄--天生自然C语言开发:递归
摘要:void recursion() { statements; ... ... ... recursion(); /* 函数调用自身 */ ... ... ... } int main() { recursion(); } #include double factorial(unsigned int i) { if(i int fibonac...
阅读全文
吴裕雄--天生自然C语言开发:错误处理
摘要:#include #include #include extern int errno ; int main () { FILE * pf; int errnum; pf = fopen ("unexist.txt", "rb"); if (pf == NULL) { errnum = errno; fprintf(stderr...
阅读全文
吴裕雄--天生自然C语言开发:强制类型转换
摘要:#include int main() { int sum = 17, count = 5; double mean; mean = (double) sum / count; printf("Value of mean : %f\n", mean ); } #include int main() { int i = 17; char...
阅读全文
吴裕雄--天生自然C语言开发:文件读写
摘要:#include int main() { FILE *fp = NULL; fp = fopen("/tmp/test.txt", "w+"); fprintf(fp, "This is testing for fprintf...\n"); fputs("This is testing for fputs...\n", fp); fclose(fp);...
阅读全文
吴裕雄--天生自然C语言开发: 输入 & 输出
摘要:#include int main() { int testInteger = 5; printf("Number = %d", testInteger); return 0; } #include int main() { float f; printf("Enter a number: "); // %f 匹配浮点型数据 sca...
阅读全文
吴裕雄--天生自然C语言开发:typedef
摘要:#include #include typedef struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } Book; int main( ) { Book book; strcpy( book.title, "C 教程"); ...
阅读全文
吴裕雄--天生自然C语言开发:位域
摘要:struct { unsigned int widthValidated; unsigned int heightValidated; } status; struct { unsigned int widthValidated : 1; unsigned int heightValidated : 1; } status; #include #include /...
阅读全文
吴裕雄--天生自然C语言开发:共同体
摘要:union [union tag] { member definition; member definition; ... member definition; } [one or more union variables]; union Data { int i; float f; char str[20]; } data; #include...
阅读全文
吴裕雄--天生自然C语言开发:结构体
摘要:struct tag { member-list member-list member-list ... } variable-list ; struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } book; ...
阅读全文
吴裕雄--天生自然C语言开发:字符串
摘要:char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; char greeting[] = "Hello"; #include int main () { char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; printf("Greeting message: %s\n...
阅读全文
吴裕雄--天生自然C语言开发:函数指针
摘要:#include int max(int x, int y) { return x > y ? x : y; } int main(void) { /* p 是函数指针 */ int (* p)(int, int) = & max; // &可以省略 int a, b, c, d; printf("请输入三个数字:"); scanf(...
阅读全文
吴裕雄--天生自然C语言开发:指针
摘要:#include int main () { int var1; char var2[10]; printf("var1 变量的地址: %p\n", &var1 ); printf("var2 变量的地址: %p\n", &var2 ); return 0; } #include int main () { int var = ...
阅读全文
吴裕雄--天生自然C语言开发:enum(枚举)
摘要:enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN }; enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN }; enum DAY day; enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN } day; enum { ...
阅读全文
吴裕雄--天生自然C语言开发:数组
摘要:double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; double salary = balance[9]; #include int main () { int n[ 10 ]; /* n 是一个包含 10 个整数的数组 */ int i,j; /* 初始化数组元素 */ for ( i...
阅读全文
吴裕雄--天生自然C语言开发:作用域规则
摘要:#include int main () { /* 局部变量声明 */ int a, b; int c; /* 实际初始化 */ a = 10; b = 20; c = a + b; printf ("value of a = %d, b = %d and c = %d\n", a, b, c); return 0; } #include...
阅读全文
吴裕雄--天生自然C语言开发:函数
摘要:return_type function_name( parameter list ) { body of the function } /* 函数返回两个数中较大的那个数 */ int max(int num1, int num2) { /* 局部变量声明 */ int result; if (num1 > num2) result = num1;...
阅读全文
吴裕雄--天生自然C语言开发:循环
摘要:while(condition) { statement(s); } #include int main () { /* 局部变量定义 */ int a = 10; /* while 循环执行 */ while( a int main () { /* for 循环执行 */ for( int a = 10; a int main ...
阅读全文
|
|