08.05

【项目1 - 三角形类1】下面设计一个三角形类,请给出各成员函数的定义 

#include <iostream>
#include <stdlib.h>

using namespace std;

class Triangle
{
    private:
        double a,b,c;//三角形的三边

    public:
        void set_triangle();//给三角形置边
        double* get_triangle();//获取三角形的三边
        double perimeter();//三角形的周长
        double acreage();//三角形的面积
};
void Triangle::set_triangle()
{
    cout<<"please enter three numbers as three sides of the triangle:";
    while(1)
    {
        cin>>a>>b>>c;
        if((a+b)<c||(a+c)<b||(b+c)<a)
        {
            cout<<"inputs are illegal,please re-enter:";
            cin>>a>>b>>c;
        }
        else
            break;
    }
}

double *Triangle::get_triangle()
{
    double *p=(double *)malloc(3*sizeof(double));
    *p=a;
    *(p+1)=b;
    *(p+2)=c;
    return p;
    free(p);
}

double Triangle::perimeter()
{
    double pm;
    pm=a+b+c;
    return pm;
}

double Triangle::acreage()
{
    double l;
    double s;
    l=perimeter()/2;
    s=l*(l-a)*(l-b)*(l-c);
    return s;

}

int main()
{
    double *p=NULL;
    double lm;//周长
    double s;//面积
    Triangle t;

    t.set_triangle();
    p=t.get_triangle();
    cout<<*p<<" "<<*(p+1)<<" "<<*(p+2)<<"\n";
    lm=t.perimeter();
    s=t.acreage();
    cout<<"the perimeter is:"<<lm<<"\n";
    cout<<"the acreage is:"<<s;

    return 0;
}





【项目2 - 三角形类2】程序功能同项目1,main()函数如下,请重新定义Triangle类,其中逻辑特别简单的set和get成员函数,要处理为内置成员函数,直接在类内定义。


#include <iostream>

using namespace std;

class Triangle
{
    private:
        double a,b,c;//三角形的三边


    public:
        bool is_triangle();

        double perimeter();//三角形的周长
        double acreage();//三角形的面积


        void set_triangle(double x,double y,double z)
        {
            a=x;
            b=y;
            c=z;
        }

        double get_a()
        {
            return(a);
        }

        double get_b()
        {
            return(b);
        }

        double get_c()
        {
            return c;
        }

};

bool Triangle::is_triangle()
{
            return((a+b)<c||(a+c)<b||(b+c)<a);
}

double Triangle::perimeter()
{
    double pm;
    pm=a+b+c;
    return pm;
}

double Triangle::acreage()
{
    double l;
    double s;
    l=perimeter()/2;
    s=l*(l-a)*(l-b)*(l-c);
    return s;

}

int main()
{
    Triangle t;
    double x,y,z;
    cout<<"please enter three numbers as three sides of the triangle:";
    while (1)
    {
        cin>>x>>y>>z;
        t.set_triangle(x,y,z);
        if(t.is_triangle())
        {
            cout<<"the inputs are illegal,please retry:";
            cin>>x>>y>>z;
        }
        else
            break;
    }
    cout<<t.get_a()<<" "<<t.get_b()<<" "<<t.get_c()<<"\n";
    cout<<"the perimeter of the triangle is:"<<t.perimeter()<<"\n";
    cout<<"the acreage of the triangle is:"<<t.acreage();

    return 0;
}


【项目3 - 程序的多文件组织】
利用多文件组织,重新实现项目2。其中,整个项目包括3个文件:

  • 主文件: main.cpp,用于定义main()函数
  • 头文件: triangle.h,头文件,声明类,定义内置成员函数
  • 类定义文件: triangle.cpp,用于定义类Triangle中其他成员函数
#ifndef TRIANGLE_H_INCLUDED
#define TRIANGLE_H_INCLUDED

#include <iostream>

using namespace std;

class Triangle
{
    private:
        double a,b,c;//三角形的三边


    public:
        bool is_triangle();

        double perimeter();//三角形的周长
        double acreage();//三角形的面积


        void set_triangle(double x,double y,double z)
        {
            a=x;
            b=y;
            c=z;
        }

        double get_a()
        {
            return(a);
        }

        double get_b()
        {
            return(b);
        }

        double get_c()
        {
            return c;
        }

};


#endif // TRIANGLE_H_INCLUDED

#include "triangle.h"

bool Triangle::is_triangle()
{
            return((a+b)<c||(a+c)<b||(b+c)<a);
}

double Triangle::perimeter()
{
    double pm;
    pm=a+b+c;
    return pm;
}

double Triangle::acreage()
{
    double l;
    double s;
    l=perimeter()/2;
    s=l*(l-a)*(l-b)*(l-c);
    return s;

}

