728x90
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char* name;
int price;
int count;
} Item;
// Market inventory
Item items[] = {
{"Quiet Quiches", 10, 12},
{"Average Apple", 15, 8},
{"Fruitful Flag", 100, 1}
};
int numItems = sizeof(items) / sizeof(Item);
int coins = 40;
void displayItems() {
printf("\nWelcome to the market!\n=====================\n");
printf("You have %d coins\n", coins);
printf("\tItem\t\t\tPrice\tCount\n");
for (int i = 0; i < numItems; i++) {
printf("(%d) %s\t\t%d\t%d\n", i, items[i].name, items[i].price, items[i].count);
}
printf("(3) Sell an Item\n(4) Exit\n");
}
void buyItem() {
int choice, qty;
printf("Choose an option: ");
scanf("%d", &choice);
if (choice < 0 || choice >= numItems) {
if (choice == 4) exit(0);
printf("Invalid option!\n");
return;
}
printf("How many do you want to buy? ");
scanf("%d", &qty);
if (qty > items[choice].count) {
printf("Not enough inventory.\n");
} else {
int totalCost = items[choice].price * qty; // Vulnerable to underflow if qty is negative
if (totalCost > coins) {
printf("Not enough coins.\n");
} else {
items[choice].count -= qty;
coins -= totalCost; // Increases coins if qty is negative
printf("You bought %d %s(s).\n", qty, items[choice].name);
}
}
}
void sellItem() {
int choice, qty;
printf("Which item do you want to sell? ");
scanf("%d", &choice);
if (choice < 0 || choice >= numItems) {
printf("Invalid option!\n");
return;
}
printf("How many do you want to sell? ");
scanf("%d", &qty);
if (qty < 0 || qty > items[choice].count) {
printf("Invalid quantity.\n");
} else {
items[choice].count += qty;
coins += items[choice].price * qty;
printf("You sold %d %s(s).\n", qty, items[choice].name);
}
}
int main() {
int choice;
while (1) {
displayItems();
printf("Choose an option: ");
scanf("%d", &choice);
switch (choice) {
case 0:
case 1:
case 2:
buyItem();
break;
case 3:
sellItem();
break;
case 4:
printf("Exiting...\n");
return 0;
default:
printf("Invalid option!\n");
break;
}
}
return 0;
}
728x90
'호그와트' 카테고리의 다른 글
tryhackme athena fantasia :: tryhackme GOD의 풀이 (2) | 2024.05.05 |
---|---|
improving fuzzy fuzzy !! (2) | 2024.05.03 |
어느새 Java가 나의 모국어가 되었다 (0) | 2024.01.24 |
컴퓨터랑 자바로 대화할 수 있는 지경에 이르렀다 (0) | 2023.12.11 |
내가 밖에서 시로 봤는데 흠흐밍 흠흐밍 하면서 돌아다니더라 (0) | 2023.12.08 |