我将一个整数作为字符串 s 的输入。我想以“-”和“|”的形式在 s 中写入整数。我确信我的逻辑是正确的。问题是字符串 s 在代码中自动被修改。当我最初打印字符串 s 时,它返回完整的 12345(我的输入是“2 12345”)但是当我之后尝试打印它时,它会被截断或其他东西。我该如何解决这个问题?
#include <iostream>
#include <string>
using namespace std;
int main(){
int n;
std::string s;
cin >> n;
cin >> s;
cout << s.at(3) <<endl;
while(n!=0){
for (int l=0;l<3+2*n;l++){
// for (int i=0;i<s.length();i++){
if (l==0){
for (int j=0;j<s.length();j++){
if (s.at(j)=='1'||s.at(j)=='4'){
cout << " ";
for (int k=0;k<n;k++){
cout << " ";
}
cout << " ";
}
else if (s.at(j)=='0'||s.at(j)=='2'||s.at(j)=='3'||s.at(j)=='5'||s.at(j)=='6'||s.at(j)=='7'||s.at(j)=='8'||s.at(j)=='9'){
cout << " ";
for (int k=0;k<n;k++){
cout << "-";
}
cout << " ";
}
cout << " ";
}
}
else if (l==n+1){
for (int j=0;j<s.length();j++){
if (s.at(j)=='1'||s.at(j)=='7'||s.at(j)=='0'){
cout << " ";
for (int k=0;k<n;k++){
cout << " ";
}
cout << " ";
}
else if (s.at(j)=='2'||s.at(j)=='3'||s.at(j)=='4'||s.at(j)=='5'||s.at(j)=='6'||s.at(j)=='8'||s.at(j)=='9'){
cout << " ";
for (int k=0;k<n;k++){
cout << "-";
}
cout << " ";
}
cout << " ";
}
}
else if (l==2*n+2){
for (int j=0;j<s.length();j++){
if (s.at(j)=='1'||s.at(j)=='4'||s.at(j)=='7'){
cout << " ";
for (int k=0;k<n;k++){
cout << " ";
}
cout << " ";
}
else if (s.at(j)=='0'||s.at(j)=='2'||s.at(j)=='3'||s.at(j)=='6'||s.at(j)=='8'||s.at(j)=='9'||s.at(j)=='5'){
cout << " ";
for (int k=0;k<n;k++){
cout << "-";
}
cout << " ";
}
cout << " ";
}
}
else if ((l>0) && (l<n+1)){
for (int j=0;j<s.length();j++){
if (s.at(j)=='1'||s.at(j)=='2'||s.at(j)=='3'||s.at(j)=='7'){
cout << " ";
for (int k=0;k<n;k++){
cout << " ";
}
cout << "|";
}
else if(s.at(j)='4'||s.at(j)=='8'||s.at(j)=='9'||s.at(j)=='0') {
cout << "|";
cout << "s "<< s<< endl;
cout << "check 2";
for (int k=0;k<n;k++){
cout << " ";
}
cout << "|";
}
else if(s.at(j)=='5'||s.at(j)=='6'){
cout << "|";
cout << "check";
for (int k=0;k<n;k++){
cout <<" ";
}
cout << " ";
}
cout << " ";
}
}
else if ((l>n+1) && (l<2*n+2)){
for (int j=0;j<s.length();j++){
if (s.at(j)=='1'||s.at(j)=='3'||s.at(j)=='5'||s.at(j)=='7'||s.at(j)=='9'||s.at(j)=='4'){
cout << " ";
for (int k=0;k<n;k++){
cout << " ";
}
cout << "|";
}
else if(s.at(j)='6'||s.at(j)=='8'||s.at(j)=='0') {
cout << "|";
for (int k=0;k<n;k++){
cout << " ";
}
cout << "|";
}
else if(s.at(j)=='2'){
cout << "|";
for (int k=0;k<n;k++){
cout <<" ";
}
cout << " ";
}
cout << " ";
}
}
cout << s << endl;
cout << endl;
}
cin >> n;
cin >> s;
}
}