编写一个函数,为用户提供5个选项的菜单:5个字符串

1,输出初始字符串列表 2,按ASCII顺序输出字符串 3,按长度递增顺序输出字符串 4,按字符串中第一个单词长度输出字符串 5,退出 

//main.cpp
#include<iostream>
#include"3.h"
using namespace std;
void main()
{
	char *str[5]={{"yang"},{"hai3i"},{"lin"},{"is"},{"a good person"}};
	

int choice=0;
cout<<"please enter 1~5 to choice\n";
while (cin>>choice)
{

   switch(choice)
	{
		case 1: 	print(str,5);	break;
		case 2:   		ASCII_sequence(str,5);break;
		case 3: len_sequence(str,5);break;
		case 4: firstword_sequence(str,5);break;
		case  5: back();break;
		default: break;

	}
}

}

//3.h
void print(char *s[],int n);
void ASCII_sequence(char *s[],int n);
void len_sequence(char *s[],int n);
int len_word(char *str,int n);
void firstword_sequence(char *s[],int n);

void back();

//33.cpp
#include<iostream>
using namespace std;

void print(char *s[],int n)
{
	cout<<"~~~~~~~~~~~~~print~~~~~~~~~~~~~~~~~~~~~~~\n";
	for(int i=0;i<n;i++)
		cout<<s[i]<<endl;
	cout<<"~~~~~~~~~~~~~print~~~~~~~~~~~~~~~~~~~~~~~\n";

}

void ASCII_sequence(char *s[],int n)
{
	cout<<"~~~~~~~~~~~~~ASCII_sequence~~~~~~~~~~~~~~~~~~~~~~~\n";
	char *tmp;
	for(int i=0;i<n-1;i++)
		for(int j=i+1;j<n;j++)
		{
			if (strcmp(s[i],s[j])>0)
			{	tmp=s[i];
			s[i]=s[j];
			s[j]=tmp;
			}
		}

		for(int m=0;m<n;m++)
			cout<<s[m]<<endl;
		cout<<"~~~~~~~~~~~~~ASCII_sequence~~~~~~~~~~~~~~~~~~~~~~~\n";
}

void len_sequence(char *s[],int n)
{
			cout<<"~~~~~~~~~~~~~len_sequence~~~~~~~~~~~~~~~~~~~~~~~\n";

	char *tmp;
	for(int i=0;i<n-1;i++)
		for(int j=i+1;j<n;j++)
			if(strlen(s[i])>strlen(s[j]))
			{
				tmp=s[i];
				s[i]=s[j];
				s[j]=tmp;
			}

			
				for(int m=0;m<n;m++)
			cout<<s[m]<<endl;
				cout<<"~~~~~~~~~~~~~len_sequence~~~~~~~~~~~~~~~~~~~~~~~\n";

}



int len_word(char *str,int n)
{
	int i=0;
	for( i=0;i<strlen(str);i++)
		if(str[i]==' ')
			break;
		return i;
   

}

void firstword_sequence(char *s[],int n)
{
			cout<<"~~~~~~~~~~~~~firstword_sequence~~~~~~~~~~~~~~~~~~~~~~~\n";

	char *tmp=NULL;
	for (int i=0;i<n-1;i++)
	  for(int j=i+1;j<n;j++)
		  if(len_word(s[i],n)>len_word(s[j],n))
		  {
			  tmp=s[i];
		     s[i]=s[j];
		     s[j]=tmp;
		  }

		  	for(int m=0;m<n;m++)
			cout<<s[m]<<endl;

			cout<<"~~~~~~~~~~~~~firstword_sequence~~~~~~~~~~~~~~~~~~~~~~~\n";


}

void back()
{
	exit(0);
}





 




你可能感兴趣的:(编程,c)