Skip to content

Commit ff9f57a

Browse files
authored
Documented and Completed project
1 parent d2eda7f commit ff9f57a

File tree

2 files changed

+75
-28
lines changed

2 files changed

+75
-28
lines changed

struct_sort.c

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
3-
#include "struct_sort.h"
4-
#include <ctype.h>
1+
/**
2+
* @author Brett Dale
3+
* @version 1.0 (2/28/2020)
4+
*
5+
* This C source file creates a collection of Contacts and sorts them by
6+
* last name first, then first name if last name's are the same.
7+
*/
58

6-
//COMMENT THEN README
7-
//WRITE METHOD SIGNATURES IN HEADER
9+
#include "struct_sort.h" /** Header file for the source file */
810

11+
/**
12+
* This method eliminates all whitespace leading or trailing the string passed
13+
*/
914
void trim(char *string) {
1015
char *ptr = string;
1116
int length = strlen(ptr);
@@ -19,6 +24,12 @@ void trim(char *string) {
1924
memmove(string, ptr, length + 1);
2025
}
2126

27+
/**
28+
* This method copy's the Contact struct passed in and returns the copy
29+
*
30+
* @param contact contact to be copied
31+
* @return Copy of Contact struct passed
32+
*/
2233
Contact copy(Contact contact){
2334
Contact temp;
2435
strcpy(temp.first_name, contact.first_name);
@@ -31,6 +42,13 @@ Contact copy(Contact contact){
3142
return temp;
3243
}
3344

45+
/**
46+
* This method was used in the building process to display all contacts
47+
* in the Contact struct passed
48+
*
49+
* @param contacts contacts array
50+
* @param num number of elements in array
51+
*/
3452
void print_contacts(Contact contacts[], int num){
3553
int i = 0;
3654
while (i < num) {
@@ -46,6 +64,13 @@ void print_contacts(Contact contacts[], int num){
4664
printf("%s\n", "-------------------------------");
4765
}
4866

67+
/**
68+
* This method sorts the Contact array passed in order of last name
69+
* first, then first name if last names are the same
70+
*
71+
* @param contacts contacts array
72+
* @param num number of elements in array passed
73+
*/
4974
void sort(Contact contacts[], int num) {
5075
int EQ = 0;
5176
for (int i = 0; i < num; ++i) {
@@ -66,6 +91,13 @@ void sort(Contact contacts[], int num) {
6691
}
6792
}
6893

94+
/**
95+
* This method reads in file passed in and creates Contact structs that
96+
* eventually go into the contacts array passed
97+
* @param file file to be read from
98+
* @param contacts contacts array to place Contact structs
99+
* @return integer of elements placed into contacts array
100+
*/
69101
int read_file(FILE *file, Contact contacts[]) {
70102
char str[NUM];
71103
int i = 0;
@@ -112,6 +144,12 @@ int read_file(FILE *file, Contact contacts[]) {
112144
return i;
113145
}
114146

147+
/**
148+
* This method writes the sorted contacts array passed in to the file passed in
149+
* @param output file to be written to
150+
* @param contacts array of Contact structs
151+
* @param num number of elements in contacts array
152+
*/
115153
void write_to_file(FILE *output, Contact contacts[], int num){
116154
for (int i = 0; i < num; ++i) {
117155
fprintf(output, "%s, ", contacts[i].first_name);
@@ -124,20 +162,22 @@ void write_to_file(FILE *output, Contact contacts[], int num){
124162
}
125163
}
126164

165+
/**
166+
* Main method that runs the file
167+
* @param argc number of how many arguments were passed
168+
* @param argv array of command line arguments passed
169+
* @return 0
170+
*/
127171
int main(int argc, char *argv[]){
128172
FILE *file;
129173
FILE *output;
130174
Contact contacts[NAME];
131175
file = fopen(argv[1], "r");
132176
int num = read_file(file, contacts);
133-
//printf("%s\n", "\n[BEFORE SORT]\n-------------------------------");
134-
//print_contacts(contacts, num);
135177
sort(contacts, num);
136-
//printf("%s\n", "\n[AFTER SORT]\n-------------------------------");
137-
//print_contacts(contacts, num);
138178
fclose(file);
139-
output = fopen(argv[2], "w+");
179+
output = fopen(argv[2], "w");
140180
write_to_file(output, contacts, num);
141-
181+
fclose(output);
142182
return (0);
143-
}
183+
}

struct_sort.h

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
1+
/**
2+
* @author Brett Dale
3+
* @version 1.0 (2/28/2020)
4+
*
5+
* This file is the header file for the C source file struct_sort.c
6+
*/
7+
18
#include <stdio.h>
29
#include <string.h>
3-
#define NUM 100
4-
#define STREET 30
5-
#define NAME 20
6-
#define STATE 3
7-
#define ZIP 6
8-
#define TELEPHONE 13
9-
10+
#include <ctype.h>
11+
#define NUM 100 /** Constant for reading file in */
12+
#define STREET 30 /** Constant for Street and City char arrays */
13+
#define NAME 20 /** Constant for First and Last name char arrays */
14+
#define STATE 3 /** Constant for State */
15+
#define ZIP 6 /** Constant for Zip code */
16+
#define TELEPHONE 13 /** Constant for Telephone */
1017

1118
typedef struct {
12-
char street[STREET];
13-
char city[STREET];
14-
char state[STATE];
15-
char zip[ZIP];
19+
char street[STREET]; /** Field for street name */
20+
char city[STREET]; /** Field for city name */
21+
char state[STATE]; /** Field for state acronym */
22+
char zip[ZIP]; /** Field for Zip code */
1623
}Address;
1724

1825
typedef struct {
19-
char first_name[NAME];
20-
char last_name[NAME];
21-
Address address;
22-
char telephone[TELEPHONE];
26+
char first_name[NAME]; /** Field for first name */
27+
char last_name[NAME]; /** Field for last name */
28+
Address address; /** Field for address struct */
29+
char telephone[TELEPHONE]; /** Field for telephone number */
2330
}Contact;

0 commit comments

Comments
 (0)