logo

המרת מחרוזת למספר שלם ב-C++

חלק זה ידון בשיטות השונות להמרת נתוני המחרוזת הנתונים למספר שלם באמצעות שפת התכנות C++. ישנם כמה מצבים או מקרים שבהם אנחנו צריכים להמיר נתונים מסויימים לסוג אחר, ומצב אחד כזה הוא להמיר מחרוזת לנתוני int בתכנות.

לדוגמה, יש לנו מחרוזת מספרית בתור ' 143 ', ואנחנו רוצים להמיר אותו לסוג מספרי. עלינו להשתמש בפונקציה שממירה מחרוזת למספר שלם ומחזירה את הנתונים המספריים כ-143. כעת, נלמד כל שיטה שעוזרת להמיר נתוני מחרוזת למספרים שלמים בשפת התכנות C++.

המרת מחרוזת למספר שלם ב-C++

שיטות שונות להמרת נתוני המחרוזת למספרים שלמים בשפת התכנות C++.

  1. שימוש במחלקה stringstream
  2. שימוש בפונקציה stoi()
  3. שימוש בפונקציה atoi()
  4. שימוש בפונקציה sscanf()

שימוש במחלקה stringstream

ה stringstream היא מחלקה המשמשת להמרת מחרוזת מספרית לסוג int. המחלקה stringstream מכריזה על אובייקט זרימה כדי להוסיף מחרוזת כאובייקט זרימה ולאחר מכן מחלצת את נתוני המספרים השלמים המומרים על סמך הזרמים. למחלקה stringstream יש אופרטורים '<>', המשמשים לאחזור נתונים מהאופרטור השמאלי (<>).

בואו ניצור תוכנית להדגמת מחלקת stringstream להמרת נתוני המחרוזת למספר שלם בשפת התכנות C++.

עלייה מנאסה

Program1.cpp

 #include #include // use stringstream class using namespace std; int main() { string str1 = &apos;143&apos;; // declare a string int intdata; // declare integer variable /* use stringstream class to declare a stream object to insert a string and then fetch as integer type data. */ stringstream obj; obj &lt;&gt; intdata; // fetch integer type data cout &lt;&lt; &apos; The string value is: &apos; &lt;&lt; str1 &lt;&lt; endl; cout &lt;&lt; &apos; The representation of the string to integer type data is: &apos; &lt;&lt; intdata &lt;&lt; endl; return 0; } 

תְפוּקָה

 The string value is: 143 The representation of the string to integer type data is: 143 

בתוכנית לעיל, אנו משתמשים במחלקה stringstream כדי ליצור אובייקט obj, וזה עוזר להמיר נתוני מחרוזת למספר שלם. לאחר מכן אנו משתמשים באופרטור '<>' כדי לחלץ את המחרוזת שהומרה מ-obj לנתונים מספריים.

שימוש בפונקציה sscanf()

פונקציה sscanf() ממירה מחרוזת נתונה לסוג נתונים מוגדר כמו מספר שלם.

ג'אווה דייט עכשיו

תחביר

 sccanf ( str, %d, &amp;intvar); 

לפונקציה sscanf() יש שלושה ארגומנטים לציון מחרוזת ה-char (str), מפרט הנתונים (%d), והמשתנה השלם (&intvar) כדי לאחסן את המחרוזת שהומרה.

אלגוריתם של הפונקציה sscanf()

  1. הפונקציה sscanf() שייכת למחלקה stringstream, ולכן עלינו לייבא את המחלקה לתוכנית שלנו.
  2. אתחול מחרוזת תווים קבועה str.
  3. צור משתנה של מספר שלם כדי להחזיק את המחרוזת שהומרה לערכים שלמים.
  4. העבירו את משתנה המחרוזת לפונקציה sscanf() והקצה את הפונקציה sscanf() למשתנה השלם כדי לאחסן את הערך השלם שנוצר על ידי הפונקציה.
  5. הדפס את ערכי המספרים השלמים.

הבה נשקול דוגמה לשימוש בפונקציה sscanf() כדי להמיר את המחרוזת למספר מספרי ב-C++.

