ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have this whole speal of scripting supposed to print out a series of information and all it ends up printing is what day it is.
The script:
Code:
# include <stdio.h>
# include <math.h>
# include <string.h>
// defines the max size and max number of passes
#define MAX_SIZE 30
#define MAX_PASSES 5
// the structs that are given in the assignment
typedef struct
{
int hour;
int minute;
char ampm[3];
} time_type;
typedef struct
{
char ride_name[MAX_SIZE];
time_type ride_time;
} fastpass_type;
typedef struct
{
char first_name[MAX_SIZE];
char last_name[MAX_SIZE];
fastpass_type passes[MAX_PASSES];
int num_passes;
} customer_type;
//prototype
int checkName (customer_type tmpP,customer_type person);
int checkRide (customer_type tmpP, customer_type person);
int checkTime (fastpass_type tmpPass, customer_type person);
int main()
{
customer_type person [500];
customer_type tmpP ;
int i,j,k;
int n, maxP, amtP=0;
int newP, tmpPass;
int time;
FILE *ifp = fopen ("fastpass.txt", "r");
fscanf ( ifp, "%d" , &n);
// first for loop to read in the number of days
for (i = 0; i < n; i++)
{
printf("UCF FAST PASSES DAY %d\n", i+1);
fscanf (ifp, "%d", &maxP);
// reads in the number of people for the day
for(j = 0; j < maxP; j++)
{
fscanf (ifp, "%s%s%s%d%d%s",
tmpP.first_name,
tmpP.last_name,
tmpP.passes[0].ride_name,
&tmpP.passes[0].ride_time.hour,
&tmpP.passes[0].ride_time.minute,
tmpP.passes[0].ride_time.ampm );
//compares the names
for (k = 0; k < amtP; k++)
{
newP = checkName (tmpP, person[k]);
// the return 0 mean the person is new
if ( newP == 0)
{
break;
}
}
if (k == amtP)
{
amtP++;
strcpy( person[k].first_name, tmpP.first_name);
strcpy( person[k].last_name, tmpP.last_name);
strcpy( person[k].passes[0].ride_name, tmpP.passes[0].ride_name);
strcpy( person[k].passes[0].ride_time.ampm, tmpP.passes[0].ride_time.ampm);
person[k].passes[0].ride_time.hour = tmpP.passes[0].ride_time.hour;
person[k].passes[0].ride_time.minute = tmpP.passes[0].ride_time.minute;
person[k].num_passes = 1;
}
else if (person[k].num_passes == MAX_PASSES)
{
printf("%s%s: Pass denied for ride %s at %d %2d%s.\n", tmpP.first_name, tmpP.last_name, tmpP.passes[i].ride_name,
&tmpP.passes[i].ride_time.hour, &tmpP.passes[i].ride_time.minute, tmpP.passes[i].ride_time.ampm);
}
else
{
tmpPass = checkRide (tmpP, person[k]);
if (tmpPass == 0 )
{
printf("%s%s: Pass denied for ride %s at %d %2d%s.\n", tmpP.first_name, tmpP.last_name, tmpP.passes[i].ride_name,
&tmpP.passes[i].ride_time.hour, &tmpP.passes[i].ride_time.minute, tmpP.passes[i].ride_time.ampm);
}
else
{
printf("%s%s: Pass granted for ride %s at %d %2d%s.\n", tmpP.first_name, tmpP.last_name, tmpP.passes[i].ride_name,
&tmpP.passes[i].ride_time.hour, &tmpP.passes[i].ride_time.minute, tmpP.passes[i].ride_time.ampm);
}
time = checkTime (tmpP.passes[0], person[k]);
if (time == 0 )
{
printf("%s%s: Pass denied for ride %s at %d %2d%s.\n", tmpP.first_name, tmpP.last_name, tmpP.passes[i].ride_name,
&tmpP.passes[i].ride_time.hour, &tmpP.passes[i].ride_time.minute, tmpP.passes[i].ride_time.ampm);
}
else
{
printf("%s%s: Pass granted for ride %s at %d %2d%s.\n", tmpP.first_name, tmpP.last_name, tmpP.passes[i].ride_name,
&tmpP.passes[i].ride_time.hour, &tmpP.passes[i].ride_time.minute, tmpP.passes[i].ride_time.ampm);
}
}
}
}
return 0;
}
//checks the names of the customer
int checkName (customer_type tmpP,customer_type person)
{
int i,j,k,l;
int char_diff;
char first_cus[30],first_temp[30],last_cus[30],last_temp[30];
int fNameLen = strlen(tmpP.first_name);
int lNameLen = strlen(tmpP.last_name);
// takes the name and makes it upper case
for(i = 0; i < strlen(tmpP.first_name); i++)
first_temp[i] = toupper(tmpP.first_name[i]);
for(j = 0; j < strlen(person.first_name); j++)
first_cus [j]= toupper (person.first_name[j]);
for(k = 0; k < strlen(tmpP.first_name); k++)
last_temp[k] = toupper (tmpP.last_name[k]);
for(l = 0; l < strlen(tmpP.first_name); l++)
last_cus[l] = toupper (person.last_name[l]);
if(fNameLen != strlen(person.first_name) || lNameLen != strlen(person.last_name))
return 1;
if(strcmp(first_cus, first_temp) == 0)
{
if(strcmp(last_cus, last_temp) == 0)
return 0;
else
{
for (i=0; i < lNameLen ; i++)
{
if(tmpP.last_name[i] == '\0')
{
if(person.last_name[lNameLen - i] == '\0')
return 0;
else
return 1;
}
if(tmpP.last_name[i] != person.last_name[lNameLen - i])
char_diff++;
if(char_diff > 1)
return 1;
}
}
}
else
{
for (i=0; i < fNameLen ; i++)
{
if(tmpP.first_name[i] == '\0')
{
if(person.first_name[fNameLen - i] == '\0')
return 0;
else
return 1;
}
if(tmpP.first_name[i] != person.first_name[fNameLen - i])
char_diff++;
if(char_diff > 1)
return 1;
}
}
}
int checkRide (customer_type tmpP, customer_type person)
{
//compares of the cutomer already used a fast pass fo a ride
if (strcmp(tmpP.passes[0].ride_name, person.passes[0].ride_name) == 0)
return 0;
else
return 1;
}
int checkTime (fastpass_type tmpPass, customer_type person)
{
int mil, min, mil_t, min_t,i;
// converts hours to miltary time then to minuets and adds the two values for the cutomer that already has fast passes
for ( i =0 ; i < person.num_passes; i++)
{
if (strcmp(person.passes[0].ride_time.ampm, "PM") ==0)
{
mil = person.passes[0].ride_time.hour +12;
min = (mil * 60) + person.passes[0].ride_time.minute;
}
else
{
mil_t = person.passes[0].ride_time.hour;
min_t = (mil * 60) + person.passes[0].ride_time.minute;
}
//converts hours to miltary time then to minuets and adds the two values for the cutomer that wants a new fast pass
if ( strcmp(tmpPass.ride_time.ampm, "PM") ==0)
{
mil_t = person.passes[0].ride_time.hour +12;
min_t = (mil * 60) + person.passes[0].ride_time.minute;
}
else
{
mil_t = person.passes[0].ride_time.hour;
min_t = (mil * 60) + person.passes[0].ride_time.minute;
}
// hopfully subtrac the two vales will gives the the time from the last fast pass
if (( abs (min-min_t)) < 60)
{
return 1;
}
else
{
return 0;
}
}
}
The input file is
Quote:
Sample Input File (fastpass.txt)
2
11
Bill Johnson RollerCoasterOfLove 11 0 AM
Will Johnson CrazyTeaCups 11 55 AM
Llib JoHnSoN Movie3D 12 0 PM
Bill Johnson HeartLand 3 0 PM
Ellen Walker RollerCoasterOfLove 11 0 AM
Bill Johnson Rave 1 1 PM
Bill Johnson CartoonHour 2 0 PM
Bill Johnson RollerCoasterOfLove 4 0 PM
Bill Johnson WhalesAreUs 5 25 PM
Bill NOSNHOJ Fireworks 8 30 PM
Ellen Walker Rave 3 25 PM
1
Rob Bradshaw Movie3D 2 13 PM
And I should wind up with:
Quote:
Sample Output
UCF FAST PASSES DAY 1
Bill Johnson: Pass granted for ride RollerCoasterOfLove at 11:00 AM.
Bill Johnson: Pass denied for ride CrazyTeaCups at 11:55 AM.
Bill Johnson: Pass granted for ride Movie3D at 12:00 PM.
Bill Johnson: Pass granted for ride HeartLand at 3:00 PM.
Ellen Walker: Pass granted for ride RollerCoasterOfLove at 11:00 AM.
Bill Johnson: Pass granted for ride Rave at 1:01 PM.
Bill Johnson: Pass denied for ride CartoonHour at 2:00 PM.
Bill Johnson: Pass denied for ride RollerCoasterOfLove at 4:00 PM.
Bill Johnson: Pass granted for ride WhalesAreUs at 5:25 PM.
Bill Johnson: Pass denied for ride Fireworks at 8:30 PM.
Ellen Walker: Pass granted for ride Rave at 3:25 PM.
UCF FAST PASSES DAY 2
Rob Bradshaw: Pass granted for ride Movie3D at 2:13 PM.
But it only prints the UCF FAST PASSES DAY 1 and 2
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.