• 6.15大数加法(十进制和十六进制)


    #include <stdio.h>
    #include <string.h>
    
    
    void Swap(char &a,char &b)
    {
    	char t = a;
    	a =b;
    	b = t;
    }
    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[200]; char BB[200];
    	strcpy(AA,A);
    	ReverseString(AA); 
    	strcpy(BB,B);
    	ReverseString(BB);
    	int carry =0 ;
    	int lenA = strlen(AA);
    	int lenB = strlen(BB);
    	int lenZ=0;
    	int i=0;
    	for (i=0; i<lenA && i<lenB ; ++i)
    	{
    		int tmp = carry+AA[i]-'0'+BB[i]-'0';
    		carry = tmp/10;
    		C[lenZ++] = tmp%10+'0';
    		
    	}
    	while (i<lenA)
    	{
    		int tmp = carry+AA[i]-'0';
    		carry = tmp/10;
    		C[lenZ++] = tmp%10 +'0';
    		++i;
    	}
    	while (i<lenB)
    	{
    		int tmp = carry+BB[i]-'0';
    		carry = tmp/10;
    		C[lenZ++] = tmp%10 +'0';
    		++i;
    	}
    	if(carry)
    	{
    		C[lenZ++] = carry+'0';
    	}
    	while (C[lenZ-1]=='0')
    	{
    		lenZ--;
    	}
    	C[lenZ]=0;
    	ReverseString(C);
    
    }
    
    int main()
    {
    	char A[200];
    	char B[200];
    	char C[201];
    
    	char S[400];
    	scanf("%s",S);
    
    	int pos ;
    	int lenS =strlen(S);
    	for (int i=0;i<lenS;++i)    //查找加号
    	{
    		if(S[i]=='+') 
    		{
    			pos=i;
    			break;
    		}
    	}
    
    
    	int i;
    	for (i=0;i<pos;++i)            //前面复制给A
    	{
    		A[i]=S[i];
    	}
    	A[i]=0;
    	
    	for (i=pos+1;i<lenS;++i)          //后面复制给B
    	{
    		B[i-pos-1]=S[i];
    	}
    	B[lenS-pos-1]=0;
    
    
    
    	//strcpy(A,"1234");
    	//strcpy(B,"23456");
    	Add(A,B,C);
    	printf("%s
    ",C);
    
    	return 0;
    
    }



    #include <stdio.h>
    #include <string.h>
    
    
    void Swap(char &a,char &b)
    {
    	char t = a;
    	a =b;
    	b = t;
    }
    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[200]; char BB[200];
    	strcpy(AA,A);
    	ReverseString(AA); 
    	strcpy(BB,B);
    	ReverseString(BB);
    	int carry =0 ;
    	int lenA = strlen(AA);
    	int lenB = strlen(BB);
    	int lenZ=0;
    	int i=0;
    	for (i=0; i<lenA && i<lenB ; ++i)
    	{
    		int tmp = carry;
    		if (AA[i]>='A' && AA[i]<='F')
    		{
    			tmp+= (AA[i]-'A'+10);
    		} 
    		else
    		{
    			tmp+= (AA[i]-'0');
    		}
    		if (BB[i]>='A' && BB[i]<='F')
    		{
    			tmp+= (BB[i]-'A'+10);
    		} 
    		else
    		{
    			tmp+= (BB[i]-'0');
    		}
    			
    		carry = tmp/16;
    		tmp = tmp%16;
    		if (tmp<10)
    		{
    			C[lenZ++] = tmp+'0';
    		} 
    		else
    		{
    			C[lenZ++] = tmp-10+'A';
    		}
    		
    
    	}
    	while (i<lenA)
    	{
    		int tmp = carry;
    		if (AA[i]>='A' && AA[i]<='F')
    		{
    			tmp+= (AA[i]-'A'+10);
    		} 
    		else
    		{
    			tmp+= (AA[i]-'0');
    		}
    		carry = tmp/16;
    		
    		tmp = tmp%16;
    		if (tmp<10)
    		{
    			C[lenZ++] = tmp+'0';
    		} 
    		else
    		{
    			C[lenZ++] = tmp-10+'A';
    		}
    
    		++i;
    	}
    	while (i<lenB)
    	{
    		int tmp = carry;
    		if (BB[i]>='A' && BB[i]<='F')
    		{
    			tmp+= (BB[i]-'A'+10);
    		} 
    		else
    		{
    			tmp+= (BB[i]-'0');
    		}
    		carry = tmp/16;
    		tmp = tmp%16;
    		if (tmp<10)
    		{
    			C[lenZ++] = tmp+'0';
    		} 
    		else
    		{
    			C[lenZ++] = tmp-10+'A';
    		}
    		++i;
    	}
    	if(carry)
    	{
    		if (carry<10)
    		{
    			C[lenZ++] = carry+'0';
    		} 
    		else
    		{
    			C[lenZ++] = carry-10+'A';
    		}
    	}
    	while (C[lenZ-1]=='0')
    	{
    		lenZ--;
    	}
    	C[lenZ]=0;
    	ReverseString(C);
    
    }
    
    int main()
    {
    	char A[200];
    	char B[200];
    	char C[201];
    
    	strcpy(A,"0000000001");
    	strcpy(B,"FFFFFFFFFFFF");
    	Add(A,B,C);
    	printf("%s
    ",C);
    
    }


  • 相关阅读:
    bash帮助文档简单学习;bash手册翻译
    jmeter的master、slave模型启动方法
    如何查看pip安装包的所有版本;以及ipython的安装
    简单过下bash/sh脚本基本知识吧
    Celery 启动报错 can_read() got an unexpected keyword argument timeout
    paramiko执行命令超时的问题
    远程的jmeter自动执行完,如何回调通知被调用者“结束”状态
    记录一下:关于mysql数据误删除恢复的问题
    数据库Sharding的基本思想和切分策略
    数据库分库分表(sharding)系列(一) 拆分实施策略和示例演示
  • 原文地址:https://www.cnblogs.com/wuhayaoshenmeai/p/3361873.html
Copyright © 2020-2023  润新知