Program2.cpp

 #include #include // use stringstream class using namespace std; int main () { // declare the character strings const char *str1 = &apos;555&apos;; const char *str2 = &apos;143&apos;; const char *str3 = &apos;101&apos;; // declare the integer variables int numdata1; int numdata2; int numdata3; /* use sscanf() function and to pass the character string str1, and an integer variable to hold the string. */ sscanf (str1, &apos;%d&apos;, &amp;numdata1); cout &lt;<' the value of character string is: ' << str1; cout '
 representation to int numdata1 <<endl; sscanf (str2, '%d', &numdata2); <<'
 str2; numdata2 (str3, &numdata3); str3; numdata3 return 0; } < pre> <p> <strong>Output</strong> </p> <pre> The value of the character string is: 555 The representation of string to int value of numdata is: 555 The value of the character string is: 143 The representation of string to int value of numdata is: 143 The value of the character string is: 101 The representation of string to int value of numdata is: 101 </pre> <h3>Using the stoi() function</h3> <p>The stoi() function converts a string data to an integer type by passing the string as a parameter to return an integer value.</p> <p> <strong>Syntax</strong> </p> <pre> stoi(str); </pre> <p>The stoi() function contains an str argument. The str string is passed inside the stoi() function to convert string data into an integer value.</p> <p> <strong>Algorithm of the stoi() function</strong> </p> <ol class="points"> <li>Initialize the string variable to store string values.</li> <li>After that, it creates an integer type variable that stores the conversion of string into integer type data using the stoi() function.</li> <li>Print the integer variable value.</li> </ol> <p>Let&apos;s create a program to use the stoi() function to convert the string value into the integer type in the C++ programming language.</p> <p> <strong>Program3.cpp</strong> </p> <pre> #include #include using namespace std; int main () { string strdata1 = &apos;108&apos;; string strdata2 = &apos;56.78&apos;; string strdata3 = &apos;578 Welcome&apos;; // use stoi() function to pass string as arguments int intdata1 = stoi (strdata1); int intdata2 = stoi (strdata2); int intdata3 = stoi (strdata3); // print the converted values cout &lt;&lt; &apos; The conversion of string to an integer using stoi(&apos;&apos; &lt;&lt; strdata1 &lt;&lt; &apos;&apos;) is &apos; &lt;&lt; intdata1 &lt;<endl; cout << ' the conversion of string to an integer using stoi(\'' strdata2 '\') is intdata2 <<endl; strdata3 intdata3 return 0; } < pre> <p> <strong>Output</strong> </p> <pre> The conversion of string to an integer using stoi(&apos;108&apos;) is 108 The conversion of string to an integer using stoi(&apos;56.78&apos;) is 56 The conversion of string to an integer using stoi(&apos;578 Welcome&apos;) is 578 </pre> <h3>Using the atoi() function</h3> <p>An atoi() function is used to convert the character string into an integer value. An atoi() function passes the character type string to return the integer data.</p> <p> <strong>Syntax</strong> </p> <pre> atoi (const char *str); </pre> <p> <strong>Algorithm of the atoi() function</strong> </p> <ol class="points"> <li>Initialize a pointer-type character array to store a string.</li> <li>After that, it creates an integer type variable that stores the conversion of string into integer type data using the atoi() function.</li> <li>Print the integer variable value.</li> </ol> <p>Let&apos;s create a program to use the atoi() function to convert the string value into the integer type in the C++ programming language.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include using namespace std; int main () { // declare a constant character array of string const char *strdata1 = &apos;256&apos;; const char *strdata2 = &apos;16.18&apos;; const char *strdata3 = &apos;1088 Good Bye&apos;; // use atoi() function to pass character type array as arguments int intdata1 = atoi (strdata1); int intdata2 = atoi (strdata2); int intdata3 = atoi (strdata3); // print the converted values cout &lt;&lt; &apos; The conversion of string to an integer value using atoi(&apos;&apos; &lt;&lt; strdata1 &lt;&lt; &apos;&apos;) is &apos; &lt;&lt; intdata1 &lt;<endl; cout << ' the conversion of string to an integer value using atoi(\'' strdata2 '\') is intdata2 <<endl; strdata3 intdata3 return 0; } < pre> <p> <strong>Output</strong> </p> <pre> The conversion of string to an integer value using atoi(&apos;256&apos;) is 256 The conversion of string to an integer value using atoi(&apos;16.18&apos;) is 16 The conversion of string to an integer value using atoi(&apos;1088 Good Bye&apos;) is 1088 </pre> <hr></endl;></pre></endl;></pre></'>

שימוש בפונקציה stoi()

הפונקציה stoi() ממירה נתוני מחרוזת לסוג של מספר שלם על ידי העברת המחרוזת כפרמטר להחזרת ערך שלם.

תחביר

 stoi(str); 

הפונקציה stoi() מכילה ארגומנט str. המחרוזת str מועברת בתוך הפונקציה stoi() כדי להמיר נתוני מחרוזת לערך שלם.

אלגוריתם של הפונקציה stoi()

רשימת גופני gimp
  1. אתחול משתנה המחרוזת לאחסון ערכי מחרוזת.
  2. לאחר מכן, הוא יוצר משתנה מסוג מספר שלם המאחסן את ההמרה של מחרוזת לנתונים מסוג מספר שלם באמצעות הפונקציה stoi() .
  3. הדפס את ערך המשתנה של מספר שלם.

בואו ניצור תוכנית שתשתמש בפונקציה stoi() כדי להמיר את ערך המחרוזת לסוג המספרים השלמים בשפת התכנות C++.

