今天有个同事也在思考简单的程序转换问题,我也想了个思路,也抵不住,自己也实现了一篇如下:
file: main.c
- /**
- * author: vincent.cws2008@gmail.com
- * history:
- * initial @ 2011-10-22
- */
- #include "stdio.h"
-
#include "malloc.h"
-
#include "string.h"
-
-
const char a[]={1,1,1,1,1,1,1,1,1};
-
const char b[]={2,2,2,2,2,2,2,2,2};
-
const char c[]={3,3,3,3,3,3,3,3,3};
-
const char d[]={4,4,4,4,4,4,4,4,4};
-
const char e[]={5,5,5,5,5,5,5,5,5};
-
-
const char *chars_table[]={a, b, c, d, e};
-
-
#ifndef __assert
-
#define __assert(x)
-
#else
-
#define __assert(x)
-
#endif
-
-
#ifndef NULL
-
#define NULL (void*)0
-
#endif
-
-
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-
-
void display(const char* pbitmap, int width, int counts)
-
{
-
int i=0, j=0;
-
__assert(pbitmap);
-
printf("bitmap display:\n");
-
while(pbitmap[i]){
-
for (j=0; j<width*counts; j++){
-
printf("%d ", pbitmap[j]);
-
i++;
-
}
-
printf("\n");
-
}
-
}
-
-
bool chars2bitmap(int width, int high, const char* ptext, const char **ptable)
-
{
-
char *pbitmap=NULL;
-
int size=0,counts=0,pos=0;
-
int i=0, j=0, k=0;
-
__assert(ptext&&ptable);
-
counts = strlen(ptext);
-
size = counts*high*width;
-
pbitmap = (char*)malloc(size+1);
- __assert(pbitmap);
-
for(i=0,pos=0; i<high; i++) {
-
for (j=0; j<counts; j++){
-
for (k=0; k<width; k++){
-
pbitmap[pos++]=ptable[ptext[j]-'a'][k+i*width];
-
}
-
}
-
}
-
pbitmap[pos]='\0';
-
-
display(pbitmap, width, counts);
-
-
if (pbitmap) {
-
free(pbitmap);
-
pbitmap = NULL;
-
}
-
return true;
-
}
-
-
-
-
int main(int argc, char* argv[])
-
{
-
chars2bitmap(3, 3, "cabe", chars_table);
-
return 0;
-
}
简单测试了一下,还好能通过,没仔细查看,输出如下:
------------------------------------------------------------------------------------------
插个网上经常考试的题型:
- void *memcpyx(void *dest, const void*src, size_t n)
-
{
-
char *dest1 = dest;
- const char*src1 = src;
-
while(n--)
-
*dest1++ = *src++;
-
return dest;
-
}
-
-
void *memmovex(void *dest, const void *src, size_t count)
-
{
-
char *tmp;
-
const char *s;
-
-
if (dest <= src) {
-
tmp = dest;
-
s = src;
-
while (count--)
-
*tmp++ = *s++;
-
} else {
-
tmp = dest;
-
tmp += count;
-
s = src;
-
s += count;
-
while (count--)
-
*--tmp = *--s;
-
}
-
return dest;
-
}
阅读(1249) | 评论(0) | 转发(0) |