首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

急请大家帮忙看看怎么用C++编写一个自行车里程表的程序,多谢

2012-02-25 
急请大家帮忙看看如何用C++编写一个自行车里程表的程序,谢谢!要准备考试,室友mm求助我这道题,我也不懂~~~

急请大家帮忙看看如何用C++编写一个自行车里程表的程序,谢谢!
要准备考试,室友mm求助我这道题,我也不懂~~~只能请大家帮忙了,多谢哈!

问题是要设计一个类来实现一个自行车里程兼测速计,简称为bikemeter,其性质如下:

bikemeter每秒钟都会从内部时钟inter_clock接收到时钟信号tick(),并且伴随着车轮每转一圈,轮轴上一个传感器会将信号click()传给bikemeter(相当于是两个计数器counter,一个用来计时一个用来计车轮转的圈数)。

在车轮转动时,会将轮的直径diameter告诉bikemeter。从以上这些信息,bikemeter就能够计算出其他信息,如当前速度current_speed,行驶距离distance以及平均速度average_speed。   同样的,bikemeter在每一个 "trip "(程序中会出现这个东西的定义,欧不知道啥米意思)开始前,可以被归零reset.

下面给的框架程序中,main()调用函数和bikemeter类去测试这个类bikemeter.在constructer中应该存放着车轮的直径diameter,单位为inch,可以设默认值为27。在行驶过程中,bikemeter会收到一系列的穿插着click和tick的信号,每一个click()代表着车轮转了一圈,每一个tick()代表着时钟走过一秒。因此,假设收到的一系列信号为
click(),click(),tick(),click(),click(),click(),tick(),click(),tick(),click(),tick(),click(),tick(),tick(),click(),click(),tick(),click().

那么在每一秒间隔内车轮转的圈数click()分别为:2,3,1,1,1,0,2.   在最后一个click后跟的是没有写出来的tick)。那么,在这么多秒内,行驶的距离为每秒内圈数之和*车轮的直径,圈数之和为2+3+1+1+1+0+2=10,直径为27   inch,那么距离即为10*27*pi(pi的值为3.14159),所经过的时间为7个tick(),即为7秒,那么平均速度average_speed即为10/7圈。

对于速度,要求bikemeter是以英里miles为单位的,表达为miles/hour,且每一秒,即每一个tick()时都要显示出当前速度speed,   即实时更新。为了知道实时的speed,要求采用的方法是知道每次最近4秒钟内的车轮转圈数click() 's。如前面的一系列信号,知道了7秒内的总圈数为10,前三秒内的圈数为2+3+1=6,则最近4个tick内的圈数为10-6=4圈,即这段时间内的click数为4。求最近4秒内的速度即为每秒显示的实时speed。

最后,框架程序还要比较两个bikemeter的累计行驶距离,利用逻辑操作符 <, <=,> ,> =,==以及!=,   如假设a的累计行驶距离比b大,那么bikemeter   a   >   bikemeter   b(是要在bikemeter的类中用操作符重载么?我瞎猜的~)

程序要求:完成class   bikemeter的相关结构体和函数,并于框架程序结合起来,形成一个完整的程序,并经过主函数中的测试函数测试,能够正确运行


这是已给出的框架程序,

void   ride(bikemeter   &a,   int   trip[],   int   ct);
void   mainTest();
void   logicalOpsTest();

int   main(){
        cout < < "Test   out   the   bikemeter   class\n ";
        mainTest();
        logicalOpsTest();
        cout < < "End   of   program\n ";
}

void   logicalOpsTest(){
    bikemeter   a,b;
    int   trip[]={5,3,2,9,8,2,5,3,6,7};
    ride(a,trip,9);
    ride(b,trip,10);
    cout < < "\n   We   compare   two   bikemeters   by   comparing   their   distances\n ";
    cout < <a < <   "   <\n   " < <b < <   ":   " < <(a <b) < <endl;
    cout < <a < <   "   > \n   " < <b < <   ":   " < <(a> b) < <endl;
    cout < <a < <   "   <=\n   " < <b < < ":   " < <(a <=b) < <endl;
    cout < <a < <   "   > =\n   " < <b < < ":   " < <(a> =b) < <endl;
    cout < <a < <   "   ==\n   " < <b < < ":   " < <(a==b) < <endl;
    cout < <a < <   "   !=\n   " < <b < < ":   " < <(a!=b) < <endl;
    a.reset();
    ride(a,trip,10);
    cout < <a < <   "   <\n   " < <b < <   ":   " < <(a <b) < <endl;
    cout < <a < <   "   > \n   " < <b < <   ":   " < <(a> b) < <endl;
    cout < <a < <   "   <=\n   " < <b < < ":   " < <(a <=b) < <endl;


    cout < <a < <   "   > =\n   " < <b < < ":   " < <(a> =b) < <endl;
    cout < <a < <   "   ==\n   " < <b < < ":   " < <(a==b) < <endl;
    cout < <a < <   "   !=\n   " < <b < < ":   " < <(a!=b) < <endl;
}

