数据结构简单问题

来源:百度知道 编辑:UC知道 时间:2024/06/28 14:36:19
我自学数据结构有问题..两个大数相加的问题......我的程序,逻辑有错误,,但不知道哪错了,,首先是存数时字符型还是整形
#include<iostream>
#include<stack>
#include<string>
using namespace std;
void main()
{

int y=0;
stack<int>s1,s2,s3;
char ch;
while(ch=cin.get()!='\n')
{
s1.push(ch);
}
while(ch=cin.get()!='\n')
{
s2.push(ch);
}
while(!s1.empty()&&!s2.empty())
{
ch=s1.top()+s2.top();
if(ch>9){ch-=10;y=1;}
s1.pop();s2.pop();
s3.push(ch);
}
while(!s3.empty())
{

cout<<s3.top();
s3.pop();
}
}

while(ch=cin.get()!='\n')
这个CH有问题。
这个是把cin.get()!='\n'给了CH
所以CH只有1....
造成你最后的结果就是错的了。

int y=0;
stack<int>s1,s2,s3;
char ch;
while((ch=cin.get())!='\n')
{
s1.push(ch);
}
while((ch=cin.get())!='\n')
{
s2.push(ch);
}
while(!s1.empty()&&!s2.empty())
{
ch=s1.top()+s2.top() - '0' - '0' + y;
if(ch>9){ch-=10;y=1;}
s1.pop();s2.pop();
s3.push(ch);
}
while(!s3.empty())
{

cout<<s3.top();
s3.pop();
}