This is a simpler function, and then just bubble sort. But I am in the interactive input series, only separated by a space and then enter, if not limited number of numbers, with scanf and can not complete this task, he cycled to get, to the end can not judge get end, and can only continue to wait for input.
This time I'm customizing a function that gets the number in the cache (space-delimited), and returns a specific value if the input ends, which is implemented using GetChar loops nested. I am novice, can only make this method. Welcome to the Great God guidance.
Maopao-complex.c
More complex array-receiving methods, and then sorted from large to small.
VC environment #include <stdio.h> #include <stdlib.h> int over=0;//is used to read number, if read ends, 1 int main (void) {//This part, receive data, judge the number of data int GetValue (void); The function prototype. Returns the number obtained from the buffer, and returns 42949672 int k if the input ends; Sub-loop temporary variable int a[100]; Establish 100-digit space, can shrink range int num; Number of inputs int ifend = 1; Logical variable, control while statement int i=0; General gauge temporary variable int value;
Element values are stored in printf ("Please enter any number of sequences, spaces separated by: \ n");
while (IfEnd) {value = GetValue ();
if (value = = 42949672) IfEnd = 0;
else {A[i] = value;
i++;
I is an array member number num = i;
Start sort for (i=0 i < num-1; i++)//Loop 8 times, Nth put the maximum number of nth digits after n {for (k=i+1; k<num; k++)///use K to indicate the following, increment.
{if (A[i] < a[k])//Select large number, put the nth bit {a[i] = A[i] + a[k];
A[K] = A[i]-a[k];
A[i] = a[i]-a[k];//above three steps, put the larger value to A[i]}}//Output new array printf ("After: \ n");
For (i=0 i<num; i++) {printf ("%d", a[i]);
printf ("\ n");
return 0; } int GetvaLue (void) {char a[16]={0};
The number receives the temporary array int k;
int value=0;
char temp;
int last = 1;
int ifend = 1; int i = 0;
Used to locate the array if (over = = 1) {IfEnd = 0;
return (42949672);
temp = GetChar ();
while (IfEnd) {if (temp = 32)//Receives the end, converts the temporary array to number {value = Atoi (a);
IfEnd = 0;
There is no value {value = Atoi (a) for the else if (temp = = \ n ')/buffer.
over = 1;
IfEnd = 0;
else//Enter a valid value of {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";
}