// w1.cpp : 定义控制台应用程序的入口点。
//

#include <stdio.h>
#include <string.h>
#include <malloc.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++];
	}
}
//字符串右对齐 前导0补齐
void stralignr(char* dest,int slen)
{
	char buffer[256]={'\0'};
	int len1=strlen(dest);
	if(len1>=slen) return;
	strcpy(buffer,dest);
	int bc=slen-len1; 
	while(bc>0)
	{
		*dest++='0';
		bc--;
	}
	int i=0;
	while(buffer[i]!='\0') *dest++=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;
	}
	char buffer[256]={'\0'};
	strcpy(buffer,dest);
	*dest=flag+0x30;
	int i=0;
	dest++;
	while(buffer[i]!='\0') *dest++=buffer[i++];
}
//字符串左移位右补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* dest3)
{
	char* buffer[100];
	int k;
	for(k=0;k<100;k++) buffer[k]=(char*)malloc(sizeof(char)*100);
	for(k=0;k<100;k++) memset(buffer[k],'\0',100);
	char buf[100]={'\0'};
	char tmp[100]={'\0'};
	char* sp=tmp;
	char tmp2[100]={'\0'};
	char* sp2=tmp2;
	strcpy(buf,dest1);
	int len=strlen(dest2);
	int i=0,j=0;
	len--;
	int m=15,n=15;
	for(;len>=0;len--)
	{
		printf("%s\n",dest1);
		strmul(dest1,dest2[len]);
		printf("%s\n",dest1);
		strmovl(dest1,i);
		printf("%s\n",dest1);
		stralignr(dest1,40);
		printf("%s\n",dest1);
		strcpy(buffer[i],dest1);
		/*
		strcpy(tmp,dest1);
		while(*sp=='0') sp++;
		strncpy(tmp2,sp,9);
		if(len==0) 
		{
			//printf("+");
			strncpy(tmp2,sp,10);
			for(m=n;m>3;m--) printf(" ");
		}
		else for(m=n;m>1;m--) printf(" ");
		//printf("%s\n",sp2);
		printf("%s\n",dest1);
		sp=tmp;
		m=--n;
		*/
    	i++;
		strcpy(dest1,buf);
	}
	int flag=0;
	for(k=39;k>=0;k--)
	{
		int tmp=0;
		for(j=0;j<i;j++)
		{
			tmp+=buffer[j][k]-0x30;
		}
		tmp+=flag;
		flag=tmp/10;
		dest3[k]=tmp%10+0x30;
	}
	//while(*dest3=='0') dest3++;
}
int main()
{
	char t1[256]={'\0'};
	char t2[256]={'\0'};
	char t3[256]={'0'};
	char* p=t3;
	strcpy(t1,"123456789");
	strcpy(t2,"987654321");
	printf("\n\n\n\t      %s\n",t1);
	printf("*\t      %s\n",t2);
	printf("-----------------------\n");
	strmuls(t1,t2,t3);
	while(*p=='0') p++;
	printf("-----------------------\n");
	printf("=     %s\n",p);
	//strcpy(t3,'9');
	//strmul(t1,'9');
    //printf("=     %s\n",t1);
	return 0;
}