繁体   English   中英

Printf 和 Echo 给出不同的输出

[英]Printf and Echo giving different outputs

我想了解一下我的程序为什么会这样。

这是“第 2 天”黑客排名挑战赛。

本质上,它是在寻找您输入膳食价格、小费百分比和税收百分比的输入,以给出正确的价格作为输出。

这段代码最终通过了 Hacker Rank 在其上测试的所有测试用例,但我遇到问题的地方是当我在本地机器上测试代码时。

顺便说一句,我使用 Arch Linux。

Hacker Rank 使用的输入是 12.00、20 和 8。

更多下面的代码...

#include <bits/stdc++.h>

using namespace std;

double totalCost;


void solve(double meal_cost, double tip_percent, double tax_percent) {

      tip_percent = tip_percent / 100;
      tip_percent = meal_cost * tip_percent;

      tax_percent = tax_percent / 100;
      tax_percent = meal_cost * tax_percent;

      totalCost = meal_cost + tip_percent + tax_percent;

}


int main(){
      double meal_cost;
      cin >> meal_cost;
      cin.ignore(numeric_limits<streamsize>::max(), '\n');

      double tip_percent;
      cin >> tip_percent;
      cin.ignore(numeric_limits<streamsize>::max(), '\n');

      double tax_percent;
      cin >> tax_percent;
      cin.ignore(numeric_limits<streamsize>::max(), '\n');

      solve(meal_cost, tip_percent, tax_percent);

      cout << nearbyint(totalCost);
      cout << totalCost << endl;

      return 0;

}

起初,当我使用...

$ echo 12 20 8 | ./a.out

它会给我 15 或 15.36 的正确输出,而没有 nearint(totalCost)。

在那之后它停止了运作,我不得不改变做......

$ printf "12\n8\n20\n" | ./a.out

这给出了输出

15
15.36

为了探究为什么会发生这种情况,我开始使用 printf() 跟踪变量中存储的值。

#include <bits/stdc++.h>

using namespace std;

double totalCost;

void solve(double meal_cost, double tip_percent, double tax_percent){

      tip_percent = tip_percent / 100;
      tip_percent = meal_cost * tip_percent;

      tax_percent = tax_percent / 100;
      tax_percent = meal_cost * tax_percent;

      totalCost = meal_cost + tip_percent + tax_percent;

}

int main(){

    printf("Enter Meal Cost\n");
    double meal_cost;
    cin >> meal_cost;
    printf("Reprinted Meal Cost\n");
    cout << meal_cost << endl;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    printf("Enter Tip Percent\n");
    double tip_percent;
    cin >> tip_percent;
    printf("Reprinted Tip Percent\n");
    cout << tip_percent << endl;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    printf("Enter Tax Percent\n");
    double tax_percent;
    cin >> tax_percent;
    printf("Reprinted Tax Percent\n");
    cout << tax_percent << endl;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    solve(meal_cost, tip_percent, tax_percent);

    printf("Total rounded to nearest int\n");
    cout << nearbyint(totalCost) << endl;

    printf("Total not rounded\n"); 
    cout << totalCost << endl;

    return 0;
}

当我跑...

$ echo 12 20 8 | ./a.out

...在第二个程序中,我得到...的输出

Enter Meal Cost
Reprinted Meal Cost
12
Enter Tip Percent
Reprinted Tip Percent
4.63597e-310
Enter Tax Percent
Reprinted Tax Percent
6.95272e-310
Total rounded to nearest int
12
Total not rounded
12

而当我跑...

$ printf "12\n8\n20\n" | ./a.out

...在第二个程序中,我得到...

Enter Meal Cost
Reprinted Meal Cost
12
Enter Tip Percent
Reprinted Tip Percent
8
Enter Tax Percent
Reprinted Tax Percent
20
Total Rounded to nearest int
15
Total not rounded
15.36

从本质上讲,我希望了解为什么它最初可以通过 echo 正确运行,而现在却不是。

肖恩是对的!

我拿出了...

cin.ignore(numeric_limits<streamsize>::max(), '\n');

这是我对代码所做的更改...

#include <bits/stdc++.h>

using namespace std;

double totalCost;

void solve(double meal_cost, double tip_percent, double tax_percent) {

      tip_percent = tip_percent / 100;
      tip_percent = meal_cost * tip_percent;

      tax_percent = tax_percent / 100;
      tax_percent = meal_cost * tax_percent;

      totalCost = meal_cost + tip_percent + tax_percent;

}

int main(){
      double meal_cost;
      cin >> meal_cost;

      double tip_percent;
      cin >> tip_percent;

      double tax_percent;
      cin >> tax_percent;

      solve(meal_cost, tip_percent, tax_percent);

      cout << nearbyint(totalCost) << endl;
      cout << totalCost << endl;

      return 0;

}

现在无论我跑...

$ echo 12 20 8 | ./a.out

或者...

$ printf "12\n20\n8\n" | ./a.out

...两者的输出是...

15
15.36

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM