2010-12-15 60 views
-2

我的程序做什麼.. 閱讀的格式哈希表:它不保存正確

store name 1 
itemcode quantity 
itemcode quantity 
. 
. 
store name 2 
itemcode quantity 
itemcode quantity 
. 
. 

當你運行我的代碼,你會要求輸入任務的文本文件。 有三個選項

L itemcode quantity 

進入上述程序將打印包含給定數量該項目的所有商店。

U itemcode quantity storename 

這個選項有三個參數itemcode INT數量和STORENAME 功能這個選項只更新量量給定存儲。

Q 

此選項調用我的Savefile方法,該方法將當前數據結構保存迴文件。

問題。

我遇到了一個問題。 每當我更新文件時,它成功地更新,但輸入命令時,Q退出並保存它不正確保存..

save_file(char *) 

它失去了整個數據只是第一個店是救..

stores.txt 

carrefour_Milan 
12345678 12 
23456766 16 
carrefour_Torino 
12345678 65 
67676765 12 
Carrefour_Vercelli 
23456766 20 

,也可以幫我找到的

int listfile(char *) 

的時間複雜度3210
int updatefile(char *,int ,char *) 

我的意思是大O.

#include<stdio.h> 
    #include<string.h> 
    #include<stdlib.h> 
    #define MAX_ITEM 1000 
    #define MAXS 129 
    #define MAXL 132 
    #define MAXC 9 
    FILE *fp; 
    typedef struct store{ 
     char Storename[MAXS]; 
     int quantity; 
     struct store *NEXT; 
     }STORE; 

    typedef struct item{ 
     char item_code[MAXC]; 
     struct store *Stores; 
     struct item *NEXT; 
     }ITEM; 


    ITEM *list_item[MAX_ITEM]; 
    int readfile(char *fname); 
    int update_file(char *item_code,int qty,char *name); 
    int hash(char *item_code); 
    int save_file(char *fname); 
    void init(); 
void init(){ 
    int i; 
    for(i=0;i<MAX_ITEM;i++) 
     list_item[i]=NULL; 
    } 
int readfile(char *fname){ 
     char *p,line[MAXL+1],storen[MAXL+1]; 
     int pos; 
     ITEM *current=NULL,*prev=NULL; 
     STORE *s_cur=NULL,*s_prev=NULL; 
     char itemcode[MAXC];int qty; 
     if((fp=fopen(fname,"r"))==NULL) 
      return -1; 
     while(!feof(fp)){ 
      if(fgets(line,MAXL+1,fp)==NULL) 
       break; 
      if((p=strchr(line,'\n'))==NULL) 
       ; 
      else 
       *p='\0'; 
      if(line[0]>='a' && line[0]<='z' ||line[0]>='A' && line[0]<='Z') 
       strcpy(storen,line); 
      else{ 
       //fgets(line,MAXL,fp); 
       if(sscanf(line,"%s %d",itemcode,&qty)>0){ 

        current=(ITEM *)malloc(sizeof(ITEM)); 
        if(current==NULL) 
         return -1; 

        pos=hash(itemcode); 

        if(list_item[pos]==NULL){ 
         list_item[pos]=current; 
         if((s_cur=(STORE *)malloc(sizeof(STORE)))==NULL) 
          return -1; 

           strcpy(s_cur->Storename,storen); 
           strcpy(current->item_code,itemcode); 
           s_cur->quantity=qty; 
           current->Stores=s_cur; 
           s_cur->NEXT=NULL; 
           current->NEXT=NULL; 
        } 
        else{ 
         ITEM *q=list_item[pos]; 
         if((s_cur=(STORE *)malloc(sizeof(STORE)))==NULL) 
          return -1; 
         while(q!=NULL){ 
          if(strcmp(q->item_code,itemcode)==0){ 
           STORE *temp=q->Stores,*temp_a=NULL; 
           if(temp==NULL){ 
            q->Stores=s_cur; 
            strcpy(s_cur->Storename,storen); 
            s_cur->quantity=qty; 

            s_cur->NEXT=NULL; 
            } 
           else{ 
          while(temp!=NULL){ 
           temp_a=temp; 
           temp=temp->NEXT; 
          } 

           temp_a->NEXT=s_cur; 
           strcpy(s_cur->Storename,storen); 
           s_cur->quantity=qty; 
           s_cur->NEXT=NULL; 
           } 
          } 
         q=q->NEXT; 
         } 
         if(q==NULL){ 
          q=current; 
          current->NEXT=NULL; 
          current->Stores=s_cur; 
          strcpy(s_cur->Storename,storen); 
          s_cur->quantity=qty; 
          s_cur->NEXT=NULL; 
          } 
         } 
        } 
      } 
     } 
    fclose(fp); 
return 0; 
} 

