【问题标题】:I/O Stream manipulator - adjustfield internal - C++ vs CI/O 流操纵器 - 内部调整域 - C++ 与 C
【发布时间】:2015-05-09 20:08:59
【问题描述】:

这里是程序员学生。我的课堂作业的一部分有问题。我们的任务是将 C++ 程序转换为 C 程序。
由于使用“内部”流操纵器,我无法转换一行代码。它需要在输出中看起来像这样:

+ 6443.- 6443.

但我似乎无法使用 C 标准而不是 C++ 来实现这一点

这是我应该转换的 C++ 代码:

cout << setprecision(4) << setw(13) << internal << showpoint << showpos << fourth << endl;

我试过了,但 +/- 仍然在数字旁边。

printf("%+13.0f. \n", fourth);   

如果整个程序更容易理解,这里是整个程序...

#include <stdio.h>
#include <stdlib.h>

main()
{
    //bool first;           //Changing data type for standard C Input
    int first;              //This is in place of the bool
    int second;
    //long third;           //Changing data type for standard C input
    int third;              //This is in place of long
    float fourth;
    float fifth;
    //double sixth;         //Changing data type for standard C input
    float sixth;            //This is place of double

    //cout << "Enter bool, int, long, float, float, and double values: ";
    printf("Enter bool, int, long, float, float, and double values: ");
    //cin >> first >> second >> third >> fourth >> fifth >> sixth;
    scanf("%d %d %d %f %f %f", &first, &second, &third, &fourth, &fifth, &sixth);
    //cout << endl;
    printf("\n");

    //1 - 3
    printf("%d", first);

    if(first > 0)
        printf(" true \n");
    else
        printf(" false \n");

    printf("%d %#x %#o \n", second, second, second);
    printf("%16d \n", third);

    //4
    //cout << setprecision(4) << setw(13) << internal << showpoint << showpos << fourth << endl;
    printf("%+13.0f. \n", fourth); //Issues

    //5
    printf("%15.4e\n", fourth);

    //6
    //cout << left << setprecision(7) << fifth << endl;
    printf("%-.7e \n", fifth); //Issues

    //7 - 12
    printf("%17.3f \n", fifth);
    printf("%-d \n", third);
    printf("%16.2f \n", fourth);
    printf("%13.0f \n", sixth);
    printf("%14.8f \n", fourth);
    printf("%16.6g \n", sixth);

    return 0;
}

【问题讨论】:

  • 感谢您为我指明正确的方向!这就是我最终使用的东西,它每次都与我的教授测试司机一起工作耶! printf ("%c %10.0f.\n", (fourth

标签: c++ c io printf cout


【解决方案1】:

将 '+' 或 '-' 作为单独的 %c 参数输出,

printf( "%c%13d\n",( (forth >=0)? '+' : '-' ), abs(forth) );

【讨论】:

    猜你喜欢
    • 2015-05-24
    • 1970-01-01
    • 2023-03-08
    • 2012-01-12
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    相关资源
    最近更新 更多