View difference between Paste ID: 7b3JDjPB and H0SQ470b
SHOW: | | - or go back to the newest paste.
1
/* 
2
Aaron McRuer
3
Lab F
4
Labcode:
5
Lab 10
6
4-17-12
7
*/
8
9
#include<stdio.h>
10
#include<stdlib.h>
11
/*maximum number of emperors listed in a single file*/
12
#define MAX 15
13
/*maximum length of the emperor's name*/
14
#define MAX_LENGTH 30
15
16
/*define structure to store emperors information*/
17
typedef struct data{
18
	char name[MAX_LENGTH];
19
	int birth;
20
	int death;
21
	int reign;
22
	} DATA; 
23
24
/*create two global file pointers for input and output files*/
25
FILE *in, *out;
26
27
/*create global array to have access from any function*/
28
DATA emperors[MAX];
29
	
30
/*prototypes*/
31
int openFiles(char* input, char* output);
32
int readContent();
33
int emperor_to_live_longest (int size);
34
int youngest_emperor_to_die (int size);
35
int longest_lifetime_reign (int size);
36
int emperor_to_rule_longest(int size);
37
void writeFile(int oldest, int youngest, int longest, int successful, int size);
38
void closeFiles();
39
40
int main(int argc, char* argv[])
41
{
42
	char* input;
43
	char* output;
44
	input = argv[1];
45
	output = argv[2];
46
	if (argc !=3) 
47
	{
48
		printf("Not enough args. Usage: a.out <input> <output>\n");
49
		return -1;
50
	}
51
	/*create emperors with default info*/
52
	DATA emperors = {"", 0, 0, 0};
53
	openFiles( input,  output);
54
	int size = readContent();
55
	int oldest = emperor_to_live_longest(size);
56
	int youngest = youngest_emperor_to_die(size);
57
	int longest = longest_lifetime_reign(size);
58
	int successful= emperor_to_rule_longest(size);
59
	writeFile( oldest,  youngest,  longest,  successful,  size);
60
	closeFiles();
61
	return 0;	
62
}
63
64
/* this function should take argv[1] and argv[2] as input and output file names.
65
 Return -1 if files couldn't be opened. Open input in read mode and output in write mode*/
66
int openFiles(char* input, char* output)
67
{
68
	
69
	/*fopen opens the file; exits if file cannot be opened*/
70
	if((((in = fopen(input, "r")) == NULL) || ((out = fopen(output, "w")) == NULL)))
71
	{
72
		printf("File could not be opened.\n");
73
		return -1;
74
	} /*end if*/
75
	
76
	else
77
	{
78
		return 0;
79
	}
80
}
81
82
/* this function reads whole file and returns the number of emperors listed in the file. This number is stored in the first line of the .txt file.*/
83
int readContent()
84
{
85
	int i = 0;
86
	int number_of_emperors;
87
	fscanf(in, "%d", &number_of_emperors);
88
	for(i = 0; i < number_of_emperors; i++)
89
	{
90
		fscanf(in, "%s", emperors[i].name);
91
		fscanf(in, "%d", &emperors[i].birth);
92
		fscanf(in, "%d", &emperors[i].death);
93
		fscanf(in, "%d", &emperors[i].reign);
94
	}
95
	
96
	return number_of_emperors; 
97
}
98
99
/* this function scans through the list of emperors, and finds the oldest emperor to die. Return the index of the array (NOT the age) back to 
100
main function.*/
101
int emperor_to_live_longest(int size)
102
{
103
	int n; /*counter*/
104
	int max; 
105
	int index;
106
	int age = (emperors[n].death - emperors[n].birth);
107
	max = age;
108
	
109
	for ( n = 0 ; n < size ; n++ )
110
	{
111
		int age = (emperors[n].death - emperors[n].birth);
112
		
113
		if ( max < age )     
114
		{
115
			max = age;
116
			index = n;
117
		}
118
	}
119
	
120
	return index;
121
}
122
123
/* this function scans the array and finds the youngest emperor to die. Similarly to the previous function, return the index, do not return 
124
the age of the emperor.*/
125
int youngest_emperor_to_die(int size)
126
{
127
	int n;
128
	int min_age;
129
	int index;
130
	int age = (emperors[n].death - emperors[n].birth);
131
	min_age = age;
132
	
133
	for (n = 0; n < size; n++)
134
	{
135
		int age = (emperors[n].death - emperors[n].birth);
136
		if (min_age > age)
137
		{
138
			min_age = age;
139
			index = n;
140
		}
141
	}
142
	
143
	return index;
144
}
145
146
/* scans the list, find the emperor that spent most of his lifetime in reign (hint: age- years of reign is the smallest). Return the index in the
147
array. With the index you will be able to extract name and age as well.*/
148
int longest_lifetime_reign(int size)
149
{
150-
	int m;
150+
151
	int max;
152
	int index;
153
	int age = (emperors[n].death - emperors[n].birth);
154
	max = age;
155
	
156
	for (n = 0; n < size; n++)
157
	{
158
		int age = (emperors[n].death - emperors[n].birth);
159
		int longest = (age - emperors[n].reign);
160
		if (max > longest)
161
		{
162
			longest = max;
163
			index = n;
164
		}
165
	}
166
	
167
	return index;
168
}
169
170
/*Emperor who spent most of years in reign. As usual, only return index to main function.*/
171
int emperor_to_rule_longest (int size)
172
{
173
	int n;
174
	int max;
175
	int mage;
176
	float age = (emperors[n].death - emperors[n].birth);
177
	float most_of_life_in_rule = (age / emperors[n].reign);
178
	
179
	for (n = 0; n > size; n++)
180
	{
181
		age = (emperors[n].death - emperors[n].birth);
182
		most_of_life_in_rule = (age / emperors[n].reign);
183
		
184
		if (max > most_of_life_in_rule)
185
		{
186
			most_of_life_in_rule = max;
187
			mage = n;
188
		}
189
	}
190
	
191
	return mage;
192
}
193
194
/*Once you calculate all the numbers, print the list of emperors into the output file, and lastly the summary required by the history department. 
195
It is worth noting the first four arguments, passed to the funciton are the INDEXES, last argument is the size of the array. Example output will give
196
you better understanding on how to format the output file.*/
197
void writeFile(int oldest, int youngest, int longest, int successful, int size)
198
{
199
	int i;
200
	
201
	for(i = 0; i < size; i++)
202
	{
203
		fprintf(out,"%s Birth: %d, Death: %d, Reign: %d \n", emperors[i].name, emperors[i].birth, emperors[i].death, emperors[i].reign);
204
	}
205
	
206
	fprintf(out, "---------------\n");
207
	fprintf(out, "Oldest emperor to die: %s, Age: %d\n", emperors[oldest].name, (emperors[oldest].death-emperors[oldest].birth));
208
	fprintf(out, "Youngest emperor to die: %s, Age: %d\n", emperors[youngest].name, (emperors[youngest].death-emperors[youngest].birth));
209
	fprintf(out, "Emperor to rule longest: %s, %d years.\n",emperors[longest].name, emperors[longest].reign);
210
	fprintf(out, "Emperor to spend most of lifetime in reign: %s, %d years in reign. Died at age of %d.", emperors[successful].name, emperors[successful].reign, (emperors[successful].death- emperors[successful].birth));
211
	
212
}
213
void closeFiles()
214
{
215
	fclose(in);
216
	fclose(out);
217
}