-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
149 lines (134 loc) · 3.67 KB
/
main.cpp
File metadata and controls
149 lines (134 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "main.hpp"
//test functions
//signed int input test
void test_01(){
std::cout<<"please enter an integer:"<<std::endl;
int value;
std::cin>>value;
if(value < 0){
std::cerr<<"invalid value!"<<std::endl;
std::clog<<"test_01()=========="<<std::endl;
std::cin>>value;
return;
}
//std::cerr<<"test_01()=========="<<std::endl;
}
//unsigned int input test
void test_02(){
unsigned int value;
std::cout<<"please enter an unsigned integer:"<<std::endl;
std::cin>>value;
std::cout<<"you have entered:"<<value<<std::endl;
}
//unsigned and signed int operation test
void test_03(){
unsigned int u=10,u2=42;
std::cout<<u2 - u<<std::endl;//32
std::cout<<u - u2<<std::endl;
int i=10,i2=42;
std::cout<<i2 - i<<std::endl;//32
std::cout<<i - i2<<std::endl;//-32
std::cout<<i - u<<std::endl;//0
std::cout<<u - i2<<std::endl;//4294967264
unsigned int u3=-1;
std::cout<<u3<<std::endl;//4294967295
}
//constexpr test
void test_04(){
constexpr double cm_per_inch=2.54;
double length=0;
std::cout<<"please enter a length in inches:"<<std::endl;
std::cin>>length;
std::cout<<length<<" inches = "<<cm_per_inch*length<<" centimeters."<<std::endl;
}
//namespace decltype test
void test_05(){
int ci = 0,&cj = ci;
decltype(ci) x = 0;
decltype(cj) y = x;
y=42;
std::cout<<"ci="<<ci<<",cj="<<cj<<",x="<<x<<",y="<<y<<std::endl;//ci=42,cj=42,x=42,y=42
}
//namespace test======================================================
void test_06(){
mynaspace::func();
mynaspace::mynasp2::func();
// using mynaspace::func;
}
void test_07(){
using std::string;
string s ;
std::cout<<"please enter a string:"<<std::endl;
getline(std::cin,s);
std::cout<<s<<std::endl;
if(s.empty()){
std::cerr<<"you entered an empty string!"<<std::endl;
}
}
//exception test
class DivideByZeroException : public std::exception {
public:
std::string warning;
std::string info;
std::string getWarning() const {
return warning+": "+info;
}
DivideByZeroException(){};
DivideByZeroException(const std::string& warn,const std::string& inf) : warning(warn),info(inf){
};
const char* what() const noexcept override {
return getWarning().c_str();
}
};
double divide(double a,double b){
if(b==0){
//throw std::runtime_error("division by zero!");
//throw std::runtime_error("Error: Division by zero");
throw DivideByZeroException("Error","Division by zero");
}
return a/b;
}
void test_08(){
int a=10;
int b=0;
try{
double c=divide(a,b);
std::cout<<"a/b="<<c<<std::endl;
}
catch(const DivideByZeroException &e){
std::cerr<<"Caught an exception: \x1b[31m"<<e.what()<<"\x1b[0m "<<std::endl;
}
catch(...){
throw;
}
}
//==========================================================================
std::string &test_09(){
static std::string str="Hello, World!";
return str;
}
std::vector<std::string> test_10(){
return {"one","two","three"};
}
//==============================main========================================
int main(){
//system("color 0D");
// test_01();
// test_02();
// test_03();
// test_04();
// test_05();
// test_06();
// test_07();
// test_08();
// std::string ref=test_09();
// std::cout<<"ref="<<ref<<std::endl;//ref=42 but ref
std::vector<std::string> vec=test_10();
for(const auto &s : vec){
std::cout<<s<<" ";
}
std::cout<<std::endl;
std::cin.get();
return EXIT_SUCCESS;
}
//=============================end==========================================