找出以“,”分割最的数值字符串里除了大值与最小值以外的数字个数

<span style="font-family: Arial, Helvetica, sans-serif;">#include<iostream></span>
#include<string>
#include <iomanip>
using std::setw;

using std::cin;
using std::cout;
using std::endl;

using std::string;

#include <vector> 
using std::vector;

void outputVector( const vector< int > &array )
   {
       size_t i; // declare control variable
 
      for ( i = 0; i < array.size(); i++ )
      {
          cout << setw( 4 ) << array[ i ];
        if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output
            cout << endl;
      } // end for

     if ( i % 4 != 0 )
        cout << endl;
    } // end function outputVector
int findMax(const vector<int> & array)
{
	int max=  array[0];
	int min=  array[0];
    for (int  i = 0; i < array.size(); i++ )
    { 
		if(array[i]>max)
		{
			max=array[i];
		}
		if(array[i]<min)
		{
			min=array[i];
		}
    } // end for
	return max;

}
int findMin(const vector<int> & array)
{
	int max=  array[0];
	int min=  array[0];
    for (int  i = 0; i < array.size(); i++ )
    { 
		if(array[i]>max)
		{
			max=array[i];
		}
		if(array[i]<min)
		{
			min=array[i];
		}
    } // end for
	return min;

}

 

int main()
{
	cout<<"Enter numbers devided by ,:";
	string s;
	cin>>s;
	cout<<"The numbers you input are:"<<s<<endl;

	const char *const_p=s.c_str();
	char *p=(char *)malloc(sizeof(char)*strlen(const_p));

	strcpy(p,const_p);


	cout<<"now start analyzing..."<<endl;

	int numCount=0;
	vector< int > integers1( 0); 

	char *p_str=NULL;
	 
	p_str=strtok(p,",");
	do
	{

		integers1.push_back(atoi(p_str));
		numCount++;
		p_str=strtok(NULL,",");


	}while(p_str!=NULL);
	cout<<"Your numbers are:"<<endl;
	outputVector(integers1);

	const int max=findMax(integers1);
	const int min=findMin(integers1);

	cout<<"MAX:"<<max<<endl;
	cout<<"MIN:"<<min<<endl;

	//now find the numbers that are not max or min
	int count=0;
	 for(int i=0;i<integers1.size();i++)
	 {
		 if(integers1[i]!=max && integers1[i]!=min)
		 {
			 count++;
		 }
	 }
	cout<<"Result:"<<count<<endl;


	system("pause");
	return 0;
}


输入和输出:

Enter numbers devided by ,:3,3,5,3,6,9,7,9
The numbers you input are:3,3,5,3,6,9,7,9
now start analyzing...
Your numbers are:
   3   3   5   3
   6   9   7   9
MAX:9
MIN:3
Result:3
请按任意键继续. . .



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值