C++中逆序输出字符串和数字

来源:互联网 发布:天穹网络 编辑:程序博客网 时间:2024/06/09 14:21
#include<iostream>#include<string>#include<LIST>using namespace std;int main(){   //字符串逆序//  string string1;//  int n,i,temp;//  cin>>string1;// //  n=string1.size();// //  for(i = 0; i < n / 2; i++)//  {//   temp=string1[i];//   string1[i]=string1[n-i-1];//   string1[n-i-1]=temp;//  }// //  cout<<string1<<endl;// ******************************************************************************************************************************//// 用list实现字符串的逆置// ******************************************************************************************************************************//// list<char> cLst;   //存储输入的字符串的每个字符// list<char> cCvLst; //存储逆置后的字符串的每个字符// string str;// cout<<"Please a word:"<<endl;// cin>>str;// // //解析将输入的字符串分解成每个字符存入cLst// for(list<char>::size_type index = 0; index != str.length(); ++index)// {// cLst.push_back(str[index]);// }// // // cout<<"Original word: "<<endl;// for(list<char>::iterator cIt = cLst.begin(); cIt != cLst.end(); ++cIt)// {// cout<<*cIt<<"\t";// }// cout<<endl;// // //用copy和front_insert实现逆置// copy(cLst.begin(), cLst.end(), front_inserter(cCvLst));// // cout<<"Converted!"<<endl;// for(cIt = cCvLst.begin(); cIt != cCvLst.end(); ++cIt)// {// cout<<*cIt<<"\t";// }// cout<<endl; //数字逆序 //位数 int digits = 1;  int testDig = 0; cin>>testDig;    int tmp = testDig; if(abs(testDig) > 10) {  while(abs(tmp) > 10)  {   tmp = abs(tmp) / 10;   digits++;  } } cout<<digits<<endl; //逆序输出 tmp = testDig; for(int cycle = 0; cycle < digits; ++cycle) {  cout<<abs(tmp) %10;  tmp = abs(tmp) / 10;   } if(testDig < 0) {  cout<<"-"<<endl; } system("pause"); return 0;}