Advertisement
Guest User

Week 17 <3 Owen

a guest
Feb 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.42 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[1000];
  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.     fgets(lineRead, 300, fptr); //ignore first line
  76.     while (!feof(fptr)){
  77.         if (fgets(lineRead, 300, fptr) == NULL) continue;
  78.         stringToken();
  79.         i++;
  80.     }
  81. }
  82.  
  83. void lowerCaseName(){
  84.     for(int z=0; z<i; z++){
  85.         int g=0;
  86.         while(info[z].firstName[g]!='\0'){
  87.             info[z].firstNameLc[g]=(char)tolower(info[z].firstName[g]);
  88.             g++;
  89.         }
  90.         g=0;
  91.         while(info[z].lastName[g]!='\0'){
  92.             info[z].lastNameLc[g]=(char)tolower(info[z].lastName[g]);
  93.             g++;
  94.         }
  95.     }
  96. }
  97.  
  98. void searchName(){
  99.     int match=0;
  100.     int result[1000];
  101.     for(int z=0; z<i; z++) {
  102.         char *res=strstr(info[z].firstNameLc, input);
  103.         if(res!=0){
  104.             result[match] = z;
  105.             match++;
  106.         }
  107.         char *res2=strstr(info[z].lastNameLc, input);
  108.         if(res2!=0&&res!=res2){
  109.             result[match] = z;
  110.             match++;
  111.         }
  112.     }
  113.     for (int g=0; g<match; g++)
  114.     {
  115.         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",
  116.                info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
  117.                info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
  118.                info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
  119.     }
  120. }
  121.  
  122. void lowerCaseCompany(){
  123.     for(int z=0; z<i; z++){
  124.         int g=0;
  125.         while(info[z].companyName[g]!='\0'){
  126.             info[z].companyNameLc[g]=(char)tolower(info[z].companyName[g]);
  127.             g++;
  128.         }
  129.     }
  130. }
  131.  
  132. void searchCompany(){
  133.     int match=0;
  134.     int result[1000];
  135.     for(int z=0; z<i; z++) {
  136.         char *res=strstr(info[z].companyNameLc, input);
  137.         if(res!=0){
  138.             result[match] = z;
  139.             match++;
  140.         }
  141.     }
  142.     for (int g=0; g<match; g++)
  143.     {
  144.         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",
  145.                info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
  146.                info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
  147.                info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
  148.     }
  149. }
  150.  
  151. void lowerCaseCity(){
  152.     for(int z=0; z<i; z++){
  153.         int g=0;
  154.         while(info[z].city[g]!='\0'){
  155.             info[z].cityLc[g]=(char)tolower(info[z].city[g]);
  156.             g++;
  157.         }
  158.     }
  159. }
  160.  
  161. void searchCity(){
  162.     int match=0;
  163.     int result[1000];
  164.     for(int z=0; z<i; z++) {
  165.         char *res=strstr(info[z].cityLc, input);
  166.         if(res!=0){
  167.             result[match] = z;
  168.             match++;
  169.         }
  170.     }
  171.     for (int g=0; g<match; g++)
  172.     {
  173.         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",
  174.                info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
  175.                info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
  176.                info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
  177.     }
  178. }
  179.  
  180. void lowerCaseCounty(){
  181.     for(int z=0; z<i; z++){
  182.         int g=0;
  183.         while(info[z].county[g]!='\0'){
  184.             info[z].countyLc[g]=(char)tolower(info[z].county[g]);
  185.             g++;
  186.         }
  187.     }
  188. }
  189.  
  190. void searchCounty(){
  191.     int match=0;
  192.     int result[1000];
  193.     for(int z=0; z<i; z++) {
  194.         char *res=strstr(info[z].countyLc, input);
  195.         if(res!=0){
  196.             result[match] = z;
  197.             match++;
  198.         }
  199.     }
  200.     for (int g=0; g<match; g++)
  201.     {
  202.         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",
  203.                info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
  204.                info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
  205.                info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
  206.     }
  207. }
  208.  
  209. void lowerCaseState(){
  210.     for(int z=0; z<i; z++){
  211.         int g=0;
  212.         while(info[z].state[g]!='\0'){
  213.             info[z].stateLc[g]=(char)tolower(info[z].state[g]);
  214.             g++;
  215.         }
  216.     }
  217. }
  218.  
  219. void searchState(){
  220.     int match=0;
  221.     int result[1000];
  222.     for(int z=0; z<i; z++) {
  223.         char *res=strstr(info[z].stateLc, input);
  224.         if(res!=0){
  225.             result[match] = z;
  226.             match++;
  227.         }
  228.     }
  229.     for (int g=0; g<match; g++)
  230.     {
  231.         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",
  232.                info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
  233.                info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
  234.                info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
  235.     }
  236. }
  237.  
  238. void searchZIP(){
  239.     int match=0;
  240.     int result[1000];
  241.     for(int z=0; z<i; z++) {
  242.         char *res=strstr(info[z].zip,input);
  243.         if(res!=0){
  244.             result[match] = z;
  245.             match++;
  246.         }
  247.     }
  248.     for (int g=0; g<match; g++)
  249.     {
  250.         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",
  251.                info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
  252.                info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
  253.                info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
  254.     }
  255. }
  256.  
  257. void inputToLowerCase()
  258. {
  259.     int z = 0;
  260.     while (input[z] != '\0'){
  261.         input[z]=(char)tolower(input[z]);
  262.         z++;
  263.     }
  264. }
  265. int main()
  266. {
  267.     int choice=-1;
  268.     fileOpen();
  269.     fileRead();
  270.     fileClose();
  271.     while(choice!=0){
  272.         printf("\n0. Exit\n1. Clear window\n2. Search by name\n3. Search by company name\n4. Search by city\n5. Search by county\n6. Search by state\n7. 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.                 system("cls");
  282.                 break;
  283.  
  284.             case 2:
  285.                 printf("Enter all <or part> of the first name or surname you are looking for:\n");
  286.                 gets(input);
  287.                 inputToLowerCase();
  288.                 lowerCaseName();
  289.                 searchName();
  290.                 break;
  291.  
  292.             case 3:
  293.                 printf("Enter all <or part> of the company name you are looking for:\n");
  294.                 gets(input);
  295.                 inputToLowerCase();
  296.                 lowerCaseCompany();
  297.                 searchCompany();
  298.                 break;
  299.  
  300.             case 4:
  301.                 printf("Enter all <or part> of the city name you are looking for:\n");
  302.                 gets(input);
  303.                 inputToLowerCase();
  304.                 lowerCaseCity();
  305.                 searchCity();
  306.                 break;
  307.  
  308.             case 5:
  309.                 printf("Enter all <or part> of the county name you are looking for: \n");
  310.                 gets(input);
  311.                 inputToLowerCase();
  312.                 lowerCaseCounty();
  313.                 searchCounty();
  314.                 break;
  315.  
  316.             case 6:
  317.                 printf("Enter all <or part> of the state name you are looking for: \n");
  318.                 gets(input);
  319.                 inputToLowerCase();
  320.                 lowerCaseState();
  321.                 searchState();
  322.                 break;
  323.  
  324.             case 7:
  325.                 printf("Enter the all <or part> of the ZIP code you are looking for: \n");
  326.                 gets(input);
  327.                 searchZIP();
  328.                 break;
  329.  
  330.             default:
  331.                 printf("\nIncorrect input! please try again!");
  332.                 break;
  333.         }
  334.     }
  335. }
  336.  
  337. //by Jaroslaw Janas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement