Printf 和 Echo 提供不同的输出
Printf and Echo giving different outputs
我想深入了解为什么我的程序会这样运行。
这是"第2天"黑客排名挑战赛。
从本质上讲,它希望您输入膳食价格、小费百分比和税收百分比,以给出正确的价格作为输出。
这段代码最终通过了 Hacker Rank 测试的所有测试用例,但我遇到问题的地方是当我在本地机器上测试代码时。
顺便说一句,我使用Arch Linux。
黑客排名使用的输入为 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(总成本(。
在那之后,它停止了这样的功能,我不得不改为做......
$ printf "12n8n20n" | ./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 Costn");
double meal_cost;
cin >> meal_cost;
printf("Reprinted Meal Costn");
cout << meal_cost << endl;
cin.ignore(numeric_limits<streamsize>::max(), 'n');
printf("Enter Tip Percentn");
double tip_percent;
cin >> tip_percent;
printf("Reprinted Tip Percentn");
cout << tip_percent << endl;
cin.ignore(numeric_limits<streamsize>::max(), 'n');
printf("Enter Tax Percentn");
double tax_percent;
cin >> tax_percent;
printf("Reprinted Tax Percentn");
cout << tax_percent << endl;
cin.ignore(numeric_limits<streamsize>::max(), 'n');
solve(meal_cost, tip_percent, tax_percent);
printf("Total rounded to nearest intn");
cout << nearbyint(totalCost) << endl;
printf("Total not roundedn");
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 "12n8n20n" | ./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 正确运行,现在不是。
>Shawn是对的!
我拿出了...
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 "12n20n8n" | ./a.out
。两者的输出是...
15
15.36
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 将值从二维数组输出到文本文件
- 集合上的输出迭代器:assign和increment迭代器
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- Printf 和 Echo 提供不同的输出
- 使用Qt从.cmd文件输出读取Echo
- 操作符重载等同于c++中的PHP, echo/print类变量作为默认输出