C ++ study notes 7

Source: Internet
Author: User

 

Use a map to create a table of foods and calories per portion. for example carrots-45, ice cream-250, and so on. place at least 10 foods in your map. use a random number generator to pick 4 foods per meal. print out the meal and its calorie total.

 

Use Map to create a table containing food and its corresponding calories, such as carrot-45, ice cream-250, and so on. Map contains at least 10 types of food, 4 types of food are randomly selected for each meal, and the selected food and its calories are printed.

 

// BookProgramCompiled with vcsp6

# Include <iostream>
# Include <string>
# Include <stdlib. h>

Using namespace STD;

# Include <map>

Typedef STD: Map <string, int> food;

Int main ()
{
Food food;
Int A [5] = {0 };


Int I = 0;

Cout <"there are several kinds of food \ n ";

Cout <"1: Mutton" <Endl;
Cout <"2: Tomato" <Endl;
Cout <"3: potato" <Endl;
Cout <"4: carrot" <Endl;
Cout <"5: Pumpkin" <Endl;
Cout <"6: Pork" <Endl;
Cout <"7: beef" <Endl;
Cout <"8: Onion" <Endl;
Cout <"9: Spinach" <Endl;
Cout <"10: radish" <Endl;
Cout <"11: laver" <Endl;
// Put all the food




// Insert the corresponding food and Caroline
Food. insert (Food: value_type ("mutton", 300 ));
Food. insert (Food: value_type ("tomato", 50 ));
Food. insert (Food: value_type ("potato", 45 ));
Food. insert (Food: value_type ("carrot", 45 ));
Food. insert (Food: value_type ("Pumpkin", 50 ));
Food. insert (Food: value_type ("pork", 300 ));
Food. insert (Food: value_type ("beef", 400 ));
Food. insert (Food: value_type ("Onion", 100 ));
Food. insert (Food: value_type ("Spinach", 100 ));
Food. insert (Food: value_type ("radish", 50 ));
Food. insert (Food: value_type ("laver", 70 ));

Cout <"your food calories: \ nfood \ t \ tcalories \ n ";



While (I <4)
{
Int flag = 1 + rand () % 10; // use a random function
Int J = 0;

For (Food: const_iterator iter = food. Begin (); iter! = Food. End (); ++ ITER, ++ J)
{
If (flag = J & A [J]! = 1)
{
Cout <ITER-> first <"\ t" <ITER-> second <Endl;
I ++;
A [J] = 1;
Break;
}
}
}

Return 0;
 
 
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.