Answer to Question #348988 in C++ for jcerth

Question #348988

Write a program that will be able to classify fruits based on their (1) shape, (2) color and (3) texture. We have the following assumptions:


shape can assume any of the two values, namely: 0 to represent a round shape, 1 to represent an oblong shape


color can assume any of the three values, namely: 0 to represent a green color, 1 to represent a yellow color, 2 to represent a red color, and 3 to represent an orange color


texture can assume any of the two values, namely: 0 to represent smooth texture and 1 to represent a rough texture


2. The program should be able to determine the following fruits with the following characteristics:



apple: round shape, red color, smooth texture


banana: oblong shape, yellow color or green color, smooth texture


orange: round shape, orange color, smooth texture


jackfruit: oblong shape, green color, rough texture


1
Expert's answer
2022-06-09T18:20:47-0400
#include <iostream>
#include <map>
#include <string>

std::map<std::string, std::string> determineFruit(int shape, int color, int texture);
int showFruit(std::map<std::string, std::string> &fruit);

int main()
{
    int shape=-1, color=-1, texture=-1;
    std::cout << "Select shape (0 - round shape, 1 - oblong shape): ";
    std::cin >> shape;
    std::cout << "Select color (0 - green, 1 - yellow, 2 - red, 3 - orange): ";
    std::cin >> color;
    std::cout << "Select texture (0 - smooth texture, 1 - rough texture): ";
    std::cin >> texture;

    std::map<std::string, std::string> fruit = determineFruit(shape, color, texture);
    showFruit(fruit);
    return 0;
}

std::map<std::string, std::string> determineFruit(int shape, int color, int texture) {
    std::map<std::string, std::string> fruits;
    fruits["apple"] = "apple: round shape, red color, smooth texture";
    fruits["banana"] = "banana: oblong shape, yellow color or green color, smooth texture";
    fruits["orange"] = "orange: round shape, orange color, smooth texture";
    fruits["jackfruit"] = "jackfruit: oblong shape, green color, rough texture";

    switch (shape) {
    case 0:
        fruits.erase("banana");
        fruits.erase("jackfruit");
        break;
    case 1:
        fruits.erase("apple");
        fruits.erase("orange");
        break;
    }

    switch (color) {
    case 0:
        fruits.erase("apple");
        fruits.erase("orange");
        break;
    case 1:
        fruits.erase("apple");
        fruits.erase("orange");
        fruits.erase("jackfruit");
        break;
    case 2:
        fruits.erase("banana");
        fruits.erase("orange");
        fruits.erase("jackfruit");
        break;
    case 3:
        fruits.erase("apple");
        fruits.erase("banana");
        fruits.erase("jackfruit");
        break;
    }

    switch (texture) {
    case 0:
        fruits.erase("jackfruit");
        break;
    case 1:
        fruits.erase("apple");
        fruits.erase("banana");
        fruits.erase("orange");
        break;
    }
    return fruits;
}

int showFruit(std::map<std::string, std::string> &fruit) {
    std::cout << std::endl;
    if (!fruit.empty()) {
        for (auto const& x : fruit) {
            std::cout << x.second << std::endl;
        }
    }
    else {
        std::cout << "No fruit found!" << std::endl;
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS