# include <iostream>
# include <iomanip>
# include <limits>
// c++17
using namespace std;
int main()
{
constexpr size_t size{1000};
double val[size] {};
size_t count {};
for(;;)
{
double input{};
cout << "Enter a non-zero value, or 0 to end: ";
cin >> input;
if (input < numeric_limits<double>::epsilon() && input > -numeric_limits<double>::epsilon())
// 输入 认为是0 退出
break;
val[count++] = input;
if (count == size)
{
cout << "Sorry, I can only store " << size << " values.\n";
break;
}
}
if (!count)
{
cout << "No data.";
return 0;
}
cout << "Starting sort." << endl;
while(1)
{
bool swapped {0};
for (size_t i{}; i < count-1; ++i)
{
if (val[i] >val[i+1])
{
swap(val[i], val[i+1]);
swapped = true;
}
}
if (!swapped)
break;
}
cout << "Your data in ascending sequence:\n"
<< fixed << setprecision(2);
for(size_t i{}; i< count ; ++i)
{
cout << setw(8) << val[i];
if (i% 10 == 9) cout << endl;
}
cout << endl;
}
|