我需要添加另外2个选项,例如删除预订和编辑预订,但无法弄明白......任何人都可以帮助我吗? [英] I need to add 2 more options such as delete reservation and edit reservation but couldn't figure it out...can anyone help me?

查看:49
本文介绍了我需要添加另外2个选项,例如删除预订和编辑预订,但无法弄明白......任何人都可以帮助我吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <conio.h>

#include <cstdio>

#include <iostream>

#include <string.h>

#include <cstdlib>

using namespace std;

static int p = 0;

class a

{

  char busn[5], driver[10], arrival[5], depart[5], from[10], to[10], seat[8][4][10];

public:

  void install();

  void allotment();

  void cancel();

  void empty();

  void show();

  void avail();

  void position(int i);

}

bus[10];

void vline(char ch)

{

  for (int i=80;i>0;i--)

  cout<<ch;

}

void a::install()

{

  cout<<"Enter bus no: ";

  cin>>bus[p].busn;

  cout<<"\nEnter Driver's name: ";

  cin>>bus[p].driver;

  cout<<"\nArrival time: ";

  cin>>bus[p].arrival;

  cout<<"\nDeparture: ";

  cin>>bus[p].depart;

  cout<<"\nFrom: \t\t\t";

  cin>>bus[p].from;

  cout<<"\nTo: \t\t\t";

  cin>>bus[p].to;

  bus[p].empty();

  p++;

}

void a::allotment()

{

  int seat;

  char number[5];

  top:

  cout<<"Bus no: ";

  cin>>number;

  int n;

  for(n=0;n<=p;n++)

  {

    if(strcmp(bus[n].busn, number)==0)

    break;

  }

  while(n<=p)

  {

    cout<<"\nSeat Number: ";

    cin>>seat;

    if(seat>32)

    {

      cout<<"\nThere are only 32 seats available in this bus.";

    }

    else

    {

    if (strcmp(bus[n].seat[seat/4][(seat%4)-1], "Empty")==0)

      {

        cout<<"Enter passanger's name: ";

        cin>>bus[n].seat[seat/4][(seat%4)-1];

        break;

      }

    else

      cout<<"The seat no. is already reserved.\n";

      }

      }

    if(n>p)

    {

      cout<<"Enter correct bus no.\n";

      goto top;

    }

  }

  void a::cancel()
  {
    int seat;

  char number[5];

  top:

  cout<<"Bus no: ";

  cin>>number;

  int n;

  for(n=0;n==p;n++)

  {

    if(strcmp(bus[n].busn, number)==0)

    break;

  }

  while(n==p)

  {

    cout<<"\nSeat Number: ";

    cin>>seat;

    if(seat>32)

    {

      cout<<"\nThere are only 32 seats available in this bus.";

    }

    else

    {

    if (strcmp(bus[n].seat[seat*4][(seat%4)+1], "Empty")==0)

      {

        cout<<"Enter passanger's name: ";

        cin>>bus[n].seat[seat*4][(seat%4)+1];

        break;

      }

    else

      cout<<"Reservation has been cancelled.\n";

      }

      }

    if(n<p)>

    {

      cout<<"Enter correct bus no.\n";

      goto top;


  }
  }


void a::empty()

{

  for(int i=0; i<8;i++)

  {

    for(int j=0;j<4;j++)

    {

      strcpy(bus[p].seat[i][j], "Empty");

    }

  }

}

void a::show()

{

  int n;

  char number[5];

  cout<<"Enter bus no: ";

  cin>>number;

  for(n=0;n<=p;n++)

  {

    if(strcmp(bus[n].busn, number)==0)

    break;

  }

while(n<=p)

{

  vline('*');

  cout<<"Bus no: \t"<<bus[n].busn

  <<"\nDriver: \t"<<bus[n].driver<<"\t\tArrival time: \t"

  <<bus[n].arrival<<"\tDeparture time:"<<bus[n].depart

  <<"\nFrom: \t\t"<<bus[n].from<<"\t\tTo: \t\t"<<

  bus[n].to<<"\n";

  vline('*');

  bus[0].position(n);

  int a=1;

  for (int i=0; i<8; i++)

  {

    for(int j=0;j<4;j++)

    {

      a++;

      if(strcmp(bus[n].seat[i][j],"Empty")!=0)

      cout<<"\nThe seat no "<<(a-1)<<" is reserved for "<<bus[n].seat[i][j]<<".";

    }

  }

  break;

  }

  if(n>p)

    cout<<"Enter correct bus no: ";

}

