商厂里调货拿包包程序

/************************************************************************/
/* 开发环境:vc6.0
/* 时间:2010.10.31 by zhangdongsheng                                    */
/************************************************************************/
#include <iostream>
#include <string>
#include <time.h>
#include <fstream>
#include <conio.h>
#include <windows.h>
#include <iomanip>


using namespace std;

class borrow
{
public:
 string name;
 string goods;
 string num;
};

const char *str="D:\\data.dat";

void FirstDisplay();//程序主菜单
void Input();       //输入
void Delete();      //删除记录
void Out();      //显示所有
string Int_to_string(int a);//将一个整形转化为string
tm *GetTime();       //获取当前时间
bool FindEqualData(string &today); //检查 str里面是否有字符串string
bool checkChoice(string str);//判断 str 是否为数字

int main()          //main函数
{
 ifstream fin(str);   //如果没有文件则创建
 if(fin.fail())
 { ofstream fon(str);
  fon.close();
 }
 fin.close();
 FirstDisplay();
 system("pause");
 return 0;
}

string Int_to_string(int a)  //整形转化为string
{
 char temp[10];
 
 memset(temp,0,sizeof(temp));
 itoa(a,temp,10);
 string ResultStr=temp;
 return ResultStr;
}

//程序主菜单
void FirstDisplay()
{
 ifstream fin;
 fin.open(str);
// if(fin.good()) cout<<"打开文件成功"<<endl;
 
 tm *prt=GetTime();
 
 //当前的年月日 转化为一人字符串如"2010.10.31"
 string today=Int_to_string(1900+prt->tm_year);
 today.append(".");
 today.append(Int_to_string(prt->tm_mon+1));
 today.append(".");
 today.append(Int_to_string(prt->tm_mday));

 cout<<"今天是"<<today<<endl;

 // 在文本中查找是否出现string"2010.10.31"
 string data;
 bool check;
 char ch;
 while(!fin.eof())
 {
  getline(fin,data);
  if(data==today) //如果出现"2010.10.31"
  {
   check=true;
   break;
  }
  else
   check=false;//
 }
 if(check)
 {
  while(!fin.eof())
  {
   ch=fin.get();
   cout<<ch;
  }
  cout<<"\n\n\n"<<endl;
 }
 else
  cout<<endl<<"今天没有记录\n\n"<<endl;
 fin.close();

 //菜单
 int choice;
 string TempChoice;
 cout<<"***************欢迎使用拿包包记录系统**************\n";
 puts("*\t+--------------------------------+        *");
 puts("*\t|         1.添加记录             |        *");
 puts("*\t|         2.查看所有记录         |        *");
 puts("*\t|         3.删掉所有记录         |        *");
 puts("*\t|         4.保存退出             |        *");
 puts("*\t|                                |        *");
 puts("*\t|                                |        *");
 puts("*\t|          c++奋斗乐园           |        *");
 puts("*\t+--------------------------------+        *");
 puts("***************************************************");
    cout<<"\t请输入您的选择:";
 cin>>TempChoice;
 while(!checkChoice(TempChoice))
 {
  cout<<"输入的不是数字,重新输入"<<endl;
  cin>>TempChoice;
 }
 choice=atoi(TempChoice.c_str());
 switch(choice)
   {
   case 1:
    Input();
    break;
   case 2:
    Out();
    break;
   case 3:
    Delete();
    break;
   case 4:
    cout<<"程序即将退出"<<endl;
    exit(1);
   default:
    cout<<"输入有误,请输入1-4的数字"<<endl;
    break;
   }
 
 
}


