Skip to content

Commit 6c7f9c2

Browse files
committed
c study
1 parent f08a075 commit 6c7f9c2

File tree

13 files changed

+234
-0
lines changed

13 files changed

+234
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ thread4
2323
thread
2424
thread1
2525
thread5
26+
*.dSYM

6.1.c

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
#include <assert.h>
3+
4+
char *find_char(char *source, char *chars);
5+
6+
int main()
7+
{
8+
char *source = "ABCDEF";
9+
char *chars = "ESmSG";
10+
char *result;
11+
result = find_char(source, chars);
12+
printf("%c\r\n", *result);
13+
14+
return 0;
15+
}
16+
17+
char *find_char(char *source, char *chars)
18+
{
19+
assert(source != NULL);
20+
assert(chars != NULL);
21+
22+
while (*source != '\0') {
23+
24+
char *char_tmp = chars;
25+
26+
while (*char_tmp != '\0') {
27+
if (*source == *char_tmp) {
28+
return source;
29+
}
30+
31+
char_tmp++;
32+
}
33+
34+
source++;
35+
}
36+
37+
return "\r\0";
38+
}

6.2

8.8 KB
Binary file not shown.

6.2.c

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <assert.h>
4+
5+
int del_substr(char *str, char *substr);
6+
int find_str(char *str, char *substr);
7+
8+
int main()
9+
{
10+
char *str = "ABCDEFGHIJK";
11+
char *substr = "DBCD";
12+
13+
printf("%d", find_str(str, substr));
14+
15+
if (! find_str(str, substr)) {
16+
printf("not exists substr in str\r\n");
17+
return 0;
18+
}
19+
20+
21+
}
22+
23+
int find_str(char *str, char *substr)
24+
{
25+
int counter = 0;
26+
27+
assert(str != NULL);
28+
if (substr == NULL) {
29+
return 1;
30+
}
31+
32+
while (*str != '\0') {
33+
char str_char = *str;
34+
char substr_char = *substr;
35+
36+
if (str_char == substr_char) {
37+
while (1) {
38+
str_char = *++str;
39+
substr_char = *++substr;
40+
41+
if (str_char != substr_char && substr_char != '\0') {
42+
break;
43+
}
44+
45+
if ((str_char != substr_char && substr_char == '\0') || (str_char == substr_char && substr_char == '\0')) {
46+
return 1;
47+
}
48+
}
49+
}
50+
51+
str++;
52+
}
53+
54+
return 0;
55+
}
56+

6.3.c

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
void reverse_string(char *string);
6+
7+
int main()
8+
{
9+
char *string = "ABCDEFG";
10+
printf("Origion:%s", string);
11+
reverse_string(string);
12+
string = strrev(*string);
13+
printf("Change:%s", string);
14+
15+
return 0;
16+
}
17+
18+
void reverse_string(char *string)
19+
{
20+
char *h = string;
21+
char *t = string;
22+
char ch;
23+
24+
while(*t++){};
25+
t--;
26+
t--;
27+
28+
while(h < t) {
29+
ch = *h;
30+
*h++ = *t;
31+
*t-- = ch;
32+
}
33+
34+
//return string;
35+
}

7.1.2

8.56 KB
Binary file not shown.

7.2.1.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdio.h>
2+
3+
int gcd(int m, int n);
4+
5+
int main()
6+
{
7+
int m,n,max;
8+
printf("Please input m and n:");
9+
scanf("%d,%d", &m, &n);
10+
max = gcd(m, n);
11+
printf("max = %d\r\n", max);
12+
13+
return 0;
14+
}
15+
16+
int gcd(int m, int n)
17+
{
18+
if (0 >= m || 0 >= n) {
19+
return 0;
20+
}
21+
22+
int r = m % n;
23+
24+
if (r == 0) {
25+
return n;
26+
}
27+
28+
if (r > 0) {
29+
return gcd(n, r);
30+
}
31+
}

7.2.2

8.56 KB
Binary file not shown.

7.2.2.c

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
3+
int main()
4+
{
5+
int max,m,n;
6+
printf("Please input m and n:");
7+
scanf("%d,%d",&m, &n);
8+
9+
if (0 >= m || 0 >= n) {
10+
max = 0;
11+
}
12+
13+
max = m % n;
14+
while (max > 0) {
15+
m = n;
16+
n = max;
17+
18+
max = m % n;
19+
}
20+
21+
printf("%d\r\n", n);
22+
}

7.4

8.76 KB
Binary file not shown.

7.4.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
#include <stdarg.h>
3+
4+
int max_list(int start, ...);
5+
6+
int main()
7+
{
8+
printf("max is %d\r\n", max_list(5,11,2,56,34,999,32,-1));
9+
10+
return 0;
11+
}
12+
13+
int max_list(int start, ...)
14+
{
15+
va_list var_arg;
16+
int count;
17+
int i = 0, max, current;
18+
19+
va_start(var_arg, start);
20+
max = start;
21+
22+
do {
23+
current = va_arg(var_arg, int);
24+
if (current > max)
25+
max = current;
26+
} while(current > 0);
27+
28+
return max;
29+
}

9_test

8.57 KB
Binary file not shown.

9_test.c

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
int main()
5+
{
6+
char *a = "abcd";
7+
char *b = "abcde";
8+
9+
if (strlen(a) > strlen(b)) {
10+
printf("1\r\n");
11+
} else {
12+
printf("2\r\n");
13+
}
14+
15+
//stlen的结果是个无符号数,所以操作符>=左边的表达式也将是无符号数
16+
//而无符号数绝不可能是负的
17+
if (strlen(a) - strlen(b) >= 0) {
18+
printf("3\r\n");
19+
} else {
20+
printf("4\r\n");
21+
}
22+
}

0 commit comments

Comments
 (0)