还有一次做过的两正整数相加减的,可能不够全面,存在有我还没有找到的错误,如果有朋友能帮忙找出错误,我一定改正。
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
void plus(char *a,char *b,char *c);
void minus(char *a,char *c);
int main()
{
char a[1000];
char b[1000];
char c[1000];
char s[2];
while (cin>>a>>s>>b) {
if (s[0] == '+') {
plus(a,b,c);
} else if (s[0] == '-') {
minus(a,c);
}
printf("%s\n",c);
}
return 0;
}
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
void plus(char *a,char *c)
{
int al,bl,len;
int d[1000],e[1000],f[1000];//用了三个数组去做,觉得很麻烦,又没想到简便的方法。ps:现在已经想出来了简单的方法,可以将原有的数组补成相同的位数,不够的前面补上0,然后就可以直接加了。
memset(d,sizeof(d));
memset(e,sizeof(e));
memset(f,sizeof(f));
char tmpa[1000] = {NULL},tmpb[1000] = {NULL};
int tmp = 0;
int ff = 1;//前导零的消去
for (int i = 0; a[i]; i++)
{
if(ff && a[i] != '0')ff = 0;
if(!ff)tmpa[tmp++] = a[i];
}tmpa[tmp] = '\0';
ff = 1,tmp = 0;
for (int i = 0; b[i]; i++)
{
if(ff && b[i]!='0')ff = 0;
if(!ff)tmpb[tmp++] = b[i];
}tmpb[tmp] = '\0';
al = strlen(tmpa);
bl = strlen(tmpb);
len = al>bl?al:bl;
for (int i = 0; i < al; i++)
d[i] = tmpa[al-i-1]-'0';//反转储存,把字符型变成int型
for (int i = 0; i < bl; i++)
e[i] = tmpb[bl-i-1]-'0';
for (int i = 0; i < len; i++) {//模拟加法
f[i] += d[i] + e[i];
if(f[i]>=10) {//进位
f[i+1]++;
f[i] -= 10;
}
}
if(f[len])len++;//可能多一位
for (int i = 0; i < len; i++)
c[i] = f[len-i-1]+'0';
c[len] = '\0';
}
void minus(char *a,char *c)
{
int al = 0,bl = 0;
char d[1000]={NULL},e[1000] = {NULL};
int flag = 0;//判断正负,flag = 0表示结果为正,同理1。
char tmpa[1000] = {NULL},tmpb[1000] = {NULL};
int tmp = 0;
int f = 1;//前导零的消去
for (int i = 0; a[i]; i++) //*******************************************************
{ //*比较减法的结果是正还是负,不能直接用字符串的长度 *
if(f && a[i] != '0')f = 0; //*因为有前导零,可以使得数值较小的数 占用的 长度更长。 *
if(!f)tmpa[tmp++] = a[i]; //* *
}tmpa[tmp] = '\0'; //* *
f = 1,tmp = 0; //* *
for (int i = 0; b[i]; i++) //* *
{ //* *
if(f && b[i]!='0')f = 0; //* *
if(!f)tmpb[tmp++] = b[i]; //* *
}tmpb[tmp] = '\0'; //* *
al = strlen(tmpa); //* *
bl = strlen(tmpb); //*******************************************************/
if(strcmp(tmpa,tmpb) == 0){//首先判断相等的情况
c[0] = '0';
c[1] = '\0';
return ;
}
if(al<bl || (al == bl&&strcmp(tmpa,tmpb) < 0)) flag = 1;//为负
if(!flag){//为正 时。
for (int i = 0; i <= al; i++)//把位数少的补上前导零,如1010 - 98变成1010 - 0098.
{
if(i<al-bl) d[i] = '0';
else d[i] = tmpb[i-al+bl];
}
for (int i = al-1; i >= 0; i--)//模拟减法
{
if(tmpa[i] >= d[i])
tmpa[i] -= d[i]-'0';
else{
tmpa[i-1] -= 1;//退位
tmpa[i] += 10-d[i]+'0';
}
}
int tmp = 1,x = 0;//用于处理结果中的前导零
for (int i = 0; i < al; i++)
{
if(tmpa[i] == '0' && tmp)
continue;
else{
c[x++] = tmpa[i];
tmp = 0;
}
}c[x] = '\0';//结束
}else{//为负 时
//好像能直接调用一下minus(b,a,c),再加个减号就行了???
for (int i = 0; i <= bl; i++)//同上面一样~~
{
if(i<bl-al) e[i] = '0';
else e[i] = tmpa[i-bl+al];
}
for (int i = bl-1; i >= 0; i--)
{
if(tmpb[i] >= e[i])
tmpb[i] -= e[i]-'0';
else{
tmpb[i-1] -= 1;
tmpb[i] += 10-e[i]+'0';
}
}
int tmp = 1,x = 1;
c[0] = '-';//加个减号就行
for (int i = 0; i < bl; i++)
{
if(tmpb[i] == '0' && tmp)
continue;
else{
c[x++] = tmpb[i];
tmp = 0;
}
}c[x] = '\0';
}
}
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */ #include <stdio.h> #include <string.h> #include <iostream> using namespace std; void plus(char *a,char *b,char *c); void minus(char *a,char *c); int main() { char a[1000]; char b[1000]; char c[1000]; char s[2]; while (cin>>a>>s>>b) { if (s[0] == '+') { plus(a,b,c); } else if (s[0] == '-') { minus(a,c); } printf("%s\n",c); } return 0; } /* PRESET CODE END - NEVER TOUCH CODE ABOVE */ void plus(char *a,char *c) { int al,bl,len; int d[1000],e[1000],f[1000];//用了三个数组去做,觉得很麻烦,又没想到简便的方法。ps:现在已经想出来了简单的方法,可以将原有的数组补成相同的位数,不够的前面补上0,然后就可以直接加了。 memset(d,sizeof(d)); memset(e,sizeof(e)); memset(f,sizeof(f)); char tmpa[1000] = {NULL},tmpb[1000] = {NULL}; int tmp = 0; int ff = 1;//前导零的消去 for (int i = 0; a[i]; i++) { if(ff && a[i] != '0')ff = 0; if(!ff)tmpa[tmp++] = a[i]; }tmpa[tmp] = '\0'; ff = 1,tmp = 0; for (int i = 0; b[i]; i++) { if(ff && b[i]!='0')ff = 0; if(!ff)tmpb[tmp++] = b[i]; }tmpb[tmp] = '\0'; al = strlen(tmpa); bl = strlen(tmpb); len = al>bl?al:bl; for (int i = 0; i < al; i++) d[i] = tmpa[al-i-1]-'0';//反转储存,把字符型变成int型 for (int i = 0; i < bl; i++) e[i] = tmpb[bl-i-1]-'0'; for (int i = 0; i < len; i++) {//模拟加法 f[i] += d[i] + e[i]; if(f[i]>=10) {//进位 f[i+1]++; f[i] -= 10; } } if(f[len])len++;//可能多一位 for (int i = 0; i < len; i++) c[i] = f[len-i-1]+'0'; c[len] = '\0'; } void minus(char *a,char *c) { int al = 0,bl = 0; char d[1000]={NULL},e[1000] = {NULL}; int flag = 0;//判断正负,flag = 0表示结果为正,同理1。 char tmpa[1000] = {NULL},tmpb[1000] = {NULL}; int tmp = 0; int f = 1;//前导零的消去 for (int i = 0; a[i]; i++) //******************************************************* { //*比较减法的结果是正还是负,不能直接用字符串的长度 * if(f && a[i] != '0')f = 0; //*因为有前导零,可以使得数值较小的数 占用的 长度更长。 * if(!f)tmpa[tmp++] = a[i]; //* * }tmpa[tmp] = '\0'; //* * f = 1,tmp = 0; //* * for (int i = 0; b[i]; i++) //* * { //* * if(f && b[i]!='0')f = 0; //* * if(!f)tmpb[tmp++] = b[i]; //* * }tmpb[tmp] = '\0'; //* * al = strlen(tmpa); //* * bl = strlen(tmpb); //*******************************************************/ if(strcmp(tmpa,tmpb) == 0){//首先判断相等的情况 c[0] = '0'; c[1] = '\0'; return ; } if(al<bl || (al == bl&&strcmp(tmpa,tmpb) < 0)) flag = 1;//为负 if(!flag){//为正 时。 for (int i = 0; i <= al; i++)//把位数少的补上前导零,如1010 - 98变成1010 - 0098. { if(i<al-bl) d[i] = '0'; else d[i] = tmpb[i-al+bl]; } for (int i = al-1; i >= 0; i--)//模拟减法 { if(tmpa[i] >= d[i]) tmpa[i] -= d[i]-'0'; else{ tmpa[i-1] -= 1;//退位 tmpa[i] += 10-d[i]+'0'; } } int tmp = 1,x = 0;//用于处理结果中的前导零 for (int i = 0; i < al; i++) { if(tmpa[i] == '0' && tmp) continue; else{ c[x++] = tmpa[i]; tmp = 0; } }c[x] = '\0';//结束 }else{//为负 时 //好像能直接调用一下minus(b,a,c),再加个减号就行了??? for (int i = 0; i <= bl; i++)//同上面一样~~ { if(i<bl-al) e[i] = '0'; else e[i] = tmpa[i-bl+al]; } for (int i = bl-1; i >= 0; i--) { if(tmpb[i] >= e[i]) tmpb[i] -= e[i]-'0'; else{ tmpb[i-1] -= 1; tmpb[i] += 10-e[i]+'0'; } } int tmp = 1,x = 1; c[0] = '-';//加个减号就行 for (int i = 0; i < bl; i++) { if(tmpb[i] == '0' && tmp) continue; else{ c[x++] = tmpb[i]; tmp = 0; } }c[x] = '\0'; } }
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem Description
I have a very simple problem for you. Given two integers A and B,your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow,each line consists of two positive integers,A and B. Notice that the integers are very large,that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case,you should output two lines. The first line is "Case #:",# means the number of the test case. The second line is the an equation "A + B = Sum",Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2 1 2 112233445566778899 998877665544332211
Sample Output
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
Author
Ignatius.L
用数组来模拟算数的加法。
#include<stdio.h> #include<string.h> #include<stdlib.h> #define N 1010 int main() { int t,al,len; char a[N],b[N] ; int d[N],e[N],c[N]; while(scanf("%d",&t)!=EOF) { for(int i = 0;i<t;i++) { memset(c,sizeof(c)); memset(a,sizeof(a)); memset(b,sizeof(b)); memset(d,sizeof(d)); memset(e,sizeof(e)); scanf("%s%s",b); al = strlen(a); bl = strlen(b); len = (al > bl) ? al : bl; for(int j = 0;j<al;j++) { d[j] = a[al-j-1]-'0';//反转储存 } for(int m = 0;m<bl;m++) { e[m] = b[bl-m-1]-'0'; } for(int k = 0;k<len;k++) { c[k] += (d[k] + e[k]); if(c[k]>=10) { c[k+1]++; c[k] -=10; } } if(c[len])len++; printf("Case %d:\n",i+1); printf("%s + %s = ",b); for(int x=len-1;x>=0;x--)printf("%d",c[x]); printf("\n"); if(i<t-1)printf("\n"); } } return 0; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。