设计一主菜单,实现如下功能:
(1) 字母排序
设置输入字母的个数,输入字母元素后,可以按从小到大也可以按从大到小的顺序排列输出。
(2) 数字排序
设置输入数字的个数,输入数字元素后,可以按从小到大也可以按从大到小的顺序排列输出。并对这些数组元素进行求和,求积。
(3) 字符串排序
输入字符串,可以按从小到大也可以按从大到小的顺序排列输出。
(4) 帮助及注意
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define N 1000
using namespace std;
void start()
{
cout<<endl<<" ============================================================"<<endl;
cout<<endl<<" | ★ ★ ★ ★ ★ ★ ★ ★ |"<<endl;
cout<<endl<<" | 欢迎您使用本系统 |"<<endl;
cout<<endl<<" | |"<<endl;
cout<<endl<<" | AUTHOR---bigbigship |"<<endl;
cout<<endl<<" ============================================================"<<endl;
}
void help()
{
cout<<endl<<" 帮助说明: "<<endl;
cout<<endl<<" 1.程序可以从大到小也可以从小到大输出 "<<endl;
cout<<endl<<" 2.字母排序需注意字母输入个数 "<<endl;
cout<<endl<<" 3.数字可以进行排序,求和,求积最小公倍数 "<<endl;
cout<<endl<<" 4.字符串排序有字符本身排序和多个字符串, "<<endl;
cout<<endl<<" 并且子啊输入时可以任意输入 "<<endl;
}
void menu()
{
cout<<endl<<" menu"<<endl;
cout<<endl<<" 1.字母排序"<<endl;
cout<<endl<<" 2.数字排序"<<endl;
cout<<endl<<" 3.字符串排序"<<endl;
cout<<endl<<" 4.帮助及注意"<<endl;
cout<<endl<<" 5.退出"<<endl;
cout<<" Please input your order: ";
}
char a[N];
int b[N];
bool cmp1(int a,int b)
{
return a>b;
}
void sortchar()
{
char x;
int n;
cout<<" K.从小到大排序"<<endl;
cout<<" L.从大到小排序"<<endl;
cout<<" 请输入您要排的顺序: ";
cin>>x;
cout<<endl<<" 请输入您要排的字符的个数: ";
cin>>n;
cout<<endl<<" 请输入您要排的字符: ";
for(int i=0;i<n;i++)
cin>>a[i];
if(x=='K'){
sort(a,a+n);
cout<<endl<<" 该字符从小到大输出:"<<endl<<" ";
cout<<a;
}
else{
sort(a,a+n,cmp1);
cout<<endl<<" 该字符从大到小输出:"<<endl<<" ";
cout<<a;
}
cout<<endl<<" "<<"Do you want to continue sorting?(Y/N)"<<" ";
cin>>x;
cout<<endl;
if(x=='Y')
sortchar();
else
return;
}
int gcd(int a,int b)
{
if(b)
return gcd(b,a%b);
return a;
}
int lcm(int a,int b)
{
int x=gcd(a,b);
return a*b/x;
}
void sortnum()
{
int n,d,ans;
char x;
cout<<" K.从小到大排序"<<endl;
cout<<" L.从大到小排序"<<endl;
cout<<" 请输入您要排的顺序: ";
cin>>x;
cout<<endl<<" 请输入您要排的字符的个数: ";
cin>>n;
cout<<endl<<" 请输入您要排的字符: ";
for(int i=0;i<n;i++)
cin>>b[i];
if(x=='K'){
sort(b,b+n);
cout<<endl<<" 该字符从小到大输出:"<<endl<<" ";
for(int i=0;i<n;i++)
cout<<b[i]<<" ";
cout<<endl;
}
else{
sort(b,b+n,cmp1);
cout<<endl<<" 该字符从大到小输出:"<<endl<<" ";
for(int i=0;i<n;i++)
cout<<b[i]<<" ";
cout<<endl;
}
cout<<" 你还可以对这组数进行求和,求积,求最小公倍数"<<endl;
cout<<" n.求和 m.求积 p.求最小公倍数 q.退出"<<endl<<" ";
cin>>x;
if(x=='n'){
ans=0;
for(int i=0;i<n;i++)
ans+=b[i];
cout<<endl<<"它们的和: "<<ans<<endl;
}
if(x=='m'){
ans=1;
for(int i=0;i<n;i++)
ans*=b[i];
cout<<endl<<"它们的积: "<<ans<<endl;
}
if(x=='p'){
ans=1;
for(int i=0;i<n;i++)
ans=lcm(ans,b[i]);
cout<<endl<<" 它们的最小公倍数: "<<ans<<endl;
}
if(x=='q'){
cout<<endl<<" "<<"请问要继续排序吗?(Y/N)"<<" ";
cin>>x;
cout<<endl;
if(x=='Y')
sortnum();
else
return;
}
}
void sortstr()
{
char x;
int l;
cout<<" 1.字符串本身排序"<<endl;
cout<<" 2.多个字符串排序"<<endl<<" ";
cin>>l;
cout<<" K.从小到大排序"<<endl;
cout<<" L.从大到小排序"<<endl;
cout<<" 请输入您要排的顺序: ";
cin>>x;
if(l==1){
cout<<endl<<" 请输入您要排的字符串: ";
cin>>a;
int len=strlen(a);
if(x=='K')
sort(a,a+len);
else
sort(a,a+len,cmp1);
cout<<a<<endl;
}
else{
int num;
cout<<endl<<" 请输入您要排的字符串个数: ";
cin>>num;
cout<<endl<<" 请输入您要排的字符串: ";
if(x=='K'){
while(num--){
cin>>a;
int len=strlen(a);
sort(a,a+len);
cout<<" "<<a<<endl;
}
}
else{
while(num--){
cin>>a;
int len=strlen(a);
sort(a,a+len);
cout<<" "<<a<<endl;
}
}
}
cout<<endl<<" "<<"请问要继续排序吗?(Y/N)"<<" ";
cin>>x;
cout<<endl;
if(x=='Y')
sortstr();
else
return;
}
int main()
{
start();
menu();
int order;
while(cin>>order){
if(order<1||order>5){
cout<<" input error ,please input again ";
continue;
}
switch(order)
{
case 1: sortchar();break;
case 2: sortnum();break;
case 3: sortstr();break;
case 4: help();break;
case 5: return 0;
}
menu();
}
return 0;
}
代码无版权 可以任意修改,共同进步