军妻txt下载:c++的输出格式问题

来源:百度文库 编辑:高校问答 时间:2024/05/07 06:18:16
如何将1输出成为001, 将23输出成为023....等等?
以上。

不用那么麻烦~用C++自带的库文件
#include <iostream.h>
#include <iomanip.h>
void main()
{
cout<<setfill('0')<<setw(3)<<1<<23;
//setfill设置用什么字符来填充(默认为空格)
//setw设置填充到多少位
}
如果要用C的输出入可以这样:
#include<stdio.h>
void main()
{
printf("%03d",1);
printf("%03d",23);
/*%03d中的0代表用0来填充;3代表填充到3位;d代表要输出的是int类型*/
}

利用字符的ASCII码 程序经调试有效,输入6输入006,输入45输出045。
用TC 2.0调试的
#include<stdio.h>
void main()
{
char str[4]; //先申明一个4字符的字符串
int n;
scanf("%d",&n); //输入数字
str[0]=0x30; //第一个字符为0(0的ascii码是30十六进制)
str[1]=(char)((n/10)+0x30);
str[2]=(char)((n%10)+0x30); //依次把十位和个位转换为字符输入
str[3]=0; //字符串以0结尾
printf("%s",str); //打印字符串
getch();
}