杭电1002

Evan_Z 2009-08-11 04:24:06
#include <iostream>
#include <cstring>
using namespace std;
class INT
{ char *p_buf; //从低位到高位存储整型数,
int buf_len; //p_buf所占空间大小(为10 的倍数)
public :
INT(char *num);
void Add(const INT &i); //把i加到*this中
INT& operator =(const INT &i);
~INT();
ostream &operator<<(ostream &out);
};
INT::INT(char *num)
{
buf_len = (strlen(num)/10+1)*10;
p_buf = new char[buf_len+1];
for (int i=0,j=strlen(num)-1; j>=0; i++,j--)
p_buf[i] = num[j];
p_buf[i] = '\0';
}
INT::~INT()
{
delete []p_buf;
p_buf = NULL;
buf_len = 0;
}
INT& INT::operator =(const INT &i)
{
if (buf_len < i.buf_len)
{
delete []p_buf;
buf_len = i.buf_len;
p_buf = new char[buf_len+1];
}
strcpy(p_buf, i.p_buf);
return *this;
}
void INT::Add(const INT &i) //绝对值加
{
int len1=strlen(p_buf);
int len2=strlen(i.p_buf);
int buf_len1=(((len1>len2?len1:len2)+1)/10+1)*10;
if (buf_len1 > buf_len)
{
char *p_buf1=new char[buf_len1+1];
strcpy(p_buf1,p_buf);
delete []p_buf;
p_buf = p_buf1;
buf_len = buf_len1;
}
char *p1,*p2; //p1 指向长的数,p2 指向短的数
if (len1 >= len2)
{
p1 = p_buf;
p2 = i.p_buf;
}
else
{
p1 = i.p_buf;
p2 = p_buf;
}
int carry=0, sum;
char *p=p_buf;
//处理公共长度部分
while (*p2 != '\0')
{
sum = (*p1-'0')+(*p2-'0')+carry;
if (sum >= 10)
{
carry = 1;
*p = (sum-10)+'0';
}
else
{
carry = 0;
*p = sum+'0';
}
p1++; p2++; p++;
}
//处理较大整数的剩余部分
while (*p1 != '\0')
{
if (carry == 0)
*p = *p1;
else
{
sum = (*p1-'0')+carry;
if (sum >= 10)
{ carry = 1;
*p = (sum-10)+'0';
}
else
{
carry = 0;
*p = sum+'0';
}
}
p1++; p++;
}
if (carry != 0) //最后检查是否还有进位,若有就放入和的最高位
{
*p = '1';
p++;
}
*p = '\0';
}
ostream& INT::operator<<(ostream &out)
{
for (int j=strlen(this->p_buf)-1; j>=0; j--)
out<<this->p_buf[j];
return out;
}
int main()
{
int n;
cin>>n;
static int count=1;
char* num1=new char[1000];
char* num2=new char[1000];
for (int i=0;i<n;i++)
{
cin>>num1>>num2;
INT n1(num1),n2(num2);
n1.Add(n2);
cout<<"Case "<<count<<":\n";
cout<<num1<<" + "<<num2<<" = ";
n1.operator <<(cout);
cout<<endl<<endl;
count++;
}
return 0;
}
Presentation Error,格式哪儿有误吗?
...全文
198 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hsdyl 2010-05-08
  • 打赏
  • 举报
回复
我也有这个问题 写了个一起输入输出的 但是 Presentation Error
http://topic.csdn.net/u/20100508/10/887d5dab-acd7-4910-8185-fa9ed0123142.html?seed=1202038373&r=65267848
Evan_Z 2009-08-15
  • 打赏
  • 举报
回复
但是不知道为什么通不过
rendao0563 2009-08-11
  • 打赏
  • 举报
回复
无误

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