void Input()      //输入
{
 ofstream fon;
 fon.open(str,ios::app);
 tm *prt=GetTime();
 
 string today=Int_to_string(1900+prt->tm_year);
 today.append(".");
 today.append(Int_to_string(prt->tm_mon+1));
 today.append(".");
 today.append(Int_to_string(prt->tm_mday));

 string finish="1";
 
 borrow one;
 
 while(finish=="1")
 {
  ifstream fin(str,ios::app);
  cout<<"谁拿了"<<endl;
  cin>>one.name;
  cout<<"拿了什么"<<endl;
  cin>>one.goods;
  cout<<"几个"<<endl;
  cin>>one.num;
  if(FindEqualData(today))
  { 
   fin.close();
//   fon<<one.name<<"\t"<<one.goods<<"\t\t\t"<<one.num<<"\t\t"<<prt->tm_hour<<":"<<prt->tm_min<<":"<<prt->tm_sec<<endl;
   fon<<one.name<<setw(20)<<one.goods<<setw(10)<<one.num<<setw(10)<<prt->tm_hour<<":"<<prt->tm_min<<":"<<prt->tm_sec<<endl;
  }
  else
  { 
   
   fon<<today<<endl;
   fon<<"------------------------------------------------------------------"<<endl;
   fon<<"姓名"<<setw(20)<<"东西"<<setw(10)<<"个数"<<setw(15)<<"日期"<<endl;
   
//   fon<<one.name<<"\t"<<one.goods<<"\t\t\t"<<one.num<<"\t\t"<<prt->tm_hour<<":"<<prt->tm_min<<":"<<prt->tm_sec<<endl;
   fon<<one.name<<setw(20)<<one.goods<<setw(10)<<one.num<<setw(10)<<prt->tm_hour<<":"<<prt->tm_min<<":"<<prt->tm_sec<<endl;
   fin.close();
  }
  cout<<"再有没有要输入的,有(1),没有(0)"<<endl;
  cin>>finish;
 }
 if(finish=="0")
 cout<<"按任意键返回主菜单"<<endl;
 else cout<<"输入有误,按任意键返回主菜单"<<endl;
 getch();
 system("CLS");
 FirstDisplay();
}

void Delete()      //删除记录
{
 
 char *password;
 char *realpassword="1234";
 char *p=NULL;
 int count=0;
 int length=4;
 password = new char[length+1];
 cout<<"请输入4位密码"<<endl;
 p=password;
 while(((*p=getch()))&&(count<length))
 {
  putch('*');
  fflush(stdin);
  p++;
  count++;
 }
 password[count]='\0';
 if(strcmp(password, realpassword) == 0)
 {
  ofstream fon(str,ios::trunc);
  fon<<""<<endl;
  cout<<"成功删除"<<endl;
  fon.close();
  getch();
  system("CLS");
  FirstDisplay();
 }
 else
 {
  cout<<"密码错误"<<endl;
  FirstDisplay();
 }
/* int secret;
 cout<<"请输入密码"<<endl;
 cin>>secret;
 if(secret==3342125)
 {
  ofstream fon(str,ios::trunc);
  fon<<""<<endl;
  cout<<"成功删除"<<endl;
 }
 else
  cout<<"密码不对"<<endl;*/
}

void Out()  //输出
{
 ifstream fin(str);
 char ch;
 while(!fin.eof())
 {
  ch=fin.get();
  cout<<ch;
 }
 cout<<"\n\n按任意键进入主菜单";
 fin.close();
 getch();
 system("CLS");
 FirstDisplay();
}

tm *GetTime()//获取当前时间的一个指针
{
 tm *prt;
 time_t t;
 t=time(NULL);
 prt=localtime(&t);
 return prt;
}

bool FindEqualData(string &today)  //判断文件中是否有 today 字符串
{
 ifstream fin(str);
 string data;
 bool check;
 while(!fin.eof())
 {
  getline(fin,data);
  if(data==today) //如果出现"2010.10.31"
  {
   check=true;
   break;
  }
  else
   check=false;//
 }
 fin.close();
 return check;


}

bool checkChoice(string str)
{
 for(int i=0;i<str.length();i++)
  if(str[i]>'9'||str[i]<'0')
   return false;
 return true;
}

原文地址:https://www.cnblogs.com/zhangdongsheng/p/1866773.html