优化代码和意见

时间:2015-03-29 04:44:25

标签: c optimization data-structures

#include <stdio.h>
#include <string.h>
typedef struct//Declares structure to hold seven created datatypes.
{
        int client_id;
        char client_business_name [30];
        char client_first_name [20];
        char client_last_name [20];
        char client_address [40];
        float client_budget;
        char client_business_info [300];
}Client;

main()
{
      Client c[100];
      void main_menu (Client[]);
      main_menu (c);
      system ("PAUSE");
}  

void main_menu (Client c[])//Determines what the user wants to do and grants access to one of the 6 functions.
{
    int choice;     
    do{           
          printf ("1.Add Client\n2.Delete Client\n3.Search Clients\n4.Change Client Information\n5.View Clients\n6.Terminate Program\nChoose an option from above:");
          scanf ("%d",&choice);
      }while (choice<1||choice>6);
    if (choice==1)
    {
        system ("cls");
        void accept (Client []);
        accept (c);
    }
    if (choice==2)
    {
        system ("cls");
        void realocate (Client []);
        realocate (c);
    }
    if (choice==3)
    {
        system ("cls");
        void search (Client []);
        search (c);
    }
    if (choice==4)
    {
        system ("cls");
        void change (Client []);
        change (c);
    }
    if (choice==5)
    {
        system ("cls");
        void view_sort (Client []);
        view_sort (c);
    }
    if (choice==6)
    {
        system ("cls");
        void end (Client []);
        end (c);
    }
}

void accept (Client c[])//Accepts data from the user. 
{
     int num,y=0;
     printf("How Many Clients Do You Want To Add:");
     scanf ("%d",&num);
     system ("cls");
     while (y<num)
     { 
          printf ("\nEnter Client ID:");
          scanf ("%d",&c[y].client_id);
          printf ("Enter Buisness Name:");
          scanf (" %[^\n]",c[y].client_business_name);
          printf ("Enter Client First Name:");
          scanf (" %[^\n]",c[y].client_first_name);     
          printf ("Enter Client Last Name:");
          scanf (" %[^\n]",c[y].client_last_name);     
          printf ("Enter Buisness Address:");
          scanf (" %[^\n]",c[y].client_address);
          printf ("Enter Client Budget:");
          scanf ("%f",&c[y].client_budget);
          printf ("Enter Buisness Information:");
          scanf (" %[^\n]",c[y].client_business_info);
          y++;
     }
     void recall (Client []);
     recall (c);                    
}

void realocate (Client c[])//Realocates memory of variable the user choses to delete.
{
     int key,max=100;
     printf ("\nEnter Client ID To Be Deleted:");
     scanf ("%d",&key);
     system ("cls");
     int first = 0;
     int last = max - 1;
     int middle = (first+last)/2;
     while( first <= last )
     {
        if (c[middle].client_id < key)
           first = middle + 1;    
        else if (c[middle].client_id == key) 
        { 
           c[middle].client_id=c[middle+1].client_id;
           strcpy(c[middle].client_business_name,c[middle+1].client_business_name);
           strcpy(c[middle].client_first_name,c[middle+1].client_first_name);
           strcpy(c[middle].client_last_name,c[middle+1].client_last_name);
           strcpy(c[middle].client_address,c[middle+1].client_address);
           c[middle].client_budget=c[middle+1].client_budget;
           strcpy(c[middle].client_business_info,c[middle+1].client_business_info);                     
           printf ("\nClient Removed!");  
           break;
        }
        else
           last = middle - 1;
           middle = (first + last)/2;
     }
     if ( first > last )
     {
        printf ("\nClient Not Registered\n");
     }
     void recall (Client []);
     recall (c);                              
}

void search (Client c[])//Searches for data via a Binary or Linear search.
{
    int choice,max=100,ch,cho;     
    do{           
          printf ("\n1.Client ID\n2.Client Buisness Name\n3.Client First Name\n4.Client Last Name\nChoose an option to search by:");
          scanf ("%d",&choice);
      }while (choice<1||choice>4);
    if (choice==1)//Binary Search
    {
        system ("cls");     
        int search_id1; 
        printf("Enter Client ID:");
        scanf("%d",&search_id1);
        system ("cls");
        int first = 0;
        int last = max - 1;
        int middle = (first+last)/2;
        while( first <= last )
        {
           if (c[middle].client_id < search_id1)
              first = middle + 1;    
           else if (c[middle].client_id == search_id1) 
           { 
               printf ("Client ID:%d",c[middle].client_id);
               printf ("\nBuisness Name:%s",c[middle].client_business_name);
               printf ("\nClient First Name:%s",c[middle].client_first_name);
               printf ("\nClient Last Name:%s",c[middle].client_last_name);
               printf ("\nBuisness Address:%s",c[middle].client_address);
               printf ("\nClient Budget:%d",c[middle].client_budget);
               printf ("\nBuisness Information:%s",c[middle].client_business_info);
               break;
           }
           else
               last = middle - 1;
               middle = (first + last)/2;
        }
        if ( first > last )
        {
           printf("Not found!\n%d is not registered to a existing client.\n",search_id1);
        }
     }
     else if (choice==2)//Binary Search
     {
        system ("cls");     
        char search_id2 [30]; 
        printf("Enter Buisness Name:");
        scanf(" %[^\n]",search_id2);
        system ("cls");
        int first = 0;
        int last = max - 1;
        int middle = (first+last)/2;
        while( first <= last )
        {
           if (strcmp(c[middle].client_business_name,search_id2)<0)
              first = middle + 1;    
           else if (strcmp(c[middle].client_business_name,search_id2)==0) 
           { 
               printf ("Client ID:%d",c[middle].client_id);
               printf ("\nBuisness Name:%s",c[middle].client_business_name);
               printf ("\nClient First Name:%s",c[middle].client_first_name);
               printf ("\nClient Last Name:%s",c[middle].client_last_name);
               printf ("\nBuisness Address:%s",c[middle].client_address);
               printf ("\nClient Budget:%d",c[middle].client_budget);
               printf ("\nBuisness Information:%s",c[middle].client_business_info);
               break;
           }
           else
               last = middle - 1;
               middle = (first + last)/2;
        }
        if ( first > last )
        {
           printf("Not found!\n%s is not a client.\n",search_id2);
        }
     }
     else if (choice==3)//Linear Search
     {
        system ("cls");     
        char search_id3 [20]; 
        printf("Enter Client's First Name:");
        scanf(" %[^\n]",search_id3);
        system ("cls"); 
        int x=0;
        while ((strcmp(c[x].client_first_name,search_id3)!=0) && x<100)
        {
            if (strcmp(c[x].client_first_name,search_id3)==0)
            {
               printf ("Client ID:%d",c[x].client_id);
               printf ("\nBuisness Name:%s",c[x].client_business_name);
               printf ("\nClient First Name:%s",c[x].client_first_name);
               printf ("\nClient Last Name:%s",c[x].client_last_name);
               printf ("\nBuisness Address:%s",c[x].client_address);
               printf ("\nClient Budget:%d",c[x].client_budget);
               printf ("\nBuisness Information:%s",c[x].client_business_info);    
            }
            else if (strcmp(c[x].client_first_name,search_id3)!=0)
            {
               printf("Not found!\n%s is not a client.\n",search_id3);
            } 
            x++;                                       
        }  
     }
     else if (choice==4)//Linear Search
     {
        system ("cls");     
        char search_id4 [20]; 
        printf("Enter Client's Last Name:");
        scanf(" %[^\n]",search_id4);
        system ("cls"); 
        int y=0;
        while ((strcmp(c[y].client_first_name,search_id4)!=0) && y<100)
        {
            if (strcmp(c[y].client_first_name,search_id4)==0)
            {
               printf ("Client ID:%d",c[y].client_id);
               printf ("\nBuisness Name:%s",c[y].client_business_name);
               printf ("\nClient First Name:%s",c[y].client_first_name);
               printf ("\nClient Last Name:%s",c[y].client_last_name);
               printf ("\nBuisness Address:%s",c[y].client_address);
               printf ("\nClient Budget:%d",c[y].client_budget);
               printf ("\nBuisness Information:%s",c[y].client_business_info);    
            }
            else if (strcmp(c[y].client_first_name,search_id4)!=0)
            {
               printf("Not found!\n%s is not a client.\n",search_id4);
            }
            y++;                                        
        }  
     }
     void recall (Client []);
     recall (c);                            
}

   void recall (Client c[])
{
     int choice;
     do{
           printf ("\n\nDo You Want To:\n1.Go Back To The Main Menu\n2.Exit\n");
           scanf ("%d",&choice);
       }while (choice<1 || choice>2);
     if (choice==1)
     {
         void main_menu (Client []);
         main_menu (c);   
     }
     else if (choice==2)
     {
         void end (Client []);
         end (c);
     }
}

void end (Client c[])
{                   
     printf ("Thank You!\n");          
     system ("pause");           
     system ("cls");
}        

我知道这看起来像很多代码,但我是编程的新手,所以我希望人们对C中的代码有所了解。 该计划的主要功能是与客户打交道。 用户可以添加,删除或更改客户端,或查看和搜索特定客户端。 我主要关注的是确保我可以删除客户端,就像重新分配功能一样。我的另一个主要问题是搜索和排序功能。我想了解您对代码的看法,以及我可以做些什么来使其更好。

1 个答案:

答案 0 :(得分:0)

将函数原型放在顶部

使用define MAX_CLIENTS 100而不是输入100

声明一个新变量Client_Count,您需要知道您有多少客户端。在这里,我将其声明为全局变量。您也可以在main中声明它,然后像使用Client

一样随身携带它

使用print_client的函数,这样您就不必重复相同的代码。

您可能必须初始化所有客户端,从零到MAX_CLIENTS,只需将client_id设置为零,因此您知道它无效或已被删除。

-

void main_menu(Client[]);
void accept(Client[]);
void realocate(Client[]);
void search(Client[]);
void view_sort(Client[]);
void change(Client[]);
void end(Client[]);
void recall(Client[]);

#define MAX_CLIENTS 100

int Client_Count;

int main()
{
    Client_Count = 0;
    Client c[MAX_CLIENTS];
    main_menu(c);
    system("pause");
    return 0;
}

void main_menu(Client c[])//Determines what the user wants to do and grants access to one of the 6 functions.
{
    int choice;
    do{
        printf("1.Add Client\n2.Delete Client\n3.Search Clients\n4.Change Client Information\n5.View Clients\n6.Terminate Program\nChoose an option from above:");
        scanf("%d", &choice);
    } while (choice < 1 || choice > 6);

    system("cls");
    if (choice == 1) accept(c);
    if (choice == 2) realocate(c);
    if (choice == 3) search(c);
    if (choice == 4) change(c);
    //if (choice == 5) view_sort(c);
    if (choice == 6) end(c);
}

void accept(Client c[])//Accepts data from the user. 
{
    if (Client_Count < MAX_CLIENTS)
    {
        printf("\nEnter Client ID:");
        scanf("%d", &c[Client_Count].client_id);
        printf("Enter Buisness Name:");
        scanf(" %[^\n]", c[Client_Count].client_business_name);
        printf("Enter Client First Name:");
        scanf(" %[^\n]", c[Client_Count].client_first_name);
        printf("Enter Client Last Name:");
        scanf(" %[^\n]", c[Client_Count].client_last_name);
        printf("Enter Buisness Address:");
        scanf(" %[^\n]", c[Client_Count].client_address);
        printf("Enter Client Budget:");
        scanf("%f", &c[Client_Count].client_budget);
        printf("Enter Buisness Information:");
        scanf(" %[^\n]", c[Client_Count].client_business_info);
        Client_Count++;
    }
    else
    {
        printf("too many\n");
    }
    recall(c);
}

void print_client(Client *c)
{
    printf("Client ID:%d", c->client_id);
    printf("\nBuisness Name:%s", c->client_business_name);
    printf("\nClient First Name:%s", c->client_first_name);
    printf("\nClient Last Name:%s", c->client_last_name);
    printf("\nBuisness Address:%s", c->client_address);
    printf("\nClient Budget:%d", c->client_budget);
    printf("\nBuisness Information:%s", c->client_business_info);
}

void search(Client c[])//Searches for data via a Binary or Linear search.
{
    int choice = 0;//initialize
    do{
        printf("\n1.Client ID\n2.Client Buisness Name\n3.Client First Name\n4.Client Last Name\nChoose an option to search by:");
        scanf("%d", &choice);
    } while (choice < 1 || choice > 4);

    system("cls");
    if (choice == 1)//Binary Search
    {
        int search_id1;
        printf("Enter Client ID:");
        scanf("%d", &search_id1);
        system("cls");
        for (int i = 0; i < Client_Count; i++)
        {
            if (c[i].client_id == search_id1)
            {
                print_client(&c[i]);
                return;
            }
        }
        printf("Not found!\n%d is not registered to a existing client.\n", search_id1);
    }
    else if (choice == 2)//Binary Search
    {
        system("cls");
        char search_id2[30];
        printf("Enter Buisness Name:");
        scanf(" %[^\n]", search_id2);

        for (int i = 0; i < Client_Count; i++)
        {
            if (strcmp(c[i].client_business_name, search_id2) == 0)
            {
                print_client(&c[i]);
                return;
            }
        }
        printf("Not found!\n%s is not a client.\n", search_id2);
    }
    //...
    recall(c);
}