优秀的编程知识分享平台

网站首页 > 技术文章 正文

C++代码解析3(c++代码大全及其含义)

nanyue 2024-10-11 13:42:38 技术文章 9 ℃

25.

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    float a;
    cin >> a;
    cout << fixed << setprecision(3) << a;  //保留小数点3位
    return 0;
}

结果:

26.

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    double a;
    cin >> a;
    cout << fixed << setprecision(12) << a; //保留小数点12位
    return 0;
}

结果:


27.

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    char a;   //声明字符变量a
    int b;    //声明整型变量b
    float c;  //声明单精度浮点型变量c
    double d;  //声明双精度浮点型变量d
    cin >> a >> b >> c >> d;  //输入a,b,c,d的值
    cout << a << ' ' << b << ' ';
    cout << fixed << setprecision(6);  //保留小数点6位
    cout << c << ' ' << d;
    return 0;
}

结果:

28.

#include <cstdio>   //scanf,printf所需的库
int main()
{
    double f;
    scanf("%lf", &f);  //scanf 输入f的值
    printf("%f\n%.5f\n%e\n%g", f, f, f, f);  //printf 输出   \n换行符
    return 0;
}

第一行是按“%f”输出的双精度浮点数;

第二行是按“%.5f”保留5位小数输出的双精度浮点数;

第三行是按“%e”输出的双精度浮点数;

第四行是按“%g”输出的双精度浮点数。

结果:

29.

#include <iostream>
using namespace std;
int main()
{
    char c;  //声明字符变量c
    cin >> c;  //输入c的值
    cout << "  " << c << endl;
    cout << " " << c << c << c << endl;
    cout << c << c << c << c << c << endl;
    cout << " " << c << c << c << endl;
    cout << "  " << c << endl;
    return 0;
}

结果:

Tags:

最近发表
标签列表