Welcome~~~


Another blog:
http://fun-st.blogspot.com/

It is easier to write an incorrect program than understand a correct one.

Friday, February 25, 2011

My atoi C++

#include
#include

using namespace std;

int atoi(string str)
{
 bool negFlag = false;
 string::iterator my_iter;
 my_iter = str.begin();
 if (str.find("-")==0)
 {
  negFlag = true;
  my_iter++;
 }
 int num = 0;
 while (my_iter!=str.end())
 {
  num*=10;
  num+=(*my_iter-'0');
  my_iter++;
 }

 if(negFlag)
 {
 num*=-1;
 }
 return num;
}


int main()
{
 string mystr;
 cout << "Input your sting: " << endl;
 getline(cin, mystr, '\n');
 cout << mystr << endl;

    cout << atoi(mystr) << endl;
    cin.get();

 return 0;
}

No comments:

Post a Comment