void a::position(int l)

{

  int s=0;p=0;

  for (int i =0; i<8;i++)

  {

    cout<<"\n";

    for (int j = 0;j<4; j++)

    {

      s++;

      if(strcmp(bus[l].seat[i][j], "Empty")==0)

        {

          cout.width(5);

          cout.fill(' ');

          cout<<s<<".";

          cout.width(10);

          cout.fill(' ');

          cout<<bus[l].seat[i][j];

          p++;

        }

        else

        {

        cout.width(5);

        cout.fill(' ');

        cout<<s<<".";

        cout.width(10);

        cout.fill(' ');

        cout<<bus[l].seat[i][j];

        }

      }

    }

  cout<<"\n\nThere are "<<p<<" seats empty in Bus No: "<<bus[l].busn;

  }

void a::avail()

{


  for(int n=0;n<p;n++)>

  {

    vline('*');

    cout<<"Bus no: \t"<<bus[n].busn<<"\nDriver: \t"<<bus[n].driver

    <<"\t\tArrival time: \t"<<bus[n].arrival<<"\tDeparture Time: \t"

    <<bus[n].depart<<"\nFrom: \t\t"<<bus[n].from<<"\t\tTo: \t\t\t"

    <<bus[n].to<<"\n";

    vline('*');

    vline('_');

  }

}

int main()

{

system("cls");

int w;

while(1)

{

    //system("cls");

  cout<<"\n\n\n\n\n";

  cout<<"\t\t\t1.Install\n\t\t\t"

  <<"2.Reservation\n\t\t\t"

  <<"3.Cancel Reservation\n\t\t\t"

  <<"4.Show\n\t\t\t"

  <<"5.Buses Available. \n\t\t\t"

  <<"6.Exit";

  cout<<"\n\t\t\tEnter your choice:-> ";

  cin>>w;

  switch(w)

  {

    case 1:  bus[p].install();

      break;

    case 2:  bus[p].allotment();

      break;

    case 3:bus[p].cancel();
        break;

    case 4:  bus[0].show();

      break;

    case 5:  bus[0].avail();

      break;

    case 6:  exit(0);

  }

}

return 0;

}

推荐答案

有很多关于该代码的内容可以改进,其中一些可能有所改进使实现新功能变得更加困难。



1.合理的格式化和命名可以使代码更容易阅读。例如,不要将您的班级命名为 a ,将其命名为 Busride 或类似的(它描述的不是一个公共汽车,这是一个预定的车程)



2. 从不使用全局变量!它们只会让你更难控制程序的状态,因为你永远不知道哪些函数会改变什么值作为副作用!



3.类方法应该只改变他们自己的实例的价值观。这是不使用全局变量的直接后果。面向对象编程的重点是类方法将更改应用于它们被调用的对象!在您的代码中,您将更改应用于某些全局对象,这在大多数情况下都是如此? - 恰好是它被调用的同一个对象(据我所见)。这纯粹是胡说八道:它使得方法的结果依赖于当前的全局状态,并且意味着你无法在不对整个全局状态进行精确关注的情况下随意调用该方法!只需将这些方法的代码复制到调用它们的位置,就不会出错并且更具可读性!这也不是面向对象的,并且它仍然是使用全局变量的错误代码,但至少你会看到代码的作用。

你的方法需要看起来像这样: br />
There are many things about that code that could be improved, and some of it may make it harder to implement new features.

1. reasonable formatting and naming can make code a lot easier to read. E. g., don't name your class a, name it Busride or similar (what it describes is not a bus at all, it is a scheduled ride)

2. never use global variables! They only make it harder to control the state of your program, because you never know what functions alters what value as a side effect!

