将五位整数(如 12345)分隔为 1 2 3 4 5,并用制表符分隔每个数字
Separate a five digit integer such as 12345 into 1 2 3 4 5, separating each digit with a tab
#include<iostream>
using namespace std;
int main()
{
int x;
int a;
int b;
int c;
int d;
int e;
cout << "Please enter a 5 digit integer.";
cin >> x;
a= x%10 ;
b= x%100 %10;
c= x%1000 %10;
d= x%10000 %10;
e= x%100000 %10;
cout << a
<< b
<< c
<< d
<< e;
return 0;
}
这就是我目前所拥有的,但我似乎无法<<每个数字都带有一个选项卡。我需要在每个数字之间添加一个制表符。
我没有看到问题。如果你想在每个数字之间有一个制表符,只需把它放在那里:
cout << a << 't'
<< b << 't'
<< c << 't'
<< d << 't'
<< e << 'n';
但是,这需要一个基于循环的解决方案,例如:
for (int div = 10000; div > 0; div /= 10)
cout << (x / div) % 10 << 't';
或者,如果您希望最后一个是换行符而不是制表符:
for (int div = 10000; div > 1; div /= 10)
cout << (x / div) % 10 << 't';
cout << x % 10 << 'n';
顺便说一下,你计算a/b/c/d/e
表达式是错误的,它们都会给你最后一个数字。如果您仍然不想使用 loop 方法,请至少修复它:
a= x / 10000;
b= x / 1000 % 10;
c= x / 100 % 10;
d= x / 10 % 10;
e= x % 10;
使用
cout<<a<<'t'<<b<<'t'<<c<<'t'<<d't'<<e;
编辑
对于您的问题,请将公式更改为:
a= x/10000 %10;
b= x/1000 %10;
c= x/100 %10;
d= x/10 %10;
e= x %10;
相关文章:
- 如何将 txt 文件中的行分隔为组件C++
- 无法通过空白将文本文件行分隔为矢量
- 如何在C++中读取空格分隔的输入 当我们不知道输入的数量时
- 读取制表符分隔的文件时出现问题 C++
- 读取以逗号分隔的数字,并将其写入以制表符分隔的文件
- 将制表符分隔的值从文本文件读取到数组中
- CComSafeArray 到制表符分隔值的建议,以提高速度
- 如何将由制表符分隔的文件读入结构
- 知道文本文件中的列数,用空格或制表符分隔
- 要存储到数据结构中的制表符分隔的文件数据
- 使用制表符分隔的文本填充字符串向量
- 当数据包含空格时,如何将制表符分隔文件的内容加载到C++字符串的 2D 向量中
- 将五位整数(如 12345)分隔为 1 2 3 4 5,并用制表符分隔每个数字
- 获取文件中由制表符分隔的所有字符串
- 从制表符分隔的文件中读取2D数据并存储在矢量C++中
- 正在从制表符分隔的文本文件中读取单列
- 快速解析c++中以制表符分隔的字符串和整型
- 处理空格(或制表符)分隔的文件"like"和数组 - C++
- 解析制表符分隔的数据
- 读取以制表符分隔的输入并跳过空白字段