[学习笔记]C++17学习第10天
```# include <iostream>
# include <iomanip>
# include <cctype>
// c++ 17
using namespace std;
int main()
{
size_t table{};
const size_t table_min {2};
const size_t table_max {12};
char reply {};
do{
cout << "What size table would you like (" << table_min << " to " << table_max << ")? ";
cin >> table;
cout << endl;
if (table < table_min || table > table_max)
{
cout << "Invalid table size entered. Program terminated." << endl;
return 1;
}
//// 表头
cout << setw(6) <<" |";
for(size_t i{1}; i<=table;++i)
{
cout << " " << setw(3) << i << " |";
}
cout << endl;
//// 表头横线
for(size_t i{}; i<= table; ++i)
{
cout << "------" ;
}
cout << endl;
///// 横列
for (size_t row{1}; row <= table; ++ row)
{
//每列开头
cout << " " << setw(3) << row << " |";
//
for(size_t col{1}; col <= table; ++col)
{
cout << " " << setw(3) << row * col << " |";
}
// 列尾
cout << endl;
}
cout << "\nDo you want another table (y or n)? ";
cin >> reply;
}while(tolower(reply) == 'y');
}
```
```
# include <iostream>
# include <cctype>
# include <iomanip>
# include <limits>
// c++17
using namespace std;
int main()
{
cout << setw(11) <<"Character " << setw(13) <<"Hexadecimal "<< setw(9) << "Decimal " << endl;
cout << uppercase;
unsigned char ch{};
do{
if (!isprint(ch)) continue;
cout << setw(6) << ch
<< hex << setw(12) << static_cast<int>(ch)
<< dec << setw(10) << static_cast<int>(ch)
<< endl;
}while(ch++ < numeric_limits<unsigned char>::max());
}
```
```
# include <iostream>
# include <iomanip>
# include <limits>
// c++17
using namespace std;
int main()
{
constexpr size_t size{1000};
double val {};
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 = 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 >val)
{
swap(val, val);
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;
if (i% 10 == 9)cout << endl;
}
cout << endl;
}
```
谢谢楼主分享,思路清晰,讲解简洁{:1_919:} 让我们一起学习。 一起学习进步。 支持楼主,一起学习! {:1_927:}冲冲冲,楼主分享实用,互勉 谢谢分享, 一起学习 !
页:
[1]
2