// w1.cpp : 定义控制台应用程序的入口点。
//
#include <stdio.h>
#include <string.h>
//补前导0
void strpre(char* dest,int sum)
{
char buffer[256]={'\0'};
strcpy(buffer,dest);
while(sum>0)
{
*dest++='0';
sum--;
}
int i=0;
while(buffer[i]!='\0') *dest++=buffer[i++];
}
//对齐字符串 用sfill来填充
void stralign(char* dest1,char* dest2,char sfill)
{
char buffer[256]={'\0'};
int len1=strlen(dest1);
int len2=strlen(dest2);
if (len1==len2) return;
int bc=0;
int i=0;
if(len1>len2)
{
i=0;
strcpy(buffer,dest2);
bc=len1-len2;
while(bc>0)
{
*dest2++=sfill;
bc--;
}
while(buffer[i]!='\0') *dest2++=buffer[i++];
}
else
{
i=0;
strcpy(buffer,dest1);
bc=len2-len1;
while(bc>0)
{
*dest1++=sfill;
bc--;
}
while(buffer[i]!='\0') *dest1++=buffer[i++];
}
}
//字符串累加器
/*
用法示例:此示例输出1-101的所有奇数
char t1[256]={'\0'};
char* p=&t1[0];
strcpy(t1,"1");
strpre(t1,5);
while(1)
{
stracc(t1,'2');
while(*p=='0') { p++;}
printf("%s\n",p);
p=&t1[0];
if(t1[3]>'0') break;
}
*/
void stracc(char* dest,char ad)
{
int len=strlen(dest);
int flag=0;
int tmp;
len--;
flag=(dest[len]-0x30+ad-0x30) /10;
dest[len]=(dest[len]-0x30+ad-0x30) %10+0x30;
while(len>0)
{
len--;
tmp=dest[len]-0x30+flag;
dest[len]=((dest[len]-0x30)+flag)%10+0x30;
flag=tmp/10;
}
}
//字符串乘法 乘数是1位数
/*
用法示例;
char t1[256]={'\0'};
char* p=&t1[0];
strcpy(t1,"987589");
strpre(t1,8);
strmul(t1,'9');
p=&t1[0];
while(*p=='0') { p++;}
printf("%s\n",p);
*/
void strmul(char* dest,char ad)
{
int len=strlen(dest);
int flag=0;
int tmp;
len--;
flag=((dest[len]-0x30)*(ad-0x30))/10;
dest[len]=((dest[len]-0x30)*(ad-0x30)) %10+0x30;
while(len>0)
{
len--;
tmp=(dest[len]-0x30)*(ad-0x30)+flag;
dest[len]=((dest[len]-0x30)*(ad-0x30)+flag)%10+0x30;
flag=tmp/10;
}
}
//字符串左移位右补0
void strmovl(char* dest,int bit)
{
int len=strlen(dest);
for(;bit>0;bit--) dest[len++]='0';
dest[len]='\0';
}
void strmuls(char* dest1,char* dest2)
{
char buffer[100][256]={'\0'};
int len=strlen(dest2);
int i=0,j=0;
for(;len>0;len--)
{
strmul(dest1,dest2[len]);
strcpy(buffer[i],dest1);
}
}
int main()
{
char t1[256]={'\0'};
char t2[256]={'\0'};
char* p=&t1[0];
strcpy(t1,"1");
//strcpy(t2,"123289324125");
//stralign(t1,t2,'0');
//printf("$1-:%s\n$2-:%s\n",t1,t2);
strpre(t1,5);
while(1)
{
stracc(t1,'2');
while(*p=='0') { p++;}
printf("%s\n",p);
p=&t1[0];
if(t1[3]>'0') break;
}
strcpy(t1,"987589");
strpre(t1,8);
strmul(t1,'9');
strmovl(t1,8);
p=&t1[0];
while(*p=='0') { p++;}
printf("%s\n",p);
//stracc(t1);
//for(int i=0;i<256;i++) printf("i=%3d",i,t1[i]);
//printf("%s\n",t1);
printf("\n");
return 0;
}