/*提示:本文内容未全部经过程序测试和经典书籍论证,作者不保证所有内容均完全正确,请正确参考,Copyright @ Shine*/
函数//以下程序都是在vc++6.0中实现,请参考
5//简单的格式化字符的函数实例,仅仅实现了一个例子,剩下的无非重复工作
#include "stdafx.h"
#include
#include
#include
using namespace std;
char *print_scn(char *format,...)//实现方法不是特别满意
{
int i;
int len= strlen(format);
char ch_format,ch_buffer;
va_list args;
va_start(args,format);
for(i = 0; i < len; i++)
{
ch_format = *(format + i);
if(ch_format == '%')
{
ch_format = *(format + i + 1);
switch(ch_format)
{
case 'c':
ch_buffer = va_arg(args,char);
*(format + i) = ch_buffer;
strcpy((format + i + 1),(format + i + 2));
i++;//move forward a character
break;
case 's':
break;//not achieved yet
case 'd'
break;
case 'f'
break;
case 'o'
break;
case 'x'
break;
default:
break;
}
}
}
va_end(args);
return format;
}
int main(int argc, char *argv[])
{
char str[50] = "the character of ch is %c.\n";
print_scn(str,'a');
printf("%s",str);
system("PAUSE");
return EXIT_SUCCESS;
}
6
#include "stdafx.h"
#include
#include
#include
using namespace std;
char *weight[]=
{
"thousand",
"hundred"
};
void written_amount(const unsigned int amount,char *buffer)//raw function,just for demo
{
unsigned int temp1,temp2;
unsigned int len = 0;
char tmp_buffer[50];
temp1 = amount % 1000;
temp2 = temp1 % 100;
sprintf(tmp_buffer,"%d ",(amount / 1000));
strcpy(buffer,tmp_buffer);
len = strlen(tmp_buffer);
strcpy((buffer + len),weight[0]);
len = len + strlen(weight[0]);
sprintf(tmp_buffer," %d ",(temp1 / 100));
strcpy((buffer + len),tmp_buffer);
len = len + strlen(tmp_buffer);
strcpy((buffer + len),weight[1]);
len = len + strlen(weight[1]);
sprintf(tmp_buffer," %d",temp2);
strcpy((buffer + len),tmp_buffer);
len = len + strlen(tmp_buffer);
}
int main(int argc, char *argv[])
{
char str_amount[400];
written_amount(152621,str_amount);
printf("%s",str_amount);
system("PAUSE");
return EXIT_SUCCESS;
}
/*关
于上面的这个程序,我在进入这家公司的时候就被考过类似的问题,不过是带浮点数的,英文的要更复杂一些,因为英文的单词长度都不同,并且不同的如20和
30的英文单词也不能象中文一样可以拆分为“二十”,“三十”;要多定义一些常量数组,然后拷贝到buffer里面,我懒得去敲字了,如果要做成循环,还
要把这个常量字串做成二维数组,然后一层一层的剥离填充就可以了,程序写出来估计会比较乱了*/