2-3
//十六进制字符串转化为整型数
#include <stdio.h>
#define MAXLINE 100
void htoi(char s[]);
int getline(char line[], int lim);
int main()
{
char s[MAXLINE];
while (getline(s, MAXLINE) > 0)
{
printf("origin:%s", s);
htoi(s);
}
}
int getline(char line[], int lim)
{
int i, c;
i = 0;
printf("input:\n");
while (i < lim - 1 && (c = getchar()) != '\n' && c != EOF)
{
line[i] = c;
i++;
}
if (c == '\n')
{
line[i] = c;
i++;
}
line[i] = '\0';
return i;
}
void htoi(char s[])//不支持负数
{
int i = 0;
int n = 0;
while (s[i] != '\0')
{
if (s[i++] != '0')
;
else if (s[i] == 'x' || s[i] == 'X')//确保为16进制数前缀
{
i++;
while ((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f') || (s[i] >= 'A' && s[i] <= 'F'))//确保为16进制数
{
if (s[i] >= '0' && s[i] <= '9')
n = s[i] - '0' + n * 16;
else if (s[i] >= 'a' && s[i] <= 'f')
n = s[i] - 'a' + 10 + n * 16;
else
n = s[i] - 'A' + 10 + n * 16;
i++;
}
if (n != 0)
printf("%d\n", n);
n = 0;
}
}
}
2-4
/*
2-4.
Write an alternate version of squeeze(s1,s2) that deletes each character in the string s1
that matches any character in the string s2 .
*/
#include <stdio.h>
#define MAXLINE 100
void squeeze(char s1[], char s2[]);
int getline(char line[], int lim);
int main()
{
char s1[MAXLINE], s2[MAXLINE];
getline(s1, MAXLINE);3
printf("s1:%ss2:%s\n", s1, s2);
squeeze(s1, s2);
printf("final:\n%s\n", s1);
return 0;
}
int getline(char line[], int lim)
{
int i, c;
i = 0;
printf("input:\n");
while (i < lim - 1 && (c = getchar()) != '\n' && c != EOF)
{
line[i] = c;
i++;
}
if (c == '\n')
{
line[i] = c;
i++;
}
line[i] = '\0';
return i;
}
void squeeze(char s1[], char s2[])
{
int i1, i2, j;
char temp;
i2 = 0;
while ((temp = s2[i2]) != '\0')
{
i1 = 0;
j = 0;
for (; s1[j] != '\0'; j++)
{
if (s1[j] != temp)
s1[i1++] = s1[j];
}
s1[i1] = '\0';
i2++;
}
}
2-5
#include <stdio.h>
#define MAXLINE 100
void any(char s1[], char s2[]);
int getline(char line[], int lim);
int main()
{
char s1[MAXLINE], s2[MAXLINE];
getline(s1, MAXLINE);
getline(s2, MAXLINE);
printf("s1:%ss2:%s", s1, s2);
any(s1, s2);
return 0;
}
int getline(char line[], int lim)
{
int i, c;
i = 0;
printf("input:\n");
while (i < lim - 1 && (c = getchar()) != '\n' && c != EOF)
{
line[i] = c;
i++;
}
if (c == '\n')
{
line[i] = c;
i++;
}
line[i] = '\0';
return i;
}
void any(char s1[], char s2[])
{
int i2, j;
char temp;
i2 = 0;
while ((temp = s2[i2]) != '\0')
{
j = 0;
for (; s1[j] != '\0' && s1[j] != temp; j++) //get j;
;
if (s1[j] == '\0')
printf("%c:%d\n", temp, -1);
else
{
if (s1[j] == '\n')
printf("\\n:%d\n", j);
else
printf("%c:%d\n", temp, j);
}
i2++;
}
}
2-6
#include <stdio.h>
unsigned int setbits(unsigned int, int, int, unsigned int);
int main()
{
unsigned int x, y,answer;
int p, n;
while (1)
{
printf("x:");
scanf("%o", &x);
printf("y:");
scanf("%o", &y);
printf("p:");
scanf("%d", &p);
printf("d:");
scanf("%d", &n);
answer = setbits(x, p, n, y);
printf("answer=%o\n", answer);
}
return 0;
}
unsigned int setbits(unsigned int x, int p, int n, unsigned int y)
{
unsigned temp101, temp010, tempx, tempy;
temp010 = (~(~0 << n)) << (p - n + 1);
temp101 = ~temp010;
tempy = (y << (p - n + 1)) & temp010;
tempx = x & temp101;
return tempx + tempy;
}
2-7
#include <stdio.h>
unsigned int invert(unsigned int, int, int);
int main()
{
unsigned int x, answer;
int p, n;
while (1)
{
printf("x:");
scanf("%o", &x);
printf("p:");
scanf("%d", &p);
printf("n:");
scanf("%d", &n);
answer = invert(x, p, n);
printf("answer=%o\n", answer);
}
return 0;
}
unsigned int invert(unsigned int x, int p, int n)
{
unsigned temp101, temp010;
temp010 = (~(~0 << n)) << (p - n + 1);
//printf("temp010:%o\n", temp010);
temp101 = ~temp010;
//printf("temp101:%o\n", temp101);
//printf("x & temp101:%o\n", x & temp101);
//printf("(~(x & temp010)) & temp010:%o\n", (~(x & temp010)) & temp010);
return x & temp101 + (~(x & temp010) & temp010);
}
2-8
#include <stdio.h>
unsigned rightrot(unsigned, int);
int main()
{
unsigned int x, answer;
int n;
while (1)
{
printf("x:");
scanf("%o", &x);
printf("n:");
scanf("%d", &n);
answer = rightrot(x, n);
printf("answer=%o\n", answer);
}
return 0;
}
unsigned rightrot(unsigned int x, int n)
{
unsigned temp, xmove, xadd;
int i;
xmove = x >> n;
printf("xmove:%o\n", xmove);
temp = xmove;
for (i = 1; temp >= 2; i++)
temp /= 2;
printf("i:%d\n", i);
xadd = (x & ~(~0 << n)) << i;
printf("xadd:%o\n", xadd);
return xmove + xadd;
}
2-9
#include <stdio.h>
#define MAXLINE 100
int bitsearch(int, int v[], int);
void buildarray(int v[],int n);
int main()
{
int x, location, numbers[MAXLINE];
x = 0;
buildarray(numbers, MAXLINE);
while (x != MAXLINE)
{
location = bitsearch(x, numbers, MAXLINE);
printf("location is %d\n", location);
x++;
}
return 0;
}
void buildarray(int v[], int n)
{
int i;
for (i = 0; i < n; i++)
v[i] = i;
}
int bitsearch(int x, int v[], int n)
{
int low, mid, high;
low = 0;
high = n - 1;
/**/
while (low <= high)
{
mid = (low + high) / 2;
if (x < v[mid])
high = mid - 1;
else if (x > v[mid])
low = mid + 1;
else
return mid;
}
return -1;
/*/
mid = (low + high) / 2;
while ((low <= high) && (x != mid))
{
if (x < v[mid])
high = mid - 1;
else
low = mid + 1;
mid = (low + high) / 2;
}
if (x == mid)
return mid;
else
return 0;
/**/
}
2-10
#include <stdio.h>
int lower(int);
int main()
{
int c, c_lower;
while ((c = getchar()) != '\n')
{
c_lower=lower(c);
printf("result:%c,%d\n", c_lower, c_lower);
}
return 0;
}
int lower(int c)
{
return (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
}