[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. Go使用protobuf

    WIN7 + Go1.9.2+protobuf3.5.1 1.首先定义一个用于测试的proto文件test.proto,内容如下: syntax = "proto3"; packa ...

  2. CURL操作

    具体代码如下: <?php$curl=curl_init(); //初始化$url='http://www.ecshop.com';//curl_setopt(curl资源,选项标志,选项值)c ...

  3. learn python the hard way习题31~40总结以及列表的扩展知识

    Python 中的列表: 形式:[ 表示打开一个列表,中间的项目用 , 隔开,然后列表以 ] 结束. for循环 两种形式: for i in ArrayName: for i in range(0, ...

  4. LeetCode--232--用栈实现队列

    问题描述: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队 ...

  5. 58Ajax

    Ajax 1 .客户端浏览器通过执行一段JS代码向服务器发送请求,服务器路由对应的视图函数返回一个json字符串作为响应,    浏览器接受响应后会触发该ajax请求的回调函数success,参数为响 ...

  6. vue select的change事件,将点击过的城市名存在数组中,下次调用不需要再调用接口

    <template> <div id="body" class="application" v-show="show" v ...

  7. VS Code行内样式提示插件

    打开vscode,在软件界面左下角找到“齿轮”标志并点击,在弹出的菜单中选择“设置”,把下面的代码添加到设置里. { "workbench.colorTheme": "C ...

  8. 4、Ubuntu系统环境变量详解

    参考:Linux公社Ubuntu系统环境变量详解 UNIX/Linux系统中的环境变量和库文件的使用方法 由于Linux系统严格的权限管理,造成Ubuntu系统有多个环境变量配置文件,因此我们需要了解 ...

  9. IE的“浏览器模式”和“文档模式的区别”

    1.浏览器模式 用于切换IE针对该网页的默认文档模式.对不同版本浏览器的条件备注解析.发送给网站服务器的用户代理(User_Agent)字符串的值.网站可以根据浏览器返回的不同用户代理字符串判断浏览器 ...

  10. Oracle11g温习-第十七章:权限管理

    2013年4月27日 星期六 10:50 1.权限(privilege):     [system privilege(系统权限):针对于database 的相关权限         object p ...