丢弃检查列表是否为空

时间:2015-03-05 20:09:19

标签: c++

当你上次设法修复我的代码时,我想再次请求你的帮助。

由于我已经有一个包含五个元素的预定义列表,因此这个代码似乎是非常无关紧要的,因为没有检查列表是否为空的目的。我似乎无法找出绕过if-else的人,只需保留"插入"函数而不是检查列表是否为空......

#include <iostream>
#include <cmath>

using namespace std;

struct node
{
    string nameOfFood;
    int eatCalories;
    int number;
    node *next;

};

bool isEmpty(node *head);
char menu();
void insertAsFirstElement(node *&head, node *&last, string name, int eatCalories);
void insert(node *&head, node *&last, string name, int eatCalories);
void showList(node *current);


bool isEmpty(node *head)
{
    if(head == NULL)
        return true;
    else
        return false;
}

char menu()
{
    char choice;

    cout << "Menu\n";
    cout << "1. Add food, beverage etc.\n";
    cout << "2. Show the list of food(s), beverage(s) etc.\n";
    cout << "3. Update your current weight\n";
    cout << "4. What have you been eaten?\n";
    cout << "5. What exercise have you done?\n";
    cout << "6. Exit program \n";

    cin >> choice;

    return choice;

}

void insertAsFirstElement(node *&head, node *&last, string nameOfFood, int eatCalories)
{
    node *temp = new node;
    temp->nameOfFood = nameOfFood;
    temp->eatCalories = eatCalories;
    temp->next = NULL;
    head = temp;
    last = temp;
}

void insert(node *&head, node *&last, string nameOfFood, int eatCalories)
{
    if(isEmpty(head))
        insertAsFirstElement(head, last, nameOfFood, eatCalories);
    else
    {
        node *temp = new node;
        temp->nameOfFood = nameOfFood;
        temp->eatCalories = eatCalories;
        temp->next = NULL;
        last->next = temp;
        last = temp;
    }

}

如果您需要更多代码,请告诉我?

希望得到你的帮助!

3 个答案:

答案 0 :(得分:2)

该检查是必要的,因为如果您的列表为空,则必须执行仅在该情况下执行的特定数量的操作。

实现自己的链表真的没有用处。标准已经定义了比您的更灵活的类,请参阅std::forward_list(单链表)和std::list(双链表)。

建议您在选择容器时默认为std::vectorstd::array。在这种情况下,如果您只有一个包含5个元素的列表,只需使用std::array和自定义类型:

struct food
{
    string nameOfFood;
    int eatCalories;
    int number;
};

然后:

std::array<food, 5> food_list { ... };

答案 1 :(得分:0)

但是我的列表永远不会是空的,因为我有五个预定义的元素。我会发布剩下的代码,也许你会明白我的意思?或者,我错了。

&#13;
&#13;
#include <iostream>
#include <cmath>

using namespace std;

struct node
{
    string nameOfFood;
    int eatCalories;
    int number;
    node *next;

};

bool isEmpty(node *head);
char menu();
void insertAsFirstElement(node *&head, node *&last, string name, int eatCalories);
void insert(node *&head, node *&last, string name, int eatCalories);
void showList(node *current);


bool isEmpty(node *head)
{
    if(head == NULL)
        return true;
    else
        return false;
}

char menu()
{
    char choice;
    
    cout << "Menu\n";
    cout << "1. Add food, beverage etc.\n";
    cout << "2. Show the list of food(s), beverage(s) etc.\n";
    cout << "3. Update your current weight\n";
    cout << "4. What have you been eaten?\n";
    cout << "5. What exercise have you done?\n";
    cout << "6. Exit program \n";
    
    cin >> choice;
    
    return choice;
    
}

void insertAsFirstElement(node *&head, node *&last, string nameOfFood, int eatCalories)
{
    node *temp = new node;
    temp->nameOfFood = nameOfFood;
    temp->eatCalories = eatCalories;
    temp->next = NULL;
    head = temp;
    last = temp;
}

void insert(node *&head, node *&last, string nameOfFood, int eatCalories)
{
    if(isEmpty(head))
        insertAsFirstElement(head, last, nameOfFood, eatCalories);
    else
    {
        node *temp = new node;
        temp->nameOfFood = nameOfFood;
        temp->eatCalories = eatCalories;
        temp->next = NULL;
        last->next = temp;
        last = temp;
    }
    
}
void showList(node *current)
{
    if(isEmpty(current))
        cout << "The list of foods is empty\n";
    else
    {
        cout << "The list of foods contain: \n";
        int  number = 0;
        
        while(current != NULL)
        {
            ++number;
            cout << number << " " << current->nameOfFood << " " << current->eatCalories << " calories" << endl;
            
            current = current->next;
            
        }
    }
}
node* subtractCalories(node *head)
{
    int choice;
    showList(head);
    cout << "Pick the food, beverage etc. from the list by number\n";
    cin >> choice;
    
    int number = 1;
    node *current = head;
    while (current != NULL && choice != number)
    {
        ++number;
        current = current->next;
    }
    cout << "You have choosen: " << current->nameOfFood << endl;
    return current;
}


