<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
请按任意键继续. . .