Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <mem.h>
- #include <ctype.h>
- struct data{
- char firstName[30];
- char firstNameLc[30];
- char lastName[30];
- char lastNameLc[30];
- char companyName[40];
- char companyNameLc[40];
- char address[30];
- char city[30];
- char cityLc[30];
- char county[30];
- char countyLc[30];
- char state[30];
- char stateLc[30];
- char zip[30];
- char phone1[30];
- char phone2[30];
- char email[50];
- char web[50];
- };
- struct data info[1000];
- int i=0;
- char lineRead [300];
- char input[50];
- FILE *fptr;
- void fileOpen(){
- fptr = fopen("us-500b.txt", "r");
- if (fptr == NULL) {
- printf("Error opening file ! \nWe couldn't find us-500b.txt Program will close now.\nSorry :-(");
- exit(0);
- }
- }
- void fileClose(){
- fclose(fptr);
- }
- void stringToken(){
- char *word=strtok(lineRead,",");
- if(*word=='\n') return;
- if(lineRead!=NULL){
- strcpy(info[i].firstName,word);
- word=strtok(NULL,",");
- strcpy(info[i].lastName, word);
- word=strtok(NULL,",");
- strcpy(info[i].companyName, word);
- word=strtok(NULL,",");
- strcpy(info[i].address, word);
- word=strtok(NULL,",");
- strcpy(info[i].city, word);
- word=strtok(NULL,",");
- strcpy(info[i].county, word);
- word=strtok(NULL,",");
- strcpy(info[i].state, word);
- word=strtok(NULL,",");
- strcpy(info[i].zip, word);
- word=strtok(NULL,",");
- strcpy(info[i].phone1, word);
- word=strtok(NULL,",");
- strcpy(info[i].phone2, word);
- word=strtok(NULL,",");
- strcpy(info[i].email, word);
- word=strtok(NULL,",");
- strcpy(info[i].web, word);
- }
- }
- void fileRead() {
- fgets(lineRead, 300, fptr); //ignore first line
- while (!feof(fptr)){
- if (fgets(lineRead, 300, fptr) == NULL) continue;
- stringToken();
- i++;
- }
- }
- void lowerCaseName(){
- for(int z=0; z<i; z++){
- int g=0;
- while(info[z].firstName[g]!='\0'){
- info[z].firstNameLc[g]=(char)tolower(info[z].firstName[g]);
- g++;
- }
- g=0;
- while(info[z].lastName[g]!='\0'){
- info[z].lastNameLc[g]=(char)tolower(info[z].lastName[g]);
- g++;
- }
- }
- }
- void searchName(){
- int match=0;
- int result[1000];
- for(int z=0; z<i; z++) {
- char *res=strstr(info[z].firstNameLc, input);
- if(res!=0){
- result[match] = z;
- match++;
- }
- char *res2=strstr(info[z].lastNameLc, input);
- if(res2!=0&&res!=res2){
- result[match] = z;
- match++;
- }
- }
- for (int g=0; g<match; g++)
- {
- 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",
- info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
- info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
- info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
- }
- }
- void lowerCaseCompany(){
- for(int z=0; z<i; z++){
- int g=0;
- while(info[z].companyName[g]!='\0'){
- info[z].companyNameLc[g]=(char)tolower(info[z].companyName[g]);
- g++;
- }
- }
- }
- void searchCompany(){
- int match=0;
- int result[1000];
- for(int z=0; z<i; z++) {
- char *res=strstr(info[z].companyNameLc, input);
- if(res!=0){
- result[match] = z;
- match++;
- }
- }
- for (int g=0; g<match; g++)
- {
- 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",
- info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
- info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
- info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
- }
- }
- void lowerCaseCity(){
- for(int z=0; z<i; z++){
- int g=0;
- while(info[z].city[g]!='\0'){
- info[z].cityLc[g]=(char)tolower(info[z].city[g]);
- g++;
- }
- }
- }
- void searchCity(){
- int match=0;
- int result[1000];
- for(int z=0; z<i; z++) {
- char *res=strstr(info[z].cityLc, input);
- if(res!=0){
- result[match] = z;
- match++;
- }
- }
- for (int g=0; g<match; g++)
- {
- 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",
- info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
- info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
- info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
- }
- }
- void lowerCaseCounty(){
- for(int z=0; z<i; z++){
- int g=0;
- while(info[z].county[g]!='\0'){
- info[z].countyLc[g]=(char)tolower(info[z].county[g]);
- g++;
- }
- }
- }
- void searchCounty(){
- int match=0;
- int result[1000];
- for(int z=0; z<i; z++) {
- char *res=strstr(info[z].countyLc, input);
- if(res!=0){
- result[match] = z;
- match++;
- }
- }
- for (int g=0; g<match; g++)
- {
- 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",
- info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
- info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
- info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
- }
- }
- void lowerCaseState(){
- for(int z=0; z<i; z++){
- int g=0;
- while(info[z].state[g]!='\0'){
- info[z].stateLc[g]=(char)tolower(info[z].state[g]);
- g++;
- }
- }
- }
- void searchState(){
- int match=0;
- int result[1000];
- for(int z=0; z<i; z++) {
- char *res=strstr(info[z].stateLc, input);
- if(res!=0){
- result[match] = z;
- match++;
- }
- }
- for (int g=0; g<match; g++)
- {
- 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",
- info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
- info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
- info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
- }
- }
- void searchZIP(){
- int match=0;
- int result[1000];
- for(int z=0; z<i; z++) {
- char *res=strstr(info[z].zip,input);
- if(res!=0){
- result[match] = z;
- match++;
- }
- }
- for (int g=0; g<match; g++)
- {
- 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",
- info[result[g]].firstName, info[result[g]].lastName, info[result[g]].companyName, info[result[g]].address,
- info[result[g]].city, info[result[g]].county, info[result[g]].state, info[result[g]].zip, info[result[g]].phone1,
- info[result[g]].phone2, info[result[g]].email, info[result[g]].web);
- }
- }
- void inputToLowerCase()
- {
- int z = 0;
- while (input[z] != '\0'){
- input[z]=(char)tolower(input[z]);
- z++;
- }
- }
- int main()
- {
- int choice=-1;
- fileOpen();
- fileRead();
- fileClose();
- while(choice!=0){
- 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: ");
- scanf("%d", &choice);
- getchar();
- switch (choice)
- {
- case 0:
- break;
- case 1:
- system("cls");
- break;
- case 2:
- printf("Enter all <or part> of the first name or surname you are looking for:\n");
- gets(input);
- inputToLowerCase();
- lowerCaseName();
- searchName();
- break;
- case 3:
- printf("Enter all <or part> of the company name you are looking for:\n");
- gets(input);
- inputToLowerCase();
- lowerCaseCompany();
- searchCompany();
- break;
- case 4:
- printf("Enter all <or part> of the city name you are looking for:\n");
- gets(input);
- inputToLowerCase();
- lowerCaseCity();
- searchCity();
- break;
- case 5:
- printf("Enter all <or part> of the county name you are looking for: \n");
- gets(input);
- inputToLowerCase();
- lowerCaseCounty();
- searchCounty();
- break;
- case 6:
- printf("Enter all <or part> of the state name you are looking for: \n");
- gets(input);
- inputToLowerCase();
- lowerCaseState();
- searchState();
- break;
- case 7:
- printf("Enter the all <or part> of the ZIP code you are looking for: \n");
- gets(input);
- searchZIP();
- break;
- default:
- printf("\nIncorrect input! please try again!");
- break;
- }
- }
- }
- //by Jaroslaw Janas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement