zoukankan      html  css  js  c++  java
  • 6.15奇特大数加法

    代码如下:

    #include <stdio.h>
    #include <string.h>
    
    int buf[6]={0,2,3,5,7,11};
    
    void Swap(char &a,char &b)
    {
    	char c=a;
    	a=b;
    	b=c;
    }
    void ReverseString(char *s)
    {
    	int i=0;
    	int j=strlen(s)-1;
    	while (i<j)
    	{
    		Swap(s[i],s[j]);
    		++i;--j;
    	}
    }
    void Add(char *A,char *B,char *C)
    {
    	char AA[100];
    	char BB[100];
    	char CC[100];
    	strcpy(AA,A);
    	strcpy(BB,B);
    	ReverseString(AA);
    	ReverseString(BB);
    	int carry=0;
    	int i;
    	int step = 1;
    	int lenCC=0;
    	for (i=0;i<strlen(AA) && i<strlen(BB);++i)
    	{
    		if (AA[i]==',') continue;
    		int tmp=carry;
    		if(AA[i]=='A') tmp+=10;
    		else tmp+=(AA[i]-'0');
    		if(BB[i]=='A') tmp+=10;
    		else tmp+=(BB[i]-'0');
    		carry = tmp/buf[step];
    		int tmp2 = tmp%buf[step];
    		if(tmp2>=10) CC[lenCC++]='A';
    		else CC[lenCC++]=tmp2+'0';
    
    		step++;
    		
    	}
    	while (i<strlen(AA))
    	{
    		if (AA[i]==',') 
    		{
    			++i;
    			continue;
    		}
    		int tmp=carry;
    		if(AA[i]=='A') tmp+=10;
    		else tmp+=(AA[i]-'0');
    		carry = tmp/buf[step];
    		int tmp2 = tmp%buf[step];
    		if(tmp2>=10) CC[lenCC++]='A';
    		else CC[lenCC++]=tmp2+'0';
    
    		step++;
    		++i;
    
    	}
    	while (i<strlen(BB))
    	{
    		if (BB[i]==',') 
    		{
    			++i;
    			continue;
    		}
    		int tmp=carry;
    		if(BB[i]=='A') tmp+=10;
    		else tmp+=(BB[i]-'0');
    
    		carry = tmp/buf[step];
    
    		int tmp2 = tmp%buf[step];
    		if(tmp2>=10) CC[lenCC++]='A';
    		else CC[lenCC++]=tmp2+'0';
    
    		step++;
    		++i;
    	}
    	if (carry)
    	{
    		if (carry>=10)
    		{
    			CC[lenCC++]='A';
    		} 
    		else
    		{
    			CC[lenCC++]=carry+'0';
    		}
    	}
    	CC[lenCC]=0;
    	ReverseString(CC);
    	//printf("%s
    ",CC);
    
    	int lenC=0;
    	bool first=true;
    	for (int i =0;i<strlen(CC);++i)
    	{
    		if(first) 
    		{
    			C[lenC++] = CC[i];
    			first =false;
    		}
    		else
    		{
    			C[lenC++] = ',';
    			C[lenC++] = CC[i];
    		}
    	}
    	C[lenC]=0;
    
    }
    
    int main()
    {
    	freopen("in.txt","r",stdin);
    	char s[20];
    	char s1[20];
    	char s2[20];
    	char s3[20];
    	//scanf("%s+%s",s1,s2);
    	scanf("%s",s);
    	int pos=0;
    	for (int i=0;i<strlen(s);++i)
    	{
    		if(s[i]=='+') 
    		{
    			pos = i;
    			break;
    		}
    	}
    	for (int i=0;i<pos;++i)
    	{
    		s1[i] = s[i];
    	}
    	s1[pos] = 0;
    	for (int i=pos+1;i<strlen(s);++i)
    	{
    		s2[i-pos-1] = s[i];
    	}
    	s2[strlen(s)-pos-1] = 0;
    
    	Add(s1,s2,s3);
    
    	printf("%s
    ",s3);
    
    
    	return 0;
    
    }


  • 相关阅读:
    简单播放器(增加sdl事件控制)
    注册表读写
    vb6 的关机代码
    设置系统时间
    获取屏幕工作区、定位任务栏、窗口置顶
    用WINSOCK API实现同步非阻塞方式的网络通讯
    使用API调用外部程序并监控程序状态
    vba截屏保存
    StrConv 内码转换
    阻止文本框获取键盘输入
  • 原文地址:https://www.cnblogs.com/wuhayaoshenmeai/p/3361872.html
Copyright © 2011-2022 走看看