zoukankan      html  css  js  c++  java
  • 03类的设计和使用

    3-1.文件和字符串基本操作:
    从文件中读取单词,统计单词的个数,单词之间以空格、标点或换行符分隔。
    注意判断文件是否打开成功。单词为英文单词。

    提示:使用系统函数getline来读取一整行到string类型的变量中去。
    其第一个参数是输入流,第二个参数是string类型的变量。
    该函数从输入流中读入字符,然后存储到string变量中,直到出现以下情况为止:

    3-2.类的设计:从以下两题中选一。
    选1.
    设计一个用于人事管理的People(人员)类。
    考虑到通用性,这里只抽象出所有类型人员都具有的属性:
    number(编号)、sex(性别)、birthday(出生日期)、id(身份证号)等。
    其中“出生日期”声明为一个“日期”类内嵌子对象。用成员函数实现对人员信息的录入和显示。
    要求至少包括:构造函数和析构函数、拷贝构造函数。
    选2. 设计一个简易的选课程序,包含两个类:CourseSchedule和Course类。
    CourseSchedule表示某个用户的选课表。其依赖关系为:CourseSchedule中有两个成员函数add和remove的参数是Course类的对象,请通过UML表示这种类的依赖关系图,并通过程序简单实现。

    作业1:

    View Code
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int CountWords(string &line) {
        line = " " + line;
        int len = line.size();
        int num = 0;
        for(int i = 1; i < len; i++) {
            if(line[i] >= 'a' && line[i] <= 'z' || line[i] >= 'A' && line[i] <= 'Z') {
                if(line[i-1] < 'a' || line[i-1] > 'z') {
                    if(line[i-1] < 'A' || line[i-1] > 'Z') {
                        num++;
                    }
                }
            }
            else
                line[i] = ' ';
        }
        return num;
    }
    
    int main() {
        ifstream input;
        input.open("in.txt");
        string buff;
        if(!input) {
            cout << "no" <<endl;
            return 0;
        }
        int ans = 0;
        int now = ans;
        while(getline(input,buff)) {
            ans += CountWords(buff);
            while(buff.find("  ") < buff.size()){
                int pos = buff.find("  ");
                buff.erase(pos,1);
            }
            buff.erase(0,1);
            cout << buff << endl;
            cout << "当前一行的单词数: " << ans - now << endl;
            now = ans;
        }
        cout << "总共的单词数: " << ans << endl;
        return 0;
    }

    作业2:

    View Code
    #include <iostream>
    #include <string>
    using namespace std;
    
    class CDate {
    public:
        CDate();
        ~CDate();
        CDate(CDate &b);
        void settime();
        void showtime();
    private:
        int year, month, day;
    };
    
    CDate :: CDate() {
        year = 0;
        month = 0;
        day = 0;
    }
    
    void CDate :: settime () {
        cout << "请输入年:" << endl;
        cin >> year;
        cout << "请输入月:" << endl;
        cin >> month;
        cout << "请输入天:" << endl;
        cin >> day;
    }
    
    void CDate :: showtime () {
        cout << "出生日期为:" << year << "" << month << "" << day << "" << endl;
    }
    
    CDate::~CDate() {
        cout << "CDate ok" <<endl;
    }
    
    CDate::CDate(CDate &b) {
        year = b.year;
        month = b.month;
        day = b.day;
    }
    
    class People {
    public:
        People();
        ~People();
        People(People &b);
        void setpeo();
        void showpeo();
    private:
        CDate birthday;
        string sex, id, number;
    };
    
    People::People() {
        sex = "";
        id = "";
        number = "";
        birthday;
    }
    
    People::~People() {
        cout << "People ok" <<endl;
    }
    
    People::People(People &b) {
        sex = b.sex;
        id = b.id;
        number = b.number;
        birthday= b.birthday;
    }
    
    void People::setpeo() {
        cout << "请输入编号" <<endl;
        cin >> id; 
        cout << "请输入性别" <<endl;
        cin >> sex;
        cout << "请输入身份证号" <<endl;
        cin >> number;
        birthday.settime();
    }
    
    void People::showpeo() {
        cout << id <<'\n'<< sex<<'\n' << number << endl;
        birthday.showtime();
    }
    
    
    int main() {
        People Sam1;
        Sam1.setpeo();
        Sam1.showpeo();
        return 0;
    }
  • 相关阅读:
    曾今的代码系列——获取当天最大流水号存储过程
    曾今的代码系列——生产者消费者模式
    利用Microsoft.VisualBasic中TextFieldParser解析器把CSV格式倒入数据库
    曾今的代码系列——自己的分页控件+存储过程实现分页
    ASP.NET那点不为人知的事(四)
    SharePoint下用C#代码上传文档至文档库的子文件夹中
    Entity Framework 4 in Action读书笔记——第四章:使用LINQ to Entities查询:使用函数
    Entity Framework 4 in Action读书笔记——第四章:使用LINQ to Entities查询:预先加载和延迟加载
    这几天Colorbox让我寝食难安
    使用ASP.NET MVC3+EF+Jquery制作文字直播系统(四)——完成篇
  • 原文地址:https://www.cnblogs.com/gray035/p/2982265.html
Copyright © 2011-2022 走看看