cpp-printf语句与判断结构

21 年 5 月 23 日 星期日
812 字
5 分钟

一、printf输出格式

注意:使用printf时最好添加头文件 #include <cstdio>

cpp
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    printf("Hello World!");

    return 0;
}
  1. Int、float、double、char等类型的输出格式: (1) int:%d (2) float: %f, 默认保留6位小数 (3) double: %lf, 默认保留6位小数 (4) char: %c, 回车也是一个字符,用'\n'表示

1. Int、float、double、char等类型的输出格式:

(1) int:%d (2) float: %f, 默认保留6位小数 (3) double: %lf, 默认保留6位小数 (4) char: %c, 回车也是一个字符,用'\n'表示

cpp
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a = 3;
    float b = 3.12345678;
    double c = 3.12345678;
    char d = 'y';

    printf("%d\n", a);
    printf("%f\n", b);
    printf("%lf\n", c);
    printf("%c\n", d);

    return 0;
}

2. 所有输出的变量均可包含在一个字符串中:

cpp
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a = 3;
    float b = 3.12345678;
    double c = 3.12345678;
    char d = 'y';

    printf("int a = %d, float b = %f\ndouble c = %lf, char d = %c\n", a, b, c, d);

    return 0;
}

练习:输入一个字符,用这个字符输出一个菱形:

cpp
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    char c;
    cin >> c;

    printf("  %c\n", c);
    printf(" %c%c%c\n", c, c, c);
    printf("%c%c%c%c%c\n", c, c, c, c, c);
    printf(" %c%c%c\n", c, c, c);
    printf("  %c\n", c);

    return 0;
}

3. 扩展功能

(1) float, double等输出保留若干位小数时用:%.4f, %.3lf

cpp
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    float b = 3.12345678;
    double c = 3.12345678;

    printf("%.4f\n", b);
    printf("%.3lf\n", c);

    return 0;
}

(2) 最小数字宽度

  1. %8.3f, 表示这个浮点数的最小宽度为8,保留3位小数,当宽度不足时在前面补空格。
cpp
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a = 3;
    float b = 3.12345678;
    double c = 3.12345678;

    printf("%5d\n", a);
    printf("%8.4f\n", b);
    printf("%7.3lf\n", c);

    return 0;
}
  1. %-8.3f,表示最小宽度为8,保留3位小数,当宽度不足时在后面补上空格
cpp
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a = 3;
    float b = 3.12345678;
    double c = 3.12345678;

    printf("%-5d!\n", a);
    printf("%-8.4f!\n", b);
    printf("%-7.3lf!\n", c);

    return 0;
}
  1. %08.3f, 表示最小宽度为8,保留3位小数,当宽度不足时在前面补上0
cpp
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a = 3;
    float b = 3.12345678;
    double c = 3.12345678;

    printf("%05d\n", a);
    printf("%08.4f\n", b);
    printf("%07.3lf\n", c);

    return 0;
}

二、if 语句

1. 基本if-else语句

当条件成立时,执行某些语句;否则执行另一些语句。

cpp
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a;
    cin >> a;

    if (a > 5)
    {
        printf("%d is big!\n", a);
        printf("%d + 1 = %d\n", a, a + 1);
    }
    else
    {
        printf("%d is small!\n", a);
        printf("%d - 1 = %d\n", a, a - 1);
    }

    return 0;
}

else 语句可以省略:

cpp
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a;
    cin >> a;

    if (a > 5)
    {
        printf("%d is big!\n", a);
        printf("%d + 1 = %d\n", a, a + 1);
    }

    return 0;
}

当只有一条语句时,大括号可以省略:

cpp
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a;
    cin >> a;

    if (a > 5)
        printf("%d is big!\n", a);
    else
        printf("%d is small!\n", a);

    return 0;
}

2. 常用比较运算符

(1) 大于 > (2) 小于 < (3) 大于等于 >= (4) 小于等于 <= (5) 等于 == (6) 不等于 !=

cpp
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a, b;
    cin >> a >> b;

    if (a > b) printf("%d > %d\n", a, b);
    if (a >= b) printf("%d >= %d\n", a, b);
    if (a < b) printf("%d < %d\n", a, b);
    if (a <= b) printf("%d <= %d\n", a, b);
    if (a == b) printf("%d == %d\n", a, b);
    if (a != b) printf("%d != %d\n", a, b);

    return 0;
}

三、条件表达式

(1) 与 && (2) 或 || (3) 非 !

文章标题:cpp-printf语句与判断结构

文章作者:梦幻の风

文章链接:https://www.hstudent.xyz/posts/cpp/cpp-printf%E8%AF%AD%E5%8F%A5%E4%B8%8E%E5%88%A4%E6%96%AD%E7%BB%93%E6%9E%84[复制]

最后修改时间:


商业转载请联系站长获得授权,非商业转载请注明本文出处及文章链接,您可以自由地在任何媒体以任何形式复制和分发作品,也可以修改和创作,但是分发衍生作品时必须采用相同的许可协议。
本文采用CC BY-NC-SA 4.0进行许可。