Added all previous files
This commit is contained in:
parent
4466ee8a4c
commit
f04fb2189a
59 changed files with 64921 additions and 0 deletions
87
lab6/path.c
Normal file
87
lab6/path.c
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
// Author: Owen Dorweiler
|
||||
// Class: Fundamentals of Computing
|
||||
// Assignment: Lab 6, Part 1
|
||||
// Due: 20 October 2024
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#define MAX_POINTS 30
|
||||
|
||||
typedef struct {
|
||||
float x;
|
||||
float y;
|
||||
} Point;
|
||||
|
||||
int readPoints(Point points[], char filename[]);
|
||||
float findPathLength(Point points[], int pointCount);
|
||||
float distanceCalc(Point a, Point b);
|
||||
void printResult(Point points[], int pointCount, float pathLength);
|
||||
|
||||
int main() {
|
||||
char filename[31];
|
||||
Point points[MAX_POINTS];
|
||||
int pointCount;
|
||||
float pathLength = 0;
|
||||
|
||||
printf("Enter file name: ");
|
||||
scanf("%99s", filename);
|
||||
|
||||
// Read points, return line count
|
||||
pointCount = readPoints(points, filename);
|
||||
|
||||
// Find path length
|
||||
pathLength = findPathLength(points, pointCount);
|
||||
|
||||
// Print the output
|
||||
printResult(points, pointCount, pathLength);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int readPoints(Point points[], char filename[]) {
|
||||
// Open file, display error message if file does not exist
|
||||
FILE *file = fopen(filename, "r");
|
||||
if (file == NULL) {
|
||||
printf("\nError: File '%s' does not exist. Please restart the program and try again.\n\n", filename);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Read from file and save to struct
|
||||
int line = 0;
|
||||
while (fscanf(file, "%f %f", &points[line].x, &points[line].y) == 2 && line < MAX_POINTS) {
|
||||
line++;
|
||||
}
|
||||
fclose(file);
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
float findPathLength(Point points[], int pointCount) {
|
||||
float length = 0;
|
||||
|
||||
// Add distance between each set of points by calling distanceCalc()
|
||||
for (int i = 0; i < pointCount - 1; i++) {
|
||||
length = length + distanceCalc(points[i], points[i+1]);
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
float distanceCalc(Point a, Point b) {
|
||||
return sqrt(pow((b.x - a.x), 2) + pow((b.y - a.y), 2));
|
||||
}
|
||||
|
||||
void printResult(Point points[], int pointCount, float pathLength) {
|
||||
printf("Number of points: %d\n\n", pointCount);
|
||||
printf(" x | y \n");
|
||||
printf("------+------\n");
|
||||
|
||||
// Print points in the table
|
||||
for (int i = 0; i < pointCount; i++) {
|
||||
printf("%5.1f | %5.1f\n", points[i].x, points[i].y);
|
||||
}
|
||||
|
||||
printf("\nTotal path length is %.2f\n", pathLength);
|
||||
}
|
||||
6
lab6/points.txt
Normal file
6
lab6/points.txt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
3 5
|
||||
4 11.7
|
||||
12 3
|
||||
7.4 -2.9
|
||||
5 -3.2
|
||||
2.5 -2
|
||||
173
lab6/states.c
Normal file
173
lab6/states.c
Normal file
|
|
@ -0,0 +1,173 @@
|
|||
// Author: Owen Dorweiler
|
||||
// Class: Fundamentals of Computing
|
||||
// Assignment: Lab 6, Part 1
|
||||
// Due: 20 October 2024
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define MAX_STATES 100
|
||||
#define MAX_LENGTH 50
|
||||
|
||||
typedef struct {
|
||||
char abbr[3];
|
||||
char name[MAX_LENGTH];
|
||||
char capital[MAX_LENGTH];
|
||||
int year;
|
||||
} States;
|
||||
|
||||
int readStates(States states[], char filename[]);
|
||||
int printMenu();
|
||||
void printData(States states[], int stateCount);
|
||||
void abbrFind(States states[], int stateCount);
|
||||
void foundedRange(States states[], int stateCount);
|
||||
void longerThan(States states[], int stateCount);
|
||||
|
||||
int main() {
|
||||
char filename[31];
|
||||
States states[MAX_STATES];
|
||||
int stateCount;
|
||||
int choice;
|
||||
|
||||
printf("Enter file name: ");
|
||||
scanf("%99s", filename);
|
||||
|
||||
// Read csv file into struct by calling readStates(), return num of states
|
||||
stateCount = readStates(states, filename);
|
||||
|
||||
choice = printMenu();
|
||||
|
||||
// Loop until user exits, call functions to perform selections
|
||||
while (choice != 5) {
|
||||
printf("\n");
|
||||
switch (choice) {
|
||||
case 1:
|
||||
printData(states, stateCount);
|
||||
break;
|
||||
case 2:
|
||||
abbrFind(states, stateCount);
|
||||
break;
|
||||
case 3:
|
||||
foundedRange(states, stateCount);
|
||||
break;
|
||||
case 4:
|
||||
longerThan(states, stateCount);
|
||||
break;
|
||||
default:
|
||||
printf("Invalid selection. Please try again.\n");
|
||||
}
|
||||
|
||||
choice = printMenu();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int readStates(States states[], char filename[]) {
|
||||
// Read file, if no file present output error message
|
||||
FILE *file = fopen(filename, "r");
|
||||
if (file == NULL) {
|
||||
printf("\nError: File '%s' does not exist. Please exit the program and try again.\n", filename);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Read csv file into struct
|
||||
int state = 0;
|
||||
while (state < MAX_STATES && fscanf(file, "%2[^,],%49[^,],%49[^,],%d\n", states[state].abbr, states[state].name, states[state].capital, &states[state].year) == 4) {
|
||||
state++;
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
int printMenu() {
|
||||
int choice;
|
||||
|
||||
// Print choices
|
||||
printf("\n1) List all data\n");
|
||||
printf("2) Find state info from abbreviation\n");
|
||||
printf("3) List states founded within year range\n");
|
||||
printf("4) List state names at least _ letters long\n");
|
||||
printf("5) Exit\n\n");
|
||||
|
||||
printf("Enter selection: ");
|
||||
scanf("%d", &choice);
|
||||
|
||||
return choice;
|
||||
}
|
||||
|
||||
void printData(States states[], int stateCount) {
|
||||
printf("Abbr. | State | Capital | Year \n");
|
||||
printf("------+--------------------+--------------------+------\n");
|
||||
for (int i = 0; i < stateCount; i++) {
|
||||
printf("%-5s | %-18s | %-18s | %-4d\n", states[i].abbr, states[i].name, states[i].capital, states[i].year);
|
||||
}
|
||||
}
|
||||
|
||||
void abbrFind(States states[], int stateCount) {
|
||||
char abbreviation[3];
|
||||
|
||||
printf(">> Enter abbreviation (uppercase): ");
|
||||
scanf("%3s", &abbreviation);
|
||||
printf("\n");
|
||||
|
||||
// Loop through struct and output state info if if there is a match
|
||||
bool stateFound = false;
|
||||
int state = 0;
|
||||
while (!stateFound && state < stateCount) {
|
||||
if (strcmp(states[state].abbr, abbreviation) == 0) {
|
||||
printf(">> %s is the abbreviation for %s, whose capital is %s and was founded in %d.\n", states[state].abbr, states[state].name, states[state].capital, states[state].year);
|
||||
stateFound = true;
|
||||
}
|
||||
state++;
|
||||
}
|
||||
|
||||
// Print error message if no match
|
||||
if (!stateFound)
|
||||
printf(">> No state has the abbreviation '%s'.", abbreviation);
|
||||
}
|
||||
|
||||
void foundedRange(States states[], int stateCount) {
|
||||
int startYear, endYear;
|
||||
|
||||
printf(">> Enter start year: ");
|
||||
scanf("%d", &startYear);
|
||||
printf(">> Enter end year: ");
|
||||
scanf("%d", &endYear);
|
||||
printf("\n");
|
||||
|
||||
// Loop through struct, output all states within range
|
||||
bool stateFound = false;
|
||||
for (int i = 0; i < stateCount; i++) {
|
||||
if (states[i].year >= startYear && states[i].year <= endYear) {
|
||||
printf(">> %s (%d)\n", states[i].name, states[i].year);
|
||||
stateFound = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!stateFound)
|
||||
printf(">> No state was founded between %d and %d.\n", startYear, endYear);
|
||||
}
|
||||
|
||||
void longerThan(States states[], int stateCount) {
|
||||
int maxLength;
|
||||
|
||||
printf(">> Enter name length: ");
|
||||
scanf("%d", &maxLength);
|
||||
printf("\n");
|
||||
|
||||
// Loop through struct, output all states at least maxLength letters long
|
||||
bool stateFound = false;
|
||||
for (int i = 0; i < stateCount; i++) {
|
||||
if (strlen(states[i].name) >= maxLength) {
|
||||
printf(">> %s\n", states[i].name);
|
||||
stateFound = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!stateFound)
|
||||
printf(">> No state name is at least %d letters long.", maxLength);
|
||||
}
|
||||
|
||||
50
lab6/states.csv
Normal file
50
lab6/states.csv
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
AL,Alabama,Montgomery,1819
|
||||
AK,Alaska,Juneau,1959
|
||||
AZ,Arizona,Phoenix,1912
|
||||
AR,Arkansas,Little Rock,1836
|
||||
CA,California,Sacramento,1850
|
||||
CO,Colorado,Denver,1876
|
||||
CT,Connecticut,Hartford,1788
|
||||
DE,Delaware,Dover,1787
|
||||
FL,Florida,Tallahassee,1845
|
||||
GA,Georgia,Atlanta,1788
|
||||
HI,Hawaii,Honolulu,1959
|
||||
ID,Idaho,Boise,1890
|
||||
IL,Illinois,Springfield,1818
|
||||
IN,Indiana,Indianapolis,1816
|
||||
IA,Iowa,Des Moines,1846
|
||||
KS,Kansas,Topeka,1861
|
||||
KY,Kentucky,Frankfort,1792
|
||||
LA,Louisiana,Baton Rouge,1812
|
||||
ME,Maine,Augusta,1820
|
||||
MD,Maryland,Annapolis,1788
|
||||
MA,Massachusetts,Boston,1788
|
||||
MI,Michigan,Lansing,1837
|
||||
MN,Minnesota,St. Paul,1858
|
||||
MS,Mississippi,Jackson,1817
|
||||
MO,Missouri,Jefferson City,1821
|
||||
MT,Montana,Helena,1889
|
||||
NE,Nebraska,Lincoln,1867
|
||||
NV,Nevada,Carson City,1864
|
||||
NH,New Hampshire,Concord,1788
|
||||
NJ,New Jersey,Trenton,1787
|
||||
NM,New Mexico,Santa Fe,1912
|
||||
NY,New York,Albany,1788
|
||||
NC,North Carolina,Raleigh,1789
|
||||
ND,North Dakota,Bismarck,1889
|
||||
OH,Ohio,Columbus,1803
|
||||
OK,Oklahoma,Oklahoma City,1907
|
||||
OR,Oregon,Salem,1859
|
||||
PA,Pennsylvania,Harrisburg,1787
|
||||
RI,Rhode Island,Providence,1790
|
||||
SC,South Carolina,Columbia,1788
|
||||
SD,South Dakota,Pierre,1889
|
||||
TN,Tennessee,Nashville,1796
|
||||
TX,Texas,Austin,1845
|
||||
UT,Utah,Salt Lake City,1896
|
||||
VT,Vermont,Montpelier,1791
|
||||
VA,Virginia,Richmond,1788
|
||||
WA,Washington,Olympia,1889
|
||||
WV,West Virginia,Charleston,1863
|
||||
WI,Wisconsin,Madison,1848
|
||||
WY,Wyoming,Cheyenne,1890
|
||||
|
Loading…
Reference in a new issue