/**** * * This file implements the functions defined in student-info.h. Note that all * of the typedef and function comments are in the .h file, and not repeated * here. * */ #include "student-info.h" #include #include Address create_address( int number, char* street, char* city, char* state, int zip) { Address address; address.number = number; strncpy(address.street, street, LARGE_STRING_SIZE-1); strncpy(address.city, city, LARGE_STRING_SIZE-1); strncpy(address.state, state, STATE_STRING_SIZE-1); address.zip = zip; return address; } Phone create_phone(int area_code, int number) { Phone phone; phone.area_code = area_code; phone.number = number; return phone; } StudentRecord create_student_record( char* last_name, char* first_and_middle_names, int empl_id, char* user_name, char* major, Address address, Phone phone) { StudentRecord rec; strncpy(rec.last_name, last_name, LARGE_STRING_SIZE-1); strncpy(rec.first_and_middle_names, first_and_middle_names, LARGE_STRING_SIZE-1); rec.empl_id = empl_id; strncpy(rec.user_name, user_name, SMALL_STRING_SIZE-1); strncpy(rec.major, major, SMALL_STRING_SIZE-1); rec.address = address; rec.phone = phone; return rec; } void print_student_record(StudentRecord rec) { printf("Name: %s, %s\n", rec.last_name, rec.first_and_middle_names); printf("EmplID: %d\nUser Name: %s\nMajor: %s\n", rec.empl_id, rec.user_name, rec.major); printf ("Address: %d %s %s, %s %d\n", rec.address.number, rec.address.street, rec.address.city, rec.address.state, rec.address.zip); printf("Phone: (%d) %d-%d\n", rec.phone.area_code, rec.phone.number/10000, rec.phone.number%10000); } StudentRecord find_student_record(StudentDB db, int empl_id) { int i; /* Search loop index */ /* * Initialize the found record to an empty student record. */ StudentRecord found_record = EMPTY_STUDENT_RECORD; /* * Loop through all records in the database, looking for one with the given * empl_id. Note that this is a very crude and inefficient way to search. * We're just scratching the surface of database search in this little * example. As with databases in general, database search is a very big * subject in computer science. Hey, it's what made Google worth billions * of dollars. */ for (i = 0; i < MAX_DB_SIZE; i++) { if (db[i].empl_id == empl_id) { found_record = db[i]; break; } } return found_record; } int main() { StudentRecord rec; static StudentDB db; /* * Create a new student record */ rec = create_student_record( "Smith", "Pat T.", 123456789, "ptsmith", "CSC", create_address(1234, "State St", "San Luis Obispo", "CA", 93406), create_phone(805, 1234567)); /* * Store the record in the first database entry. */ db[0] = rec; /* * Print the record. */ printf("Record 0:\n"); print_student_record(db[0]); /* * Find a record by its empl id. There's not much to this search, given * that there's only one record in the database. As the comment above * notes, we're just scratching the surface of the very large subject of * database search. */ printf("\nFound Record with Empl ID 123456789:\n"); print_student_record(find_student_record(db, 123456789)); printf("\nFound Record with Empl ID 987654321:\n"); print_student_record(find_student_record(db, 987654321)); return 0; }