C++ implements sorting input number groups
- 2020-05-05 11:40:53
- OfStack
Ben is a very simple feature, and then just bubble sort. However, when I input the sequence interactively, I only use Spaces to separate them and then press enter. If there is an unlimited number of Numbers, scanf cannot complete this task. It loops to obtain, and at the end, it cannot judge the end of obtaining, but can only continue to wait for input.
At this point I define a custom function that takes the number in the cache (separated by Spaces) and returns a specific value if the input ends. This function is implemented using getchar loop nesting. I am a novice, can only make this method. Welcome to your guidance.
maopao-complex.c
// Compare the complex array receive method and then sort from large to small. VC The environment
#include <stdio.h>
#include <stdlib.h>
int over=0;// Used to read the number, if the read ends, is 1
int main(void)
{
// This is the part that receives the data and determines the number of data
int getvalue(void); // Function prototype. Returns the number retrieved from the buffer, and if the input ends, returns 42949672
int k; // Subloop temporary variables
int a[100]; // To establish 100 Number space, can increase shrink range
int num; // Number of inputs
int ifend = 1; // Logical variables, control while statements
int i=0; // General time - dependent temporary variable
int value; // Where element values are stored
printf(" Please enter any number of sequences, separated by Spaces: \n");
while (ifend)
{
value = getvalue();
if(value == 42949672)
ifend = 0;
else
{
a[i] = value;
i++;
}
} // After the end, i Number of group members
num = i;
// Start sorting
for (i=0; i < num-1; i++) // cycle 8 Time, the first n Once the first n The maximum value of the number after the bit is placed in order n a
{
for(k=i+1; k<num; k++) // with k That's the guys in the back, increasing.
{
if (a[i] < a[k]) // Choose the big number and put the number n position
{
a[i] = a[i] + a[k];
a[k] = a[i] - a[k];
a[i] = a[i] - a[k];// So in the three steps above, put the larger value into a[i]
}
}
}
// Output new array
printf(" After the order: \n");
for (i=0; i<num; i++)
{
printf("%d ",a[i]);
}
printf("\n");
return 0;
}
int getvalue(void)
{
char a[16]={0}; // Numbers receive temporary arrays
int k;
int value=0;
char temp;
int last = 1;
int ifend = 1;
int i = 0; // To locate the array
if (over == 1)
{
ifend = 0;
return (42949672);
}
temp = getchar();
while(ifend)
{
if (temp == 32)// After receiving, convert the temporary array to a number
{
value = atoi(a);
ifend = 0;
}
else if (temp == '\n')// The buffer has an infinite number of values
{
value = atoi(a);
over = 1;
ifend = 0;
}
else // Input effective value
{
if (temp == '-')
{
last = -1;
}
else
{
a[i] = temp;
i++;
}
temp = getchar();
}
}
value*=last;
return (value);
}
Here's a simple bubble sort:
#include <iostream>
using namespace std;
void BubbleSort (int* pData,int Count)
{
int iTemp;
for(int i=1; i<Count; i++)
{
for (int j=Count-1; j>=1; j--)
{
if (pData[j]<pData[j-1])
{
iTemp=pData[j-1];
pData[j-1]=pData[j];
pData[j]=iTemp;
}
}
}
}
void main()
{
int data[]= {10,9,8,7,6,5,4}
BubbleSort(data,7);
for (int i=0;i<7;i++)
cout<<data[i]<<" ";
cout<<"\n";
}