c++类、函数

1、编写一个函数,求从n个不同的数中取r个数的所有选择的种数。
要求:
(1)将main()函数放在一个.cpp文件中;
(2)将计算阶乘的函数long fn(int n) ,计算组合的函数long Cnr(int n, int r)放在另一个.cpp文件中;
(3)将函数原型说明放在一个头文件中;
(4)建立一个项目,将这三个文件加到你的项目中,编译连接使你的程序正常运行。

:#include <iostream>
using namespace std;
#include"head.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
    int n,r; 
    cout<<"请输入n:";
    cin>>n;
    cout<<"请输入r:" ;
    cin>>r;
    Cnr( n,  r);
    return 0;
}
(2):long fn(int n);
long Cnr(int n, int r);
(3):#include <iostream>
using namespace std;
#include"head.h"
long fn(int n){
    while(n>1){
        return n*fn(n-1);
    }
}
long Cnr(int n, int r){
    int a,b,c,total;
    a=fn(n);
    b=fn(r);
    c=fn(n-r);
    total=a/(b*c);
    cout<<"组合数:"<<total<<endl;
}

这里写图片描述
2、定义一个Employee类,在Employee类中增加一个静态数据成员来设置本公司员工编号基数,新增加的员工编号将在创建对象的同时自动在基数上增加。另外,将Employee类的声明部分和实现部分分成两个文件来实现。

#include <iostream>
#include"employee.h" 
using namespace std;  
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    cout<<"员工信息;"<<endl; 
    employee mymessage("zhangsan",20);
    mymessage.show();
    employee mymessages("lisi",20);
    mymessages.show();
    employee mymessagess("wangwu",21);
    mymessagess.show();
    return 0;
}
(2):#include<string>
using namespace std;
class employee{
    public:
        employee(string names,int ages);
        void set(string names,int ages);
        void show();
        ~employee();
    private:
        static int number;
        string name;
        int age;            
};
(3):#include <iostream>
#include<string>
#include"employee.h" 
using namespace std;
int employee::number=0;
employee::employee(string names,int ages){
    number++;
    name=names;
    age=ages;
}
void employee::show(){
    cout<<"编号  "<<number<<"  "<<"姓名  "<<name<<"  "<<"年龄  "<<age<<endl;
} 
employee::~employee(){
            number--;
        }

这里写图片描述
3、假设有一个点类point,具有两个实数坐标。希望主程序使用这个类完成下述功能:
(l)主程序为类point申请10个连续存储空间。
(2)要求调用一个函数Set()从键盘输入10个对象的属性,并顺序存入申请的内存中。
(3)要求调用一个函数Display()显示10个对象的值。
(4)要求调用一个函数Lenth(),计算将这些点连成一条折线时,这条折线的长度。
(5)程序结束时,删除申请的内存。
(6)演示析构对象(动态对象或堆对象)的执行顺序。
设计这个类和各个函数并验证运算结果的正确性。

#include<iostream>
#include<math.h>
using namespace std;
int j=1,n=10;
class point{
public:
    point(){
        j++;
    }
    point(double x1,double y1){
        x=x1,y=y1;
        j++;
    }
    ~point(){
            j--;
    }
    double x,y;
};
void set(point *p){
    for(int i=0;i<n;i++)
    {
        cout<<"\n请设置第"<<i+1<<"个对象的属性"<<endl;
        cout<<"请输入x的值\n"<<"x=";
        cin>>p[i].x;
        cout<<"请输入y的值"<<"y=";
        cin>>p[i].y;
    }
}
int length(point *p){
    double s=0,m,q;
    for(int i=0;i<n-1;i++)
    {
        q=p[i+1].x-p[i].x;
        m=p[i+1].y-p[i].y;
        s+=sqrt(m*m+q*q);
    }
    cout<<"这"<<n<<"个点所连接而成的长度为:"<<s<<endl;
    return 0;
}
void display(point *p)
{
    for(int i=0;i<n;i++){
        cout<<"\np["<<i<<"]="<<"("<<p[i].x<<","<<p[i].y<<")"<<endl;
    }
}
int main(){
    point *p;
    p=new point[n];
    set(p);
    display(p);
    length(p);
    delete []p;
    return 0;
}

这里写图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值