📄 student.cpp
字号:
// tongxunlu000.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <stdio.h>
const int size=100; //定义记录总数
using namespace std;
class Student //定义通讯录的类
{
private:
char StuName[20];
int StuScore;
public:
friend class function; //定义友员类
void display() //显示记录
{
cout << " 姓名:" << StuName<< endl;
cout << " 成绩:" <<StuScore<<endl;
}
};
class function // 定义功能类
{
private:
Student stu[size]; //数组型对象
public:
int TotalRecords; //当前记录总数
function() //记录总数赋初值
{
TotalRecords=0;
}
~function() //记录总数归零
{
TotalRecords=0;
}
void countrecord() //计算当前记录总数
{
fstream f1;
f1.open("record.dat",ios::in|ios::out);
f1.seekg(0,ios::end);
TotalRecords=f1.tellg()/sizeof(Student);
f1.close();
}
void Pause() //暂停
{
cout << "按任意键返回主菜单......" << endl;
char ch;
ch=getch();
Contents();
mainselect();
}
void write() //写入数据
{
system("cls");
ofstream of1;
of1.open("record.dat",ios::app);
countrecord();
char ch;
while(true)
{
//判断记录的空余空间
TotalRecords++;
if (TotalRecords>size)
{
cout <<"存储空间已满!";
TotalRecords--;
}
//写入数据
cout << "请输入姓名:";
cin >> stu[TotalRecords].StuName;
cout <<"请输入成绩:";
cin >>stu[TotalRecords].StuScore;
of1.write((char *)&stu[TotalRecords],sizeof(Student));
cout << "还继续输入吗(y/n)?";
cin >> ch;
if(ch!='y' && ch!='Y')
{
of1.close();
Contents();
mainselect();
}
}
}
void browser()//显示记录
{
system("cls");
ifstream f1;
// countrecord();
f1.open("record.dat");
f1.seekg(0,ios::beg); //指针回到文件按记录首部
if(TotalRecords>0 && TotalRecords<size)
{
for(int i=1;i<=TotalRecords;i++)
{
f1.read((char *)&stu[i],sizeof(Student));
cout << "记录号:" << i <<endl;
stu[i].display();
cout << endl;
}
}
else
{
cout<<"当前记录为空!\n";
}
f1.close();
Pause();
}
void inquire() //查找
{
Student stu[size];
ifstream f1;
f1.open("record.dat");
f1.seekg(0,ios::beg); //指针回到文件按记录首部
if(TotalRecords>0 && TotalRecords<size)
{
for(int i=1;i<=TotalRecords;i++)
{
f1.read((char *)&stu,sizeof(Student));
}
}
system("cls");
char name[10]; //当前记录号
// f1.open("record.dat",ios::in|ios::out);
f1.seekg(0,ios::end);
TotalRecords=f1.tellg()/sizeof(Student);
cout <<TotalRecords<<endl;
cout <<f1.tellg();
cout << "输入要查找的姓名:";
cin >> name;
for(int i=1;i<=TotalRecords;i++)
{
if(strcmp(name,stu[i].StuName)==0)
{
cout <<"yes";
f1.seekg((i-1)*sizeof(Student),ios::beg);
f1.read((char *)&stu,sizeof(Student));
stu[i].display();
}
}
f1.close();
Pause();
}
void Contents() //主目录
{
system("cls");
cout << "\t\t*" << "************************************************" << endl;
cout << "\t\t*" << " 1. 浏览通讯录信息, "<< "*" << endl;
cout << "\t\t*" << " 2. 增加通讯录信息, "<< "*"<< endl;
cout << "\t\t*" << " 3. 按姓名号查询通讯录信息 "<< "*" << endl;
cout << "\t\t*" << " 0. 退出通讯录, "<< "*" <<endl;
cout << "\t\t*" << "************************************************" << endl;
cout << "\t\t请选择(0-3):";
}
void mainselect() //功能选择
{
char x[4];
cin>>x[0];
switch(x[0])
{
case '1': //浏览功能
browser();
break;
case '2': //增加新记录
write();
break;
case '3':
inquire(); //查找记录
break;
case '0': //退出系统
cout <<"确定退出?(y/n)\n";
char ch;
ch=getch();
if(ch=='y'||ch=='Y')
{
cout <<"欢迎使用,再见!\n";
exit(0);
}
else
{
Contents();
mainselect();
}
default:
{
cout<<"请原谅,您的输入有误"<<endl;
mainselect();
}
}
}
};
int main()
{
cout <<"\t\t\t——欢迎使用通讯录系统——\n";
function stu;
stu.countrecord();
stu.Contents();
stu.mainselect();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -