簡體   English   中英

使用計數器數組從文件中提取整數並計算響應數?

[英]Using an array of counters extract the integers from a file and count the number of responses?

#include <iostream>
#include <fstream>
#include <windows.h>

using namespace std;

void addRecordstoFile();
void readRecordsFromFile();

int main(){


    addRecordstoFile();
    readRecordsFromFile();
}

void addRecordstoFile(){

這會將整數添加到文件中,但我希望它退出時(用戶輸入-1時)不顯示在.txt文件中

    int num=1;
    char response;

    ofstream outFile("numbers.txt",ios::out);

    if (!outFile){
        cerr<<"File was not opened correctly"<<endl;
        exit(1);
    }

    cout<<"\t\t\tSurvey Response Analyzer"<<endl;


    while(num!=-1){

            cout<<"\n Enter a survey response from 1 to 10 (-1 to end): ";
            cin>>num;


            if(num<1 || num>10){
                cout<<"\n Please enter a valid response from (1-10): ";
                cin >>num;

            }

            outFile << num <<" ";

    }

            outFile.close();
            cout<<"Saved to file..."<<endl;
            system("pause");
}

這是主要問題所在,我應該從文本文件中提取整數並將其放入計數器數組中,以計算用戶輸入1到10之間的數字的次數。我完全迷失了,因為我需要數組的大小確定我的循環。

    void readRecordsFromFile(){

    ifstream inFile("numbers.txt",ios::in);

    if(!inFile){
        cerr<<"File was not found";
        exit (1);
    }

在不知道文本文件中數組大小的情況下,如何在此處創建計數器數組?

1.第一個查詢:

這會將整數添加到文件中,但我希望它退出時(用戶輸入-1時)不顯示在.txt文件中

更換

if (num < 1 || num > 10) {
    cout<<"\n Please enter a valid response from (1-10): ";
    cin >>num;
}
outFile << num <<" ";

if (num < 1 || num > 10) {
    cout<<"\n Please enter a valid response from (1-10): ";
    cin >>num;
} else {
    outFile << num <<" ";
}

2.第二個查詢:

我需要數組的大小來確定我的循環

不,你不會。 您可以使用while循環檢查結束。

首先,在您的void addRecordstoFile()函數中,您可以將while循環設計為

while(num!=-1){

        cout<<"\n Enter a survey response from 1 to 10 (-1 to end): ";
        cin>>num;

        if(num<1 || num>10){
            cout<<"\n Please enter a valid response from (1-10) ";
        }
        else{
            outFile << num <<" ";
        }

}

接下來,對於void readRecordsFromFile()函數,如果您的最終目標只是計算輸入的數字的出現次數,則可以使用10個元素的計數器數組。 從文件中讀取任何數字時,只需增加計數器數組特定索引處的元素即可。 例如,您從文件中讀取了3 ,因此您只需將counter[3]增加為counter[3]++即可對文件中3的出現進行計數。 想一想。

while(/*end of file condition*/){
    int a;
    inFile>>a;
    counter[a]++;
}

希望能有所幫助...

好吧,好消息是您有很多解決問題的選擇。 這里有一些建議:

為了避免在.txt文件中包含“ -1”,我可以將循環更改為以下形式:

while(num!=-1){

        cout<<"\n Enter a survey response from 1 to 10 (-1 to end): ";
        cin>>num;

        if (num == -1) break;  //  will break out of loop before adding num!
        if (num>0 && num<=10) outFile << num <<" ";  //  add only a valid number
        //  Note:  if number is not 1 thru 10 or -1, loop simply repeats!
}

至於第二個功能,您可以有多種選擇,但是我建議使用向量來包含數字。 要使用向量,您需要使用以下庫:

  #include <vector>

向量是動態數組-您可以根據需要向其添加或刪除元素。 您的函數可能如下所示:

void readRecordsFromFile(){
ifstream inFile("numbers.txt",ios::in);
if(!inFile){
    cerr<<"File was not found";
    exit (1);
}

vector <int> vNums;  //  a container for your int values
int inNum;           //  hold int for loading into vector

//  Read each number to inNum, then add it to the vector
while (!inFile.eof()) {
    inFile >> inNum;         //  load your input
    vNums.push_back(inNum);  //  "push_back" adds an element
}

//  Output the numbers... you may want to use a vector iterator for this
int x = 0;
while (x < vNums.size() ) {
    cout << x+1 << " - " << vNums[x] << "\n";
    x++;
}

}

另一種選擇是使用帶有鏈接列表的動態內存(新建/刪除)。 這是一個教程: http : //www.cprogramming.com/tutorial/lesson15.html

我認為使用向量是更好的解決方案! 祝你好運。

return static_cast<double>(numerator/denominator);

可能您希望將每個變量先轉換為double精度,然后再進行除法。 區別在於:

  • 如果int2/4 == 0 ,則static_cast<double>(numerator/denominator)返回0。
  • 如果為double (或float ): 2./4. == 0.5 2./4. == 0.5 ,因此static_cast<double>(numerator) / static_cast<double>(denominator)返回0.5

暫無
暫無

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

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