void   mainTest(){
      bikemeter   a(26.5);
      b;
      int   trip[]={5,3,2,9,8,2,5,3,6,7};
      ride(a,trip,10);
      ride(b,trip,10);
      cout < < "For   the   following   'trip '\n "
              < < "second   #of   clicks\n ";
      for(int   j=0;j <10;j++)
          {
              cout.width(5);
              cout < <(j+1);
              cout.width(10);
              cout < <trip[j] < <endl;
          }
      cout < < "For   26.5   inch   tire:   \n   " < <a
              < < "\nFor   a   27   inch   tire:   \n " < <b < <endl;
//Now   test   the   copy   constructor
    bikemeter   c(a);
    cout < < "The   two   bikemeters   below   should   be   the   same\n " < <a < <endl < <c < <endl;
}

void   ride(bikemeter   &   a,   int   trip[],   int   ct){
        for(int   sec=0;sec <ct;sec++)
              {
                  for   (int   t=0;   t <trip[sec];t++)
                  a.click();
                  a.tick();
              }
}


可模仿下面的计数器class   Counter来完成class   bikemeter,也可直接将class   Counter用于上面的程序和类的设计中,但要求不能改动class   Counter内的代码。

#   include   <iostream>
using   namespace   std;

class   Counter{
public:
          static   const   int   MAX=1000;
          Counter();   //a   counter   with   count==0
          Counter(int   k);   //a   counter   with   count==k,   but   if   k <0,   crash
          Counter(const   Counter   &c);//   a   counter   with   the   contents   of   c
          ~Counter();//nothing   much   to   kill   off   in   this   case
          void   reset(int   k=0);   //set   the   count   to   k.   if   k <=   or   k> MAX,crash
          int   GetCount()const;   //return   the   count


          Counter   &   inc();   //increment   the   count   by   one,   if   the   count   exceeds    
                                            //MAX   then   crash,   return   a   reference   to   the   counter
          Counter   &   operator+=(int   k);   //precondition:   k> 0
                                                                    //current   count+k <=MAX
                                                                    //postcondition:count   incremented   by   k
                                                                    //   if   precondition   are   not   satisfied,
                                                                    //crash
                                                                    //return   a   reference   to   the   counter
          Counter   operator+(const   Counter   &   c);//precondition:   sum   of   counts;
                                                                                    //on   self   and   c   is   <=MAX;
                                                                                    //postcondition:   return   a   new
                                                                                    //counter   with   sum   of   counts   on
                                                                                    //old   counters,   crash   if  
                                                                                    //precondition   not   met


          friend   Counter   operator*(const   Counter   &a,   const   Counter   &b);
                                                                                    //precondition:   sum   of   counts
                                                                                    //on   a   and   b   is   <=MAX
                                                                                    //postcondition:return   a   new  
                                                                                    //counter   with   product   of   counts  
                                                                                    //on   old   counters.   Crash   if  
                                                                                    //preconditions   not   met
//Display   the   contents   of   the   counters   as   [ <count> ]
friend   ostream   &   operator < <(ostream   &out,const   Counter   &c);
static   void   test();
private:
                int   count;
                static   void   check(bool   b,char   *mess);
};

  void   Counter::test(){
          Counter   a,b(20),c(b);
          b.inc().inc();
          cout < <a < <b < <c < <endl;
          a.reset(9);
          cout < <a < <endl;
          cout < < "The   count   on   the   counter   is   " < <a.getCount() < <endl;
          (a+=2)+=3;
          cout < < "Now   we   should   have   14:   " < <a < <endl;

          cout < <a < < "   +   " < <b < < "   ==   " < <(a+b) < <endl;


          cout < <a < < "   +   " < <3 < < "   ==   " < <(a+3) < <endl;

          cout < <a < < "   *   " < <b < < "   ==   " < <(a*b) < <endl;
          cout < <a < < "   *   " < <3 < < "   ==   " < <(a*3) < <endl;
}

    Counter::Counter(){
            reset();
}

    Counter::Counter(int   k){
            reset(k);
    }

    Counter::   Counter(const   Counter   &c):count(c.count){}

    Counter::~Counter(){
        cout < < "Killing   off   a   counter   with   count   ==   " < <count < <endl;
}

      void   Counter::reset(int   k){
          check(k> =0   &&   k <=MAX,   "(reset())   reset   value   out   of   range ");
          count=k;
  }

      int   Counter::getCount()const{return   count;}

      Counter   &   Counter::inc(){
          count++;
          check(count <=MAX, "(inc())   counter   overflow ");
          return   *this;
}

      Counter   &   Counter::operator+=(int   k){
          check(k> 0, "(+=)   increment   must   be   positive ");
          return   *this;
}

      Counter   Counter::operator+(const   Counter   &   c){
            return   Counter(count+c.count);
}

      Counter   operator*(const   Counter   &a,   const   Counter   &b){
        return   Counter(a.count*b.count);
}

      ostream   &   operator < <(ostream   &out,   const   Counter   &c){
          out < < "[ " < <c.count < < "] ";
          return   out;
}

      void   Counter::check(bool   b,   char   *mess){
            if(!b){
                cerr < < "ERROR[Counter]:   " < <mess < <endl;
            exit(1);
            }
}




[解决办法]
..........这么多

热点排行