#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); }