c代码:电话号码和字母转换
这两天看到有人讨论电话键盘上的字母、号码和字母的转换,我也随便写了一段
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_LEN 15
char *tbl_itoa[] =
{
"0", // 0
"1", // 1
"ABC", // 2
"DEF", // 3
"GHI", // 4
"JKL", // 5
"MNO", // 6
"PQRS", // 7
"TUV", // 8
"WXYZ" // 9
};
int tbl_atoi['Z'-'A'+1] = {0};
void init_tbl()
{
int c;
int i;
for (c='A'; c<='Z'; c++)
{
for (i=0; i<=9; i++)
{
if (strchr(tbl_itoa[i], c) != NULL)
{
tbl_atoi[c-'A'] = i;
break;
}
}
}
#if 0 // DEBUG
for (c='A'; c<='Z'; c++)
{
printf("%c %d\n", (char)c, tbl_atoi[c-'A']);
}
#endif
}
void to_digits(const char *s);
void to_alpha(const char *s);
int is_all_digits(const char *s);
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Usage: %s STRING\n", argv[0]);
return 1;
}
if (strlen(argv[1]) > MAX_LEN)
{
printf("string too long. only %d allowed\n", MAX_LEN);
return 1;
}
init_tbl();
if (is_all_digits(argv[1]))
to_alpha(argv[1]);
else
to_digits(argv[1]);
return 0;
}
int is_all_digits(const char *s)
{
char c;
while ((c=*s++) != '\0')
if (!isdigit(c))
return 0;
return 1;
}
void to_digits(const char *s)
{
char c;
while ((c=*s++) != '\0')
{
if (islower(c))
c = toupper(c);
if (isupper(c))
printf("%d", tbl_atoi[(int)(c-'A')]);
else
printf("%c", c);
}
printf("\n");
}
void to_alpha(const char *s)
{
char *p;
static char result[MAX_LEN+2] = {0};
static int idx = 0;
if (!isdigit(*s))
{
result[idx] = '\0';
printf("%s\n", result);
idx--;
return;
}
p = tbl_itoa[(int)(*s-'0')];
while(*p)
{
result[idx++] = *p;
to_alpha(s+1);
p++;
}
idx--;
}
相关文档:
源码:
/* 学生成绩查询系统 */
# include <stdio.h>
# include <stdlib.h>
int main( )
{
int select;
int i, j;
int score[5][7];
int average = 0;
int sum = 0;
&n ......
(转自)http://blog.pfan.cn/whyhappy/6030.html
函数名与函数指针
一 通常的函数调用
一个通常的函数调用的例子:
//自行包含头文件
void MyFun(int x); //此处的申明也可写成:void MyFun( int );
int main(int argc, char* argv[])
{
......
Keil C里用到了unsigned long长整型变量,编译时都能通过,但运行时老是溢出,同伙百度到了此贴,解决了问题,于是转帖于此:
今天调试一个乘法,出了点问题,先看代码示意:
void test(void)
{
unsigned long mid1,mid2,mid3;
mid1 ......
---- 在数据库的应用开发中,常常会遇到性能和代价的之间矛盾。以作者在开发股市行
情查询和交易系统中遇到的问题为例,要在实时记录1000多只股票每分钟更新一次的行
情数据的同时,响应大量并发用户的数据查询请求。考虑到性价比和易维护性,系统又
要求在基于PC服务器,Windows NT平台的软硬件环境下实现。开 ......