https://cs50.harvard.edu/x/2023/problems/3/snackbar/
더보기
strings.h
- bcmp - compare byte sequences
- bcopy - copy byte sequence
- bzero - zero a byte string
- explicit_bzero - zero a byte string
- ffs - find first bit set in a word
- ffsl - find first bit set in a word
- ffsll - find first bit set in a word
- index - locate character in string
- rindex - locate character in string
- strcasecmp - compare two strings ignoring case strncasecmp(str1, str2)
- string - string operations
- strncasecmp - compare two strings ignoring case(지정한 숫자만큼만) strncasecmp(str1, str2, 2) 앞 두자리만 비교
// Practice using structs
// Practice writing a linear search function
/**
* Beach Burger Shack has the following 10 items on their menu
* Burger: $9.5
* Vegan Burger: $11
* Hot Dog: $5
* Cheese Dog: $7
* Fries: $5
* Cheese Fries: $6
* Cold Pressed Juice: $7
* Cold Brew: $3
* Water: $2
* Soda: $2
*/
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
// Number of menu items
// Adjust this value (10) to number of items input below
#define NUM_ITEMS 10
// Menu itmes have item name and price
typedef struct
{
string item;
float price;
}
menu_item;
// Array of menu items
menu_item menu[NUM_ITEMS];
// Add items to menu
void add_items(void);
// Calculate total cost
float get_cost(string item);
string topper (string array);
int main(void)
{
add_items();
printf("\nWelcome to Beach Burger Shack!\n");
printf("Choose from the following menu to order. Press enter when done.\n\n");
for (int i = 0; i < NUM_ITEMS; i++)
{
printf("%s: $%.2f\n", menu[i].item, menu[i]. price);
}
printf("\n");
float total = 0;
while (true)
{
string item = get_string("Enter a food item: ");
if (strlen(item) == 0)
{
printf("\n");
break;
}
total += get_cost(item);
}
printf("Your total cost is: $%.2f\n", total);
}
// Add at least the first for items to the menu array
void add_items(void)
{
menu[0].item = "Burger";
menu[1].item = "Vegan Burger";
menu[2].item = "Hot Dog";
menu[3].item = "Cheese Dog";
menu[4].item = "Fries";
menu[5].item = "Cheese Fries";
menu[6].item = "Cold Pressed Juice";
menu[7].item = "Cold Brew";
menu[8].item = "Water";
menu[9].item = "Soda";
menu[0].price = 9.5;
menu[1].price = 11;
menu[2].price = 5;
menu[3].price = 7;
menu[4].price = 5;
menu[5].price = 6;
menu[6].price = 7;
menu[7].price = 3;
menu[8].price = 2;
menu[9].price = 2;
return;
}
// Search through the menu array to find an item's cost
float get_cost(string item)
{
int key_length = strlen(item);
int menu_number;
bool invalid_item = true;
float cost = 0.0;
string item_top = item;
for (int i = 0; i < key_length; i++) //입력값 대문자화
{
if (item[i] > 96 && item[i] < 123)
{
item_top[i] = item[i] - 32;
};
};
string temp_menu[NUM_ITEMS]; //대문자메뉴판
for (int i = 0; i < NUM_ITEMS; i++)
{
temp_menu[i] = topper (menu[i].item);
}
for (int i = 0; i < NUM_ITEMS; i++)
{
if (item_top == temp_menu[i])
{
menu_number = i;
invalid_item = false;
};
};
if(invalid_item == true)
{
printf("INVALID ITEM!\n");
}
else
{
cost = menu[menu_number].price;
};
return cost;
}
string topper (string menu_name)
{
string return_string = menu_name;
int string_length = strlen(return_string);
for (int j = 0; j < string_length; j++)
{
if (return_string[j] > 96 && return_string[j] < 123)
{
return_string[j] = return_string[j] - 32;
};
};
return return_string;
}
더보기
snackbar/ $ ./snackbar
Welcome 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: soda
Segmentation fault (core dumped)
snackbar/ $
위에는 아직 성공 못한 코드. 계속 메모리 오류가 나온다 ㅠㅠ
아래는 github에 올라온 코드인데, 라이브러리로 내 50줄 코드를 5줄로 끝냈다.
본받아야지...
다만 메모리 오류의 원인은 아직 찾지 못해서 기록을 위해 여기 남겨두고,
다른 문제 몇개 풀고 다시 와서 살펴볼 예정이다.
// Practice using structs
// Practice writing a linear search function
/**
* Beach Burger Shack has the following 10 items on their menu
* Burger: $9.5
* Vegan Burger: $11
* Hot Dog: $5
* Cheese Dog: $7
* Fries: $5
* Cheese Fries: $6
* Cold Pressed Juice: $7
* Cold Brew: $3
* Water: $2
* Soda: $2
*/
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
// Number of menu items
// Adjust this value (10) to number of items input below
#define NUM_ITEMS 10
// Menu itmes have item name and price
typedef struct
{
string item;
float price;
}
menu_item;
// Array of menu items
menu_item menu[NUM_ITEMS];
// Add items to menu
void add_items(void);
// Calculate total cost
float get_cost(string item);
int main(void)
{
add_items();
printf("\nWelcome to Beach Burger Shack!\n");
printf("Choose from the following menu to order. Press enter when done.\n\n");
for (int i = 0; i < NUM_ITEMS; i++)
{
printf("%s: $%.2f\n", menu[i].item, menu[i]. price);
}
printf("\n");
float total = 0;
while (true)
{
string item = get_string("Enter a food item: ");
if (strlen(item) == 0)
{
printf("\n");
break;
}
total += get_cost(item);
}
printf("Your total cost is: $%.2f\n", total);
}
// Add at least the first for items to the menu array
void add_items(void)
{
menu[0].item = "Burger";
menu[1].item = "Vegan Burger";
menu[2].item = "Hot Dog";
menu[3].item = "Cheese Dog";
menu[4].item = "Fries";
menu[5].item = "Cheese Fries";
menu[6].item = "Cold Pressed Juice";
menu[7].item = "Cold Brew";
menu[8].item = "Water";
menu[9].item = "Soda";
menu[0].price = 9.5;
menu[1].price = 11;
menu[2].price = 5;
menu[3].price = 7;
menu[4].price = 5;
menu[5].price = 6;
menu[6].price = 7;
menu[7].price = 3;
menu[8].price = 2;
menu[9].price = 2;
return;
}
// Search through the menu array to find an item's cost
float get_cost(string item)
{
for(int i = 0; i < NUM_ITEMS; i++)
{
if (strcasecmp(menu[i].item, item) == 0)
{
return menu[i].price;
}
}
return 0.0;
}