随笔分类 - c/c++
摘要:1、线性查找法 #include <stdio.h> #define FAILED -1 int len(char x[]) { int len = 0; while(x[len]) len++; return len; } int search(char x[], char key) { int
阅读全文
摘要:1、 #include <stdio.h> void null_string(char x[]) { x[0] = '\0'; } int main(void) { char str[128]; printf("str: "); scanf("%s", str); printf("begin: %s
阅读全文
摘要:c语言中输出字符串的长度。 1、 #include <stdio.h> int str_len(const char x[]) { int len = 0; while(x[len]) len++; return len; } int main(void) { char str[100]; prin
阅读全文
摘要:1、 #include <stdio.h> #define NUMBER 10 int main(void) { int i; char s[NUMBER][128]; for(i = 0; i < NUMBER; i++) { printf("s[%d] = ", i); scanf("%s",
阅读全文
摘要:1、 #include <stdio.h> int main(void) { char s[] = "ABC"; printf("begin: %s\n", s); int i; for(i = 0; i < 4; i++) { s[i] = '\0'; } printf("end: %s\n",
阅读全文
摘要:字符串字面量的中间也可以有null字符,不过应注意区分。字符串字面量“ABC”是字符串,而字符串字面量“AB\0CD”不是字符串。 1、 #include <stdio.h> int main(void) { char str[] = "ABC\0DEF"; printf("result: %s.\
阅读全文
摘要:1、 #include <stdio.h> int gcd(int x, int y) { int max, min, tmp; max = x > y ? x : y; min = x > y ? y : x; tmp = x % y; if(tmp != 0) { gcd(min, tmp);
阅读全文
摘要:1、 #include <stdio.h> int fac(int x) { if(x > 0) return x * fac(x - 1); else return 1; } int main(void) { int n, r; puts("please input two integers.")
阅读全文
摘要:1、 #include <stdio.h> enum a {x, y, z}; int main(void) { printf("x = %d\n", x); printf("y = %d\n", y); printf("z = %d\n", z); return 0; } 2、 #include
阅读全文
摘要:1、从尾至头,升序 #include <stdio.h> #define NUMBER 5 void sort_1(int x[], int n) { int i, j; for(i = 0; i < n - 1; i++) { for(j = n - 1; j > i; j--) { if(x[j
阅读全文
摘要:1、 #include <stdio.h> #define swap(type, x, y) type tmp = x; x = y; y = tmp; int main(void) { int x = 10, y = 5; swap(int, x, y); printf("x = %d\n", x
阅读全文
摘要:1、 #include <stdio.h> #define max(x, y) ((x > y) ? x : y) int main(void) { int a, b, c, d; puts("please input four integers."); printf("a = "); scanf(
阅读全文
摘要:c语言 8-1 1、 #include <stdio.h> #define diff(x, y) ((x) - (y)) int main(void) { int a, b; puts("please input two integers."); printf("a = "); scanf("%d"
阅读全文
摘要:c语言中函数、函数式宏例子 返回一个数的平方。 1、函数 #include <stdio.h> int sqr_int(int x) { return x * x; } double sqr_double(double x) { return x * x; } int main(void) { in
阅读全文
摘要:1、 #include <stdio.h> int main(void) { int i, j, ch; int cnt[10] = {}; while((ch = getchar()) != EOF) { if(ch > '0' && ch < '9') { cnt[ch - '0']++; }
阅读全文
摘要:c语言中数字字符计数 1、 #include <stdio.h> int main(void) { int i, ch; int cnt[10] = {}; while((ch = getchar()) != EOF) { switch(ch) { case '0': cnt[0]++; break
阅读全文
摘要:1、 #include <stdio.h> int main(void) { int ch; int rows = 0; while((ch = getchar()) != EOF) { switch(ch) { case '\n': rows++; break; } } printf("numbe
阅读全文
摘要:c语言中数字字符计数。 1、 #include <stdio.h> int main(void) { int i, ch; int cnt[10] = {}; while((ch = getchar()) != EOF) { switch(ch) { case '0': cnt[0]++; brea
阅读全文
摘要:c语言中将输入的字符直接输出 getchar函数 和EOF getchar函数用于读取字符并返回,(getchar和putchar都只处理一个字符); EOF是对象式宏,为一个负值。 1、 #include <stdio.h> int main(void) { int ch; while((ch =
阅读全文
摘要:1、 #include <stdio.h> int main(void) { int ch; int time[1] = {}; while((ch = getchar()) != EOF) { switch(ch) { case '\n': time[0]++; break; } } printf
阅读全文

浙公网安备 33010602011771号