#include <iostream>
using namespace std;
#include "triangle.h"

int main()
{
    Triangle t;
    double x,y,z;
    cout<<"please enter three numbers as three sides of the triangle:";
    while (1)
    {
        cin>>x>>y>>z;
        t.set_triangle(x,y,z);
        if(t.is_triangle())
        {
            cout<<"the inputs are illegal,please retry:";
            cin>>x>>y>>z;
        }
        else
            break;
    }
    cout<<t.get_a()<<" "<<t.get_b()<<" "<<t.get_c()<<"\n";
    cout<<"the perimeter of the triangle is:"<<t.perimeter()<<"\n";
    cout<<"the acreage of the triangle is:"<<t.acreage();
    return 0;
}




【项目4 - 考了语文数学的学生】
下面提供了类Stu的数据成员定义,和用于测试的main函数,参考如图的运行结果,完成类的定义,并用多文件形式组织程序


#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED

#include <iostream>
#include <string.h>

using namespace std;
class Student
{
    private:
        string name;
        float chinese;
        float math;

    public:
        void show_info();//显示学生信息
        void set_info(string,float,float);//学生信息的修改

};
#endif // STUDENT_H_INCLUDED

#include <iostream>
#include <string.h>
#include "student.h"

void Student::set_info(string s,float c,float m)
{
    name=s;
    chinese=c;
    math=m;
}

void Student::show_info()
{
    cout<<name<<" "<<chinese<<" "<<math<<endl;
}
#include <iostream>
#include <string.h>
#include "student.h"

using namespace std;

int main()
{
    Student stu;
    Student stu1;
    stu.set_info("bob",55,66);
    stu1.set_info("yyy",99,99);
    stu.show_info();
    stu1.show_info();
    return 0;
}





【项目5 - 数组作数据成员】设计一个工资类(Salary),其中类的数据成员如下:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. class Salary  
  2. {  
  3. private:  
  4.     double salarys[50]; //多人的工资  
  5.     int number;  //实际人数  
  6. };  
要设计的成员函数有:
  • void set_salarys( ):输入职工工资(输入-1标志着工资输入结束),工资保存到salary数组中,实际人数保存到number中;
  • void add_salarys(int x):给每个人涨x元工资
  • void sort_salarys():对工资排序
  • void show_salarys( ):显示工资信息
(1)在main函数定义Salary类的对象,输入工资,再给每个人涨500元工资,排序后工资数据,然后输出结果。
(2)用salary[50]有限制,实际人数少时,会浪费空间,人数多了,无法完成任务。在main()中先输入职工人数,作为参数传递给输入职工工资的成员函数,然后利用动态分配内存的机制,开辟一个大小正好的连续空间,完成上面的工作。
(3)手工输入工资?!太让人不能忍受了。现给出包含了不足500个职工工资的文件salary.txt( 点击打开链接 ),从文件中读数据,完成上面的工作。
(4)增加一个成员函数,将排序后结果保存到一个文件中。
(5)用多文件的方式组织最后的程序。

#ifndef SALARY_H_INCLUDED
#define SALARY_H_INCLUDED
const int maxsize=100;
#include <iostream>
using namespace std;

class Salary
{
    private:
        double salary[maxsize];//用于存储工资
        int num;//人数
    public:
        void set_salary();//置工资
        void add_salary(int);//涨工资
        void sort_salary();//排序
        void show_salary();//显示工资
};


#endif // SALARY_H_INCLUDED

#include "salary.h"

void Salary::set_salary()
{
    int i=0;
    double s;
    cout<<"please enter the salary:"<<endl;
    while(1)
    {
        cin>>s;
        if(s!=-1)
        {
            salary[i]=s;
            ++i;
            cout<<"please enter the salary:";
        }
        else
            break;
    }
    num=i+1;
}

void Salary::add_salary(int x)
{
    int i;
    for(i=0;i<num;++i)
    {
        *(salary+i) += x;
    }
}


void Salary::sort_salary()
{
    int i,j;
    double s;
    for(i=0;i<num;++i)
    {
        for(j=0;j<num-i-1;++j)
        {
            if(*(salary+j)>*(salary+j+1))
            {
                s=*(salary+j);
                *(salary+j)=*(salary+j+1);
                *(salary+j+1)=s;
            }
        }
    }
}


void Salary::show_salary()
{
    int i=0;
    while(i<num-1)
    {
        cout<<*(salary+i)<<endl;
        ++i;
    }
}


#include <iostream>
#include "salary.h"


using namespace std;


int main()
{
    Salary s;
    s.set_salary();
    s.show_salary();
    cout<<"\n";
    s.add_salary(500);
    s.sort_salary();
    s.show_salary();
    return 0;
}












































































































































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值