3. class methods should only change values of their own instance. This is a direct consequence of not using global variables. The whole point of object oriented programming is that class methods apply changes to the object they are called on! In your code you instead apply changes to some global object, which - most of the time? - happens to be the same object it is called on (as far as I can see). This is pure nonsense: it makes the result of the method dependend on the current global state, and means you can not freely call the method anywhere without paying precise attention to the entire global state! It would be less error prone and more readable to just copy the code of these methods to the place where they are called! That would not be object-oriented either, and it would still be bad code for using global variables, but at least you'd see what the code does.
Your methods need to look like this:
void Busride::install() {
  cout << "Enter bus no: ";
  cin >> busn;
  cout << "\nEnter Driver's name: ";
  cin >> driver;
  cout << "\nArrival time: ";
  cin >> arrival;
  cout << "\nDeparture: ";
  cin >> depart;
  cout << "\nFrom: \t\t\t";
  cin >> from;
  cout << "\nTo: \t\t\t";
  cin >> to;
  empty();
}



4.不要没有充分理由使用C字符串。你已经在使用STL类 cin cout ,为什么,那么,你也不是STL string class?至少它会解决乘客和公共汽车司机的名字可能比您在计划中预期的名字更长的问题!



5.不要滥用变量,用于存储两个语义上独立的信息,特别是在语义类型不匹配的情况下。这是糟糕的设计,当您稍后尝试扩展程序的功能时,它可能会让您遇到麻烦。如果有效值发生冲突,它甚至可能会让您陷入困境:e。 G。如果乘客告诉你他的名字是空的。好的,不太可能,但这些是您必须考虑的情况,以确保您的程序始终正确运行。座位预订的正确设计是使用两个变量:一个用于存储其当前状态(保留或空,或者未来的扩展版本中的其他内容),另一个用于存储乘客姓名。为简单起见,您可以将它们组合成结构或次要类,如下所示:


4. don't use C-strings for no good reason. You are already using the STL classes cin and cout, why, then, don't you also you the STL string class? At the very least it will take care of the problem that passengers and bus drivers may have longer names than you anticipated in your program!

5. don't abuse a variable to store two semantically separate pieces of information, especially if the semantical types don't match. That is bad design, and it may get you in trouble when you later try to extend the functionality of your program. It may even get you in trouble already, in case of a conflict of valid values: e. g. if a passenger told you his name was "Empty". Ok, unlikely, but these are cases you must consider to ensure your program runs correctly at all times. The correct design for a seat reservation would be to use two variables: one to store its current state (reserved or empty, or maybe something else in a future extended version), and the other to store the passenger name. For simplicity's sake, you can combine them into a struct or minor class, like this:

class SeatReservation {
   bool reserved_;
   std::string name_;
public:
   SeatReservation() : reserved_(false) {}
   bool reserved() const { return reserved_; }
   bool reserve(const std::string& name) {
      if (reserved())
         return false; // reservation failed!
      else {
         name_ = name;
         reserved_ = true;
      }
      return true;
   }
   void cancel() { reserved_ = false; }
   std::string get_name() const { return reserved() ? name : "Empty"; }
};





注意方法 SeatReservation :: cancel()已经实现了您在最低级别询问的删除预约功能。你只需要找到正确的座位并调用方法 cancel()



至于你的问题:如果你写了这个程序,你究竟需要帮助的是什么?显然该程序已经打印并处理各种选择,为什么不能再添加两个?



如果你没有写程序,那么,在那种情况下,我只是不知道从哪里开始解释,特别是因为代码不是'写得不错。



Note that the method SeatReservation::cancel() already implements the delete reservation function you asked about on the lowest level. You'll only need to address the correct seat and call the method cancel().

As to your question: if you wrote this program, what is it you need help with, exactly? Apparently the program already does print and handle various choices, so why can't you add two more?

If you didn't write the program, ... well, in that case I just wouldn't know where to start explaining, especially since the code isn't written well to start with.


这篇关于我需要添加另外2个选项,例如删除预订和编辑预订,但无法弄明白......任何人都可以帮助我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