如何用C++模拟竖式

本文中所有头文件由#include<bits/stdc++.h>
要模拟竖式,首先得解决对齐的问题
iostream中有一个函数是setw()
实例:

#include<bits/stdc++.h>
using namespace std;
int main(){
    cout<<setw(10)<<"dui\n"<<setw(10)<<"qi";
}

运行结果如下↓

      dui
       qi
--------------------------------
Process exited after 0.1581 seconds with return value 0

setw(n)原理大概就是在字符串的前面加上一堆空格使其长度等于n

例:setw(10)<<"hello";---->"_____hello"(下划线为空格)
                           |---------|
                             10字符

先看样例:

99
1
输出:
  9 9
 +  1 
-1-1-
1 0 0

接下来正片开始
首先初始化变量:

#include<bits/stdc++.h>
using namespace std;
string str;
int l;
int a[101];
int b[101];
int c[101];
int d[101];
int main(){
	cin>>str;//输入a
	l=str.length();
	for(int i=1;i<=l;i++){
		a[i]=str[l-i]-'0';
	}
	cin>>str;//输入b
	l=str.length();
	for(int i=1;i<=l;i++){
		b[i]=str[l-i]-'0';
	}
}

接着是计算部分,c表示结果,d表示进位。

for(int i=1;i<=99;i++){
	c[i]=a[i]+b[i]+d[i-1];
	d[i]=c[i]/10;
	c[i]%=10;
}

输出部分:

string stra="";
bool has=false;
for(int i=99;i>=1;i--){
	if(a[i]>0)has=true;
	if(has){
		stra=stra+char(a[i]+'0');
		if(i!=1)stra=stra+" ";
	}
}
if(!has)stra="0";
string strb="";
has=false;
for(int i=99;i>=1;i--){
	if(b[i]>0)has=true;
	if(has){
		strb=strb+char(b[i]+'0');
		if(i!=1)strb=strb+" ";
	}
}
if(!has)strb="0";
int maxl=max(stra.length(),strb.length());
cout<<"  "<<setw(maxl)<<stra<<endl;
cout<<" +"<<setw(maxl)<<strb<<endl;
cout<<"-";
for(int i=maxl/2+1;i>=1;i--){
	if(d[i]==0)cout<<"--";
	else cout<<d[i]<<"-";
}
string strc="";
has=false;
for(int i=99;i>=1;i--){
	if(c[i]>0)has=true;
	if(has){
		strc=strc+char(c[i]+'0');
		if(i!=1)strc=strc+" ";
	}
}
if(!has)strc="0";
if(d[maxl/2+1]==1)cout<<"\n"<<setw(maxl)<<strc<<endl;
else cout<<"\n  "<<setw(maxl)<<strc<<endl;

完整代码:

#include<bits/stdc++.h>
using namespace std;
string str;
int l;
int a[101];
int b[101];
int c[101];
int d[101];
int main(){
	cin>>str;
	l=str.length();
	for(int i=1;i<=l;i++){
		a[i]=str[l-i]-'0';
	}
	cin>>str;
	l=str.length();
	for(int i=1;i<=l;i++){
		b[i]=str[l-i]-'0';
	}
	for(int i=1;i<=99;i++){
		c[i]=a[i]+b[i]+d[i-1];
		d[i]=c[i]/10;
		c[i]%=10;
	}
	string stra="";
	bool has=false;
	for(int i=99;i>=1;i--){
		if(a[i]>0)has=true;
		if(has){
			stra=stra+char(a[i]+'0');
			if(i!=1)stra=stra+" ";
		}
	}
	if(!has)stra="0";
	string strb="";
	has=false;
	for(int i=99;i>=1;i--){
		if(b[i]>0)has=true;
		if(has){
			strb=strb+char(b[i]+'0');
			if(i!=1)strb=strb+" ";
		}
	}
	if(!has)strb="0";
	int maxl=max(stra.length(),strb.length());
	cout<<"  "<<setw(maxl)<<stra<<endl;
	cout<<" +"<<setw(maxl)<<strb<<endl;
	cout<<"-";
	for(int i=maxl/2+1;i>=1;i--){
		if(d[i]==0)cout<<"--";
		else cout<<d[i]<<"-";
	}
	string strc="";
	has=false;
	for(int i=99;i>=1;i--){
		if(c[i]>0)has=true;
		if(has){
			strc=strc+char(c[i]+'0');
			if(i!=1)strc=strc+" ";
		}
	}
	if(!has)strc="0";
	if(d[maxl/2+1]==1)cout<<"\n"<<setw(maxl)<<strc<<endl;
	else cout<<"\n  "<<setw(maxl)<<strc<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值