Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.38 KB | None | 0 0
  1. /* City Ranking Viewer:
  2. */
  3.  
  4. #include<stdio.h>
  5.  
  6. #define SWAP 1
  7. #define NO_SWAP 0
  8.  
  9.  
  10. typedef struct {
  11.     int Ranking;
  12.     char CityName[25];
  13.     char State[15];
  14.     int Population;
  15.     float PercentChange;
  16. } city_t;
  17.  
  18. void swap(city_t *x, city_t *y);
  19. void bubblesort(city_t *x, int len, int sorttype);
  20. void printcities(city_t *x, int len);
  21.  
  22.  
  23. int main(void)
  24. {
  25.    
  26.     char fileName[50];
  27.     city_t cities[10];
  28.     int choice, i;
  29.    
  30.        
  31.    
  32.     printf("Welcome to Cities Database Viewer v0.1!\n");
  33.     printf("Please enter the filename you wish to open:");
  34.     scanf("%s", fileName);
  35.    
  36.     FILE *inp = fopen(fileName, "r");
  37.  
  38.  
  39.    
  40. //  while(1)
  41. //  {
  42.         if(inp==NULL)
  43.         {
  44.             printf("File does not exist! Program will terminate");
  45.             return(0);
  46.         }
  47.            
  48.         else
  49.         {
  50.            
  51.             while(1)
  52.             {
  53.                 if(feof(inp))
  54.                     break;
  55.                
  56.                 else
  57.                 {
  58.                     i=0;
  59.                     fscanf(inp, "%d%s%s%d%f", &cities[i].Ranking, cities[i].CityName, cities[i].State, &cities[i].Population, &cities[i].PercentChange);
  60.                     i++;
  61.                 }
  62.             }
  63.         }
  64. //  }
  65.    
  66.     printf("Program uploaded successfully!");
  67.    
  68.     while(1)
  69.     {
  70.         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");
  71.         fflush(stdin);
  72.         scanf("%d", &choice);
  73.  
  74.         if(choice<1 || choice>7)
  75.             printf("Invalid selection!\n");
  76.            
  77.                
  78.         else if(choice>0 && choice<6)
  79.         {
  80. //          bubblesort(cities, 10, choice);
  81.             printcities(cities, 10);
  82.         }
  83.            
  84.         else
  85.             break;
  86.                
  87.     }
  88.        
  89.     return(0);
  90.    
  91. }
  92.  
  93. void swap(city_t *x, city_t *y)
  94. {
  95.     city_t temp;
  96.     temp = *x;
  97.     *x = *y;
  98.     *y=temp;
  99. }
  100.  
  101. void bubblesort(city_t *x, int len, int sorttype)
  102. {
  103.     int flag=SWAP;
  104.     city_t *i;
  105.    
  106.     while(flag==SWAP)
  107.     {
  108.         flag=NO_SWAP;
  109.         for(i=x; i<x+len-1; i++)
  110.         {
  111.             if(sorttype==1)
  112.             {
  113.                 if(i->Ranking > (i+1)->Ranking)
  114.                 {
  115.                     swap(i, i+1);
  116.                     flag=SWAP;
  117.                 }
  118.             }
  119.  
  120.             if(sorttype==2)
  121.             {
  122.                 if(i->CityName > (i+1)->CityName)
  123.                 {
  124.                     swap(i, i+1);
  125.                     flag=SWAP;
  126.                 }
  127.             }
  128.             if(sorttype==3)
  129.             {
  130.                 if(i->State > (i+1)->State)
  131.                 {
  132.                     swap(i, i+1);
  133.                     flag=SWAP;
  134.                 }
  135.             }
  136.             if(sorttype==4)
  137.             {
  138.                 if(i->Population > (i+1)->Population)
  139.                 {
  140.                     swap(i, i+1);
  141.                     flag=SWAP;
  142.                 }
  143.             }
  144.             if(sorttype==5)
  145.             {
  146.                 if(i->PercentChange > (i+1)->PercentChange)
  147.                 {
  148.                     swap(i, i+1);
  149.                     flag=SWAP;
  150.                 }
  151.             }
  152.            
  153.         }
  154.     }
  155. }
  156.  
  157.  
  158. void printcities(city_t *x, int len)
  159. {
  160.     int i;
  161.     for(i=0; i<len; i++)
  162.     {
  163.         printf("%d %s %s %d %2f\n", x[i].Ranking, x[i].CityName, x[i].State, x[i].Population, x[i].PercentChange);
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement