当前位置:编程学习 > C/C++ >>

四个C++的小题目 急急急!!!!!

1.给定某个年、月、日的值,例如,2005年7月11日,计算出这一天属于该年的第几天,要求写出计算闰年

的函数和计算日期的函数。

2.使用指针变量对一字符串按照字母,空格、数字和其他字符进行分类统计( 提示:读一行字符包括空格

用函数cin.getline(ch, 81) )。

3.定义一个圆类,计算圆的面积和周长。
  要求:分别用成员函数和友元函数来求圆的面积和周长。

4.定义一个学生类,其中有3个数据成员有学号、姓名、年龄,以及若干成员函数。同时编写主函数使用这

个类,实现对学生数据的赋值和输出。
要求:
(1)使用成员函数实现对数据的输入、输出;
(2)使用构造函数和析构函数实现对数据的输入、输出。

发到邮箱1149201590@qq.com   回答的好可以再加分的 你懂的

答案:第1题:
#include <iostream>
#include <string>
using namespace std;

void main()
{
    //平年每个月的天数
    int month_day[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
    int year, month, day, sumday=0;
    char buffer[10];

    cout<<"输入年月日,用空格分格: ";
    cin>>year>>month>>day;

    _itoa(year, buffer, 10);
    if ((strlen(buffer)!=4) || (year<0))
    {
        cout<<"年份输入有误!"<<endl;
        return;
    }
    if (month<0 || month>12)
    {
        cout<<"月份输入有误!"<<endl;
        return;
    }
    if (day<0 || day>31)
    {
        cout<<"日期输入有误!"<<endl;
        return;
    }

    if ((year%4==0 && year%100!=0 || year%400==0) && month>2)
    {
        for (int i=0; i<month-1; i++)
            sumday += month_day[i];

        sumday += day;
        sumday += 1;
    }
    else
    {
        for (int i=0; i<month-1; i++)
            sumday += month_day[i];

        sumday += day;
    }

    cout<<year<<"-"<<month<<"-"<<day<<"是当年的第 "<<sumday<<" 天."<<endl<<endl;
}

第2题:
#include <iostream>
#include <string>
using namespace std;

void main()
{
    char ch[81] = {0};
    int zmCnt,bkCnt,szCnt,otCnt;

    cout<<"请输入要统计的字符串: ";
    cin.getline(ch, 81);

    zmCnt = bkCnt = szCnt = otCnt = 0;
    for (int i=0; i<strlen(ch); i++)
    {
        if ((ch[i]>='A' && ch[i]<='Z') || (ch[i]>='a' && ch[i]<='z'))
            zmCnt++;
        else if (ch[i] == ' ')
            bkCnt++;
        else if (ch[i]>='0' && ch[i]<='9')
            szCnt++;
        else
            otCnt++;
    }

    cout<<"字母总数: "<<zmCnt<<endl
        <<"空格总数: "<<bkCnt<<endl
        <<"数字总数: "<<szCnt<<endl
        <<"其它总数: "<<otCnt<<endl<<endl;
}

第3题:
#include <iostream>
using namespace std;
#define PI 3.1415

class Circle
{
public:
    Circle(double radius)
    {
        this->radius = radius;
    }
    ~Circle()
    {
    }

    double getArea()
    {
        return PI*radius*radius;
    }
    double getGirth()
    {
        return 2*PI*radius;
    }

    friend double calArea(double r);
    friend double calGirth(double r);

    double getRadius()
    {
        return radius;
    }

private:
    double radius;
};

double calArea(double r)
{
    return PI*r*r;
}

double calGirth(double r)
{
    return 2*PI*r;
}

void main()
{
    double area1,area2,girth1,girth2;

    Circle *cl = new Circle(3);
    area1 = cl->getArea();
    girth1 = cl->getGirth();
    area2 = calArea(cl->getRadius());
    girth2 = calGirth(cl->getRadius());

    cout<<"面积(成员): "<<area1<<endl
        <<"周长(成员): "<<girth1<<endl
        <<"面积(友员): "<<area2<<endl
        <<"周长(友员): "<<girth2<<endl<<endl;
}

第4题:
#include <iostream>
#include <string>
using namespace std;

class Student
{
public:
    Student() : ID(0), name(""), age(0)
    {
    }
    Student(int ID, string name, int age)
    {
        this->ID = ID;
        this->name = name;
        this->age = age;
    }
    ~Student()
    {
        cout<<"学号: "<<ID<<endl
            <<"姓名: "<<name<<endl
            <<"年龄: "<<age<<endl<<endl;
    }

    void setID(int ID)
    {
        this->ID = ID;
    }
    void setName(string name)
    {
        this->name = name;
    }
    void setAge(int age)
    {
        this->age = age;
    }

    int getID()
    {
        return ID;
    }
    string getName()
    {
        return name;
    }
    int getAge()
    {
        return age;
    }


private:
    int ID;
    string name;
    int age;
};

void main()
{
    Student *st1 = new Student();
    st1->setID(1);
    st1->setName("张三");
    st1->setAge(20);
    cout<<"学号: "<<st1->getID()<<endl
        <<"姓名: "<<st1->getName()<<endl
        <<"年龄: "<<st1->getAge()<<endl<<endl;

    Student *st2 = new Student(2, "李四", 30);
    st2->~Student();
}

上一个:谁有C++免费的教程啊
下一个:求C++编程,编写一个学生类

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,