第十四周项目3:OOP版电子词典

问题:

/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:project.cpp
*作    者:陈文青
*完成日期:2015年6月17日
*版 本 号:v1.0
*
*问题描述:做一个简单的电子词典。在文件dictionary.txt中,保存的是英汉对照的一个词典,词汇量近8000个,英文、中文释义与词性间用’\t’隔开。
编程序,由用户输入英文词,显示词性和中文释义。
提示1:如果要用OOP完成这个词典(当然也可以用OO方法实现),可以定义一个Word类表示一个词条,其中的数据成员string english; 表示英文单词,string chinese;表示对应中文意思,string word_class;表示该词的词性;还可以定义一个Dictionary类,用来表示词典,其中Word words[8000]成员表示词典中的词条,int wordsNum;表示词典中的词条数,在构造函数中从文件读入词条,而专门增加一个成员函数用于查单词。
提示2:文件中的词汇已经排序,故在查找时,用二分查找法提高效率。
提示3:这样的项目,最好用多文件的形式组织
*程序输入:
*程序输出:
*/


代码:

head.h

#ifndef HEAD_H_INCLUDED
#define HEAD_H_INCLUDED

#include<string>

using namespace std;

//定义词条类
class Word
{
public:
    int compare(string);    //英语部分与给定字符串比较,等于返回,大于返回,小于返回-1,用于二分法查找
    string getChinese()     //返回中文解释
    {
        return chinese;
    }
    string getWord_class()  //返回词性
    {
        return word_class;
    }
    void set(string e, string c, string wc);  //读入数据时使用
private:
    string english;
    string chinese;
    string word_class;
};

//定义字典类
class Dictionary
{
public:
    Dictionary();
    void searchWord(string k);    //查词
private:
    int BinSeareh(int low, int high, string k);  //二分法查找
    int wordsNum;     //词条个数
    Word words[8000]; //用于保存词库
};


#endif // HEAD_H_INCLUDED


word_class.cpp

#include"head.h"

void Word::set(string e, string c, string wc)
{
    english=e;
    chinese=c;
    word_class=wc;
}

int Word::compare(string k)  //用于二分法查找
{
    return english.compare(k);
}


dictionary_class.cpp

#include"head.h"
#include<fstream>
#include<iostream>
#include<cstdlib>
using namespace std;

Dictionary::Dictionary()
{
    string e,c,wc;
    wordsNum=0;
    //将文件中的数据读入到对象数组中
    ifstream infile("dictionary.txt",ios::in);  //以输入的方式打开文件
    if(!infile)       //测试是否成功打开
    {
        cerr<<"Can't open the dictionary!"<<endl;
        exit(1);
    }
    while (!infile.eof())
    {
        infile>>e>>c>>wc;
        (words[wordsNum]).set(e, c, wc);
        ++wordsNum;
    }
    infile.close();
}

int Dictionary::BinSeareh(int low, int high, string key)
{
    int mid;
    while(low<=high)
    {
        mid=(low + high) / 2;
        if(words[mid].compare(key)==0)
        {
            return mid; //查找成功返回
        }
        if(words[mid].compare(key)>0)
            high=mid-1; //继续在w[low..mid-1]中查找
        else
            low=mid+1; //继续在w[mid+1..high]中查找
    }
    return -1;         //当low>high时表示查找区间为空,查找失败
}

void Dictionary::searchWord(string key)
{
    int low=0,high=wordsNum-1;  //置当前查找区间上、下界的初值
    int index=BinSeareh(low, high, key);
    if(index>=0)
        cout<<key<<"--->"<<words[index].getWord_class()+"\t"<<words[index].getChinese();
    else
        cout<<"查无此词";
    cout<<endl<<endl;
}


main.cpp

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

int main( )
{
    Dictionary dict;
    string key;
    do
    {
        cout<<"请输入待查询的关键词(英文),0000结束:"<<endl;
        cin>>key;
        if (key!="0000")
        {
            dict.searchWord(key);
        }
    }
    while(key!="0000");
    cout<<"欢迎再次使用!"<<endl<<endl;
    return 0;
}


运行结果:

知识点总结:

二分法查找

类的构造

文件流的使用

学习心得:

 写一个这样的程序,首先要把该有的类定义好,把框架大体构思好,

然后在定义类的时候,每个成员函数可以划分成几个小的成员函数,进而补充各个类的数据成员和成员函数,

然后细心专注的解决每一个小的问题。像构造函数还有友元函数。一定要注意细节,不然虽然error修复了,但还是程序还是不能运行的。

 

需要钻研的是二分法查找,这里构造了一个compare函数,返回 -1,0,1几个数值从而确定比较的范围。

最后就是,抄之有道。O(∩_∩)O哈哈~

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用C++实现的oop租车系统(多重继承)的代码示例: ```cpp #include <iostream> #include <string> #include <vector> using namespace std; // 定义车辆 class Vehicle { public: Vehicle(const string& make, const string& model, int year, int rent_per_day) : make_(make), model_(model), year_(year), rent_per_day_(rent_per_day) {} // 打印车辆信息 void print_info() const { cout << make_ << " " << model_ << " (" << year_ << ") - $" << rent_per_day_ << "/day" << endl; } private: string make_; // 品牌 string model_; // 型号 int year_; // 年份 int rent_per_day_; // 租金/天 }; // 定义客户 class Customer { public: Customer(const string& name, int age) : name_(name), age_(age) {} // 打印客户信息 void print_info() const { cout << name_ << " (" << age_ << " years old)" << endl; if (rented_vehicle_) { cout << "Rented vehicle: "; rented_vehicle_->print_info(); } } // 租用车辆 void rent_vehicle(Vehicle* vehicle) { rented_vehicle_ = vehicle; cout << name_ << " rented "; rented_vehicle_->print_info(); } private: string name_; // 姓名 int age_; // 年龄 Vehicle* rented_vehicle_ = nullptr; // 租用的车辆 }; // 定义租车公司 class CarRentalCompany { public: // 初始化车辆和客户 CarRentalCompany() { // 添加车辆 vehicles_.push_back(new Vehicle("Make1", "Model1", 2020, 50)); vehicles_.push_back(new Vehicle("Make2", "Model2", 2020, 60)); vehicles_.push_back(new Vehicle("Make3", "Model3", 2020, 70)); // 添加客户 customers_.push_back(new Customer("Customer1", 25)); customers_.push_back(new Customer("Customer2", 30)); } // 打印所有车辆信息 void print_all_vehicles() const { cout << "All vehicles:" << endl; for (const auto& vehicle : vehicles_) { vehicle->print_info(); } } // 打印所有客户信息 void print_all_customers() const { cout << "All customers:" << endl; for (const auto& customer : customers_) { customer->print_info(); } } private: vector<Vehicle*> vehicles_; // 所有车辆的数组 vector<Customer*> customers_; // 所有客户的数组 }; int main() { // 创建租车公司 CarRentalCompany company; // 打印所有车辆和客户信息 company.print_all_vehicles(); company.print_all_customers(); // 客户1租用第2辆车 company.print_all_customers(); company.print_all_vehicles(); company.print_all_customers(); company.print_all_vehicles(); return 0; } ``` 该代码定义了三个:Vehicle、Customer 和 CarRentalCompany。其中,Vehicle 和 Customer 分别表示车辆和客户,而 CarRentalCompany 则表示租车公司。使用多重继承,CarRentalCompany 同时继承了 Vehicle 和 Customer 的属性和方法。 使用该代码,我们可以创建一个租车公司,并初始化其中的车辆和客户。然后,我们可以打印所有车辆和客户的信息,以及让某个客户租用某辆车。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值