int listfile(char *item_code,int qty){ 
      int i; 
      ITEM *u=NULL; 
      item_code[strlen(item_code)]='\0'; 
      if(list_item[hash(item_code)]==NULL) 
       return -1; 
      else{ 
       u=list_item[hash(item_code)]; 
       while(u!=NULL){ 
        if(strcmp(u->item_code,item_code)==0){ 
         STORE *temp=u->Stores; 
         while(temp!=NULL){ 
          if(temp->quantity>=qty){ 

          printf("STORE %s\n",temp->Storename); 
          } 
          temp=temp->NEXT; 
          } 
      } 
      u=u->NEXT; 
       } 
      } 
      return 0; 
    } 
    int update_file(char *item_code,int qty,char *name){ 

     ITEM *u=NULL; 
     item_code[strlen(item_code)]='\0'; 
     name[strlen(name)]='\0'; 
     if(list_item[hash(item_code)]==NULL) 
     return -1; 

     u=list_item[hash(item_code)]; 
     if(u==NULL) 
      return -1; 
     while(u!=NULL){ 
      if(strcmp(u->item_code,item_code)==0){ 
       STORE *temp=u->Stores; 
       while(temp!=NULL){ 
        if(strcmp(temp->Storename,name)==0) 
         temp->quantity+=qty; 
         temp=temp->NEXT; 
       } 
      } 
     u=u->NEXT; 
     } 
     return 0; 
    } 
    int hash(char *item_code){ 
     int sum=0,s=0; 
     while(item_code[s]!='\0'){ 
     sum+=33*item_code[s]; 
     s++;} 
     return sum%MAX_ITEM; 
     } 

    void clear(){ 
      char c; 
      while(c!='\n') 
       scanf("%c",&c); 
      } 

    main(){ 
     int y; 
     char fname[]="stores.txt",line[MAXL],command,z[MAXS]; 
     char x[MAXC]; 
     init(); 
     if(readfile(fname)==-1) 
      printf("Error reading file!"); 
     else{ 
     do{ 
      printf("Enter task:"); 
      fgets(line,MAXL,stdin); 
      sscanf(line,"%c",&command); 
      switch(command){ 
       case 'L': sscanf(line,"%c%s%d",&command,x,&y); 

          if(listfile(x,y)==-1) 
          printf("No items were found\n"); 
          break; 
       case 'U':sscanf(line,"%c%s%d%s",&command,x,&y,z); 
         if(update_file(x,y,z)==0) 
          printf("Update OK\n"); 
         else 
          printf("Error when updating\n"); 
          break; 
       case 'Q':if(save_file(fname)==0) 
          printf("Done\n!"); 
          break; 
       default:printf("Enter correct command\n"); 
         break; 
       } 
      }while(command!='Q'); 
     } 
    } 
