Snackbar
Learning Goals
- Practice using
structs - Write a linear 搜索 algorithm

背景
Imagine you’re at the beach and want to order a number of items from the snack bar. You have a limited amount of cash on you, and you want to get a total cost for your items before ordering. In snackbar.c you will complete two functions. First is add_items which will add at least the first 4 items on the Beach Burger Shack 菜单. Then you will complete get_cost which will 实现一个 linear 搜索 algorithm to 搜索 for each item you choose, and return the corresponding price.
- 提示
- You can hard code the 菜单 items and prices in the
add_itemsfunction. - There is a function in
strings.hthat 五月 come in handy here.
- You can hard code the 菜单 items and prices in the
演示
入门
- Log into cs50.dev using your GitHub account.
- Click inside the terminal window and execute
cd. - Execute
wget https://cdn.cs50.net/2022/fall/实验s/3/snackbar.zipfollowed by Enter in order to 下载 a zip calledsnackbar.zipin your codespace. Take care not to overlook the space betweenwgetand the following URL, or any other character for that matter! - Now execute
unzip snackbar.zipto create 的文件夹中名为snackbar. - You 不再 need the ZIP file, so you can execute
rm snackbar.zipand respond with “y” followed by Enter at the prompt.
实现细节
The main function is already complete. After calling add_items to initialize the menu array, it will print out the 菜单 items and their prices, prompting you to keep selecting items until you press enter without typing anything in. You are to complete two functions, add_items, which adds at least the first four 菜单 items, and get_cost to return the cost of each item. When you are creating a linear 搜索 algorithm in get_cost, do make sure that it is case insentive.
Thought 问题
- Why is an array of
structs a better solution here than multiple arrays?
如何测试 Your Code
你的程序应该 behave as follows:
snackbar/ $ ./snackbar
欢迎 to Beach Burger Shack!
Choose from the following menu to order. Press enter when done.
Burger: $9.50
Vegan Burger: $11.00
Hot Dog: $5.00
Cheese Dog: $7.00
Fries: $5.00
Cheese Fries: $6.00
Cold Pressed Juice: $7.00
Cold Brew: $3.00
Water: $2.00
Soda: $2.00
Enter a food item: burger
Enter a food item: fries
Enter a food item: soda
Enter a food item:
Your total cost is: $16.50
snackbar/ $ ./snackbar
欢迎 to Beach Burger Shack!
Choose from the following menu to order. Press enter when done.
Burger: $9.50
Vegan Burger: $11.00
Hot Dog: $5.00
Cheese Dog: $7.00
Fries: $5.00
Cheese Fries: $6.00
Cold Pressed Juice: $7.00
Cold Brew: $3.00
Water: $2.00
Soda: $2.00
Enter a food item: cold brew
Enter a food item: hot dog
Enter a food item:
Your total cost is: $8.00
笔记 that the 菜单 should only print out the items that you saved in the menu array.
No check50 for this one!
To evaluate that the style of your code, type in the following at the $ prompt.
style50 snackbar.c
如何提交
No need to 提交! This is an 可选 练习题.