Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int str2i(char* ch)
- {
- int val = 0;
- for (int i = 0; ch[i] != '\0'; i++) {
- val = val * 10 + ch[i] - '0';
- }
- return val;
- }
- void i2str(char* ch, int n)
- {
- if (n == 0) {
- ch[0] = '0';
- ch[1] = '\0';
- } else {
- int digits[80] = { 0 };
- int ord = 0;
- while (n > 0) {
- digits[ord++] = n % 10;
- n /= 10;
- }
- ch[ord] = '\0';
- for (int i = 0; i < ord; i++) {
- ch[i] = digits[ord - 1 - i] + '0';
- }
- }
- }
- int main()
- {
- char buffer[80] = { '\0' };
- int n = 0;
- i2str(buffer, n);
- cout << buffer << endl;
- cout << str2i(buffer) << endl;
- n = 1;
- i2str(buffer, n);
- cout << buffer << endl;
- cout << str2i(buffer) << endl;
- n = 29876543;
- i2str(buffer, n);
- cout << buffer << endl;
- cout << str2i(buffer) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement