רשימת תפוצה
הרשמו היום לרשימת התפוצה וקבלו:
• את כל השאלות והתשובות בקובץ מרוכז אחד.
• את כל המאמרים.
• הצעות עבודה חדשות.
הכנס אימייל:
• את כל השאלות והתשובות בקובץ מרוכז אחד.
• את כל המאמרים.
• הצעות עבודה חדשות.
הכנס אימייל:
maor
עבודה וראיון עבודה ב סי פלוס פלוס , שאלות מראיון עבודה בנושא C++.
- השאלה: ממש ITOA
התשובה:
זהו המימוש הנפוץ היעיל והנכון ביותר
דוגמאת קוד מצורפת:
char *itoa(int value)
{
int count, /* number of characters in string */
i, /* loop control variable */
sign; /* determine if the value is negative */
char *ptr, /* temporary pointer, index into string */
*string, /* return value */
*temp; /* temporary string array */
count = 0;
if ((sign = value) < 0) /* assign value to sign, if negative */
{ /* keep track and invert value */
value = -value;
count++; /* increment count */
}
/* allocate INTSIZE plus 2 bytes (sign and NULL) */
temp = (char *) malloc(INTSIZE + 2);
if (temp == NULL)
{
return(NULL);
}
memset(temp,' ', INTSIZE + 2);
string = (char *) malloc(INTSIZE + 2);
if (string == NULL)
{
return(NULL);
}
memset(string,' ', INTSIZE + 2);
ptr = string; /* set temporary ptr to string */
/*--------------------------------------------------------------------+
| NOTE: This process reverses the order of an integer, ie: |
| value = -1234 equates to: char [4321-] |
| Reorder the values using for {} loop below |
+--------------------------------------------------------------------*/
do {
*temp++ = value % 10 + '0'; /* obtain modulus and or with '0' */
count++; /* increment count, track iterations*/
} while (( value /= 10) >0);
if (sign < 0) /* add '-' when sign is negative */
*temp++ = '-';
*temp-- = ' '; /* ensure null terminated and point */
/* to last char in array */
/*--------------------------------------------------------------------+
| reorder the resulting char *string: |
| temp - points to the last char in the temporary array |
| ptr - points to the first element in the string array |
+--------------------------------------------------------------------*/
for (i = 0; i < count; i++, temp--, ptr++)
{
memcpy(ptr,temp,sizeof(char));
}
return(string);
}
דוגמאת קוד מצורפת:
char *itoa(int value)
{
int count, /* number of characters in string */
i, /* loop control variable */
sign; /* determine if the value is negative */
char *ptr, /* temporary pointer, index into string */
*string, /* return value */
*temp; /* temporary string array */
count = 0;
if ((sign = value) < 0) /* assign value to sign, if negative */
{ /* keep track and invert value */
value = -value;
count++; /* increment count */
}
/* allocate INTSIZE plus 2 bytes (sign and NULL) */
temp = (char *) malloc(INTSIZE + 2);
if (temp == NULL)
{
return(NULL);
}
memset(temp,' ', INTSIZE + 2);
string = (char *) malloc(INTSIZE + 2);
if (string == NULL)
{
return(NULL);
}
memset(string,' ', INTSIZE + 2);
ptr = string; /* set temporary ptr to string */
/*--------------------------------------------------------------------+
| NOTE: This process reverses the order of an integer, ie: |
| value = -1234 equates to: char [4321-] |
| Reorder the values using for {} loop below |
+--------------------------------------------------------------------*/
do {
*temp++ = value % 10 + '0'; /* obtain modulus and or with '0' */
count++; /* increment count, track iterations*/
} while (( value /= 10) >0);
if (sign < 0) /* add '-' when sign is negative */
*temp++ = '-';
*temp-- = ' '; /* ensure null terminated and point */
/* to last char in array */
/*--------------------------------------------------------------------+
| reorder the resulting char *string: |
| temp - points to the last char in the temporary array |
| ptr - points to the first element in the string array |
+--------------------------------------------------------------------*/
for (i = 0; i < count; i++, temp--, ptr++)
{
memcpy(ptr,temp,sizeof(char));
}
return(string);
}
• נתקלתם באתגר במקום העבודה שאינכם יודעים איך לפתור?
• רשמו כאן את השאלה ושלחו אותה אלינו.
• אנו נפתור אותה בשבילכם ונפרסם את הפתרון באתר.
• רשמו כאן את השאלה ושלחו אותה אלינו.
• אנו נפתור אותה בשבילכם ונפרסם את הפתרון באתר.
| שאלה בנושא : C++ |
![]() |
||||
| שם מלא : | כתובת אימייל : | ||||
| שאלה למאגר : | |||||
הצע שאלה
שלח שאלה