int main(int argc, const char * argv[]) {
    
    double caloriesToBurn;
    double weight;
    double height;
    double BMI;
    
    int answerWhile = 1;
    int answerToExercise = 0;
    float minutesExerciseInHour;
    float caloriesBurned;
    
    node *head = NULL;
    node *last = NULL;
    node *current;
    char choice;
    int eatCalories;
    string nameOfFood;
    
    insert(head, last, "Tuna (in oil)", 190);
    insert(head, last, "Milkchocolate (Marabou)", 540);
    insert(head, last, "Milk (skimmed)", 33 );
    insert(head, last, "Ice Tea (white peach)", 1);
    insert(head, last, "Peanuts (roasted and salted)", 624);
    
    cout << "*** Welcome to the calorie calculator! ***\n";
    cout << "To get started, i need some numbers from you\n";
    cout << "Please enter the number of calories you need to burn today:\n";
    cin >> caloriesToBurn;
    cout << "What are your current weight? (please enter in kilograms)\n";
    cin >> weight;
    cout << "And lastly i need to know your height (please enter in centimeters)\n";
    cin >> height;
    BMI = (weight / pow(height/100,2));
    cout << "Your BMI is: " << BMI << endl;
    
    if (BMI <= 18.5)
        cout << "You are in the range: under weight" << endl;
    else if ((BMI > 18.5) && (BMI < 25))
        cout << "You are in the range: normal weight" << endl;
    else
        cout << "You are in the range: over weight" << endl;
    do{
        
        choice = menu();
        switch(choice)
        {
            case '1':
                cout << "Enter the name of the food, beverage etc.:";
                cin >> nameOfFood;
                cout << "How many calories did it contain? (measured per 100 grams):";
                cin >> eatCalories;
                
                insert(head, last, nameOfFood, eatCalories);
                break;
                
            case '2': showList(head);
                break;
                
            case '3': cout << "What you wanna update your weight to?\n";
                cin >> weight;
                cout << "Your weight have been update to: \n";
                cout << weight << endl;
                if (BMI <= 18.5)
                    cout << "You are in the range: under weight" << endl;
                else if ((BMI > 18.5) && (BMI < 25))
                    cout << "You are in the range: normal weight" << endl;
                else
                    cout << "You are in the range: over weight" << endl;
                
                break;
                
            case '4':
                cout << endl << "You need to consume " << caloriesToBurn << " calories today!" << endl << endl;
                current = subtractCalories(head);
                caloriesToBurn = caloriesToBurn-current->eatCalories;
                cout << endl << "You need to eat " << caloriesToBurn << " calories more today!" << endl;
                break;
                
            case '5':
                
                do
                {
                    cout << "How many minuttes have you ben exercising (please enter in hours)\n";
                    cin >> minutesExerciseInHour;
                    cout << "What type of exercise have you been doing?" <<endl;
                    cout << "1. Running" <<endl;
                    cout << "2. Swimming" <<endl;
                    cout << "3. Cycling" <<endl;
                    cin >> answerToExercise;
                    
                    switch(answerToExercise)
                    {
                        case 1:
                            caloriesBurned = weight*7.5*minutesExerciseInHour;
                            cout << "You have burned: " << caloriesBurned << "calories\n";
                            caloriesToBurn = caloriesToBurn + caloriesBurned;
                            cout << "You have: " << caloriesToBurn << " left today\n";
                            break;
                            
                        case 2:
                            caloriesBurned = weight*7*minutesExerciseInHour;
                            cout << "You have burned: " << caloriesBurned << "calories\n";
                            caloriesToBurn = caloriesToBurn + caloriesBurned;
                            cout << "You have: " << caloriesToBurn << " left today\n";
                            break;
                            
                        case 3:
                            caloriesBurned = weight*6*minutesExerciseInHour;
                            cout << "You have burned: " << caloriesBurned << "calories\n";
                            caloriesToBurn = caloriesToBurn + caloriesBurned;
                            cout << "You have: " << caloriesToBurn << " left today\n";
                            break;
                            
                        default: cout << "I think something went wrong? Try again..";
                    }
                }
                
                while (answerWhile != 1);
                
            default: cout << "System exit\n";
        }
    }while(choice != '6');
    
    
    return 0;
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我想我理解检查空列表 - 因此我会将我的代码带到另一个问题。

我得到了InsertAsFirstElement函数,但此刻,我想我已经忘记了函数的功能,因为在代码中没有任何地方会有&#34;插入第一个元素&#34;所以有人可以告诉那是什么功能吗?

很抱歉给您带来不便!