环境:Dev-C++( Version:5.6.1)
1.循环输入一个1-1000的整数,判断是否为素数(输入1时程序结束)
素数:只能被1和自身整除.
实现代码:
#include <iostream> #include <cstdlib> #include <cassert> using namespace std; /* *function name:isPrimeNumber. *precondition:parameter value is more than zero and less than or equal one thousand. *postcondition:return true when parameter value is prime number. */ bool isPrimeNumber(const int &number); int main() { while(true) { int number; cout<<"please input a integer number(0-1000):"<<endl; cin>>number; //condition assert(number>0&&number<=1000); if(number == 1) { break; } bool flag; flag = isPrimeNumber(number); if(flag) { cout<<"yes, the number is prime number."<<endl; } else { cout<<"no, the number is not prime number."<<endl; } } return EXIT_SUCCESS; } bool isPrimeNumber(const int &number) { for(int i=2;i<number/2+1;i++) { if(number%i == 0) { return false; } } return true; }
2.用类来实现输入和输出时间(时:分:秒)
要求:将数据成员改为私有;将输入和输出的功能有成员函数实现;在类体内定义成员函数
代码实现:
(对于时间输入的合理性,简单的采用断言assert函数来进行处理,不合理,程序直接结束运行)
1 #include <iostream> 2 #include <cstdlib> 3 #include <cassert> 4 using namespace std; 5 class Time 6 { 7 public: 8 void set_time() 9 { 10 cout<<"hour(0-23):"<<endl; 11 cin>>hour; 12 assert(hour>0&&hour<24); 13 14 cout<<"minute(0-59):"<<endl; 15 cin>>minute; 16 assert(minute>0&&minute<60); 17 18 cout<<"second(0-59):"<<endl; 19 cin>>sec; 20 assert(sec>0&&sec<60); 21 } 22 void show_time() 23 { 24 cout<<"time:"<<endl; 25 cout<<hour<<":"<<minute<<":"<<sec<<endl; 26 } 27 private: 28 int hour; 29 int minute; 30 int sec; 31 }; 32 int main() 33 { 34 Time t; 35 t.set_time(); 36 t.show_time(); 37 return EXIT_SUCCESS; 38 }
3.在上题的基础上进行修改:在类体内声明成员函数,而在类外定义成员函数
代码实现:
1 #include <iostream> 2 #include <cstdlib> 3 #include <cassert> 4 using namespace std; 5 class Time 6 { 7 public: 8 void set_time(); 9 void show_time(); 10 private: 11 int hour; 12 int minute; 13 int sec; 14 }; 15 void Time::set_time() 16 { 17 cout<<"hour(0-23):"<<endl; 18 cin>>hour; 19 assert(hour>0&&hour<24); 20 21 cout<<"minute(0-59):"<<endl; 22 cin>>minute; 23 assert(minute>0&&minute<60); 24 25 cout<<"second(0-59):"<<endl; 26 cin>>sec; 27 assert(sec>0&&sec<60); 28 } 29 void Time::show_time() 30 { 31 cout<<"time:"<<endl; 32 cout<<hour<<":"<<minute<<":"<<sec<<endl; 33 } 34 int main() 35 { 36 Time t; 37 t.set_time(); 38 t.show_time(); 39 return EXIT_SUCCESS; 40 }
4.求三个长方柱的体积
由成员函数完成如下功能:
1>由键盘分别输入3个长方柱的长,宽,高;
2>计算长方柱的体积;
3>输出3个长方柱的体积;
实现代码:
1 #include <iostream> 2 #include <cstdlib> 3 #include <cassert> 4 using namespace std; 5 class Box 6 { 7 public: 8 void set_box(); 9 void volume() 10 { 11 cout<<"volume :"<<length*width*height<<endl; 12 } 13 private: 14 double length; 15 double width; 16 double height; 17 }; 18 /* 19 *precondition:null 20 *postcondition:set instance data 21 */ 22 void Box::set_box() 23 { 24 cout<<"please enter the length, width and height of a rectangle:"<<endl; 25 cin>>length>>width>>height; 26 assert(length>0&&width>0&&height>0); 27 } 28 int main() 29 { 30 Box box1; 31 box1.set_box(); 32 box1.volume(); 33 34 Box box2; 35 box2.set_box(); 36 box2.volume(); 37 38 Box box3; 39 box3.set_box(); 40 box3.volume(); 41 return EXIT_SUCCESS; 42 }