如何在不使用库的情况下切断数字的前导数字以仅显示最后两位数字.例如:
1923年至23日
2001年至01年
1234至34
123至23
只有
#include <iomanip>
#include <iostream>
Run Code Online (Sandbox Code Playgroud)
谢谢!
在C++中是否有一种方法可以使编译器获取一定数量的数字,即使它们的第一个数字是0.例如:
我有一个00001的项目编号,当我从文件中导入数字时,它显示1.我希望它导入所有五位数字并显示为00001.
我真的没有要显示的代码,因为我甚至不知道使用什么函数,而且我所拥有的代码正如宣传的那样工作,这不是我想要的.我可以将数字设为字符串,但我宁愿保持整数.
这可能是这个问题的重复,但我觉得它实际上没有得到正确回答.注意:
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
float p = 1.00;
cout << showpoint << setprecision(3) << p << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出: 1.00
现在,如果我们将该行更改为:
cout << fixed << showpoint << setprecision(3) << p << endl;
Run Code Online (Sandbox Code Playgroud)
我们得到: 1.000
如果我们使用固定的"相反",我们会得到完全不同的东西:
cout << scientific << showpoint << setprecision(3) << p << endl;
Run Code Online (Sandbox Code Playgroud)
输出: 1.000e+00
如何fixed在设置之后回到第一个版本的行为?
拿这个最小的工作示例
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setw(10) << "aaaaaaa"
<< setw(10) << "bbbb"
<< setw(10) << "ccc"
<< setw(10) << "ddd"
<< setw(10) << endl;
for(int i(0); i < 5; ++i){
char ch = ' ';
if ( i == 0 )
ch = '%';
cout << setw(10) << i
<< setw(10) << i << ch
<< setw(10) << i
<< setw(10) << i
<< setw(10) << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是 …
如果我有两个表存储在std::string变量中,我怎么能并排显示它们?特别是...
我有std::string table1它包含以下内容:
X | Y
-------
2 | 3
1 | 3
5 | 2
Run Code Online (Sandbox Code Playgroud)
我有std::string table2它包含以下内容:
X | Y
-------
1 | 6
1 | 1
2 | 1
3 | 5
2 | 3
Run Code Online (Sandbox Code Playgroud)
我需要修改它们(或者只是将它们打印到标准输出),以便出现以下内容:
X | Y X | Y
------- -------
2 | 3 1 | 6
1 | 3 1 | 1
5 | 2 2 | 1
3 | 5
2 | 3
Run Code Online (Sandbox Code Playgroud)
换句话说,我有两个表存储在std::string变量中,换行符分隔行.
我想将它们打印到屏幕(使用 …
我已经查看过该线程,但如果我使用 setfill( '0' ),它不会考虑使用负数。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//Code
int num1;
cin >> num1;
cout << setw(5) << setfill( '0' ) << num1 << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用此方法,如果我输入“5”,它将显示为“00005”。但如果我输入 -5 它将显示为“000-5”
我该如何修复它以使输出为“-0005”?
当使用浮点数,即31.14159时,如何设置cout以在浮点上使用setprecision(4):
cout <<setprecision(4)<< 31.14159<<endl; // returns 31.14
Run Code Online (Sandbox Code Playgroud)
按原样,它用整数表示十进制数字,输出:31.14.但是,我想得到:31.1416.
我正在尝试调整我的输出,但由于某种原因我无法达到我想要的效果,这真的很令人沮丧。标题不会正确对齐。我不知道我是否正确使用 setw() 。
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <assert.h>
#include <iomanip>
using std::setw;
using std::right;
using std::left;
//Movie Struct to hold movie data
struct MOVIES
{
string Title; //Name of movie
int CriticRating; //Critic rating 1-100
int AudRating; //Audiences' rating 1-100
};
MOVIES Movies[10] = { { "The Wizard of Oz", 99 , 70 },
{ "The Third Man" , 78, 45 },
{ "Citizen Kane" , 86, 85 },
{ "Charlie Chaplin in …Run Code Online (Sandbox Code Playgroud) 我目前是计算机科学专业的学生,今天我收到了一份非常普通的作业,应该用C++编写.直到今天我才学习C语言.这更像是盲目的任务.
在C中,我通常使用这个:
printf("\n\n\t%-30s %-7d liters\n\t%-30s %-7d liters\n\t%-30s %-7d km",
"Current gasoline in reserve:",
db.currentGas,
"Total gasoline used:",
db.usedGas,
"Total travel distance:",
db.usedGas);
Run Code Online (Sandbox Code Playgroud)
由于赋值的条件是它应该用C++编写,这就是我尝试过的:
cout << setw(30) << "\n\n\tCurrent gasoline in reserve: "
<< setw(7) << db.currentGas << "litres"
<< setw(30) << "\n\tTotal gasoline used: "
<< setw(7) << db.usedGas << "litres"
<< setw(30) << "\n\tTotal travel distance: "
<< setw(7) << db.travelDistance << "km";
Run Code Online (Sandbox Code Playgroud)
但看起来C %-30s和C++ 之间存在差异setw(30)?
我在 C++ 中输入了一个基本示例,其中我尝试将一个数字以十六进制形式打印到屏幕上,如下所示:
#include <iostream>
#include <iomanip>
int main()
{
unsigned number {314};
auto flags {std::ios::showbase | std::ios::hex};
std::cout.setf(flags);
// expected output: 0x13A
std::cout << number << std::endl;
std::cout.unsetf(flags);
// expected output: 314
std::cout << number << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,该数字永远不会以十六进制格式显示。我是否正确设置了标志?