The program is a text-based command processing system written in C that simulates buying and selling items, querying inventory, and tracking the location of people. It processes commands entered by the user and executes actions based on the given instructions.
- The program uses a structured approach to process commands and manage inventories and locations.
- The functionality is flexible, supporting multiple actions, queries, and conditional statements.
- It efficiently validates user input and provides feedback if the commands are not correctly formatted.
- The test cases demonstrate the system's ability to handle complex interactions and maintain accurate inventory records.
The program includes several standard headers:
stdio.h
: For input/output operations.string.h
: For string manipulation.stdlib.h
: For dynamic memory management.ctype.h
: For character handling.
Macros are defined to set limits for word lengths, number of words, keywords, and keyword lengths:
MAX_WORD_LENGTH
: Maximum length of a word.MAX_WORDS
: Maximum number of words.NUM_KEYWORDS
: Number of keywords used in commands.MAX_KEYWORD_LENGTH
: Maximum length of a keyword.
An array of 19 keywords is defined, such as "sell," "buy," "go," "to," "from," "and," etc. These keywords are essential for interpreting commands entered by the user.
Item
: Represents an item with aname
andquantity
.Subject
: Represents a person with aname
,location
,previousLocation
,inventoryCount
, and an array ofItem
structures.Location
: Represents a location with aname
.
people
,locationlist
,itemlist
: Arrays to store people, locations, and items, respectively.locationcnt
,itemcnt
,people_count
: Counters to track the number of locations, items, and people.
containsNumber
andisFullNumber
: Check if a string contains or is entirely composed of numbers.isIntheList
: Checks if a string is present in a given list.isInKeywords
: Checks if a string is a keyword.ifFinder
,atFinder
,hasFinder
: Find the indices of keywords like "if", "at", and "has" in the word array.comparator
: Compares two integers.findPerson
andfindPersonsItem
: Find a person or an item in a person's inventory.buy
andsell
: Execute buying and selling operations for people and items.
The main loop reads user input, tokenizes the input into words, and processes the commands.
- Validity Check: Validates the input command format and ensures that it follows the correct syntax and semantics.
- Question Processing: Handles queries about total items, locations, and inventory.
- Condition Handling: Evaluates conditional statements using "if", "at", and "has" for logical operations.
- Sentence Execution: Executes buy, sell, and go commands based on the parsed and validated input.
Here are examples of test cases from the "input_01.txt" with commands and their expected outputs:
Input:
Galadriel and Elrond and Cirdan buy 100 Nenya and 100 Narya and 100 Vilya
Output:
OK
Input:
Cirdan total ?
Output:
100 Nenya and 80 Vilya and 80 Narya
Input:
Balrog total ?
Output:
10 Narya and 10 Vilya
Input:
Balrog and Cirdan sell 10 Vilya to Legolas
Output:
OK
Input:
Balrog and Cirdan sell 10 Vilya to Legolas
Output:
OK
Input:
Balrog and Cirdan total Narya ?
Output:
90