[cpp] view plain copy
第一步:构建一个头文件(**.h)
[cpp] view plain copy
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
using namespace std;
class CStudentRec
{
public:
char chFlag;//标志,A表示正常,N表示空
char strName[];//姓名
char strID[];//学号
float fScore[];//3门成绩
float fAve;//总平均分
CStudentRec(char *name,char *id,float score[]);
CStudentRec(){chFlag='N';};//默认构造函数
~CStudentRec(){};//默认析构函数
void Input(void);
float Validate(void);//成绩数据的输入验证,返回正确值
void Print(bool isTitle=false);
friend ostream& operator<<( ostream& os,CStudentRec& stu);
friend istream& operator>>( istream& is,CStudentRec& stu);
}; CStudentRec::CStudentRec(char *name,char *id,float score[])
{
strncpy(strName,name,);
strncpy(strID,id,);
fAve=;
for(int i=;i<;i++)
{
fScore[i]=score[i];
fAve+=fScore[i];
}
fAve=float(fAve/3.0);
chFlag='A';
}
void CStudentRec::Input(void)
{
cout<<"姓名:";
cin>>strName;
cout<<"学号:";
cin>>strID;
float fSum=;
for(int i=;i<;i++)
{
cout<<"成绩"<<i+<<":";
fScore[i]=Validate();
fSum+=fScore[i];
}
fAve=(float)(fSum/3.0);
chFlag='A';
getchar();
}
float CStudentRec::Validate(void)
{
int s;
char buf[];
float res;
for(;;)
{
cin>>res;
s=cin.rdstate();
while(s)
{
cin.clear();
cin.getline(buf,);
cout<<"非法输入,重新输入:";
cin>>res;
s=cin.rdstate();
}
if((res<=100.0)&&(res>=0.0))break;
else
cout<<"输入的成绩超过范围!请重新输入:";
}
return res;
}
void CStudentRec::Print(bool isTitle)
{
cout.setf(ios::left);
if(isTitle)
cout<<setw()<<"姓名:"<<setw()<<"学号:"<<"\t成绩1:"<<"\t成绩2:"<<"\t成绩3:"<<"\t平均分:"<<endl;
cout<<setw()<<strName<<setw()<<strID;
for(int i=;i<;i++)
cout<<"\t"<<fScore[i];
cout<<"\t"<<fAve<<endl;
getchar();
}
ostream& operator<<(ostream& os,CStudentRec& stu)
{
os.write(&stu.chFlag,sizeof(char));
os.write(stu.strName,sizeof(stu.strName));
os.write(stu.strID,sizeof(stu.strID));
os.write((char*)&stu.fScore,sizeof(float)*);
os.write((char*)&stu.fAve,sizeof(float));
return os;
}
istream& operator>>(istream& is,CStudentRec& stu)
{
char name[],id[];
is.read(&stu.chFlag,sizeof(char));
is.read(name,sizeof(name));
is.read(id,sizeof(id));
is.read((char*)stu.fScore,sizeof(float)*);
is.read((char*)&stu.fAve,sizeof(float));
strncpy(stu.strName,name,sizeof(name));
strncpy(stu.strID,id,sizeof(id));
return is;
} class CStuFile
{
public:
CStuFile(char* filename);
~CStuFile();
void Add(CStudentRec stu);
int Seek(char* id,CStudentRec &stu);
int List(int nNum=-);
private:
char* strFileName;
};
CStuFile::CStuFile(char* filename)
{
strFileName=new char[strlen(filename)+];
strcpy(strFileName,filename);
}
CStuFile::~CStuFile()
{
if(strFileName)delete []strFileName;
}
void CStuFile::Add(CStudentRec stu)
{
fstream file(strFileName,ios::out|ios::app|ios::binary);
file<<stu;
file.close();
}
int CStuFile::Seek(char* id,CStudentRec& stu)
{
int nRec=-;
fstream file(strFileName,ios::in|ios::_Nocreate);
if(!file)
{
cout<<"文件"<<strFileName<<"不能打开!\n";
return nRec;
}
int i=;
while(!file.eof())
{
file>>stu;
if((strcmp(id,stu.strID)==)&&(stu.chFlag!='N'))
{
nRec=i;break;
}
i++;
}
file.close();
return nRec;
getchar();
}
int CStuFile::List(int nNum)
{
fstream file(strFileName,ios::in|ios::_Nocreate);
if(!file)
{
cout<<"文件"<<strFileName<<"不能打开!\n";
return ;
}
int nRec=;
if((nNum==-)||(nNum>))
{
cout.setf(ios::left);
cout<<setw()<<"记录"<<setw()<<"姓名"<<setw()<<"学号"<<"\t成绩1\t成绩2\t成绩3\t平均分"<<endl;
}
while(!file.eof())
{
CStudentRec data;
file>>data;
if(data.chFlag=='A')
{
nRec++;
if((nNum==-)||(nRec<=nNum))
{
cout.setf(ios::left);
cout<<setw()<<nRec;
data.Print();
}
}
}
file.close();
return nRec;
}
[cpp] view plain copy
第二部:创建一个.cpp文件
[cpp] view plain copy
#include<iostream>
#include"**.h"
CStuFile theStu("student.txt");
void AddTo(int nNum)
{
CStudentRec stu;
for(int i=;i<nNum;i++)
{
cout<<"请输入第"<<i+<<"记录:"<<endl;
stu.Input();
theStu.Add(stu);
}
}
void main()
{
AddTo();
theStu.List();
CStudentRec one;
if(theStu.Seek("",one)>=)
one.Print(true);
else
cout<<"没有找到!\n";
theStu.List();
getchar();
}
[cpp] view plain copy
ok

用c++写一个数据库的更多相关文章

  1. 如何写一个数据库How do you build a database?(转载)

    转载自:http://www.reddit.com/r/Database/comments/27u6dy/how_do_you_build_a_database/ciggal8 Its a great ...

  2. 跟我一起用Symfony写一个博客网站;

    我的微信公众号感兴趣的话可以扫一下, 或者加微信号   whenDreams 第一部分:基础设置,跑起一个页面-首页 第一步: composer create-project symfony/fram ...

  3. 用 Python 写一个 NoSQL 数据库Python

    NoSQL 这个词在近些年正变得随处可见. 但是到底 “NoSQL” 指的是什么? 它是如何并且为什么这么有用? 在本文, 我们将会通过纯 Python (我比较喜欢叫它, “轻结构化的伪代码”) 写 ...

  4. 用javaweb写一个注册界面,并将数据保存到后台数据库(全部完成)(课堂测试)

    一.题目:WEB界面链接数据库 1.考试要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母. ...

  5. 独自handle一个数据库大程有感

    这学期数据库课程,最后的大程是写一个MiniSQL的数据库实现,要求很简单,建删表,建删单值索引,支持主键和unique定义,支持最简单的select,只要支持3个类型:int,float,char( ...

  6. 利用Sql实现将指定表数据导入到另一个数据库示例

    因为工作中经常需要将数据从一个数据库导入到另一个数据库中,所以将这个功能写成一个存储过程,以方便调用.现在粘贴出来供大家参考: 注意:1,以下示例中用到了syscolumns,sysobjects等系 ...

  7. Java Web 开发利用Struts2+Spring+mybatis写一个用户登录界面以及简单的数据交互

    框架的东西太复杂也难以讲通,直接上代码: 一.首先得配置环境 和导入必要的jar包 有一些重要的如下: Filter文件夹下的SafetyFilter.java   model文件夹下的 Global ...

  8. MySQL创建一个用户,指定一个数据库 授权

    Mysql 创建一个用户 hail,密码 hail,指定一个数据库 haildb 给 hail mysql -u root -ppassworduse mysql;insert into user(h ...

  9. MNIST手写数字数据库

    手写数字库很容易建立,但是总会很浪费时间.Google实验室的Corinna Cortes和纽约大学柯朗研究所的Yann LeCun建有一个手写数字数据库,训练库有60,000张手写数字图像,测试库有 ...

随机推荐

  1. Unity --- 纹理为什么要设置为2的N次方

    1.图片的纹理像素在Unity3D中需要遵循2的N次方,由图形学决定的,只识别2的N次方.   非2的N次方的图片会转化为2的N次方图片(500 x 500 → 512 x 512),是因为转化过程比 ...

  2. ubuntu12.04 安装CAJViewer-ubuntu(待解决)

    ubuntu12.04测试通过 1.sudo apt-get install wine 2.unzip CAJViewer-ubuntu12.04版.zip 3.wine CAJVieweru.exe

  3. 雷林鹏分享:jQuery EasyUI 树形菜单 - 树形菜单加载父/子节点

    jQuery EasyUI 树形菜单 - 树形菜单加载父/子节点 通常表示一个树节点的方式就是在每一个节点存储一个 parentid. 这个也被称为邻接列表模型. 直接加载这些数据到树形菜单(Tree ...

  4. 混合线性模型(linear mixed models)

    一般线性模型.混合线性模型.广义线性模型 广义线性模型GLM很简单,举个例子,药物的疗效和服用药物的剂量有关.这个相关性可能是多种多样的,可能是简单线性关系(发烧时吃一片药退烧0.1度,两片药退烧0. ...

  5. G.711是一种由国际电信联盟(ITU-T)制定的音频编码方式

    http://zh.wikipedia.org/zh-cn/G.711 ITU-T G.711 page ITU-T G.191 software tools for speech and audio ...

  6. ssh登陆被拒?(云:使用云的网页版远程登陆) 不好用。

    设定的密

  7. 安装 zookeeper cluster

    安装 zookeeper cluster zookeeper-3.4.5.tar.gz n0 10.69.10.193 n1 10.69.10.188 n2 10.69.10.192 n0 上解压 z ...

  8. Xmanager Power Suit 6.0.0009 最新版注册激活

    Xmanager Power Suit 6.0.0009 最新版注册激活 手工操作步骤Xmanger Power Suit 官方 其实有两种 .exe 文件,一个是用于试用的,在注册的时候不能直接输入 ...

  9. SQL SERVER 一组数据按规律横着放置,少则补空,如人员按一进一出的规律,进出为一组,缺少的补null

    假设一组数据:人员进出刷卡数据表[SwingCard] ID MenID Door 1 1 In 2 1 In 3 1 Out 4 1 In 5 1 Out 6 1 Out 想要变成如下:一进一出为一 ...

  10. Python mongoDB读取

    class db_class(): def __init__(self): mongo_DB='test1' self.mongo_TABEL='test' client=pymongo.MongoC ...