[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. Android JNI 传递对象

    JNI初步入门后,在传递数据的时候,遇到一个需求:有多个数据需要在Java与C代码之间进行传递.如果都做为函数参数传入,则函数很长很难看,并且多个数据的返回也不好实现.所以想到了把数据打包后传递.这在 ...

  2. 00-python语言介绍

    以下为摘录的python的介绍 Python是一种解释型语言.这就是说,与C语言和C的衍生语言不同,Python代码在运行之前不需要编译.其他解释型语言还包括PHP和Ruby. Python是动态类型 ...

  3. maven的安装以及问题

    https://blog.csdn.net/machao0903/article/details/73368909https://www.cnblogs.com/jiejiecool/p/421885 ...

  4. Effective java 系列之避免过度同步和不要使用原生态类型,优先考虑泛型

    避免过度同步(67):在一个被同步的方法或代码块中,不要调用哪些被设计成被覆盖的方法或者是由客户端以函数对象的形式提供的方法(21). 有点拗口,书上提供的创建者与观察者模式,add方法太多,看得眼花 ...

  5. Linux(centos7)上安装最新版R3.4.1

    说来惭愧,居然没有在Linux安装R的经验,因为一直很少用R,用也是在win平台. 下载路径:https://cran.rstudio.com/src/base/R-3/ 强烈建议不要安装最新的R,除 ...

  6. (转)解决windows10下无法安装.net framework 3.5,错误代码0x800F081F

    1.下载 NET Framework 3.5的安装包netfx3.cab 将下载的文件复制到复制到 C 盘的 Windows 文件夹 后请在“命令提示符(管理员)”中执行下面的命令: dism /on ...

  7. webpack添加热更新

    之前的wbepack一直没有加上热更新,这是一种遗憾,今天终于加上去了,看不懂我博客的可以看这篇文章:http://blog.csdn.net/hyy1115/article/details/5302 ...

  8. spark app

    使用Spark和Scala分析Apache访问日志 http://www.jdon.com/bigdata/analyzing-apache-access-logs-files-spark-scala ...

  9. Bacterial Melee CodeForces - 756D (dp去重)

    大意: 给定字符串, 每次可以任选一个字符$x$, 将$x$左侧或右侧也改为$x$, 求最终能得到多少种字符串. 首先可以观察到最终字符串将连续相同字符合并后一定是原字符串的子序列 并且可以观察到相同 ...

  10. 77. Combinations (java 求C(n,k)的组合,排除重复元素)

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. 解析:同求全 ...