簡體   English   中英

在C ++中顯示大小未知的'雙'數字數組

[英]Display an array of 'double' numbers with unknown size in c++

更新1:在第二部分中,我需要幫助。 我要用戶輸入數字並使用零停止。 但是,然后我想顯示該代碼。 這就是我能走多遠,因為當我想要顯示它時,它會給我提供不同的數字。 我忽略了用戶錯誤,假設他們在第一次輸入時是正確的。 但是我確實希望他們輸入負數。

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

int main ()
{
    float data;
    int count=0;
    int*arr=new int[count];

    //get amount of numbers inputted 
    cout<<"Please enter floating point data.\n";
    cout<<"After the last number has been entered press 0 (zero) \n";
    cin>>data;
    for (; data != 0 ; ++count)
    {
        cin>>data;
    }
    cout<<count<<endl;
    cout<<endl;
    //display back data
    cout<<"The numbers enterd are: "<<endl; 
    for(int i=0; i<count; i++)
    {
        cout<<arr[i]<<endl;
    }

    cout<<endl;

system ("pause");
return 0;
}

**更新2:**此代碼是我正在從事的一個較大項目的一部分。 我的教授永遠也不會看到他只是在乎它在起作用的代碼。 在這種情況下,我的代碼設計不是優先事項。 而且我之前從未使用過std::vector ,所以我需要另一種方式來做。 但這就是我所做的,並且效果很好。 我還注釋了delete []arr因為如果不這樣做,我的代碼將無法正常運行。

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

int main ()
{ 

    double data; 
    int count=0; 
    double *arr =new double[count]; 

    //get data 
    cout<<"Please enter floating point data."<<endl; 
    cout<<"After the last number has been entered press 0 (zero)"<<endl; 
    do
    {
        cin>>arr[count]; 
        data = arr[count]; 

        count++; 

    }while(data != 0); 

    //display back data 
    cout<<"The numbers entered are: "<<endl; 
    for(int i=0; i<(count-1); i++) 
    { 
        cout<<arr[i]<<endl;
    } 


    //delete []arr;  

 system ("pause");
 return 0;
 }

給定的示例代碼,

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

int main ()
{
    float data;
    int count=0;
    int*arr=new int[count];

    //get amount of numbers inputted 
    cout<<"Please enter floating point data.\n";
    cout<<"After the last number has been entered press 0 (zero) \n";
    cin>>data;
    for (; data != 0 ; ++count)
    {
        cin>>data;
    }
    cout<<count<<endl;
    cout<<endl;
    //display back data
    cout<<"The numbers enterd are: "<<endl; 
    for(int i=0; i<count; i++)
    {
        cout<<arr[i]<<endl;
    }

    cout<<endl;

system ("pause");
return 0;
}

……有很多問題:

  • 編碼:無緣無故地使用float
    C和C ++中的默認浮點類型為double 例如,文字3.14的類型為double ,當傳遞給可變參數C函數時,任何float值都會提升為double 僅在上下文提出要求的情況下使用float ,例如C API,或在有限的內存中存儲不計其數的值。

  • 編碼:使用原始數組和new的-expression。
    使用std::vector時,這項任務微不足道。 一次又一次地重新發明輪子不是一個好主意。 要求您重新發明std::vector或其最基本功能的練習應該在這方面非常清楚:這不是必需的,因此大概不需要重新發明或其含義。

  • 設計:通過特殊值來表示輸入結束。
    該值無法輸入。

  • 編碼:不存儲數字的輸入循環。
    每個數字都存儲在與前一個數字相同的變量中,從而擦除前一個數字。 將數字存儲在std::vector 例如,您可以使用push_back方法。

  • 編碼:輸出換行符約定不一致。
    默認使用endl"\\n" 不要隨意混合。 一致性非常重要,因為讀代碼的人(假設有能力的程序員)必須將由於某種原因而偏離已建立的默認值的所有情況視為錯誤。 如果沒有理由,這會浪費時間和精力。

  • 工具用法: system( "pause" )愚蠢,適得其反且不可移植。
    在您正在使用的Visual Studio中,只需通過Ctrl + F5運行該程序。 或在main的最后一個右括號上放置一個斷點,然后通過F5在調試器中運行程序。

  • 編碼: return 0; 是多余的。
    main是唯一具有默認返回值的函數。 它在C和C ++中都有。 將使用默認值0(表示成功)。


具有以上幾點的示例代碼,再加上一些固定的內容。

此代碼適用於C ++ 11或更高版本,因此不會直接與Visual C ++ 2010一起編譯

我將其保留為練習,以將其轉換為C ++ 03,這是您當前的編譯器可以處理的。 請考慮升級。 這是一個免費工具。

#include <iostream>
#include <iomanip>              // std::setw
#include <string>               // std::(string, stod)
#include <vector>               // std::vector
using namespace std;

using Half_float = float;                       // Most often a silly choice.
using Float = double;                           // Default in C++.
using Extended_float = long double;             // Same as double in Visual C++.

auto read_line()
    -> string
{ string line; getline( cin, line ); return line; }

auto main() -> int
{
    cout << "Please enter floating point numbers like " << 3.15 << ", one per line.\n";
    cout << "After the last number just press return again (i.e. a blank line).\n";

    vector<Float> numbers;
    for( ;; )
    {
        cout << "#" << numbers.size() + 1 << "? ";
        string const line = read_line();
        if( line.empty() )
        {
            break;
        }
        numbers.push_back( stod( line ) );      // Here you CAN do input validation.
    }

    cout << numbers.size() << " numbers entered.\n";
    cout << "\n";

    cout << "The numbers entered were: \n";
    for( int i = 0; i < int( numbers.size() ); ++i )
    {
        cout << setw( 3 ) << i << ": " << numbers[i] << "\n";
    }
}
  • 該代碼將訪問arr的邊界,該區域包含零個元素,除非您在第一個提示中輸入0
  • 你沒有給您讀“元素或什么arr ”(實際上會出現在任何元素arr )在所有。
  • 沒有理由應該使用int數組存儲float數據。

您應該使用std::vector ,它用作可變大小的數組。

#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

int main ()
{
    float data;
    int count=0;
    vector<float> arr;

    //get amount of numbers inputted 
    cout<<"Please enter floating point data.\n";
    cout<<"After the last number has been entered press 0 (zero) \n";
    while(cin >> data && data != 0)
    {
        arr.push_back(data);
    }
    count = arr.size();
    cout<<count<<endl;
    cout<<endl;
    //display back data
    cout<<"The numbers enterd are: "<<endl; 
    for(int i=0; i<count; i++)
    {
        cout<<arr[i]<<endl;
    }

    cout<<endl;

    return 0;
}

如果您具有C ++ 11或更高版本,請使用auto

更換

for(int i=0; i<count; i++)
{
    cout<<arr[i]<<endl;
}

for(auto v: arr){
    cout << v << endl;
}

請記住,您需要支持C ++ 11的編譯器才能使用auto。

如果C ++ <11,請使用std :: for_each

#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

void print(float v){
    cout << v << endl;
}


int main ()
{
    float data;
    int count=0;
    std::vector<float> arr;

    //get amount of numbers inputted
    cout<<"Please enter floating point data.\n";
    cout<<"After the last number has been entered press 0 (zero) \n";

    while(cin>> data && data != 0)
    {
        arr.push_back(data);
        ++count;
    }
    cout<<count<<endl;
    cout<<endl;

    //display back data
    cout<<"The numbers enterd are: "<<endl;
    std:for_each(arr.begin(), arr.end(), print);
    cout<<endl;

    system ("pause");
    return 0;
}

暫無
暫無

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM