这一章覆盖下面几个问题
#include
#include
int
main ()
{
printf("Size of Char %d\n", CHAR_BIT);
printf("Size of Char Max %d\n", CHAR_MAX);
printf("Size of Char Min %d\n", CHAR_MIN);
printf("Size of int min %d\n", INT_MIN);
printf("Size of int max %d\n", INT_MAX);
printf("Size of long min %ld\n", LONG_MIN); /* RB */
printf("Size of long max %ld\n", LONG_MAX); /* RB */
printf("Size of short min %d\n", SHRT_MIN);
printf("Size of short max %d\n", SHRT_MAX);
printf("Size of unsigned char %u\n", UCHAR_MAX); /* SF */
printf("Size of unsigned long %lu\n", ULONG_MAX); /* RB */
printf("Size of unsigned int %u\n", UINT_MAX); /* RB */
printf("Size of unsigned short %u\n", USHRT_MAX); /* SF */
return 0;
}
结果是非常有用的,输出如下:
Size of Char 8
Size of Char Max 127
Size of Char Min -128
Size of int min -2147483648
Size of int max 2147483647
Size of long min -2147483648//对半分的话,由于max多一个0,所以max要少一,最后一位7
Size of long max 2147483647
Size of short min -32768
Size of short max 32767
Size of unsigned char 255
Size of unsigned long 4294967295//(2^32-1)
Size of unsigned int 4294967295
Size of unsigned short 65535
#include
#include
/* strlen: return length of s */
int strlen1(char s[])
{
int i;
while (s[i] != '\0')
++i;
return i;
}
intmain (){ char str[]="what's your name"; int length=strlen1(str); printf("%d",length);} for(i=0; i
solution I,code:
#include
#define MAX_STRING_LENGTH 100
int main(void)
{
/*
for (i = 0; i < lim-1 && (c=getchar()) != '\n' && c != EOF; ++i)
s[i] = c;
*/
int i = 0,
lim = MAX_STRING_LENGTH,
c;
char s[MAX_STRING_LENGTH];
while (i < (lim - 1))
{
c = getchar();
if (c == EOF)
break;
else if (c == '\n')
break;
s[i++] = c;
}
s[i] = '\0'; /* terminate the string */
return 0;
}
#include
#define lim 80
int main()
{
int i, c;
char s[lim];
/* There is a sequence point after the first operand of ?: */
for(i=0; i
/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;
n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + (s[i] - '0');
return n;
}
/* lower: convert c to lower case; ASCII only */
int lower(int c)
{
if (c >= 'A' && c <= 'Z')
return c + 'a' - 'A';
else
return c;
}
float x;
int i;
x=i;//float回被转为成int
int n
sqrt((double)n)//强制转换n为double
#include
unsigned long int next = 1;
/* rand: return pseudo-random integer on 0..32767 */
int rand(void)
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}
/* srand: set seed for rand()
* 而使用同种子相同的数调用 srand()会导致相同的随机数序列被生成。
* */
void srand(unsigned int seed)
{
next = seed;
}
int main(){
int i=rand();
printf("%d",i);
}
/* Write the function htoi(s), which converts a string of hexadecimal
* digits (including an optional 0x or 0X) into its equivalent integer
* value. The allowable digits are 0 through 9, a through f, and
* A through F.
*
* I've tried hard to restrict the solution code to use only what
* has been presented in the book at this point (page 46). As a
* result, the implementation may seem a little naive. Error
* handling is a problem. I chose to adopt atoi's approach, and
* return 0 on error. Not ideal, but the interface doesn't leave
* me much choice.
*
* I've used unsigned int to keep the behaviour well-defined even
* if overflow occurs. After all, the exercise calls for conversion
* to 'an integer', and unsigned ints are integers!
*/
/* These two header files are only needed for the test driver */
#include
#include
/* Here's a helper function to get me around the problem of not
* having strchr
*/
int hexalpha_to_int(int c)
{
char hexalpha[] = "aAbBcCdDeEfF";
int i;
int answer = 0;
for(i = 0; answer == 0 && hexalpha[i] != '\0'; i++)
{
if(hexalpha[i] == c)
{
answer = 10 + (i / 2);//原来是这样处理的哇
}
}
return answer;
}
unsigned int htoi(const char s[])
{
unsigned int answer = 0;
int i = 0;
int valid = 1;
int hexit;
if(s[i] == '0')//去掉0
{
++i;
if(s[i] == 'x' || s[i] == 'X')//去掉X
{
++i;
}
}
while(valid && s[i] != '\0')
{
answer = answer * 16;//因为先算得是最高位的,又是16进制的
if(s[i] >= '0' && s[i] <= '9')
{
answer = answer + (s[i] - '0');
}
else
{
hexit = hexalpha_to_int(s[i]);//zog_c这个字符串在这里会被当作0来处理掉。
if(hexit == 0)
{
valid = 0;
}
else
{
answer = answer + hexit;
}
}
++i;
}
if(!valid)
{
answer = 0;
}
return answer;
}
/* Solution finished. This bit's just a test driver, so
* I've relaxed the rules on what's allowed.
*/
int main(void)
{
char *endp = NULL;
char *test[] =
{
"F00",
"bar",
"0100",
"0x1",
"0XA",
"0X0C0BE",
"abcdef",
"123456",
"0x123456",
"deadbeef",
"zog_c"
};
unsigned int result;
unsigned int check;
//size_t是标准C库中定义的,应为unsigned int,在64位系统中为 long unsigned int
size_t numtests = sizeof test / sizeof test[0];//这里的test不用加()么。得到数组的长度
size_t thistest;
for(thistest = 0; thistest < numtests; thistest++)
{
result = htoi(test[thistest]);
//strtoul是c标准提高的函数,16代表的将字符串转换为16进制的,当然也可以填写10
check = (unsigned int)strtoul(test[thistest], &endp, 16);
if((*endp != '\0' && result == 0) || result == check)
{
printf("Testing %s. Correct. %u\n", test[thistest], result);
}
else
{
printf("Testing %s. Incorrect. %u\n", test[thistest], result);
}
}
return 0;
}
/* squeeze: delete all c from s */
void squeeze(char s[], int c)
{
int i, j;
for (i = j = 0; s[i] != '\0'; i++)
if (s[i] != c)
s[j++] = s[i];
s[j] = '\0';
}
/* strcat: concatenate t to end of s; s must be big enough */
void strcat(char s[], char t[])
{
int i, j;
i = j = 0;
while (s[i] != '\0') /* find end of s */
i++;
while ((s[i++] = t[j++]) != '\0') /* copy t */
;
}
/*
* Exercise 2-4 Page 48
*
* Write an alternate version of squeeze(s1,s2) that deletes each
* character in s1 that matches any character in the string s2.
*
*/
/* squeeze2: delete all characters occurring in s2 from string s1. */
void squeeze2(char s1[], char s2[])
{
int i, j, k;
int instr2 = 0;
for(i = j = 0; s1[i] != '\0'; i++)
{
instr2 = 0;
for(k = 0; s2[k] != '\0' && !instr2; k++)
{
if(s2[k] == s1[i])
{
instr2 = 1;
}
}
if(!instr2)
{
s1[j++] = s1[i];
}
}
s1[j] = '\0';
}
/* test driver */
#include
#include
int main(void)
{
char *leftstr[] =
{
"",
"a",
"antidisestablishmentarianism",
"beautifications",
"characteristically",
"deterministically",
"electroencephalography",
"familiarisation",
"gastrointestinal",
"heterogeneousness",
"incomprehensibility",
"justifications",
"knowledgeable",
"lexicographically",
"microarchitectures",
"nondeterministically",
"organizationally",
"phenomenologically",
"quantifications",
"representationally",
"straightforwardness",
"telecommunications",
"uncontrollability",
"vulnerabilities",
"wholeheartedly",
"xylophonically", /* if there is such a word :-) */
"youthfulness",
"zoologically"
};
char *rightstr[] =
{
"",
"a",
"the",
"quick",
"brown",
"dog",
"jumps",
"over",
"lazy",
"fox",
"get",
"rid",
"of",
"windows",
"and",
"install",
"linux"
};
char buffer[32];
size_t numlefts = sizeof leftstr / sizeof leftstr[0];
size_t numrights = sizeof rightstr / sizeof rightstr[0];
size_t left = 0;
size_t right = 0;
for(left = 0; left < numlefts; left++)
{
for(right = 0; right < numrights; right++)
{
strcpy(buffer, leftstr[left]);
squeeze2(buffer, rightstr[right]);
printf("[%s] - [%s] = [%s]\n", leftstr[left], rightstr[right], buffer);
}
}
return 0;
}
#include /* for NULL */
int any(char *s1, char *s2)
{
char array[256]; /* rjh comments
* (a) by making this char array[256] = {0}; the first loop becomes unnecessary.
* (b) for full ANSIness, #include , make the array unsigned char,
* cast as required, and specify an array size of UCHAR_MAX + 1.
* (c) the return statements' (parentheses) are not required.
*/
int i;
if (s1 == NULL) {
if (s2 == NULL) {
return(0);
} else {
return(-1);
}
}
for(i = 0; i < 256; i++) {
array[i] = 0;
}
//核心所在:居然让s2的值去作为一个下标
while(*s2 != '\0') {
array[*s2] = 1;//比如当*s2=‘a’时,就代表了ASCII里面的97,array数组内的下标为97的位置改为了1
s2++;
}
i = 0;
while(s1[i] != '\0') {
if (array[s1[i]] == 1) {
return(i);
}
i++;
}
return(-1);
}
/* test driver by Richard Heathfield */
/* We get a helpful boost for testing from the question text, because we are
* told that the function's behaviour is identical to strpbrk except that it
* returns a pointer instead of a position. We use this fact to validate the
* function's correctness.
*/
#include
int main(void)
{
char *leftstr[] =
{
"",
"a",
"antidisestablishmentarianism",
"beautifications",
"characteristically",
"deterministically",
"electroencephalography",
"familiarisation",
"gastrointestinal",
"heterogeneousness",
"incomprehensibility",
"justifications",
"knowledgeable",
"lexicographically",
"microarchitectures",
"nondeterministically",
"organizationally",
"phenomenologically",
"quantifications",
"representationally",
"straightforwardness",
"telecommunications",
"uncontrollability",
"vulnerabilities",
"wholeheartedly",
"xylophonically",
"youthfulness",
"zoologically"
};
char *rightstr[] =
{
"",
"a",
"the",
"quick",
"brown",
"dog",
"jumps",
"over",
"lazy",
"fox",
"get",
"rid",
"of",
"windows",
"and",
"install",
"linux"
};
size_t numlefts = sizeof leftstr / sizeof leftstr[0];
size_t numrights = sizeof rightstr / sizeof rightstr[0];
size_t left = 0;
size_t right = 0;
int passed = 0;
int failed = 0;
int pos = -1;
char *ptr = NULL;
for(left = 0; left < numlefts; left++)
{
for(right = 0; right < numrights; right++)
{
pos = any(leftstr[left], rightstr[right]);
ptr = strpbrk(leftstr[left], rightstr[right]);
if(-1 == pos)
{
if(ptr != NULL)
{
printf("Test %d/%d failed.\n", left, right);
++failed;
}
else
{
printf("Test %d/%d passed.\n", left, right);
++passed;
}
}
else
{
if(ptr == NULL)
{
printf("Test %d/%d failed.\n", left, right);
++failed;
}
else
{
if(ptr - leftstr[left] == pos)
{
printf("Test %d/%d passed.\n", left, right);
++passed;
}
else
{
printf("Test %d/%d failed.\n", left, right);
++failed;
}
}
}
}
}
printf("\n\nTotal passes %d, fails %d, total tests %d\n",
passed,
failed,
passed + failed);
return 0;
}
还有一种做法,时间复杂度稍稍高很多,是常规的想法,O(m*n):
int any(char s1[], char s2[])
{
int i;
int j;
int pos;
pos = -1;
for(i = 0; pos == -1 && s1[i] != '\0'; i++)
{
for(j = 0; pos == -1 && s2[j] != '\0'; j++)
{
if(s2[j] == s1[i])
{
pos = i;
}
}
}
return pos;
}
/* getbits: get n bits from position p */
unsigned getbits(unsigned x, int p, int n)
{
return (x >> (p+1-n)) & ~(~0 << n);
}
x>>(p+1-n)将期望获得的字段移动到最右端#include
unsigned setbits(unsigned x, int p, int n, unsigned y)
{
return (x & ((~0 << (p + 1)) | (~(~0 << (p + 1 - n))))) | ((y & ~(~0 << n)) << (p + 1 - n));
}
int main(void)
{
unsigned i;
unsigned j;
unsigned k;
int p;
int n;
for(i = 0; i < 30000; i += 511)
{
for(j = 0; j < 1000; j += 37)
{
for(p = 0; p < 16; p++)
{
for(n = 1; n <= p + 1; n++)
{
k = setbits(i, p, n, j);
printf("setbits(%u, %d, %d, %u) = %u\n", i, p, n, j, k);
}
}
}
}
return 0;
}
unsigned invert(unsigned x, int p, int n)
{
return x ^ (~(~0U << n) << p);
}
/* main driver added, in a hurry while tired, by RJH. Better test driver suggestions are welcomed! */
#include
int main(void)
{
unsigned x;
int p, n;
for(x = 0; x < 700; x += 49)
for(n = 1; n < 8; n++)
for(p = 1; p < 8; p++)
printf("%u, %d, %d: %u\n", x, n, p, invert(x, n, p));
return 0;
}
unsigned rightrot(unsigned x, unsigned n)
{
while (n > 0) {
if ((x & 1) == 1)
x = (x >> 1) | ~(~0U >> 1);
else
x = (x >> 1);
n--;
}
return x;
}
/* main driver added, in a hurry while tired, by RJH. Better test driver suggestions are welcomed! */
#include
int main(void)
{
unsigned x;
int n;
for(x = 0; x < 700; x += 49)
for(n = 1; n < 8; n++)
printf("%u, %d: %u\n", x, n, rightrot(x, n));
return 0;
}
/* bitcount: count 1 bits in x */
int bitcount(unsigned x)
{
int b;
for (b = 0; x != 0; x >>= 1)
if (x & 01)
b++;
return b;
}
/* bitcount: count 1 bits in x */
int bitcount(unsigned x)
{
int b;
for (b = 0; x != 0; x &= (x-1))//x &= (x-1)会得到0,如果不是0,就表示之前为0
b++;
return b;
}
int lower(int c)
{
if(c >= 'A' && c <= 'Z')
return c + 'a' - 'A';
else
return c;
}
/* lower: convert c to lower case; ASCII only */
int lower(int c)
{
return c >= 'A' && c <= 'Z' ? c + 'a' - 'A' : c;
}
++n;
printf("%d %d\n", n, power(2, n));
zy@zy:~/Documents/eclipseWorkSpace/test/src$ gcc -g test.c
zy@zy:~/Documents/eclipseWorkSpace/test/src$ gdb a.out