Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* City Ranking Viewer:
- */
- #include<stdio.h>
- #define SWAP 1
- #define NO_SWAP 0
- typedef struct {
- int Ranking;
- char CityName[25];
- char State[15];
- int Population;
- float PercentChange;
- } city_t;
- void swap(city_t *x, city_t *y);
- void bubblesort(city_t *x, int len, int sorttype);
- void printcities(city_t *x, int len);
- int main(void)
- {
- char fileName[50];
- city_t cities[10];
- int choice, i;
- printf("Welcome to Cities Database Viewer v0.1!\n");
- printf("Please enter the filename you wish to open:");
- scanf("%s", fileName);
- FILE *inp = fopen(fileName, "r");
- // while(1)
- // {
- if(inp==NULL)
- {
- printf("File does not exist! Program will terminate");
- return(0);
- }
- else
- {
- while(1)
- {
- if(feof(inp))
- break;
- else
- {
- i=0;
- fscanf(inp, "%d%s%s%d%f", &cities[i].Ranking, cities[i].CityName, cities[i].State, &cities[i].Population, &cities[i].PercentChange);
- i++;
- }
- }
- }
- // }
- printf("Program uploaded successfully!");
- while(1)
- {
- printf("How do you wish to view the cities?\n1:Ranking\n2:City Name (A-z)\n3:State(A-Z)\n4:Population\n5:Percent Change\n6:Quit\n");
- fflush(stdin);
- scanf("%d", &choice);
- if(choice<1 || choice>7)
- printf("Invalid selection!\n");
- else if(choice>0 && choice<6)
- {
- // bubblesort(cities, 10, choice);
- printcities(cities, 10);
- }
- else
- break;
- }
- return(0);
- }
- void swap(city_t *x, city_t *y)
- {
- city_t temp;
- temp = *x;
- *x = *y;
- *y=temp;
- }
- void bubblesort(city_t *x, int len, int sorttype)
- {
- int flag=SWAP;
- city_t *i;
- while(flag==SWAP)
- {
- flag=NO_SWAP;
- for(i=x; i<x+len-1; i++)
- {
- if(sorttype==1)
- {
- if(i->Ranking > (i+1)->Ranking)
- {
- swap(i, i+1);
- flag=SWAP;
- }
- }
- if(sorttype==2)
- {
- if(i->CityName > (i+1)->CityName)
- {
- swap(i, i+1);
- flag=SWAP;
- }
- }
- if(sorttype==3)
- {
- if(i->State > (i+1)->State)
- {
- swap(i, i+1);
- flag=SWAP;
- }
- }
- if(sorttype==4)
- {
- if(i->Population > (i+1)->Population)
- {
- swap(i, i+1);
- flag=SWAP;
- }
- }
- if(sorttype==5)
- {
- if(i->PercentChange > (i+1)->PercentChange)
- {
- swap(i, i+1);
- flag=SWAP;
- }
- }
- }
- }
- }
- void printcities(city_t *x, int len)
- {
- int i;
- for(i=0; i<len; i++)
- {
- printf("%d %s %s %d %2f\n", x[i].Ranking, x[i].CityName, x[i].State, x[i].Population, x[i].PercentChange);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement