大神帮我做个题 用C语言
将 电话号码 one two 。。。nine zero 翻译成1 2 、、、 9 0
中间会有double
例如输入:OneTwoThree 输出:123
输入:OneTwoDoubleTwo 输出:1222
输入:1Two2 输出:ERROR
输入:DoubleDoubleTwo 输出:ERROR
第三题:有空格,非法字符,两个Double相连,Double位于最后一个单词都错误
2014-12-18 20:20
2014-12-18 20:30
2014-12-19 10:34
2014-12-19 10:54

2014-12-19 10:56
2014-12-19 19:22
2014-12-19 19:26
2014-12-19 21:03
2014-12-19 21:05
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int checkStr(char *);
int splitWord(char *);
void cmpWord(char *);
char phoneWord[20][7];
int main(void) {
int i = 0, cur = 0;
char *phoneStr = malloc(140 * sizeof(char));
if(phoneStr != NULL) {
printf("INPUT...\n");
fgets(phoneStr, 100, stdin);
if(checkStr(phoneStr) == 1) {
cur = splitWord(phoneStr);
if(strcmp(phoneWord[cur - 1], "Double") == 0) {
printf("ERROR 输入字符串最后一个单词不能为Double\n");
goto OUT;
}
for(i = 0; i < cur; i++) {
if(strcmp(phoneWord[i], "Double") == 0
&& strcmp(phoneWord[i + 1], "Double") == 0) {
printf("ERROR 输入字符串中不能有连续的Double\n");
goto OUT;
}
}
for(i = 0; i < cur; i++) {
if(0 == strcmp(phoneWord[i], "Double")) {
strcpy(phoneWord[i], phoneWord[i + 1]);
}
cmpWord(phoneWord[i]);
}
} else {
printf("ERROR 输入字符串合法检测未通过\n");
}
} else {
printf("内存分配错误\n");
exit(1);
}
OUT:
free(phoneStr);
printf("\n");
return 0;
}
int checkStr(char *p) {
int i = 0, flag = -1, LEN = strlen(p) - 1;
if((*p < 'A') || (*p > 'Z') ||
*(p + LEN - 1) < 'a' || *(p + LEN - 1) > 'z') {
flag = 0;
}
for(i = 0; i < LEN; i++) {
if(*(p + i) == ' ') {
flag = 0;
break;
} else if((*(p + i) < 'A') || *(p + i) > 'z' ||
(*(p + i) > 'Z' && *(p + i) < 'a')) {
flag = 0;
break;
} else {
flag = 1;
}
}
return flag;
}
int splitWord(char *p) {
int i = 0, j = 0, k = 0, cur = 0, LEN = strlen(p) - 1;
for(i = 0; i < LEN - 2; i++) {
if(*(p + i) >= 'A' && *(p + i) <= 'Z') {
cur++;
}
}
for(i = 0, j = 0; j < cur; j++) {
for(k = 0; k < 7; i++, k++) {
if(0 == k) {
phoneWord[j][0] = *(p + i);
} else if(*(p + i) >= 'a' && *(p + i) <= 'z') {
phoneWord[j][k] = *(p + i);
} else {
break;
}
}
}
return cur;
}
void cmpWord(char *p) {
int i;
char cmp[10][7] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
for(i = 0; i < 10; i++) {
if(0 == strcmp(cmp[i], p)) {
printf("%d", i);
}
}
}

2014-12-20 09:45