All pastes #2052723 Raw Edit

Someone

public c v1 · immutable
#2052723 ·published 2011-05-02 04:00 UTC
rendered paste body
#include <iostream>#include <cstdio>//#include <conio.h>#include <cctype>#include <iomanip>#include <stdlib.h>#include <cstring>#define SIZE 30//Add existance of city check//returnType (*functionName)(paramType1, paramType2, ...)using namespace std;bool hyphen(char *zip);int  convert(char);void enter(char places[][30],int &effective);  //E,0void search(char places[][30],int &effective); //S,1void display(char places[][30],int &effective);//D,2void clear(char places[][30],int &effective);  //C,3int  menu();int password();void selection(char **,int);bool validity(char *city,char cities[][30],int effective);bool notDuplicate   (char *city,char *&accessZip,int effective,char cities[][30]);bool letterCheck    (char *zip,char *&accessZip,int effective,char cities[][30]);bool numberCount1   (char *zip,char *&accessZip,int effective,char cities[][30]);bool numberCount2(char *last,char *&a,int effective,char cities[][30]);bool comma(char *city, char *&comma_locus, int b,char cities[][30]);bool zipComparison(char *input, char *key);bool citComparison(char *input, char *key);int main(){    int (*startUp[])()={password,menu};    void (*actions[])(char [][30],int &)={enter,search,display,clear};    int choice;    char places[6][30]={'\0'};    int effective=0;    bool exit=false;    	//E=0,S=1,D=2,C=3,Q=4    //startUp[0]();	do{        choice=startUp[1]();        if(choice==4){            cout<<"Good Bye"<<endl;            exit=true;        }        else if(choice<4&&choice>=0)            actions[choice](places,effective);    }while(!exit);    system("pause");}/*******************************************************************************purpose:                    takes in an menu choice and outputs the function                            pointer array element.input:      (char)input     Menu choiceoutput:     (int)           pointer array element number.*******************************************************************************/int convert(char input){    //E=0,S=1,D=2,C=3,Q=4    int output;    if(input=='E')        output=0;    else if(input=='S')        output=1;    else if(input=='D')        output=2;    else if(input=='C')        output=3;    else if(input=='Q')        output=4;    else        output=5;    return output;}    /*******************************************************************************purpose:    if effective<6 gets the city, does this until city is valid, then            increments effective.input:      char cities[][30]  array containing all cities            int &effective     number of validated elements in the arrayoutput:     void*******************************************************************************/void enter(char cities[][30],int &effective){//hereistheplace    if(effective<6){        do{             cout<<"Enter Location: ";            fgets(cities[effective],30,stdin);            fflush(stdin);            strtok(cities[effective],"\n");        }while(!validity(cities[effective],cities,effective));        effective++;    }    else        cout<<"Array Full"<<endl;}/*******************************************************************************purpose:    if effective>0 gets and formats a search key then searches for the             string in formatted array elementsinput:      char cities[][30]  array containing all cities            int &effective     number of validated elements in the arrayoutput:     void*******************************************************************************/void search(char places[][30],int &effective){    bool found=false;    char key[30]={'\0'};    char element[30]={'\0'};    int write=0;        if(effective==0)        cout<<"Nothing to Search"<<endl;    else{        cout<<"Enter Search Key: ";        fgets(key,30,stdin);        strtok(key,"\n");        for(int read=0;key[read]!='\0';read++) //copies array sans spaces            if(key[read]!=' ')                key[write++]=key[read];        key[write]='\0';        strupr(key);    }    for(int count=0;count<effective&&!found;count++){        strset(element,'\0');        for(int read=0,write=0;places[count][read]!='\0';read++)//copies arry sans spaces            if(places[count][read]!=' ')                element[write++]=places[count][read];        strupr(element);        found=strstr(element,key);    }    if(strlen(key)==0)//size of key with without spaces,         cout<<"No Search Key Given"<<endl;    else if(found)//if(strlen(key)!=0&&found)        cout<<"found"<<endl;}/*******************************************************************************purpose:    if effective>0          sorts and displays data in the arrayinput:      char cities[][SIZE]     array containing all cities            int &effective          number of validated elements in the arrayoutput:     void*******************************************************************************/void display(char places[][SIZE],int &effective){    char *sort[6];    if(effective>0){        cout<<"The cities are: "<<endl;        for(int count=0;count<effective;count++)//copi            sort[count]=places[count];        selection(sort,effective-1);        for(int count=0;count<effective;count++)            cout<<*(sort+count)<<endl;    }    else        cout<<"No cities to display."<<endl;}/*******************************************************************************purpose:    clears the array and sets effective to 0input:      char cities[][SIZE]  array containing all cities            int &effective     number of validated elements in the arrayoutput:     void*******************************************************************************/void clear(char places[][SIZE],int &effective){    for(int count=0;count<effective;count++){        strset(places[count],'\0');    }    effective=0;    cout<<"Addresses Cleared"<<endl;}/*******************************************************************************purpose:                gets a menu choice from user validates then converts                         it to the array of functions elementinput:      (void)      output:     int         array of function elements*******************************************************************************/int menu(){    char selection;    bool valid=false;    do{        cout<<"Select a menu option: "<<endl            <<"(E) Enter zipcode and city information"<<endl            <<"(S) Search by city or zipcode"<<endl            <<"(D) Display contents of the list"<<endl            <<"(C) Clear the list"<<endl            <<"(Q) Quit"<<endl            <<"Enter your selection: ";        //selection=toupper(getche());        cin>>selection;        selection=toupper(selection);        cout<<endl;        valid=strchr("ESDCQ",selection);        if(!valid)            cout<<"Error: ";    }while(!valid);    return convert(selection);}/*******************************************************************************purpose:                loops until correct password is enteredinput:      (void)      output:     int         returns nothing, used for placing into an array*******************************************************************************/int password(){    /*    bool accepted;    char password[5]={'\0'};    char mother[]="mama";    do{        accepted=true;        cout<<"Enter Password: ";        for(int count=0;count<4&&accepted;count++){            password[count]=tolower(getche());            if(password[count]!= mother[count]){                accepted=false;                cout<<endl<<"Incorrect Password"<<endl;            }        }    }while(!accepted);    cout<<endl<<"Password Accepted"<<endl<<endl;    */    return 0;}/*******************************************************************************purpose:                    sorts an array of pointers pointing to stringsinput:      char **array    array of pointers            int limit       effective size of array decremententedoutput:     void*******************************************************************************/void selection(char **array,int limit){    char *temp;    int index_of_largest,index;    for (;limit > 0;limit--){        index_of_largest=0;        for (index=1;index<=limit;index++) {            if(strcmpi(*(array+index_of_largest),*(array+index))<0)                index_of_largest=index;        }        if (limit!=index_of_largest){            temp=array[limit];            array[limit]=array[index_of_largest];            array[index_of_largest]=temp;        }    }}/*******************************************************************************purpose:                        detirmines if enter string is validinput:      char *city          city to be validated            char cities[][SIZE]   array containing all cities            int effective       effective size of arrayoutput:     bool                result of validity check*******************************************************************************/bool validity(char *city,char cities[][SIZE],int effective){    bool(* func[])(char *, char*&,int, char [][SIZE])={comma,letterCheck,numberCount1,                                                       numberCount2,notDuplicate};    char *commaLocus;    char *accessZip;    bool valid;    //caseunsensitive city match   95678==95674-1234        valid=  func[0](city,commaLocus,effective,cities)&& //return            func[1](commaLocus+1,accessZip,effective,cities)&&            func[2](commaLocus+1,accessZip,effective,cities);    if(valid&&*accessZip=='-')            valid=func[3](accessZip+1,accessZip,effective,cities);    if(valid)        valid=func[4](city,accessZip,effective,cities);    if(!valid)        strset(city,'\0');            return valid;}/**********************************************************************purpose:    detirmines          checks if city has a common then modifies                                the location in the function that called itinput:      char *city          city to be checked            char *&comma_locus  pointer to the location of the comma            int c               not used            char d[][SIZE]      not usedoutput:     bool                result: true if comma is present false otherwise**********************************************************************/bool comma(char *city, char *&comma_locus, int c,char d[][SIZE]){    bool valid=true;    comma_locus=strchr(city,',');    if(comma_locus==0){        cout<<"Comma needed"<<endl;        valid=false;    }    return valid;}/**********************************************************************purpose:                        checks if non numbers or '-' are present in zipinput:      char *zip           location of the comma space after the comma,                                 then zip code            char *&b            not used                int c               not used            char d[][SIZE]      not usedoutput:     bool                result: true if only numbers or '-' are present                                false otherwise**********************************************************************/bool letterCheck(char *zip,char *&b,int c,char d[][SIZE]){    bool valid=true;    char * location = zip;    char * hifins = zip;    int h=0;        for(;*location==' ';location++);//counts past spaces    for(;valid&&*location!='\0';location++)        valid=isdigit(*location)||*location=='-';    if(valid){//check with professor        for(int count=0;h<2&&hifins[count]!='\0';count++)//more than one hyphen            if(hifins[count]=='-')                h++;        valid=h<2;    }    if(!valid)        cout<<"need digits"<<endl;    return valid;}/**********************************************************************purpose:                        counts numbers before hyphen or all sans hyphen                                while updating that location through accessZipinput:      char *zip           location of the comma space after the comma,                                 then zip code            char *&accessZip    location of space after the 5 digit or space after                                numbers                int c               not used            char d[][SIZE]      not usedoutput:     bool                result: True if only and only if first 5                                contains only 5 digits **********************************************************************/bool numberCount1(char *zip,char *&accessZip,int c,char d[][SIZE]){    accessZip=zip;    bool valid=true;    bool latter;    int count;        accessZip=zip;    for(;*accessZip==' ';accessZip++);//increments past spaces    for(count=0;count<5&&valid;count++){//counts if their are at lease 5 digits        valid=isdigit(*(accessZip));        accessZip++;//moves pointer for next function    }    if(count<5){//not enough numbers before hyphen        cout<<"Needs more Numbers"<<endl;        valid=false;    }    else if(count==5&&isdigit(*accessZip)&&strchr(zip,'-')==0){    //        cout<<"needs a hyphen"<<endl;        valid=false;    }    else if(count==5&&isdigit(*accessZip)&&strchr(zip,'-')!=0){        hyphen(zip);        valid=false;    }    return valid;}/**********************************************************************purpose:                        counts digits after hyphen returns true if it is 4                                called only if the main part is the right lengthinput:      char *last          location of current check                                 then zip code            char *&b            not used                int c               not used            char d[][SIZE]        not usedoutput:     bool                result true if second part of zip only has                                4 digits.**********************************************************************/bool numberCount2(char *last,char *&b,int c,char d[][SIZE]){    bool valid=true;    int count=0;    for(count;count<4&&valid;count++)        valid=isdigit(last[count]);    if(!valid)        cout<<"need more numbers"<<endl;    else if(last[4]!='\0'){//&&valid        cout<<"need less numbers"<<endl;        valid=false;    }    return valid;    }/**********************************************************************purpose:                        counts digits after hyphen returns true if it is 4input:      char *city          city information                                then zip code            char *&b            not used                int effective       effective size of array            char cities[][SIZE]   array of cities to be compared againstoutput:     bool                returns if zip or place name is found**********************************************************************/bool notDuplicate(char *city,char *&b,int effective,char cities[][SIZE]){    bool valid=true;    bool (*check[])(char *, char *)={zipComparison,citComparison};    for(int compare=0;valid&&(compare<effective);compare++)        for(int method=0;valid&&(method<2);method++)            valid=check[method](city,cities[compare]);//calls the two comparisons    return valid;}/**********************************************************************purpose:                        outputs error message for data after the hypheninput:      char *zip           the zip code to be checkedoutput:     bool latter         returns if the error was due to 2nd 5 being too long**********************************************************************/bool hyphen(char *zip){    char check[SIZE]={'\0'};    char *location;    strcpy(check,zip);    strtok(check,"-");    location=strtok(NULL,"-");    if(strlen(location)>4)        cout<<"fewer digits"<<endl;//if last 4 has too many digits    else if(strlen(location)<4)        cout<<"more digits"<<endl;//if last 4 hase too few  digits    else        cout<<"needs fewer numbers"<<endl;  //if last 4 has the right number of digits                                            //since the input has too many digits before                                            //the hyphanent this one is     return strlen(location)==4;}/**********************************************************************purpose:                        checks if Zip Code had already been enteredinput:      char *input         entry to be check            char *key           search keyoutput:     bool                returns true if first 5 of digits of key is in input                                (opposite of found)**********************************************************************/bool zipComparison(char *input, char *key){    bool found;    char * ziplocation;    char zip[6]={'\0'};    char keyTemp[30]={'\0'};        ziplocation=strchr(input,',')+1;//first space after the comma    while(*ziplocation==' ')        ziplocation++;    strncpy(zip,ziplocation,5);//extracts zip code    strcpy(keyTemp,key);    strtok(keyTemp,",");    found=strstr(strtok(NULL,","),zip); //searches for zip code in part of keyTemp after                                        //','    if(found)        cout<<"duplicate"<<endl;    return !found; //turns the found into valid.}/**********************************************************************purpose:                        formats(all caps no spaces) the name of input                                and key then detirmines if they                                are equalinput:      char *input         entry to be check            char *key           search keyoutput:     bool                returns valid, (if entry repeated);**********************************************************************/bool citComparison(char *input, char *key){    bool valid;    char inputFormat[SIZE]={'\0'};    char keyFormat[SIZE]={'\0'};    for(int location=0, count=0;count<SIZE&&input[count]!=',';count++)        if(input[count]!=' ')            inputFormat[location++]=input[count];    for(int location=0, count=0;count<SIZE&&key[count]!=',';count++)        if(key[count]!=' ')            keyFormat[location++]=key[count];    valid=strcmpi(keyFormat,inputFormat)!=0;    if(!valid)        cout<<"duplicate"<<endl;    return valid;}