赞
踩
#include <iostream> #include <string> using namespace std; int main(){ string str[80], temp; int i, j, n; cout<<"Please enter string number: "; cin>>n; for(i = 0; i < n; i++){ cout<<"Please enter No."<<i+1<<" string: "; cin>>str[i]; } for(i = 0; i<n; i++) for(j=i+1; j<n; j++) if (str[i]>str[j]){ temp=str[i], str[i]=str[j], str[j]=temp; } for (i=0, cout<<"Sort by\n"; i<n; cout<<str[i++]<<endl); system("pause"); return 0; }
#include <iostream> #include <string> using namespace std; const int N = 3; void inputStr(string *str, int n){ cout<<"Enter "<<n<<" Strings: "<<endl; for(int i = 0; i < n; i++){ cout<<"Enter No."<<i + 1<<" Strings: "; getline(cin, str[i]); } } void sortStr(string *str, int n){ string temp; for(int i = 0; i < n; i++){ for(int j = i + 1; j < n; j++){ if(str[i] > str[j]){ temp = str[i]; str[i] = str[j]; str[j] = temp; } } } } void outputStr(string *str, int n){ cout<<"Sort String: "<<endl; for(int i = 0; i < n; i++){ cout<<str[i]<<endl; } cout<<endl; } int main(){ string *str = new string[N]; inputStr(str, N); sortStr(str, N); outputStr(str, N); delete []str; system("pause"); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。