3-2
#include <stdio.h>
#define MAXLINE 100
void escape(char s[], char t[]);
void dis_escape(char s[], char t[]);
int getline(char line[], int);
int main()
{
char s[MAXLINE], t[MAXLINE];
while (1)
{
getline(s, MAXLINE);
getline(t, MAXLINE);
escape(s, t);
printf("After:\ns:%s\n", s);
escape(t, s);
printf("After:\nt:%s\n", t);
}
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 escape(char s[], char t[])
{
int sn, tn;
sn = 0;
tn = 0;
while (s[sn] != '\n')
sn++;
while (t[tn] != '\0')
{
switch (t[tn])
{
case '\n':
s[sn++] = '\\';
s[sn++] = 'n';
break;
case '\t':
s[sn++] = '\\';
s[sn++] = 't';
break;
default:
s[sn++] = t[tn];
break;
}
tn++;
//s[sn++] = t[tn++];
}
s[sn] = '\0';
}
void dis_escape(char s[], char t[])
{
int sn, tn;
sn = 0;
tn = 0;
while (s[sn] != '\n')
sn++;
while (t[tn] != '\0')
{
if (t[tn] == '\\')
{
tn++;
switch (t[tn])
{
case 'n':
s[sn++] = '\n';
break;
case 't':
s[sn++] = '\t';
break;
default:
s[sn++] = '\\';
s[sn++] = t[tn];
break;
}
}
else
s[sn++] = t[tn];
tn++;
}
s[sn] = '\0';
}
3-3
#include <stdio.h>
#define MAXLINE 100
void espand(char s1[], char s2[]);
int getline(char line[], int);
int main()
{
char s1[MAXLINE], s2[MAXLINE];
while (1)
{
getline(s1, MAXLINE);
espand(s1, s2);
printf("t:\n%s\n", 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 espand(char s1[], char s2[])
{
char before, now,i;
int n1, n2, status, before_status, flag,end;
n1 = 0;
n2 = 0;
before = '\0';
status = 0;
before_status = 0;
flag = 0;
end = 0;
while (s1[n1] != '\0')
{
now = s1[n1];
if (now >= '0' && now <= '9')
status = 1;
else if (now >= 'a' && now <= 'z')
status = 2;
else if (now >= 'A' && now <= 'Z')
status = 3;
else
status = 0;
if (flag)
{
if (status == before_status && before < now)
{
for (i = before; i < now; i++)
s2[n2++] = i;
}
else
{
s2[n2++] = before;
s2[n2++] = '-';
}
end = 1;
s2[n2++] = now;
}
else if (now == '-' && before == '\0')
{
s2[n2++] = '-';
end = 1;
}
else if (now != '-' && before != '\0')
{
s2[n2++] = before;
}
if (end)
{
before = 0;
flag = 0;
end = 0;
}
else
{
if (now != '-')
{
flag = 0;
before = now;
before_status = status;
}
else
flag = 1;
}
n1++;
}
s2[n2] = '\0';
}
3-4
#include <stdio.h>
#include <string.h>
#define MAXLINE 100
void itoa(char n, char s[]);
void reverse(char s[]);
int main()
{
int n;
char s[MAXLINE];
while (1)
{
printf("input:\n");
scanf("%d", &n);
itoa(n, s);
printf("s:\n%s\n", s);
}
return 0;
}
void itoa(char n, char s[])
{
char i, sign;
printf("n:\n%o\n", n);//'~'是取反
if ((sign = n) < 0)
n = -n;//'-'是求补码
i = 0;
if (n != -128)
{
do
{
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
if (sign < 0)
s[i++] = '-';
}
else
{
n++;
n = -n;
do
{
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
s[0]++;
if (sign < 0)
s[i++] = '-';
}
s[i] = '\0';
reverse(s);
}
void reverse(char s[])
{
int c, i, j;
for (i = 0, j = strlen(s) - 1; i < j; i++, j--)
{
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
3-5
#include <stdio.h>
#include <string.h>
#define MAXLINE 100
void itob(int n, char s[], int b);
void reverse(char s[]);
int main()
{
int n, b;
char s[MAXLINE];
while (1)
{
printf("input n:\n");
scanf("%d", &n);
printf("input b:\n");
scanf("%d", &b);
itob(n, s, b);
printf("s:\n%s\n", s);
}
return 0;
}
void itob(int n, char s[],int b)
{
int i, sign;
if ((sign = n) < 0)
n = -n;//'-'是求补码
i = 0;
if(b > 10)
{
do
{
s[i] = n % b + '0';
if (s[i] > '9')
{
s[i] = s[i] - '9' - 1 + 'A';
}
i++;
} while ((n /= b) > 0);
}
else
{
do
{
s[i++] = n % b + '0';
} while ((n /= b) > 0);
}
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
void reverse(char s[])
{
int c, i, j;
for (i = 0, j = strlen(s) - 1; i < j; i++, j--)
{
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
3-6
#include <stdio.h>
#include <string.h>
#define MAXLINE 100
void itoa(char n, char s[],int width);
void reverse(char s[]);
int main()
{
int n,width;
char s[MAXLINE];
while (1)
{
printf("input n:\n");
scanf("%d", &n);
printf("input width:\n");
scanf("%d", &width);
itoa(n, s, width);
printf("s:\n%s\n", s);
}
return 0;
}
void itoa(char n, char s[], int width)
{
char i, sign;
printf("n:\n%o\n", n);//'~'是取反
if ((sign = n) < 0)
n = -n;//'-'是求补码
i = 0;
if (n != -128)
{
do
{
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
if (sign < 0)
s[i++] = '-';
}
else
{
n++;
n = -n;
do
{
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
s[0]++;
if (sign < 0)
s[i++] = '-';
}
for (; i < width; i++)
s[i] = ' ';
s[i] = '\0';
reverse(s);
}
void reverse(char s[])
{
int c, i, j;
for (i = 0, j = strlen(s) - 1; i < j; i++, j--)
{
c = s[i];
s[i] = s[j];
s[j] = c;
}
}