int save_file(char *fname){ 
ITEM *p=NULL,*q=NULL; 
int num=0,i,j; 
char str[MAXS]; 

if((fp=fopen(fname,"w"))==NULL) 
    return -1; 
    for(i=0;i<MAX_ITEM;i++){ 
     if(list_item[i]==NULL) 
      ; 
     else{ 
      p=list_item[i]; 
      while(p!=NULL){ 
       STORE *s=p->Stores; 
       if(s==NULL) 
        ; 
       else{ 
        if(strcmp(s->Storename,"0000\0")!=0){ 
        strcpy(str,s->Storename); 
        // puts(str); 
        fprintf(fp,"%s\n",str); 
        } 
        while(s!=NULL){ 
        for(j=0;j<MAX_ITEM;j++){ 
         if(list_item[j]==NULL) 
          ; 
         else{ 
          q=list_item[j]; 
          while(q!=NULL){ 
           STORE *st=q->Stores; 
           if(st==NULL) 
            ; 
            else{ 
             while(st!=NULL){ 
             if(strcmp(st->Storename,str)==0 && strcmp(st->Storename,"0000\0")!=0){ 

              printf("%s %d\n",q->item_code,st->quantity); 
              fprintf(fp,"%s %d\n",q->item_code,st->quantity); 
              strcpy(st->Storename,"0000\0"); 
              } 
              st=st->NEXT; 
             } 
             } 
           q=q->NEXT; 
           } 
          } 
         } 
         s=s->NEXT; 
         } 
       } 
     p=p->NEXT; 
     } 
     } 
    } 
    fclose(fp); 
    return 0; 
     } 
+2

請格式化您的代碼,並將其縮小爲一小段代碼,這些代碼實際上證明您的問題。這是很多代碼。 – birryree 2010-12-15 03:48:39

+1

這是一個功課問題嗎?如果是這樣,請標記爲這樣。 – George 2010-12-15 03:48:51

+4

我的建議是首先詳盡地評論你的代碼,通過每一步說出自己的意思。 – 2010-12-15 03:49:39

回答

3

這是一個不一致和不可讀的混亂。我建議作爲重構佈局的第一步。

修復縮進,以便它反映代碼結構。選擇一種支撐風格並持續使用它。這樣

if(x){ 
    ; 
    }else{ 
     foo(); 
     } 

東西應該更好的是這樣的:

if (x) { 
    ; 
} 
else { 
    foo(); 
} 

這對任何調試和維護一個更好的起點。並且有很多必要的維護。

+0

我實際上將一些代碼剪切並粘貼到文本編輯器中,以便我可以正確縮進它,以便準確地讀取它。安全是一個很好的觀點,特別是如果你想要其他人真的懶得閱讀和評論你的代碼。 – AlastairG 2010-12-15 10:04:21

3

你的代碼是非常低效的。例如,當讀取文件時,您在if語句的兩個分支中單獨存儲結構malloc,並將商店名稱複製到三個不同的位置,再次在所有不同的代碼路徑中複製。爲什麼不簡單地在商店結構中初始化malloc,然後再確定應該放哪裏?

同樣在讀取文件函數中,如果與該項目對應的哈希表位置不爲空,則分配給「當前」的內存被泄漏。

此外,如果你真的找到一個匹配的項目,你不打破的循環,這意味着開始的代碼塊:

    if(q==NULL){ 
         q=current; 

得到執行。

最後(現在),如果哈希表中的一個槽被填滿,但沒有匹配的條目碼,那麼該條目將不會被放入哈希表。看看你的代碼。您在什麼時候將「當前」分配給以「list_item [pos]」開頭的鏈的任何部分?你沒有。做「q =當前」只是將一個值存儲在另一個變量中。你需要的東西是這樣的:

current->next = list_item[pos]; 
list_item[pos] = current; 

將其添加到列表的開頭。

我建議你在擔心你的文件寫入功能之前修復你的文件讀取功能。

P.s. upvote和更多評論的請求可能會讓你更多的幫助。這取決於我是多麼忙碌,還有其他人是否也會受到困擾。