I/O流操纵器-AndingField Internal -C vsC

I/O Stream manipulator - adjustfield internal - C++ vs C

本文关键字:-AndingField Internal vsC 操纵      更新时间:2023-10-16

程序员在这里。我在课堂作业的一部分方面遇到了麻烦。我们的任务是将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.4en", 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;
}

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

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