Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <mem.h>
  4. #include <ctype.h>
  5.  
  6. struct data{
  7.     char firstName[30];
  8.     char firstNameLc[30];
  9.     char lastName[30];
  10.     char lastNameLc[30];
  11.     char companyName[40];
  12.     char companyNameLc[40];
  13.     char address[30];
  14.     char city[30];
  15.     char cityLc[30];
  16.     char county[30];
  17.     char countyLc[30];
  18.     char state[30];
  19.     char stateLc[30];
  20.     char zip[30];
  21.     char phone1[30];
  22.     char phone2[30];
  23.     char email[50];
  24.     char web[50];
  25. };
  26. struct data info[900];
  27. int i=0;
  28. char lineRead [300];
  29. char input[50];
  30. FILE *fptr;
  31.  
  32. void fileOpen(){
  33.     fptr = fopen("us-500b.txt", "r");
  34.     if (fptr == NULL) {
  35.         printf("Error opening file ! \nWe couldn't find us-500b.txt Program will close now.\nSorry :-(");
  36.         exit(0);
  37.     }
  38. }
  39.  
  40. void fileClose(){
  41.     fclose(fptr);
  42. }
  43.  
  44. void stringToken(){
  45.     char *word=strtok(lineRead,",");
  46.     if(*word=='\n') return;
  47.     if(lineRead!=NULL){
  48.         strcpy(info[i].firstName,word);
  49.         word=strtok(NULL,",");
  50.         strcpy(info[i].lastName, word);
  51.         word=strtok(NULL,",");
  52.         strcpy(info[i].companyName, word);
  53.         word=strtok(NULL,",");
  54.         strcpy(info[i].address, word);
  55.         word=strtok(NULL,",");
  56.         strcpy(info[i].city, word);
  57.         word=strtok(NULL,",");
  58.         strcpy(info[i].county, word);
  59.         word=strtok(NULL,",");
  60.         strcpy(info[i].state, word);
  61.         word=strtok(NULL,",");
  62.         strcpy(info[i].zip, word);
  63.         word=strtok(NULL,",");
  64.         strcpy(info[i].phone1, word);
  65.         word=strtok(NULL,",");
  66.         strcpy(info[i].phone2, word);
  67.         word=strtok(NULL,",");
  68.         strcpy(info[i].email, word);
  69.         word=strtok(NULL,",");
  70.         strcpy(info[i].web, word);
  71.     }
  72. }
  73.  
  74. void fileRead() {
  75.     while (!feof(fptr)){
  76.         fgets(lineRead, 300, fptr);
  77.         stringToken();
  78.         i++;
  79.     }
  80. }
  81.  
  82. void lowerCaseName(){
  83.     for(int z=0; z<i; z++){
  84.         int g=0;
  85.         while(info[z].firstName[g]!='\0'){
  86.             info[z].firstNameLc[g]=(char)tolower(info[z].firstName[g]);
  87.             g++;
  88.         }
  89.         g=0;
  90.         while(info[z].lastName[g]!='\0'){
  91.             info[z].lastNameLc[g]=(char)tolower(info[z].lastName[g]);
  92.             g++;
  93.         }
  94.     }
  95. }
  96.  
  97. void searchName(){
  98.     int match=0;
  99.     int result[100];
  100.     for(int z=0; z<i; z++) {
  101.         char *res=strstr(info[z].firstNameLc, input);
  102.         if(res!=0){
  103.             result[match] = z;
  104.             match++;
  105.         }
  106.         char *res2=strstr(info[z].lastNameLc, input);
  107.         if(res2!=0&&res!=res2){
  108.             result[match] = z;
  109.             match++;
  110.         }
  111.     }
  112.     for (int g=0; g<match; g++)
  113.     {
  114.         printf("\n\n FirstName: %s\n Last Name: %s\n Company Name: %s\n Address: %s\n City: %s\n County: %s\n State: %s\n Zip: %s\n Phone1: %s\n Phonne2: %s\n Email: %s\n Web: %s",
  115.                info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
  116.                info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
  117.                info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
  118.     }
  119. }
  120.  
  121. void lowerCaseCompany(){
  122.     for(int z=0; z<i; z++){
  123.         int g=0;
  124.         while(info[z].companyName[g]!='\0'){
  125.             info[z].companyNameLc[g]=(char)tolower(info[z].companyName[g]);
  126.             g++;
  127.         }
  128.     }
  129. }
  130.  
  131. void searchCompany(){
  132.     int match=0;
  133.     int result[100];
  134.     for(int z=0; z<i; z++) {
  135.         char *res=strstr(info[z].companyNameLc, input);
  136.         if(res!=0){
  137.             result[match] = z;
  138.             match++;
  139.         }
  140.     }
  141.     for (int g=0; g<match; g++)
  142.     {
  143.         printf("\n\n FirstName: %s\n Last Name: %s\n Company Name: %s\n Address: %s\n City: %s\n County: %s\n State: %s\n Zip: %s\n Phone1: %s\n Phonne2: %s\n Email: %s\n Web: %s",
  144.                info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
  145.                info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
  146.                info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
  147.     }
  148. }
  149.  
  150. void lowerCaseCity(){
  151.     for(int z=0; z<i; z++){
  152.         int g=0;
  153.         while(info[z].city[g]!='\0'){
  154.             info[z].cityLc[g]=(char)tolower(info[z].city[g]);
  155.             g++;
  156.         }
  157.     }
  158. }
  159.  
  160. void searchCity(){
  161.     int match=0;
  162.     int result[100];
  163.     for(int z=0; z<i; z++) {
  164.         char *res=strstr(info[z].cityLc, input);
  165.         if(res!=0){
  166.             result[match] = z;
  167.             match++;
  168.         }
  169.     }
  170.     for (int g=0; g<match; g++)
  171.     {
  172.         printf("\n\n FirstName: %s\n Last Name: %s\n Company Name: %s\n Address: %s\n City: %s\n County: %s\n State: %s\n Zip: %s\n Phone1: %s\n Phonne2: %s\n Email: %s\n Web: %s",
  173.                info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
  174.                info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
  175.                info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
  176.     }
  177. }
  178.  
  179. void lowerCaseCounty(){
  180.     for(int z=0; z<i; z++){
  181.         int g=0;
  182.         while(info[z].county[g]!='\0'){
  183.             info[z].countyLc[g]=(char)tolower(info[z].county[g]);
  184.             g++;
  185.         }
  186.     }
  187. }
  188.  
  189. void searchCounty(){
  190.     int match=0;
  191.     int result[100];
  192.     for(int z=0; z<i; z++) {
  193.         char *res=strstr(info[z].countyLc, input);
  194.         if(res!=0){
  195.             result[match] = z;
  196.             match++;
  197.         }
  198.     }
  199.     for (int g=0; g<match; g++)
  200.     {
  201.         printf("\n\n FirstName: %s\n Last Name: %s\n Company Name: %s\n Address: %s\n City: %s\n County: %s\n State: %s\n Zip: %s\n Phone1: %s\n Phonne2: %s\n Email: %s\n Web: %s",
  202.                info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
  203.                info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
  204.                info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
  205.     }
  206. }
  207.  
  208. void lowerCaseState(){
  209.     for(int z=0; z<i; z++){
  210.         int g=0;
  211.         while(info[z].state[g]!='\0'){
  212.             info[z].stateLc[g]=(char)tolower(info[z].state[g]);
  213.             g++;
  214.         }
  215.     }
  216. }
  217.  
  218. void searchState(){
  219.     int match=0;
  220.     int result[100];
  221.     for(int z=0; z<i; z++) {
  222.         char *res=strstr(info[z].stateLc, input);
  223.         if(res!=0){
  224.             result[match] = z;
  225.             match++;
  226.         }
  227.     }
  228.     for (int g=0; g<match; g++)
  229.     {
  230.         printf("\n\n FirstName: %s\n Last Name: %s\n Company Name: %s\n Address: %s\n City: %s\n County: %s\n State: %s\n Zip: %s\n Phone1: %s\n Phonne2: %s\n Email: %s\n Web: %s",
  231.                info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
  232.                info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
  233.                info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
  234.     }
  235. }
  236.  
  237. void searchZIP(){
  238.     int match=0;
  239.     int result[100];
  240.     for(int z=0; z<i; z++) {
  241.         char *res=strstr(info[z].zip,input);
  242.         if(res!=0){
  243.             result[match] = z;
  244.             match++;
  245.         }
  246.     }
  247.     for (int g=0; g<match; g++)
  248.     {
  249.         printf("\n\n FirstName: %s\n Last Name: %s\n Company Name: %s\n Address: %s\n City: %s\n County: %s\n State: %s\n Zip: %s\n Phone1: %s\n Phonne2: %s\n Email: %s\n Web: %s",
  250.                info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
  251.                info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
  252.                info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
  253.     }
  254. }
  255.  
  256. void inputToLowerCase()
  257. {
  258.     int z = 0;
  259.     while (input[z] != '\0'){
  260.         input[z]=(char)tolower(input[z]);
  261.         z++;
  262.     }
  263. }
  264. int main()
  265. {
  266.     int choice=-1;
  267.     fileOpen();
  268.     fgets(lineRead, 300, fptr); //ignore first line
  269.     fileRead();
  270.     fileClose();
  271.     while(choice!=0){
  272.         printf("\n0. Exit\n1. Search by name\n2. Search by company name\n3. Search by city\n4. Search by county\n5. Search by state\n6. Search by ZIP\nYour choice: ");
  273.         scanf("%d", &choice);
  274.         getchar();
  275.         switch (choice)
  276.         {
  277.             case 0:
  278.                 break;
  279.  
  280.             case 1:
  281.                 printf("Enter all <or part> of the first name or surname you are looking for:\n");
  282.                 gets(input);
  283.                 inputToLowerCase();
  284.                 lowerCaseName();
  285.                 searchName();
  286.                 break;
  287.  
  288.             case 2:
  289.                 printf("Enter all <or part> of the company name you are looking for:\n");
  290.                 gets(input);
  291.                 inputToLowerCase();
  292.                 lowerCaseCompany();
  293.                 searchCompany();
  294.                 break;
  295.  
  296.             case 3:
  297.                 printf("Enter all <or part> of the city name you are looking for:\n");
  298.                 gets(input);
  299.                 inputToLowerCase();
  300.                 lowerCaseCity();
  301.                 searchCity();
  302.                 break;
  303.  
  304.             case 4:
  305.                 printf("Enter all <or part> of the county name you are looking for: \n");
  306.                 gets(input);
  307.                 inputToLowerCase();
  308.                 lowerCaseCounty();
  309.                 searchCounty();
  310.                 break;
  311.  
  312.             case 5:
  313.                 printf("Enter all <or part> of the state name you are looking for: \n");
  314.                 gets(input);
  315.                 inputToLowerCase();
  316.                 lowerCaseState();
  317.                 searchState();
  318.                 break;
  319.  
  320.             case 6:
  321.                 printf("Enter the full ZIP code you are looking for: \n");
  322.                 gets(input);
  323.                 searchZIP();
  324.                 break;
  325.  
  326.             default:
  327.                 printf("\nIncorrect input! please try again!");
  328.                 break;
  329.         }
  330.     }
  331. }
  332.  
  333. //by Jaroslaw Janas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement