我怎么能改进这个c++程序没有向量

How could I improve this c++ program without vectors?

本文关键字:程序 向量 c++ 怎么能      更新时间:2023-10-16

**指南:**获得户主、年收入和家庭成员的3个独立数据列表(数组),然后获得所有年收入并将其平均。在一个整洁的表格中显示。

这是来自学校的一个项目,我不允许使用任何非常先进的东西,但我现在想回去改进它。我想让它更干净,特别想找到更多我可以从它身上拿走的东西,而不是增加的东西。

// <Program Name> Programming Project #3 Average Income (Using Functions and Arrays)
// <Author> Brendan Jackson
// <Date of Programs Release> 08/05/15
// <Program Description> takes 3 arrays and displays them with average income
#include <iostream> // allows cin and cout statements
#include <iomanip> //allows setprecision
#include <string> //allows strings
using namespace std; // Namespace std allows program to use entities from <iostream>
int input(string[], int[], double[]); //function 1
double calculate_average_income(double[], int);  //function 2
void display_survey_data(string[], int[], double[],int , double);  //function 3

int main() // main function
{
    //variables for functions
    string name[10];
    int members[10];
    double income[10];
    int count_of_households;
    double average;
    //get input
    count_of_households = input(name, members, income);
    //calculate average
    average = calculate_average_income(income, count_of_households);

    //output all data in table
    display_survey_data(name, members, income, count_of_households, average);

    return 0;
}

int input(string name[], int members[], double income[]) //function 1
{
    // get household info
    int count_of_households = 0;
    cout << "How many house holds were there? ";
    cin >> count_of_households;
    //TODO: handle bad input (characters and decimals)
    if (count_of_households >= 11 || count_of_households < 0) 
    {
        cout << "must enter valid # " ; //TODO: more description
        count_of_households = 0; //set back to safe value
    }
    else 
    {
           //cycle through arrays                  
        for (int count = 0; count < count_of_households; count++) //TODO:  take out (count + 1) start from 1 alternatively
        {
            // get survey info for names
            cout << "Enter household #" << (count + 1) << "'s head of household namet"  ;
            cin.ignore() ; // ignores keyboard buffer characters
            getline (cin, name[count]) ; 

            // get survey info for income
            cout << "Enter household #" << (count + 1) << "'s annual incomet"  ;
            cin >>  income[count];

            // get survey info for members
            cout << "Enter household #" << (count + 1) << "'s household memberst"  ;   
            cin >>  members[count]; 
        }   
    }
    return count_of_households;
}
double calculate_average_income(double income[], int count_of_households) //function 2
{

    //add incomes together
    double total = 0.0;
    double average = 0.0;
    //loop over income
    for (int count = 0 ; count < count_of_households; count++)
    {
        //add income to runnning total
        total += income[count];  
    }
    // save calculations
    average = total / count_of_households; 
    return average;
}


void display_survey_data(string name[], int members[], double income[],int count_of_households, double average) //funtion 3
{
    //print out header
    cout << setw(30) << ""
         << setw(30) << ""
         << setw(30) << "NUMBER OFn" ;
    cout << setw(30) << "HOUSEHOLD NAME"
         << setw(30) << "ANNUAL INCOME"
         << setw(30) << "HOUSEHOLD MEMBERSn" ;
    cout << setw(30) << "--------------------" 
         << setw(30) << "---------------" 
         << setw(30) << "------------------------n" ;      
    ///loop over values
    for (int count = 0 ; count < count_of_households; count++)
    {
        cout << setw(30) << name[count]
             << setw(30) << setprecision(2) << fixed << showpoint << income[count]
             << setw(30) << members[count] 
             << endl;   
    }   
    // display average
    cout << endl
         << setw(30) << "AVERAGE INCOME"
         << setw(30) << average
         << endl;
}

您可以使用std::array

这只是一个堆栈上的数组,就像你使用的那样,但是有迭代器,类型安全,绑定安全,使用值语义,并且可以使用大多数stl算法。

它是这样声明的:

array<string, 3> myArray;

必须通过引用传递,因为通过值传递会复制它的内容:

void doSomething(array<int, 6>& aArray) {
    // do something
}

注意你必须指定数组的长度,因为它是一个模板参数。如果你想要一个任意大小的数组,使用模板:

template<size_t length>
void foobar(array<double, length> theArray) {
    // do something else
}