Skip to content

Commit

Permalink
c study
Browse files Browse the repository at this point in the history
  • Loading branch information
wenjun1055 committed Oct 10, 2013
1 parent f08a075 commit 6c7f9c2
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ thread4
thread
thread1
thread5
*.dSYM
38 changes: 38 additions & 0 deletions 6.1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>
#include <assert.h>

char *find_char(char *source, char *chars);

int main()
{
char *source = "ABCDEF";
char *chars = "ESmSG";
char *result;
result = find_char(source, chars);
printf("%c\r\n", *result);

return 0;
}

char *find_char(char *source, char *chars)
{
assert(source != NULL);
assert(chars != NULL);

while (*source != '\0') {

char *char_tmp = chars;

while (*char_tmp != '\0') {
if (*source == *char_tmp) {
return source;
}

char_tmp++;
}

source++;
}

return "\r\0";
}
Binary file added 6.2
Binary file not shown.
56 changes: 56 additions & 0 deletions 6.2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>

int del_substr(char *str, char *substr);
int find_str(char *str, char *substr);

int main()
{
char *str = "ABCDEFGHIJK";
char *substr = "DBCD";

printf("%d", find_str(str, substr));

if (! find_str(str, substr)) {
printf("not exists substr in str\r\n");
return 0;
}


}

int find_str(char *str, char *substr)
{
int counter = 0;

assert(str != NULL);
if (substr == NULL) {
return 1;
}

while (*str != '\0') {
char str_char = *str;
char substr_char = *substr;

if (str_char == substr_char) {
while (1) {
str_char = *++str;
substr_char = *++substr;

if (str_char != substr_char && substr_char != '\0') {
break;
}

if ((str_char != substr_char && substr_char == '\0') || (str_char == substr_char && substr_char == '\0')) {
return 1;
}
}
}

str++;
}

return 0;
}

35 changes: 35 additions & 0 deletions 6.3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void reverse_string(char *string);

int main()
{
char *string = "ABCDEFG";
printf("Origion:%s", string);
reverse_string(string);
string = strrev(*string);
printf("Change:%s", string);

return 0;
}

void reverse_string(char *string)
{
char *h = string;
char *t = string;
char ch;

while(*t++){};
t--;
t--;

while(h < t) {
ch = *h;
*h++ = *t;
*t-- = ch;
}

//return string;
}
Binary file added 7.1.2
Binary file not shown.
31 changes: 31 additions & 0 deletions 7.2.1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdio.h>

int gcd(int m, int n);

int main()
{
int m,n,max;
printf("Please input m and n:");
scanf("%d,%d", &m, &n);
max = gcd(m, n);
printf("max = %d\r\n", max);

return 0;
}

int gcd(int m, int n)
{
if (0 >= m || 0 >= n) {
return 0;
}

int r = m % n;

if (r == 0) {
return n;
}

if (r > 0) {
return gcd(n, r);
}
}
Binary file added 7.2.2
Binary file not shown.
22 changes: 22 additions & 0 deletions 7.2.2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>

int main()
{
int max,m,n;
printf("Please input m and n:");
scanf("%d,%d",&m, &n);

if (0 >= m || 0 >= n) {
max = 0;
}

max = m % n;
while (max > 0) {
m = n;
n = max;

max = m % n;
}

printf("%d\r\n", n);
}
Binary file added 7.4
Binary file not shown.
29 changes: 29 additions & 0 deletions 7.4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>
#include <stdarg.h>

int max_list(int start, ...);

int main()
{
printf("max is %d\r\n", max_list(5,11,2,56,34,999,32,-1));

return 0;
}

int max_list(int start, ...)
{
va_list var_arg;
int count;
int i = 0, max, current;

va_start(var_arg, start);
max = start;

do {
current = va_arg(var_arg, int);
if (current > max)
max = current;
} while(current > 0);

return max;
}
Binary file added 9_test
Binary file not shown.
22 changes: 22 additions & 0 deletions 9_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <string.h>

int main()
{
char *a = "abcd";
char *b = "abcde";

if (strlen(a) > strlen(b)) {
printf("1\r\n");
} else {
printf("2\r\n");
}

//stlen的结果是个无符号数,所以操作符>=左边的表达式也将是无符号数
//而无符号数绝不可能是负的
if (strlen(a) - strlen(b) >= 0) {
printf("3\r\n");
} else {
printf("4\r\n");
}
}

0 comments on commit 6c7f9c2

Please sign in to comment.