#include<iostream>
#include<sstream>
#include<algorithm>
#include<string>
#include<stdlib.h>
using namespace std;
string lltostring(long long input);
long long rev_stringtoll(string my_string);
void rev_result(long long input);
int main(int argc, char* argv[])
{
long long input;
cout<<"输入翻转数值"<<endl;
cin>>input;
rev_result(input);
return 0;
}
void rev_result(long long input)
{
long long my_ll;
int symbol=1;
if(input>2147483647||input<-2147483648)
{
cout<<"输入数溢出"<<endl;
return;
}
if(input==0)
{
cout<<"翻转结果为:"<<endl<<"0"<<endl;
return;
}
if(input<0)
{
symbol=-1;
input=-input;
}
my_ll=rev_stringtoll(lltostring(input))*symbol;
if(my_ll>2147483647||my_ll<-2147483648)
{
cout<<"翻转数溢出"<<endl;
}
else
{
cout<<"翻转结果为:"<<endl<<my_ll<<endl;
}
}
long long rev_stringtoll(string my_string)
{
reverse(my_string.begin(),my_string.end());
return strtoll(my_string.c_str(),NULL,10);
}
string lltostring(long long input)
{
stringstream my_stream;
string res_string;
my_stream<<input;
my_stream>>res_string;
return res_string;
}
/*
//实现字符串翻转
char *reverse_str(char *str) {
if(NULL == str) { //字符串为空直接返回
return str;
}
char *begin;
char *end;
begin = end = str;
while(*end != ‘\0‘) { //end指向字符串的末尾
end++;
}
--end;
char temp;
while(begin < end) { //交换两个字符
temp = *begin;
*begin = *end;
*end = temp;
begin++;
end--;
}
return str; //返回结果
}
void main() {
char str[] = "123456";
printf(reverse_str(str));
}
*/