-5

我真的很想得到这个程序的帮助,因为我不知道该怎么做。我已经尝试在网上搜索答案或可以为我指明正确方向的东西,但这对我来说有点困难,因为我只是在学习如何编码。我将不胜感激!

你要编写一个程序来帮助圣诞老人处理他的精灵所做的工作。您将使用一个名为 elves.dat 的数据文件。每个精灵都会有一条线。该行将包含小精灵的名字和小精灵制作的玩具数量。您将从文件中读取并将值放在并行数组中。您不知道有多少精灵,因此您必须阅读到文件末尾并计数。您将需要第三个并行字符串数组来记录每个精灵的评分。您应该声明可容纳 50 个组件的数组。

程序应该读入数组。它应该查看每个精灵制作的玩具数量,并以平行数组的形式记录评分。下表确定了评级。然后程序应该在整齐的、有标签的列中并排打印出数组。应该打印出精灵制作的玩具总数,制作超过500个玩具的精灵数量,制作玩具最多的精灵的名字,制作玩具最少的精灵的名字。每个计算都应该有自己的功能。在进行计算的函数中不应进行任何输出。所有输出都应在一个输出函数中完成。请记住始终将数组中的元素数量与数组一起传递给函数。

玩具制造........评级

500 或更多:***** 5 星

300 到 499 之间:*** 3 星

200 到 299 之间:* 1 星

200 岁以下:- 无

这是 elves.dat 中的信息: Smiley 662 Curley 88 Clementine 335 Jasper 105 Lucinda 775 Brunhilda 103 Florence 441 Oskar 820 Snowflake 990 Bernard 690 Punch 298 Chuckie 10 Frosty 102 Snowman 311 April 830 Merry 299 Sunshine 331 Buddy 1234 Carol 271 Misty 111 Harold 52 Henry 292 Twinkle 308 Starlight 703 Burr 112 Angelica 444 Bluenose 689 Harry 254 Twinkle 259 Stardust 121 Greensleeves 453 Noel 312 Happy 209 Yukon 534 Snowcap 190 Northpole 598

这是我的代码,我觉得有一个非常基本的理解,但我知道我什至没有接近!我会很感激任何帮助,我真的需要它。

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

int main() {

  ifstream inFile;
  inFile.open("elves.dat");

  string elfName[50];
  int ToysMade[50];
  int count = 0;
  int i;
  //Read file until you've reached the end                                        
  while (!inFile.eof()){
    inFile >> elfName;
    inFile >> ToysMade;
    count++;}
    cout << "Elf: " << elfName <<endl;
    cout << "Toys made: " << ToysMade <<endl;

    inFile.close();
  return 0;
}
4

1 回答 1

0

根据您所说的:这就是我设计程序的方式。我什至可以使用structor使它看起来更简单class,但我收集到你在培训过程中并没有那么远。即使这可能比所要求的要先进一些。你确实提到了parallel arrays。而不是arrays我使用它来std::vectors代替它。您可以轻松地将 替换为vectors常规arrays,但是由于我使用library的是适用于容器的函数,因此进行计算的函数会​​发生变化。与raw arrays你一起做一堆for&while loopscomparison检查,同时也做bounds checks那些arrays。您的整个程序将与此类似。

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <fstream>

// Typedefs
typedef std::vector<std::string> Strings;
typedef std::vector<unsigned> Values;

unsigned countToys( const Values& toys );
void determineRatings( const Values& toys, Values& ratings );
unsigned count5StarElves( const Values& toys );

std::string getBestElf( const Strings& elves, const Values& toys );
std::string getWorstElf( const Strings& elves, const Values& toys );

void displayStats( const unsigned numElves, const unsigned fiveStarElves,
                   const Strings& elves, const Values& toys, const Values& ratings,
                   const std::string& bestElf, const std::string& worstElf );

int main() {

    // Create Containers Giving 50 available spots.
    Strings elves;
    elves.resize( 50 );
    Values ratings;
    ratings.resize( 50 );
    Values toysMade;
    toysMade.resize( 50 );

    // Create, Open, Read From File & Close It
    unsigned numElves = 0;  
    std::ifstream file;
    file.open( "elves.dat" );
    while ( file >> elves[numElves] >> toysMade[numElves] ) {
        numElves++; // Need this to resize containers.
    }
    file.close();

    // Adjust containers to fit number of elves.
    const std::size_t newSize = numElves;
    elves.resize( newSize );
    ratings.resize( newSize );
    toysMade.resize( newSize );

    // Get The Stats -- Counting the total number of elves is called within the display function.
    determineRatings( toysMade, ratings );
    unsigned fiveStarElves = count5StarElves( ratings );
    std::string bestElf  = getBestElf( elves, toysMade );
    std::string worstElf = getWorstElf( elves, toysMade );

    // Display The Results
    displayStats( numElves, fiveStarElves, elves, toysMade, ratings, bestElf, worstElf );


    std::cout << "\nPress any key and enter to quit." << std::endl;
    char c;
    std::cin >> c;

    return 0;
}

unsigned countToys( const Values& toys ) {
    unsigned total = std::accumulate( toys.begin(), toys.end(), 0 );
    return total;
}

void determineRatings( const Values& toys, Values& ratings ) {
    for ( unsigned i = 0; i < toys.size(); i++ ) {
        if ( toys[i] >= 500 ) {
            ratings[i] = 5;
        }

        if ( toys[i] < 500 && toys[i] >= 300 ) {
            ratings[i] = 3;
        }

        if ( toys[i] < 300 && toys[i] >= 200 ) {
            ratings[i] = 1;
        }

        if ( toys[i] < 200 ) {
            ratings[i] = 0;
        }
    }
};

unsigned count5StarElves( const Values& ratings ) {
    unsigned fiveStartCount = 0;
    for ( auto val : ratings ) {
        if ( val == 5 ) {
            fiveStartCount++;
        }
    }

    return fiveStartCount;
}

std::string getBestElf( const Strings& elves, const Values& toys ) {
    auto it = std::max_element( toys.begin(), toys.end() );
    if ( it == toys.end() ) {
        return "";
    }

    // for random access containers with O(1) - constant
    return elves[it - toys.begin()];

    // If using non-random containers use the following with O(n) - linear
    // return std::distance(toys.begin(), it );
}

std::string getWorstElf( const Strings& elves, const Values& toys ) {
    auto it = std::min_element( toys.begin(), toys.end() );
    if ( it == toys.end() ) {
        return "";
    }

    // for random access containers with O(1) - constant
    return elves[it - toys.begin()];

    // If using non-random containers use the following with O(n) - linear
    // return std::distance(toys.begin(), it);
}

void displayStats( const unsigned numElves, const unsigned fiveStarElves, 
                   const Strings& elves, const Values& toys, const Values& ratings, 
                   const std::string& bestElf, const std::string& worstElf ) {

    std::cout << "***********************************************\n";
    std::cout << "* Welcome To Santa's Workshop:                *\n";
    std::cout << "* We have " << numElves << " working elves.                   *\n";
    std::cout << "* Our Elves made a total of " << countToys( toys ) << " toys today. *\n";
    std::cout << "***********************************************\n\n";
    std::cout << "Here are their stats:\n";
    std::cout << "===============================================\n";


    // Calculate the longest name for proper screen output formatting.
    std::size_t maxLength = 0;
    for ( const auto name : elves ) {
        if ( name.length() > maxLength ) {
            maxLength = name.length();
        }
    }

    std::cout << std::left << std::setw( maxLength + '\t' ) << "Elf Name:" << std::setfill( ' ' ) << "Toys Made:\t\tRating:\n" << std::endl;

    for ( unsigned i = 0; i < numElves; i++ ) {
        std::cout << std::left << std::setw( maxLength + '\t' ) << elves[i] << std::left << std::setfill( ' ' )
            << std::setw( 4 ) << std::left << toys[i] << std::left
            << "\t\t" << ratings[i] << " stars" << std::endl;
    }

    std::cout << "\n\n";

    // Additional Stats:
    std::cout << "There are " << fiveStarElves << " 5 Star Elves!\n";
    std::cout << "The Best Elf is: " << bestElf << std::endl;
    std::cout << "The Worst Elf is: " << worstElf << std::endl;
}

精灵.dat

Smiley 662
Curley 88
Clementine 335
Jasper 105
Lucinda 775
Brunhilda 103
Florence 441
Oskar 820
Snowflake 990
Bernard 690
Punch 298
Chuckie 10
Frosty 102
Snowman 311
April 830
Merry 299
Sunshine 331
Buddy 1234
Carol 271
Misty 111
Harold 52
Henry 292
Twinkle 308
Starlight 703
Burr 112
Angelica 444
Bluenose 689
Harry 254
Twinkle 259
Stardust 121
Greensleeves 453
Noel 312
Happy 209
Yukon 534
Snowcap 190
Northpole 598

编辑- 如果您想在显示功能中做一些花哨的事情。把displayStats()函数改成这样:

void displayStats( const unsigned numElves, const unsigned fiveStarElves, 
                   const Strings& elves, const Values& toys, const Values& ratings, 
                   const std::string& bestElf, const std::string& worstElf ) {

    std::cout << "***********************************************\n";
    std::cout << "* Welcome To Santa's Workshop:                *\n";
    std::cout << "* We have " << numElves << " working elves.                   *\n";
    std::cout << "* Our Elves made a total of " << countToys( toys ) << " toys today. *\n";
    std::cout << "***********************************************\n\n";
    std::cout << "Here are their stats:\n";
    std::cout << "===============================================\n";


    // Calculate the longest name for proper screen output formatting.
    std::size_t maxLength = 0;
    for ( const auto name : elves ) {
        if ( name.length() > maxLength ) {
            maxLength = name.length();
        }
    }

    std::cout << std::left << std::setw( maxLength + '\t' ) << "Elf Name:" << std::setfill( ' ' ) << "Toys Made:\t\tRating:\n" << std::endl;

    // A little bit of magic: (not really) just pretty 
    Strings stars;
    std::string str;
    for each (auto star in ratings) {
        if ( star == 0 ) {
            str = std::string( "" );
        }

        if ( star == 1 ) {
            str = std::string( "*" );
        }

        if ( star == 3 ) {
            str = std::string( "***" );
        }

        if ( star == 5 ) {
            str = std::string( "*****" );
        }

        stars.push_back( str );
    }

    for ( unsigned i = 0; i < numElves; i++ ) {
        std::cout << std::left << std::setw( maxLength + '\t' ) << elves[i] << std::left << std::setfill( ' ' )
            << std::setw( 4 ) << std::left << toys[i] << std::left
            << "\t\t" << stars[i] /*ratings[i] << " stars"*/ << std::endl;
    }

    std::cout << "\n\n";

    // Additional Stats:
    std::cout << "There are " << fiveStarElves << " 5 Star Elves!\n";
    std::cout << "The Best Elf is: " << bestElf << std::endl;
    std::cout << "The Worst Elf is: " << worstElf << std::endl;
}
于 2017-12-12T07:57:18.347 回答