|
> いかがでしょうか?これらの処理はC言語でできる物なのでしょうか?
確かに、C は文字列処理が苦手ですが、手順が決まっているものは
たいてい処理できますよ。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void step3(int n)
{
do {
int sum = 0;
while (n != 0) sum += n % 10, n /= 10;
n = sum;
} while (n >= 10 && n != 11 && n != 22);
printf("Step 3: %d\n", n);
}
void next(char *s) { for (; *s; s++) if (++*s > '9') *s = '0'; }
void zero(char *s) { strchr(s, '0') && printf(" %s\n", s); }
void step5(int n1, int n2)
{
char b1[20], b2[20]; int i;
printf("Step 5:\n"), sprintf(b1, "%d", n1), sprintf(b2, "%d", n2);
for (i = 0; i < 10; i++)
printf(" %s %s\n", b1, b2), next(b1), next(b2);
}
void step6(int n1, int n2)
{
char b1[20], b2[20]; int i;
printf("Step 6:\n"), sprintf(b1, "%d", n1), sprintf(b2, "%d", n2);
for (i = 0; i < 10; i++)
zero(b1), zero(b2), next(b1), next(b2);
}
void proc(const char *s)
{
unsigned char c; int sum = 0, n1, n2;
printf("Step 1: ");
while ((c = *s++) != 0 && c < 0x80)
isdigit(c) ? (putchar(c), sum += c - '0') : putchar(' ');
printf("\nStep 2: %d\n", sum);
step3(sum);
while ((c = *s) != 0 && !isdigit(c)) s++;
if (sscanf(s, "%d%d", &n1, &n2) != 2) exit(1);
printf("Step 4: %d %d\n", n1, n2);
step5(n1, n2);
step6(n1, n2);
}
int main(void)
{
char *s = "012 345/678/901全角 234 567";
printf("string: %s\n", s);
proc(s);
return 0;
}
|