05-03-2014, 16:06
|
|
|
חבר מתאריך: 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
אשמח אם מישהו יוכל לעזור לי. אני רוצה שברגע שהמשתמש הכניס אנטר במהלך הפונקציה, אז שהיא תחזור.
|