c ++,显示数据库中具有相同日期的记录

时间:2015-12-30 07:20:28

标签: c++

我需要显示具有特定日期的记录。请检查“void display_fromDate”。我收到错误“char之前的预期主表达式”并指向“int main() - >案例2.感谢您的建议。

#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;



class patient
{
    int contactnum;
    char name[50];
    //float add;
    char bookingDate[15];
    char bookingTime[5];
    char trtType[15];
    //int add, bookingDate, bookingTime;
    //double per;
    //char grade;
    //void calculate(); 
public:
    void getdata();     
    void showdata() const;  
    void show_tabular() const;
    int getIDNum() const;
    char bDate() const;
}; 


/*void patient::calculate()
{
    per=(physics+chemistry+mathematics+english+comscience)/5.0;
    if(per>=90)
        grade='A+';
    else if(per>=80)
        grade='A';
    else if(per>=75)
        grade='A-';
    else if(per>=70)
        grade='B+';
    else if(per>=65)
        grade='B';
    else if(per>=60)
        grade='B-';
    else if(per>=55)
        grade='C+';
    else if(per>=50)
        grade='C';
    else
        grade='F';
}*/

void patient::getdata()
{
    cout<<"\nEnter The Contact Number of the Patient ";
    cin>>contactnum;
    cout<<"\n\nEnter Patient's Name: ";
    cin.ignore();
    cin.getline(name,50);   
    cout<<"\nEnter Booking Date: ";
    cin>>bookingDate;
    cout<<"\nEnter Booking Time: ";
    cin.ignore();
    cin>>bookingTime;
    cout<<"\nEnter Treatment Type: ";
    cin>>trtType;
    //calculate();
}

void patient::showdata() const
{
    cout<<"\nContact Number: "<<contactnum;
    cout<<"\nName: "<<name;
    cout<<"\nBooking Date: "<<bookingDate;
    cout<<"\nBooking Time: "<<bookingTime;
    cout<<"\nTreatment Type: "<<trtType;

}

void patient::show_tabular() const
{
    cout<<contactnum<<setw(6)<<" "<<name<<setw(4)<<bookingDate<<setw(4)<<bookingTime<<setw(4)<<trtType<<endl;
}

int  patient::getIDNum() const
{
    return contactnum;
}



void Savepatient(); 
void displayAll();  
void Searchdisplay(int);    
void modifypatient(int);    
void deletepatient(int);    
void DisplayClassResult();  
void DisplayResult();           





void write_patient()
{
    patient st;
    ofstream outFile;
    outFile.open("patient.dat",ios::binary|ios::app);
    st.getdata();
    outFile.write(reinterpret_cast<char *> (&st), sizeof(patient));
    outFile.close();
    cout<<"\n\nPatient record has been Created ";
    cin.ignore();
    cin.get();
}



/*void display_all()
{
    patient st;
    ifstream inFile;
    inFile.open("patient.dat",ios::binary);
    if(!inFile)
    {
        cout<<"File could not be open !! Press any Key...";
        cin.ignore();
        cin.get();
        return;
    }
    cout<<"\n\n\n\t\tDISPLAY ALL RECORD !!!\n\n";
    while(inFile.read(reinterpret_cast<char *> (&st), sizeof(patient)))
    {
        st.showdata();
        cout<<"\n\n====================================\n";
    }
    inFile.close();
    cin.ignore();
    cin.get();
}*/

void display_fromDate(char n)
{
    patient st;
    ifstream inFile;
    inFile.open("patient.dat",ios::binary);
    if(!inFile)
    {
        cout<<"File could not be open !! Press any Key...";
        cin.ignore();
        cin.get();
        return;
    }
    bool flag=false;
    while(inFile.read(reinterpret_cast<char *> (&st), sizeof(patient)))
    {
        if(st.bDate()==n)
        {
             st.showdata();
             flag=true;
        }
    }
    inFile.close();
    if(flag==false)
        cout<<"\n\nrecord not exist...";
    cin.ignore();
    cin.get();
}





void display_sp(int n)
{
    patient st;
    ifstream inFile;
    inFile.open("patient.dat",ios::binary);
    if(!inFile)
    {
        cout<<"File could not be open !! Press any Key...";
        cin.ignore();
        cin.get();
        return;
    }
    bool flag=false;
    while(inFile.read(reinterpret_cast<char *> (&st), sizeof(patient)))
    {
        if(st.getIDNum()==n)
        {
             st.showdata();
             flag=true;
        }
    }
    inFile.close();
    if(flag==false)
        cout<<"\n\nrecord not exist...";
    cin.ignore();
    cin.get();
}


void modify_patient(int n)
{
    bool found=false;
    patient st;
    fstream File;
    File.open("patient.dat",ios::binary|ios::in|ios::out);
    if(!File)
    {
        cout<<"File could not be open !! Press any Key...";
        cin.ignore();
        cin.get();
        return;
    }
        while(!File.eof() && found==false)
    {

        File.read(reinterpret_cast<char *> (&st), sizeof(patient));
        if(st.getIDNum()==n)
        {
            st.showdata();
            cout<<"\n\nPlease Enter The New Details of Patient"<<endl;
            st.getdata();
                int pos=(-1)*static_cast<int>(sizeof(st));
                File.seekp(pos,ios::cur);
                File.write(reinterpret_cast<char *> (&st), sizeof(patient));
                cout<<"\n\n\t Record Updated";
                found=true;
        }
    }
    File.close();
    if(found==false)
        cout<<"\n\n Record Not Found ";
    cin.ignore();
    cin.get();
}



void delete_patient(int n)
{
    patient st;
    ifstream inFile;
    inFile.open("patient.dat",ios::binary);
    if(!inFile)
    {
        cout<<"File could not be open !! Press any Key...";
        cin.ignore();
        cin.get();
        return;
    }
    ofstream outFile;
    outFile.open("Temp.dat",ios::out);
    inFile.seekg(0,ios::beg);
    while(inFile.read(reinterpret_cast<char *> (&st), sizeof(patient)))
    {
        if(st.getIDNum()!=n)
        {
            outFile.write(reinterpret_cast<char *> (&st), sizeof(patient));
        }
    }
    outFile.close();
    inFile.close();
    remove("patient.dat");
    rename("Temp.dat","patient.dat");
    cout<<"\n\n\tRecord Deleted ..";
    cin.ignore();
    cin.get();
}


void class_result()
{
    patient st;
    ifstream inFile;
    inFile.open("patient.dat",ios::binary);
    if(!inFile)
    {
        cout<<"File could not be open !! Press any Key...";
        cin.ignore();
        cin.get();
        return;
    }
    cout<<"\n\n\t\tALL PATIENTS BOOKING DETAILS\n\n";
    cout<<"==============================================================\n";
    cout<<"Mobile.No      Name                Booking Date   Booking Time"<<endl;
    cout<<"==============================================================\n";
    while(inFile.read(reinterpret_cast<char *> (&st), sizeof(patient)))
    {
        st.show_tabular();
    }
    cin.ignore();
    cin.get();
    inFile.close();
}




int main()
{
    char ch;
    int num;
    cout.setf(ios::fixed|ios::showpoint);
    cout<<setprecision(2); 
    do
    {
    system("cls");
    cout<<"\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
    cout<<"\n\n\t1.CREATE PATIENT RECORD";
    cout<<"\n\n\t2.DISPLAY ALL PATIENTS RECORDS";
    cout<<"\n\n\t3.SEARCH PATIENT RECORD ";
    cout<<"\n\n\t4.MODIFY PATIENT RECORD";
    cout<<"\n\n\t5.DELETE PATIENT RECORD";
    cout<<"\n\n\t6.DISPLAY CLASS RESULT";
    cout<<"\n\n\t7.EXIT";
    cout<<"\n\n\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
    cout<<"\n\n\tPlease Enter Your Choice (1-7): ";
    cin>>ch;
    system("cls");
    switch(ch)
    {
    case '1':   write_patient(); break;
    case '2':   display_fromDate(char n); break;
    case '3':   cout<<"\n\n\tPlease Enter Patient's Contact Number: "; cin>>num;
                display_sp(num); break;
    case '4':   cout<<"\n\n\tPlease Enter Patient's Contact Number: "; cin>>num;
            modify_patient(num);break;
    case '5':   cout<<"\n\n\tPlease Enter Patient's Contact Number: "; cin>>num;
            delete_patient(num);break;
    case '6' :  class_result(); break;
    case '7':   exit(0);;
    default:    cout<<"\a"; 

    }
    }while(ch!='7');

    return 0;
}

1 个答案:

答案 0 :(得分:0)

这可能无法解决您的问题,但可以提供帮助 (对于Downvoters:这太大了,不适合评论。另外,请在downvoting时发表评论。)

一些基础:

struct Time
{
  unsigned int hours;
  unsigned int minutes;
  unsigned int seconds;
};
struct Date
{
  unsigned int day;
  unsigned int month;
  unsigned int year;
};

建立时间和日期结构后,患者结构现在看起来像:

class Patient
{
  public:
  unsigned int  contact_num;
  std::string   name;
  std::string   trtype;
  Date          booking_date;
  Time          booking_time;
};

要使日期具有可比性,您可以添加一些方法:

bool  operator==(const Date& d)
{
  return (year == d.year) && (month == d.month) && (day == d.day);
}
bool  operator< (const Date& d)
{
  if (year == d.year)
  {
    if (month == d.month)
    {
      return day < d.day;
    }
    else
    {
      return month < d.month;
    }
  }
  return year < d.year;
}

通过这种方式,您可以为患者编写一些订购(比较)功能:

bool Order_By_Date(const Patient& p1, const Patient& p2)
{
  return p1.booking_date < p2.booking_date;
}

如果你有Patient的向量,你可以通过使用以下方式订购(排序)它们:

std::vector<Patient> database;
//...
std::sort(database.begin(), database.end(), Order_By_Date);

这些概念同样适用于Time结构和预订时间。

高级排序功能(例如按名称,按日期)留作OP的练习。

编辑1:按日期搜索或查找

STL搜索算法可以与排序或比较功能一起使用,类似于上面的std::sort功能。

按日期向find人提出:

static Date  search_key = 
{
  .year = 2016;
  .month = 01;
  .day   = 22;
};

bool Find_By_Date(const Person& p)
{
  return p.booking_date == search_key;
}

std::vector<patients>::iterator iter =
  std::find(database.begin(), database.end(),
            search_key,
            Find_By_Date);
if (iter != database.end())
{
  cout << "Person found: " << iter->name << "\n";
}

我建议您查看其他std搜索功能,例如lower_bound

如果这些都不够,你可以随时自己编写。