10个数反序输出的10种方法

方式一:

#include <iostream.h>

int main(int argc, char* argv[])
{
 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
 cout<<"Input 10 integers:"<<endl;
 cin>>a1>>a2>>a3>>a4>>a5>>a6>>a7>>a8>>a9>>a10;
 cout<<"---The result---"<<endl;
 cout<<a10<<" "<<a9<<" "<<a8<<" "<<a7<<" "<<a6<<" ";
 cout<<a5<<" "<<a4<<" "<<a3<<" "<<a2<<" "<<a1<<endl;

 return 0;
}

 

方式二:

#include <iostream.h>

int main(int argc, char* argv[])
{
 int a[10];
 int i = 0;
 cout<<"Input 10 integers:"<<endl;
 for(i = 0; i < 10; i++)
 {
  cin>>a[i];
 }
 cout<<"---The result---"<<endl;
 for(i = 9; i >=0; i--)
 {
  cout<<a[i]<<" ";
 }
 cout<<endl;

 

 return 0;
}

 

方式三:

#include <stdio.h>
#include <iostream.h>
#include <string.h>

int main(int argc, char* argv[])
{
 char s[80];
 cout<<"Input 10 integers:"<<endl;
 gets(s);
 cout<<"---The result---"<<endl;
 int nLastInd = strlen(s) - 1;
 while(nLastInd >= 0)
 {
  int nDat = 0;
  int nWeight = 1;
  while(nLastInd >= 0 && s[nLastInd] >= '0' && s[nLastInd] <= '9')
  {
   nDat += (s[nLastInd] - '0') * nWeight;
   nLastInd--;
   nWeight *= 10;
  }

  if(nLastInd >= 0)
  {
   char ch = s[nLastInd];
   if(ch == '-' || ch == '+')
   {
    nLastInd--;
    if(ch == '-')
    {
     nDat *= -1;
    }
   }
  }

  cout<<nDat<<" ";
  if(nLastInd < 0)
  {
   break;
  }

  while(nLastInd >= 0 && s[nLastInd] == ' ')
  {
   nLastInd--;
  }
 }
 cout<<endl;

 

 return 0;
}

 

方式四:

#include <stdio.h>
#include <iostream.h>
#include <string.h>

 

void func(int n)
{
 int i = 0;
 cin>>i;
 if(n > 1)
 {
  func(n - 1);
 }
 else
 {
  cout<<"---The result---"<<endl;
 }
 cout<<i<<" ";
}

 

int main(int argc, char* argv[])
{
 cout<<"Input 10 Integers:"<<endl;
 func(10);
 cout<<endl;

 

 return 0;
}

 

方式五:

#include <stdio.h>
#include <iostream.h>
#include <string.h>

 

int main(int argc, char* argv[])
{
 int i = 0;
 int* b;
 int* p;

 b = new int[10];
 cout<<"Input 10 Integers:"<<endl;
 for(i = 0; i < 10; i++)
 {
  cin>>*(b + i);
 }
 cout<<"---The result---"<<endl;
 for(p = b + 9; p >= b; p--)
 {
  cout<<*p<<" ";
 }
 cout<<endl;
 delete[] b;

 

 return 0;
}

 

方式六:

#include <stdio.h>
#include <iostream.h>
#include <string.h>

 

int main(int argc, char* argv[])
{

struct item
 {
  int nDat;
  item* next;
 };
 item* top = NULL;
 item* tmp = NULL;

 cout<<"Input 10 Integers:"<<endl;
 for(int i = 0; i < 10; i++)
 {
  int nTmp;
  cin>>nTmp;
  tmp = new item;

  tmp->nDat = nTmp;
  tmp->next = top;
  top = tmp;
 }
 cout<<"---The result---"<<endl;
 tmp = top;
 item* del = NULL;
 while(tmp != NULL)
 {
  del = tmp;
  cout<<del->nDat<<" ";
  tmp = tmp->next;
  delete del;
 }
 cout<<endl;


 return 0;
}

 

方式七:

#include <stdio.h>
#include <iostream.h>
#include <string.h>

 

int main(int argc, char* argv[])
{

struct item
 {
  int nDat;
  item* next;
 };
 item* front = NULL;
 item* rear = NULL;
 item* tmp = NULL;

 cout<<"Input 10 Integers:"<<endl;
 for(int i = 0; i < 10; i++)
 {
  int nTmp;
  cin>>nTmp;
  tmp = new item;
  tmp->nDat = nTmp;
  if(i == 0)
  {
   front = tmp;
   rear = tmp;
  }
  else
  {
   rear->next = tmp;
   rear = tmp;
  }
 }
 cout<<"---The result---"<<endl;
 for(int j = 9; j >=0; j--)
 {
  tmp = front;
  for(int k = 1; k <= j; k++)
  {
   tmp = tmp->next;
  }
  cout<<tmp->nDat<<" ";
  delete tmp;
 }
 cout<<endl;


 return 0;
}

 

方式八:

#include <iostream.h>

 

class myCls
{
private:
 int arr[10];

public:
 void myIn();
 void myOut();
};

 

void myCls::myIn()
{
 cout<<"Input 10 integers:"<<endl;
 for(int i = 0; i < 10; i++)
 {
  cin>>arr[i];
 }
}

 

void myCls::myOut()
{
 cout<<"---The result---"<<endl;
 for(int i = 9; i >= 0; i--)
 {
  cout<<arr[i]<<" ";
 }
 cout<<endl;
}

 

int main(int argc, char* argv[])
{
 myCls obj;
 obj.myIn();
 obj.myOut();

 

 return 0;
}

 

方式九:

#include <stdio.h>
#include <iostream.h>

#include <fstream.h>
#include <string.h>

 

int main(int argc, char* argv[])
{

const int n = 10;
 int a = 0;
 int i = 0;

 ofstream fout("fb.bin", ios::binary);
 cout<<"Input 10 Integers:"<<endl;
 for(i = 1; i <= n; i++)
 {
  cin>>a;
  fout.write((char*)&a, sizeof(int));
 }
 fout.close();

 cout<<"---The result---"<<endl;
 ifstream fin("fb.bin");
 for(i = n-1; i >= 0; i--)
 {
  fin.seekg(i * sizeof(int));
  fin.read((char*)&a, sizeof(int));
  cout<<a<<" ";
 }
 fin.close();
 cout<<endl;


 return 0;
}

 

方式十:

#include <stdio.h>
#include <iostream.h>

#include <fstream.h>
#include <string.h>

 

int main(int argc, char* argv[])
{

const int n = 10;
 int a = 0;
 int i = 0;
 ofstream fout("fa.txt");

 cout<<"Input 10 Integers:"<<endl;
 for(i = 1; i <= n; i++)
 {
  cin>>a;
  fout<<a<<" ";
 }
 fout.close();

 cout<<"---The result---"<<endl;
 ifstream fin("fa.txt");
 for(i = n; i >= 1; i--)
 {
  fin.seekg(0);
  for(int j = 1; j <= i; j++)
  {
   fin>>a;
  }
  cout<<a<<" ";
 }
 fin.close();
 cout<<endl;


 return 0;
}

 

你可能感兴趣的:(10个数反序输出的10种方法)