[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. 《剑指offer》第四十五题(把数组排成最小的数)

    // 面试题45:把数组排成最小的数 // 题目:输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼 // 接出的所有数字中最小的一个.例如输入数组{3, 32, 321},则打印出这3 ...

  2. 学习笔记25—python基本运算法则

    1.矩阵的点乘: a*b, 矩阵乘法:dot(a*b),矩阵的次方:a**num (num = 2,表示2次)2.数组的并集,交集: >>> a = [1,2,3] >> ...

  3. windows 网卡配置的设置命令

    (1)设置为DHCP自动分配 netsh interface ip set address "本地连接"  dhcp netsh interface ip set dns &quo ...

  4. Codeforces 600 E - Lomsat gelral

    E - Lomsat gelral 思路1: 树上启发式合并 代码: #include<bits/stdc++.h> using namespace std; #define fi fir ...

  5. Python全栈开发-有趣的小程序

    进度条的打印 import sys,time for i in range(20): sys.stdout.write('$')      #stdout是标准输出的意思,在一般电脑上,stdout的 ...

  6. 雇佣K个工人的最小费用 Minimum Cost to Hire K Workers

    2018-10-06 20:17:30 问题描述: 问题求解: 问题规模是10000,已经基本说明是O(nlogn)复杂度的算法,这个复杂度最常见的就是排序算法了,本题确实是使用排序算法来进行进行求解 ...

  7. (转)winform之RichTextBox

    RichTextBox是一种可用于显示.输入和操作格式文本,除了可以实现TextBox的所有功能,还能提供富文本的显示功能. 控件除具有TextBox 控件的所有功能外,还能设定文字颜色.字体和段落格 ...

  8. HTML第十章总结

    前言 这一章节讲了以下内容: 两个新的 HTML elelments:它们是 <div>和 <span>,使用这两个 element 可以使得 HTML 有更加 serious ...

  9. 20165327 2017-2018-2 《Java程序设计》第一周学习总结

    第1章 Java入门 一.Java 的特点 简单 面向对象 平台无关 多线程:允许同时完成多个任务 动态:Java程序的基本组成单元就是类(有些类是自己编写的,有一些是从类库中引入的,而类又是运行时动 ...

  10. Juniper基础配置

    root> show configuration | display set      配置按set行显示,查看的配置为未commit的配置(commit check)root# set sys ...