לוגו אתר Fresh          
 
 
  אפשרות תפריט  ראשי     אפשרות תפריט  צ'אט     אפשרות תפריט  מבזקים     אפשרות תפריט  צור קשר     חץ שמאלה ‎print ‎"Hello World!"; if‎ ‎not rules.‎know ‎then rules.‎read(); חץ ימינה  

לך אחורה   לובי הפורומים > מחשבים > תכנות ובניית אתרים
שמור לעצמך קישור לדף זה באתרי שמירת קישורים חברתיים
תגובה
 
כלי אשכול חפש באשכול זה



  #1  
ישן 05-03-2014, 16:06
  hwarang hwarang אינו מחובר  
 
חבר מתאריך: 18.07.05
הודעות: 3,884
קריאת נתונים עם דלימיטר

היי,

אני נתבקשתי לקרוא מהמשתמש בסיסים וחזקות של פולינום כאשר של ערך מופרד ברווח, וכאשר המשתמש ילחץ אנטר אז הוא סיים להכניס את הפולינום.
למשל: 1 1 2 2 3 3
ייתן את הפולינום 3X^3+2X^2+X

הבעיה שלי היא שאני לא מצליח לעשות זאת.

נתון לי struct:
קוד:
typedef struct monom{ int coefficient; int power; }Monom;

ואני רוצה לכתוב את הפונציה הבאה:
קוד:
Monom* getPolynom(unsigned int * size) { int coefficient,power,index=0,counter=0,currLen=2; char stop=' '; Monom*userData=(Monom*)malloc(currLen*sizeof(Monom ));//allocate new array with size=2 confirmMemoryAllocation((void*)userData);//check the allocation //printf("Please enter pairs of coefficients and their power,\n enter the pair (0,0) when finish:\n\n"); while (stop!='\n'){ scanf("%d",&coefficient); scanf("%d",&power); scanf("%c",&stop); //flushall(); if(index==currLen){//if we used all the space in the array userData=(Monom*)realloc(userData,(currLen*2)*size of(Monom));//reallocate an array with double size confirmMemoryAllocation((void*)userData);//check the allocation currLen=currLen*2;//update the length }// if(coefficient!=0){//if the user inserted the coefficient 0, we can ignore it userData[index].coefficient=coefficient;//insert values userData[index].power=power; index++; counter++; }//if // flushall(); }//while *size=counter;//the real size of the polynomial when the user finished inserting its values userData=(Monom*)realloc(userData,counter*sizeof(M onom));//reallocate an array with final actual size confirmMemoryAllocation((void*)userData);//check the allocation //arrangePoly(userData,&counter); return userData; }//getDataFromUser

אשמח אם מישהו יוכל לעזור לי. אני רוצה שברגע שהמשתמש הכניס אנטר במהלך הפונקציה, אז שהיא תחזור.
תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
  #4  
ישן 05-03-2014, 20:22
צלמית המשתמש של Eran
  משתמש זכר Eran Eran אינו מחובר  
 
חבר מתאריך: 27.02.02
הודעות: 4,536
שלח הודעה דרך ICQ אל Eran
בתגובה להודעה מספר 3 שנכתבה על ידי hwarang שמתחילה ב "תודה על התיקון. למדנו שימוש..."

הקוד שלך לא עובד כי scanf מדלג על רווחים.


אם למדת מה strtok עושה.
אז מה שחסר לך לפני זה לקבל את השורה מהמשתמש. אתה יכול להשתמש ב fgets
קוד:
char buf[50]; fgets(buf, sizeof(buf), stdin);

כמובן שכאן אתה מקצה גודל מקסימלי לפני...


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

מה שזורם לך...
_____________________________________

[התמונה הבאה מגיעה מקישור שלא מתחיל ב https ולכן לא הוטמעה בדף כדי לשמור על https תקין: http://fresh.clanteam.com/list.png]

[התמונה הבאה מגיעה מקישור שלא מתחיל ב https ולכן לא הוטמעה בדף כדי לשמור על https תקין: http://fresh.clanteam.com/?https://2010-uploaded.fresh.co.il/2010/09/21/52868411.gif,http://www.boredpanda.com/blog/wp-content/themes/mimbo2.2/images/subscribe-panda-605px.png]

תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
  #5  
ישן 05-03-2014, 19:57
  משתמש זכר Mike-Opeth Mike-Opeth אינו מחובר  
 
חבר מתאריך: 02.11.06
הודעות: 394
בתגובה להודעה מספר 1 שנכתבה על ידי hwarang שמתחילה ב "קריאת נתונים עם דלימיטר"

הבעיה נראית מוכרת, יש מצב שזה מהאקדמית ת"א יפו?
בכל מקרה, מה לא עובד לך בקוד?
הרצתי את הקוד שלך, מחקתי רווחים(בחלק המודגש) וזה עבד כמו שצריך..
קוד:
Monom* getPolynom( unsigned int * size) { int coefficient,power,index=0,counter=0,currLen=2; char stop=' '; Monom*userData=(Monom*)malloc(currLen*sizeof(Monom ));//allocate new array with size=2 confirmMemoryAllocation((void*)userData);//check the allocation //printf("Please enter pairs of coefficients and their power,\n enter the pair (0,0) when finish:\n\n"); while (stop!='\n'){ scanf("%d",&coefficient); scanf("%d",&power); scanf("%c",&stop); //flushall(); if(index==currLen){//if we used all the space in the array userData=(Monom*)realloc(userData,(currLen*2)*sizeof(Monom));//reallocate an array with double size confirmMemoryAllocation((void*)userData);//check the allocation currLen=currLen*2;//update the length }// if(coefficient!=0){//if the user inserted the coefficient 0, we can ignore it userData[index].coefficient=coefficient;//insert values userData[index].power=power; index++; counter++; }//if // flushall(); }//while *size=counter;//the real size of the polynomial when the user finished inserting its values userData=(Monom*)realloc(userData,counter*sizeof(Monom));//reallocate an array with final actual size confirmMemoryAllocation((void*)userData);//check the allocation //arrangePoly(userData,&counter); return userData; }//getDataFromUser
_____________________________________
תמונה שהועלתה על ידי גולש באתר ולכן אין אנו יכולים לדעת מה היא מכילה


נערך לאחרונה ע"י Mike-Opeth בתאריך 05-03-2014 בשעה 20:25.
תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
תגובה

כלי אשכול חפש באשכול זה
חפש באשכול זה:

חיפוש מתקדם
מצבי תצוגה דרג אשכול זה
דרג אשכול זה:

מזער את תיבת המידע אפשרויות משלוח הודעות
אתה לא יכול לפתוח אשכולות חדשים
אתה לא יכול להגיב לאשכולות
אתה לא יכול לצרף קבצים
אתה לא יכול לערוך את ההודעות שלך

קוד vB פעיל
קוד [IMG] פעיל
קוד HTML כבוי
מעבר לפורום



כל הזמנים המוצגים בדף זה הם לפי איזור זמן GMT +2. השעה כעת היא 01:40

הדף נוצר ב 0.04 שניות עם 12 שאילתות

הפורום מבוסס על vBulletin, גירסא 3.0.6
כל הזכויות לתוכנת הפורומים שמורות © 2024 - 2000 לחברת Jelsoft Enterprises.
כל הזכויות שמורות ל Fresh.co.il ©

צור קשר | תקנון האתר