将 stdio.h 程序转换为 iostream

Converting stdio.h program to iostream

这是我的主要模块。我有一些外部模块。

int main(void)
{
    char ln[15+1];
    char fn[10+1];
    float fed,state,ssi;
    float g,h,p,d,n;

    InputEmployeeData(&ln[0],&fn[0],&h,&p,&d); // call 3.3
    CalculateGross(h,p, &g); // call 3.4
    computeTaxes(g,d,ADDR(fed),ADDR(state),ADDR(ssi)); // call 3.5
    n = g-fed-state-ssi-d;
    printf("  Fed   =   %8.2f\n",fed);
    printf("  State =   %8.2f\n",state);
    printf("  SSI   =   %8.2f\n",ssi);
    printf("  Net   =   %8.2f\n",n);
    while(getchar() != '\n'); // flush(stdin)
    return 0;

main.cpp 使用 iostream

cout << "  Fed   =   %8.2f\n" << fed;
cout << "  State =   %8.2f\n" << state;
cout << "  SSI   =   %8.2f\n" << ssi;
cout << "  Net   =   %8.2f\n" << n;
 cin.sync();
//while(getchar() != '\n'); 
// flush(stdin)
return 0;

inputemployeedata.cpp 使用 stdio.h

//3.3
#include <stdio.h>
#define ADDR(var) &var

void InputEmployeeData(char *lastname,char *firstname, // 3.3
                       float *hours,float *payrate, float *defr);

void InputEmployeeData(char *lastname,char *firstname, // 3.3
                       float *hours,float *payrate, float *defr)
{
    printf(" Enter the name ==> ");
    scanf("%s%s",firstname,lastname);
    printf(" Enter the hours and payrate ==> ");
    scanf("%f%f",hours,payrate);
    printf("  Enter the deferred earning amount ==> ");
    scanf("%f",defr);
}

inputemployeedata.cpp 使用 iostream

{
    cout << " Enter the name ==> ";
    cin >> *firstname >> *lastname;
    cout <<" Enter the hours and payrate ==> ";
    cin >> *hours >> *payrate;
    cout << "  Enter the deferred earning amount ==> ";
    cin >> *defr;

我不知道 cout 对应的 %8.2f。我只是卡住了

C: printf a float value

TL;DR : %f 表示正在打印一个浮点数。 %8.2f 表示总字符数为 8(或“%8.2f”中点之前的任何其他数字),点之后为 2(或 %f 命令中点之后的任何其他数字)。

在这种情况下,可能会打印出诸如“12345.78”之类的数字。

呃?但是8个数字!不,人物。该点包含在总数中。

希望对您有所帮助!