Program3.cpp

 #include #include using namespace std; int main () { string strdata1 = &apos;108&apos;; string strdata2 = &apos;56.78&apos;; string strdata3 = &apos;578 Welcome&apos;; // use stoi() function to pass string as arguments int intdata1 = stoi (strdata1); int intdata2 = stoi (strdata2); int intdata3 = stoi (strdata3); // print the converted values cout &lt;&lt; &apos; The conversion of string to an integer using stoi(&apos;&apos; &lt;&lt; strdata1 &lt;&lt; &apos;&apos;) is &apos; &lt;&lt; intdata1 &lt;<endl; cout << \' the conversion of string to an integer using stoi(\'\' strdata2 \'\') is intdata2 <<endl; strdata3 intdata3 return 0; } < pre> <p> <strong>Output</strong> </p> <pre> The conversion of string to an integer using stoi(&apos;108&apos;) is 108 The conversion of string to an integer using stoi(&apos;56.78&apos;) is 56 The conversion of string to an integer using stoi(&apos;578 Welcome&apos;) is 578 </pre> <h3>Using the atoi() function</h3> <p>An atoi() function is used to convert the character string into an integer value. An atoi() function passes the character type string to return the integer data.</p> <p> <strong>Syntax</strong> </p> <pre> atoi (const char *str); </pre> <p> <strong>Algorithm of the atoi() function</strong> </p> <ol class="points"> <li>Initialize a pointer-type character array to store a string.</li> <li>After that, it creates an integer type variable that stores the conversion of string into integer type data using the atoi() function.</li> <li>Print the integer variable value.</li> </ol> <p>Let&apos;s create a program to use the atoi() function to convert the string value into the integer type in the C++ programming language.</p> <p> <strong>Program4.cpp</strong> </p> <pre> #include #include using namespace std; int main () { // declare a constant character array of string const char *strdata1 = &apos;256&apos;; const char *strdata2 = &apos;16.18&apos;; const char *strdata3 = &apos;1088 Good Bye&apos;; // use atoi() function to pass character type array as arguments int intdata1 = atoi (strdata1); int intdata2 = atoi (strdata2); int intdata3 = atoi (strdata3); // print the converted values cout &lt;&lt; &apos; The conversion of string to an integer value using atoi(&apos;&apos; &lt;&lt; strdata1 &lt;&lt; &apos;&apos;) is &apos; &lt;&lt; intdata1 &lt;<endl; cout << \' the conversion of string to an integer value using atoi(\'\' strdata2 \'\') is intdata2 <<endl; strdata3 intdata3 return 0; } < pre> <p> <strong>Output</strong> </p> <pre> The conversion of string to an integer value using atoi(&apos;256&apos;) is 256 The conversion of string to an integer value using atoi(&apos;16.18&apos;) is 16 The conversion of string to an integer value using atoi(&apos;1088 Good Bye&apos;) is 1088 </pre> <hr></endl;></pre></endl;>

שימוש בפונקציה atoi().

פונקציה atoi() משמשת להמרת מחרוזת התווים לערך מספר שלם. פונקציה atoi() מעבירה את מחרוזת סוג התווים כדי להחזיר את הנתונים שלמים.

תחביר

 atoi (const char *str); 

אלגוריתם של הפונקציה atoi().

  1. אתחול מערך תווים מסוג מצביע כדי לאחסן מחרוזת.
  2. לאחר מכן, הוא יוצר משתנה מסוג מספר שלם המאחסן את ההמרה של מחרוזת לנתונים מסוג מספר שלם באמצעות הפונקציה atoi() .
  3. הדפס את ערך המשתנה של מספר שלם.

בואו ניצור תוכנית שתשתמש בפונקציה atoi() כדי להמיר את ערך המחרוזת לסוג המספרים השלמים בשפת התכנות C++.

Program4.cpp

 #include #include using namespace std; int main () { // declare a constant character array of string const char *strdata1 = &apos;256&apos;; const char *strdata2 = &apos;16.18&apos;; const char *strdata3 = &apos;1088 Good Bye&apos;; // use atoi() function to pass character type array as arguments int intdata1 = atoi (strdata1); int intdata2 = atoi (strdata2); int intdata3 = atoi (strdata3); // print the converted values cout &lt;&lt; &apos; The conversion of string to an integer value using atoi(&apos;&apos; &lt;&lt; strdata1 &lt;&lt; &apos;&apos;) is &apos; &lt;&lt; intdata1 &lt;<endl; cout << \' the conversion of string to an integer value using atoi(\'\' strdata2 \'\') is intdata2 <<endl; strdata3 intdata3 return 0; } < pre> <p> <strong>Output</strong> </p> <pre> The conversion of string to an integer value using atoi(&apos;256&apos;) is 256 The conversion of string to an integer value using atoi(&apos;16.18&apos;) is 16 The conversion of string to an integer value using atoi(&apos;1088 Good Bye&apos;) is 1088 </pre> <hr></endl;>