עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

בעיה בפונקציית מיון עם אינדקסים בקובץ

Featured Replies

פורסם

קיבלתי משימה ליצור מערכת של ספרייה שקולטת נתונים של ספרים: שם ספר, שם מחבר, קוד ושנת הוצאה ומכניסה אותו לקובץ, אבל המערכת צריכה גם למיין את הקובץ לפי אינדקסים של שם המחבר, כך שעבור כל ספר יהיה משתנה IndexAuthor שיצביע לשורה של הספר הבא לפי סדר א"ב של הסופרים.

אותו דבר גם עבור שם הספר.

(המערכת לא צריכה למיין את הספרים לפי סדר הא"ב בתוך הקובץ אלא רק לפי אינדקסים כפי שהוסבר)

לדוגמא:

(השורה הראשונה ריקה IndexAuthor=-1, IndexName=-1)

מספר שורה | שם המחבר | IndexAuthor

שורה 1: | \0 | 2

שורה 2: | אבי | 4

שורה 3: | יוסי | 1-

שורה 4: | גלית | 5

שורה 5: | דן | 3

הבעיה היא שבפונקציה sort שאחרי add (הוספת ספר) יש בעיה והאינדקסים שהיא נותנת הם לא נכונים...

פונקציית sort (היא אמנם ארוכה... אבל בבקשה תקראו לפחות עד "הטיפול" בIndexAuthor, בIndexName יש עדיין בעיות שלא טיפלנו בהן)

void sort(FILE *fin, struct book c)
{

struct book b;
char author[50];
char name[80];
int prv_index, prv_prv_index=0,i=0;
if(fin==NULL)
{ printf("Cannot open file."); return; }

strcpy(name,c.name);
strcpy(author,c.author);
//printf("author is %s",author); //- working

rewind(fin);

i=count_structs(fin);

if( i==1)
{
rewind(fin);
puts("been here");
fread(&b,sizeof(b),1,fin);
b.indexAthor=2;
b.indexName=2;
rewind(fin);
fwrite(&b,sizeof(b),1,fin);
c.indexAthor=-1;
c.indexName=-1;
printf("code= %s , name= %s ,author= %s , year= %d , indexAthor= %d , indexName=%d \n",c.code,c.name,c.author,c.year,c.indexAthor,c.indexName);
fwrite(&c,sizeof(c),1,fin);
}

else
{
// sort by author
rewind(fin);
fread(&b,sizeof(b),1,fin);
prv_index=b.indexAthor;
fseek(fin,(b.indexAthor-1)*sizeof(b),SEEK_SET);
fread(&b,sizeof(b),1,fin);
if(i==2) { if(strcmp(b.author,author)<=0) { prv_prv_index=prv_index; prv_index=b.indexAthor;} else { prv_index=i; prv_prv_index=1; } }

while( strcmp(b.author,author)<=0 && b.indexAthor!=-1 /* > ? < */ )
{
prv_prv_index=prv_index;
prv_index=b.indexAthor;
fseek(fin,(b.indexAthor-1)*sizeof(b),SEEK_SET);
fread(&b,sizeof(b),1,fin);
}

c.indexAthor=prv_index; //gives an index to the new author
fseek(fin,(prv_prv_index-1)*sizeof(b),SEEK_SET);
fread(&b,sizeof(b),1,fin);
b.indexAthor=count_structs(fin);
fseek(fin,(prv_prv_index-1)*sizeof(b),SEEK_SET);
fwrite(&b, sizeof(struct book), 1, fin);

// sort by name
rewind(fin);
fread(&b,sizeof(b),1,fin);
prv_index=b.indexName;
fseek(fin,(b.indexName-1)*sizeof(b),SEEK_SET);
fread(&b,sizeof(b),1,fin);
while( strcmp(b.name,name)<=0 && b.indexName!=-1 /* > ? < */ )
{
prv_prv_index=prv_index;
prv_index=b.indexAthor;
fseek(fin,(b.indexName-1)*sizeof(b),SEEK_SET);
fread(&b,sizeof(b),1,fin);
}

c.indexAthor=prv_index; //gives an index to the new book name
fseek(fin,(prv_prv_index-1)*sizeof(b),SEEK_SET);
fread(&b,sizeof(b),1,fin);
b.indexName=count_structs(fin);
fseek(fin,(prv_prv_index-1)*sizeof(b),SEEK_SET);
fwrite(&b, sizeof(struct book), 1, fin);

fseek(fin,0,SEEK_END);
fwrite(&c,sizeof(c),1,fin);

}

}

והפונקצייה add:

void add(FILE *fin) 
{
struct book b;
fseek(fin,0,SEEK_END); // ? what if we need to run over somthing (delete) (?)
flushall();
printf("New book's name: "); scanf("%s",&b.name);
printf("New book's author: "); scanf("%s",&b.author);
printf("New book's year: "); scanf("%d",&b.year);
printf("New book code: "); scanf("%s",&b.code); // see remark above
//should also use the sort function to give the new book an index
//fwrite(&b, sizeof(b), 1, fin);
sort(fin,b);
}

הstructs:

struct book
{
char author[50];
char name[80];
int year;
int indexAthor;
int indexName;
char code[7];
};

תמונה של הבעיה:

אחרי הרצה ראשונה:

קלט: Author = aa

http://imageshack.us/photo/my-images/31/21311498.jpg/

עבור הפעם הראשונה הIndex בסדר

עבור ההרצה 2:

קלט: Author = zz

http://imageshack.us/photo/my-images/857/86665716.jpg/

יש שגיאה (תתעלמו מהIndexName)! השינוי חל על כל האינדקסים והוא לא נכון!

האלגוריתם שמאחורי הפעולה sort:

הספר החדש מקבל את האינדקס_מחבר (Index_Author) של הספר החדש מקבל את האינדקס_מחבר של הספר שלפניו, והאינדקס_מחבר של הספר שלפניו מקבל את הערך של מספר השורה של הספר החדש (בעזרת פונקציית count structs שעובדת טוב).

תודה פשוט ענקית לכל מי שרק יעזור פה :)

אם יש משהו לא מובן אל תהססו תשאלו! גם זה יעזור :)

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.