C++第二次作业
2016-04-01 22:24
477 查看
一:
#include <iostream> using namespace std; class Time { public: void set_time( ); void show_time( ); private: bool is_time(int h, int m, int s); //这个成员函数设置为私有的,是合适的,请品味 int hour; int minute; int sec; }; void Time::set_time( ) { char c1,c2; cout<<"请输入时间(格式hh:mm:ss)"; while(1) { cin>>hour>>c1>>minute>>c2>>sec; if(c1!=':'||c2!=':') cout<<"格式不正确,请重新输入"<<endl; else if (!is_time(hour,minute,sec)) cout<<"时间非法,请重新输入"<<endl; else break; } } void Time::show_time( ) { cout<<hour<<":"<<minute<<":"<<sec<<endl; } bool Time::is_time(int h,int m, int s) { if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60) return false; return true; } int main( ) { Time t1; t1.set_time( ); T1.show_time( ); return 0; }
二:<pre name="code" class="cpp">#include<iostream> using namespace std; class NaturalNumber {private: int n; public: void setValue (int x);//置数据成员n的值,要求判断是否是正整数 int getValue(); //返回私有数据成员n的值 bool isPrime(); //判断数据成员n是否为素数,是返回true,否则返回false void printFactor(); //输出数据成员n的所有因子,包括1和n自身 bool isPerfect(); //判断数据成员n是否为完全数。若一个正整数n的所有小于n的因子之和等于n, 则称n为完全数, 如6=1+2+3是完全数。 bool isReverse(int x);//判断形式参数x是否为数据成员n的逆向数(例321是123的逆向数)。 bool isDaffodil(int x); //判断形式参数x是否是水仙花数。水仙花数的各位数字立方和等于该数,如153=1*1*1+5*5*5+3*3*3 void print Daffodils(); //显示所有大于1,且小于数据成员n的水仙花数; }; void main(void) { NaturalNumber nn; //定义类的一个实例(对象) nn.setValue (6); cout<<nn.getValue()<<(nn.isPrime()?”是”:”不是”)<<”素数” <<endl; nn.setValue (37); cout<<nn.getValue()<<(nn.isPrime()?”是”:”不是”)<<”素数” <<endl; nn.setValue (84); cout<<nn.getValue()<<”的因子有:”; printFactor(); //随着成员函数的实现,增加代码以完成相关的测试。注意判断类的成员函数需要测试是或否两种情况…… } //请在下面定义类中的各个成员函数
三;
<pre name="code" class="cpp">#include<iostream> #include<string> using namespace std; class Stu { private: string name; //学生姓名 float chinese; //语文成绩 float math; //数学成绩 //接下去写 }; int main() { Stu s1,s2; s1.setStudent("Lin daiyu", 98, 96); //对象置初值 s2.setStudent("Jia baoyu", 90, 88); //对象置初值 s1.show();//打印信息 s2.show();//打印信息 s1.setName("xue baochai");//重新置p1对象的名字 s1.show(); cout<<"s1.Name: "<<s1.getName()<<endl;//打印对象的名字 cout<<"s1.average: "<<s1.average()<<endl;//打印对象的成绩 return 0; }
四:
<pre name="code" class="cpp">class CFraction { private: int nume; // 分子 int deno; // 分母 public: CFraction(int nu=0,int de=1); //构造函数,初始化用 void set(int nu=0,int de=1); //置值,改变值时用 void input(); //按照"nu/de"的格式,如"5/2"的形式输入 void simplify(); //化简(使分子分母没有公因子) void amplify(int n); //放大n倍,如2/3放大5倍为10/3 void output(int style=0); //输出:以8/6为例,style为0时,原样输出8/6; //style为1时,输出化简后形式4/3; //style为2时,输出1(1/3)形式,表示一又三分之一; //style为3时,用小数形式输出,如1.3333; //默认方式0 };
相关文章推荐
- c++第二次作业
- C++第二次实验
- 笔记:C语言文件处理
- 1002. 写出这个数 (20)
- 一些特别有趣的C++面试题(持续更新中)
- C语言编程在Xcode中Scanf 输入跳过以及清空键盘缓冲区总结
- c语言操作mysql
- 【c/c++】C语言main()函数详解
- 二级指针作输入之内存模型一
- c++课程实训 银行储蓄系统
- 不能声明为虚函数的函数
- Fibonacci数列
- C/C++—— 一个特别奇怪的C++程序
- C语言基础
- C语言atexit()函数
- C++泛型编程总结(二)
- 【c++程序】map一般用法
- HDU 1874-畅通工程续(Floyd)
- C++上机练习
- C++指针【数组、字符串作为函数的参数】