2014-09-01 121 views
0

我有一個程序來計算(致命百分比和罐的數量),但是我遇到的問題是輸出與測試文件不匹配。我得到 [30266666, 8647618]的輸出,而測試文件有[302667, 864762]。我是新來的C + +,我試圖看着intdouble但似乎沒有工作。C++不匹配的結果

以下是我有:

#include <iostream> 
#include <string> 
using namespace std; 

#include "cs150check.h" 

//// Include your own header files here 



//// Define any file-wide constants here 



/** 
* A government research lab has concluded that an artificial sweetener commonly used in diet soda will cause death in laboratory mice. 
* A friend of yours is desperate to lose weight but cannot give up soda. 
* Your friend wants to know how much diet soda it is possible to drink without dying as a result. Write a program to supply the answer. 
* 
* Input: The amount of artificial sweetener need to kill a mouse, the weight of the mouse, and the weight of the dieter. 
* Output: Amount of Lethal and Soda Cans 
* 
* @param cin the standard input stream. 
* @param cout the standard output stream. 
* @return 0 for success. 
*/ 
int run(istream& cin, ostream& cout) 
{ 
    // Weight of the mouse in grams: 15 
    //Lethal dose for the mouse(in grams) : 100 
    //Desired weight of the dieter(in pounds) : 100 
    //Lethal dose in grams, cans is[302667, 864762] 
    const double artificial_Sweetener = .01; 
    const int WEIGHT_OF_SODA = 350; 
    const int pound = 454; 

    int numLethalDose; 
    int numSodaCan; 
    int weightMouse; 
    int lethal; 
    int desiredWeight; 

    cout << "Weight of the mouse in grams: "; 
    cin >> weightMouse; 
    cout << "Lethal dose for the mouse (in grams): "; 
    cin >> lethal; 
    cout << "Desired weight of the dieter (in pounds): "; 
    cin >> desiredWeight; 
    //Math 
    numLethalDose = (lethal * (desiredWeight * pound))/(weightMouse * artificial_Sweetener); 
    numSodaCan = (1/((WEIGHT_OF_SODA * artificial_Sweetener)/(numLethalDose))); 

    cout << "Lethl dose in grams, cans is: " << "[" << numLethalDose << ", " << numSodaCan << "]" << endl; 
    return 0; 
} 
+2

這些數字是以「100」爲因子的,這與「1/artificial_Sweetener」相同。我懷疑這跟它有關係。 – 2014-09-01 04:26:18

+0

也許你應該使用那個「磅」的東西,或者修改那個「以磅爲單位」的要求。 – 2014-09-01 06:38:36

回答

1

使用一個師都行給我一個警告C4244:

警告C4244: '=':轉換從 '常量雙' 到' int',可能丟失數據

這給你一個提示,你在混合整數和浮點數據。像權重和轉換因子的數量一定是double而不是int。例如,在這種情況下,即使numLethalDosenumSodaCan應該是double,因爲它可能需要4.5個汽水罐來殺死你。

const double artificial_Sweetener = .01; 
const double WEIGHT_OF_SODA = 350.0; 
const double pound = 454.0; 

double numLethalDose; 
double numSodaCan; 
double weightMouse; 
double lethal; 
double desiredWeight; 

請注意使用小數零。從技術上講,它沒有做任何事情,但它使代碼更容易理解。

儘管如此,結果仍然是[30266666.667 8647619.048]與給定的輸入。結論是你的方程本身是錯誤的,或者你的測試文件是錯誤的。我有點太累,無法找出給定問題的正確數學。

編輯:問題確實在你的數學中的某個地方。你應該找出紙上的數學,然後用評論來表達你在做什麼。另外,使用更多的顯式變量名稱。它使得它更容易遵循,而且它變得非常簡單。

// Weight of the mouse in grams: 15 
//Lethal dose for the mouse(in grams) : 100 
//Desired weight of the dieter(in pounds) : 100 
//Lethal dose in grams, cans is[302667, 864762] 
// 

const double artificial_Sweetener = .01; 
const double WEIGHT_OF_SODA = 350.0; 
const double POUND_PER_GRAM = 454.0; 

double mouseWeight; 
double humanWeight; 

double mouseLethalDose; 
double humanLethalDose; 

double numSodaCan; 

cout << "Weight of the mouse in grams: "; 
cin >> mouseWeight; 
cout << "Lethal dose for the mouse (in grams): "; 
cin >> mouseLethalDose; 
cout << "Desired weight of the dieter (in pounds): "; 
cin >> humanWeight; 

// Part 1: Figure out the lethal dose for our human 
// 
// mouseLethalDose/mouseWeight = humanLethalDose/humanWeight 
// 
// humanLethalDose = humanWeight * (mouseLethalDose/mouseWeight) 

humanLethalDose = humanWeight*POUND_PER_GRAM * (mouseLethalDose/mouseWeight); 

// Part 2: Figure out how much cans we need to have "humanLethalDose" grams of sweetener 

// sweetenerPerCan = artificial_Sweetener * WEIGHT_OF_SODA 
// totalSweetener = numSodaCans * sweetenerPerCan 
// numSodaCans  = sweetenerPerCan/totalSweetener 

numSodaCan = humanLethalDose/(artificial_Sweetener * WEIGHT_OF_SODA); 

printf("[%.3f %.3f]\n", humanLethalDose, numSodaCan); 
+0

謝謝你的幫助。我已經看過我的數學,並以另一種方式表達了自己的想法。我還研究了'#include '還有'cout << fixed << setprecision(0);',它幫助浮點數。 – YoYo 2014-09-01 07:03:51

+0

我認爲問題的根源在於你在第一行中用'artificialSweetener'劃分,它不屬於那裏。甜味劑的比例對致死劑量沒有影響。所以除以0.01有效地乘以100你的輸出。 – 2014-09-01 17:20:02