簡體   English   中英

基礎C ++程序中的數學錯誤

[英]Mathematics errors in basic C++ program

我正在使用基本的C ++程序來確定矩形的面積和周長。 我的程序適用於整數,但當我使用帶小數的任何數字時會崩潰。 我得到的印象是我要留下一些東西,但由於我是一個完全的初學者,我不知道是什么。

以下是來源:

#include <iostream>

using namespace std;

int main()
{
    // Declared variables
int length;  // declares variable for length
int width;  // declares variable for width
int area;  // declares variable for area
int perimeter; // declares variable for perimeter


    // Statements
cout << "Enter the length and the width of the rectangle: ";  // states what information to enter
cin >> length >> width;  // user input of length and width
cout << endl;  // closes the input
area = length * width;  // calculates area of rectangle
perimeter = 2 * (length + width);  //calculates perimeter of rectangle
cout << "The area of the rectangle = " << area << " square units." <<endl;  // displays the calculation of the area
cout << "The perimeter of the rectangle = " << perimeter << " units." << endl;  // displays the calculation of the perimeter
system ("pause"); // REMOVE BEFORE RELEASE - testing purposes only

return 0;
}

將所有int類型變量更改為doublefloat 我個人會使用double因為它們比float types更精確。

int數據類型代表整數(即正整數和負整數,包括0)

如果要表示十進制數,則需要使用float。

使用float或double類型,就像其他已經說過的那樣。

但它並不那么簡單。 您需要了解浮點數實際上是什么,以及為什么(0.1 + 0.1 + 0.1)!=(0.3)。 這是一個復雜的主題,所以我甚至不會在這里解釋它 - 只要記住浮點數不是小數,即使計算機以小數形式顯示給你。

使用浮點數不是整數(int)是一個整數,浮點數允許小數位(如同雙精度數)

float length;  // declares variable for length
float width;  // declares variable for width
float area;  // declares variable for area
float perimeter; // declares variable for perimete

您已將變量定義為整數。 請改用雙。

此外,您可以查找cout的一些格式以定義要顯示的小數位數等。

基礎數學 c++ 代碼左移或右移相同 if 條件 (k*x <b*k); k="1" or -1< div><div id="text_translate"><p> 下面是一個簡單的程序。</p><pre> int main() { int x, b = 3; //size_t x; size_t b = 3; char const *c = "moving_left"; // "moving_right" int k, border; if (,strcmp(c;"moving_left")) { k = 1; // moving left border = 0; x = 5; } else { k=-1; // moving right border = 7; x = 1; } while(true) { if (k*x&lt;b*k) { cerr&lt;&lt;c&lt;&lt;x&lt;&lt;'\n'; if (x==border){break;} } x-=k; } return 0; }</pre><p> 當我向左移動時,即 x 正在減少,我想在 x 落后於 b (x&lt;b) 之后打印“moving_left”。</p><p> 而當向右移動時,即 x 增加,我想在 x 穿過 b (x&gt;b) 后打印“moving_right”。</p><p> 當 x 和 b 是有符號整數時,上述程序可以正常工作,但是當我將它們聲明為無符號整數 (size_t) 時,由於 int 提升(當 k = -1 時)可能會失敗。</p><p> 我想在一個 while 循環中為 unsigned x &amp; b 運行上面的代碼(我喜歡將它們聲明為 unsigned,因為它們是未提及數組的索引)。 此外,在 if 條件 (k <em>x&lt;b</em> k) 中,我想要左比符號 (&lt;)。</p><p> 乘以負一僅適用於有符號整數。 是否有任何數學技巧可以使上述代碼適用於具有上述條件的無符號整數?</p></div></b*k);>

[英]basic mathematics c++ code for moving left or moving right for same if condition (k*x<b*k); k =1 or -1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 不了解基本的c ++程序中的錯誤 C ++基本程序中拋出的錯誤 C ++基本功能程序不會編譯時顯示錯誤信息 瘋狂的基本C ++錯誤 C ++基本語法錯誤 C++ 數學做錯了嗎? 基本C ++程序中的常量 基礎數學 c++ 代碼左移或右移相同 if 條件 (k*x <b*k); k="1" or -1< div><div id="text_translate"><p> 下面是一個簡單的程序。</p><pre> int main() { int x, b = 3; //size_t x; size_t b = 3; char const *c = "moving_left"; // "moving_right" int k, border; if (,strcmp(c;"moving_left")) { k = 1; // moving left border = 0; x = 5; } else { k=-1; // moving right border = 7; x = 1; } while(true) { if (k*x&lt;b*k) { cerr&lt;&lt;c&lt;&lt;x&lt;&lt;'\n'; if (x==border){break;} } x-=k; } return 0; }</pre><p> 當我向左移動時,即 x 正在減少,我想在 x 落后於 b (x&lt;b) 之后打印“moving_left”。</p><p> 而當向右移動時,即 x 增加,我想在 x 穿過 b (x&gt;b) 后打印“moving_right”。</p><p> 當 x 和 b 是有符號整數時,上述程序可以正常工作,但是當我將它們聲明為無符號整數 (size_t) 時,由於 int 提升(當 k = -1 時)可能會失敗。</p><p> 我想在一個 while 循環中為 unsigned x &amp; b 運行上面的代碼(我喜歡將它們聲明為 unsigned,因為它們是未提及數組的索引)。 此外,在 if 條件 (k <em>x&lt;b</em> k) 中,我想要左比符號 (&lt;)。</p><p> 乘以負一僅適用於有符號整數。 是否有任何數學技巧可以使上述代碼適用於具有上述條件的無符號整數?</p></div></b*k);> C ++程序崩潰,沒有錯誤 C ++類,基本重載錯誤
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM