2015-03-29 3 views
-3
#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"); 
}   

Я знаю, что это, кажется, как много кода, но я новичок в программировании, поэтому я хотел мнение людей по этому коду в С. Основной функцией программы является работа с клиентами , Пользователь может добавлять, удалять или изменять клиентов или просматривать и искать конкретные клиенты. Основное внимание уделяется тому, чтобы я мог удалить клиента, как это сделано в функции перераспределения. Другая важная проблема - функции поиска и сортировки. Я хотел бы узнать ваше мнение о коде, и что я могу сделать, чтобы сделать его лучше.Оптимизация кода и мнения

+0

ли, как ожидается, весь ваш код работает? –

+0

Большинство, но когда я пытаюсь найти идентификатор клиента после его ввода, он возвращает не найденный. – user3871400

+0

В этом случае удалите необработанные детали и укажите пример минимальной, полной и проверяемой. –

ответ

0

Поместите прототипы функций на верхних

Использование define MAX_CLIENTS 100 вместо ввода в 100

Объявите новую переменную Client_Count вам нужно это знать, сколько клиентов у вас есть. Здесь я объявляю его глобальной переменной. Вы также можете объявить его в основном, а затем перенести его, как вы делаете с 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); 
} 
Смежные вопросы