forked from kuroneko995/cosc301_proj01
/
main.c
182 lines (160 loc) · 4.13 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
*
* Parker Reynolds
* 28 September 2014
* Worked wtih: Courtney McGill
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "list.h"
void process_data(FILE *input_file) {
// !! your code should start here. the input_file parameter
// is an already-open file. you can read data from it using
// the fgets() C library function. close it with the fclose()
// built-in function
struct rusage usage;
struct node *list = malloc(sizeof(struct node*));
list = list->next;
char filestream[2000000];
if (input_file == stdin){
printf("Input a list of numbers \n");
while(fgets(filestream, 2000000, stdin)!= NULL){
int slen = strlen(filestream);
filestream[slen-1] = '\0';
char *token = strtok(filestream, " \t\n");
if(token!=NULL){
int counter = 0;
for(int i=0; i<strlen(token); i++){
if(i==0 && (isdigit(token[i]) || token[i]=='-')){
counter++;
}
else if(isdigit(token[i])){
counter++;
}
}
if (counter == strlen(token)){
int num = atoi(token);
list_append(num, &list);
}
}
while(token!=NULL){
token = strtok(NULL," \t\n");
if (token!=NULL){
int counter = 0;
for(int i=0; i<strlen(token); i++){
if(i==0 && (isdigit(token[i]) || token[i]=='-')){
counter++;
}
else if(isdigit(token[i])){
counter++;
}
}
if (counter == strlen(token)){
int num = atoi(token);
list_append(num, &list);
}
}
}
}
fflush(stdout);
}
else{
while(fgets(filestream, 2000000, input_file)!=NULL){
int slen = strlen(filestream);
filestream[slen-1] = '\0';
char *token = strtok(filestream," \t\n");
if(token!=NULL){
int counter = 0;
for(int i=0; i<strlen(token); i++){
if(i==0 && (isdigit(token[i]) || token[i]=='-')){
counter++;
}
else if(isdigit(token[i])){
counter++;
}
}
if (counter == strlen(token)){
int num = atoi(token);
list_append(num, &list);
}
if (token[0] == '#'){
token = strtok(NULL, "\n");
}
}
while(token!=NULL){
token = strtok(NULL," \t\n");
if (token!=NULL){
int counter = 0;
for(int i=0; i<strlen(token); i++){
if(i==0 && (isdigit(token[i]) || token[i]=='-')){
counter++;
}
if(isdigit(token[i])){
counter++;
}
}
if (counter == strlen(token)){
int num = atoi(token);
list_append(num, &list);
}
if (token[0] == '#'){
token = strtok(NULL, "\n");
}
}
}
}
fflush(stdout);
}
list_sort(&list);
list_print(list);
list_clear(list);
getrusage(RUSAGE_SELF, &usage);
if (getrusage(RUSAGE_SELF, &usage) == -1){
fprintf(stderr, "Error with getrusage");
exit(1);
}
printf("User time: %ld.%09ld\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec);
printf("System time: %ld.%09ld\n", usage.ru_stime.tv_sec, usage.ru_utime.tv_usec);
}
void usage(char *program) {
fprintf(stderr, "usage: %s [<datafile>]\n", program);
exit(1);
}
#ifndef AUTOTEST
int main(int argc, char **argv) {
FILE *datafile = NULL;
/* find out how we got invoked and deal with it */
switch (argc) {
case 1:
/* only one program argument (the program name) */
/* just equate stdin with our datafile */
datafile = stdin;
break;
case 2:
/* two arguments: program name and input file */
/* open the file, assign to datafile */
datafile = fopen(argv[1], "r");
if (datafile == NULL) {
printf("Unable to open file %s: %s\n", argv[1], strerror(errno));
exit(-1);
}
break;
default:
/* more than two arguments? throw hands up in resignation */
usage(argv[0]);
}
/*
* you should be able to just read from datafile regardless
* whether it's stdin or a "real" file.
*/
process_data(datafile);
fclose(datafile);
return 0;
}
#endif