⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 opera.cpp

📁 用C++编写的通讯录,好用,是大一时的课程设计!运行通过,绝对正确
💻 CPP
字号:
#include "opera.hpp"
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;

int i, tall;

int compare(const void *m, const void *n)                                       // 对地址簿排序的函数
 {
  return strcmp((*(class extpersonType *)m).fn, (*(class extpersonType *)n).fn);
 }


void addressBookType::getin()                                                   // 1、数据库读取
 {
  char fname[50];
   
  cout << "请输入数据库文件名:";
  cin >> fname;
  ifstream fin(fname);                                                          // 处理文件找不到的情况 
  if (!fin)
   {
    cout << "对不起,文件未找到!" << endl;
    return;
   }
  fin >> total;
  for (i = 0; i < total; i++)
   {
    fin.get();
    fin.getline(person[i].fn, 30);
    fin.getline(person[i].ln, 30);
    fin.getline(person[i].sex, 10);
    fin.getline(person[i].birthday, 20);
    fin.getline(person[i].street, 50);
    fin.getline(person[i].city, 30);
    fin.getline(person[i].provice, 30);
    fin.getline(person[i].postcode, 30);
    fin >> person[i].att;
    fin >> person[i].tel;
   }
  cout << "读取完毕,共读取通讯录" << total << "条..." << endl;
 }


void addressBookType::namesort()                                                // 2、排序
 {
  qsort(person, total, sizeof(person[0]), compare);
  cout << "排序完成..." << endl;
 }


void addressBookType::searchfn()                                                // 3、按姓氏查询
 {
  char sfn[30];
 
  cout << "请输入需要查询的姓氏:";
  cin >> sfn;
  cout << "查询到的人员名单:" << endl;
  tall = 0;
  for (i = 0; i < total; i++)
    if (!strcmp(sfn, person[i].fn))
     {
      tall++;
      cout << person[i].fn << " " << person[i].ln << endl;
     }
  cout << "总计找到符合条件人员" << tall << "名" << endl;
  system("pause");
 }


void addressBookType::prn()                                                     // 4、打印指定人员详细资料 
 {
  char sfn[30], sln[30];

  cin.get();
  cout << "请输入需要打印人员的姓氏:";
  cin.getline(sfn, 30);
  cout << "请输入需要打印人员的名字:";
  cin.getline(sln, 30);
  for (i = 0; i < total; i++)
    if (!strcmp(sfn, person[i].fn) && !strcmp(sln, person[i].ln))
      person[i].out();
 }


void addressBookType::searchbir()                                               // 5、打印生日在指定日期间的人员名单
 {
  char fbir[20], sbir[20], ts[20];
 
  cout << "请输入两个生日,用空格隔开(例如 19880601):";
  cin >> fbir >> sbir;
  if (strcmp(fbir, sbir) > 0)                                                   // 处理生日1比生日2大的判断
   {
    strcmp(ts, fbir);
    strcmp(fbir, sbir);
    strcmp(sbir, ts);
   }
  cout << "查询到的人员名单:" << endl;
  tall = 0;
  for (i = 0; i < total; i++)
    if (strcmp(person[i].birthday, fbir) >= 0 && strcmp(person[i].birthday, sbir) <= 0)
     {
      tall++;
      cout << person[i].fn << " " << person[i].ln <<endl;
     }
  cout << "总计找到符合条件人员" << tall << "名" << endl;
  system("pause");
 }


void addressBookType::serach_bfn()
 {
  char fn1[30], fn2[30], ts[30];

  cin.get();
  cout << "请输入需要查询姓氏1:";
  cin.getline(fn1, 30);
  cout << "请输入需要查询姓氏2:";
  cin.getline(fn2, 30);
  cout << fn1 << " " << fn2 << endl;
  if (strcmp(fn1, fn2) > 0)                                                     // 处理姓氏1比姓氏2大的判断 
   {
    strcmp(ts, fn1);
    strcmp(fn1, fn2);
    strcmp(fn2, ts);
   }
  cout << "查询到的人员名单:" << endl;
  tall = 0;
  for (i = 0; i < total; i++)
    if (strcmp(person[i].fn, fn1) >= 0 && strcmp(person[i].fn, fn2) <= 0)
     {
      tall++;
      cout << person[i].fn << " " << person[i].ln <<endl;
     }
  cout << "总计找到符合条件人员" << tall << "名" << endl;
  system("pause");
 }


void addressBookType::print_att()                                               // 7、打印属性 
 {
  int i, need_att, tall = 0;

  for (i = 0; i <= 2; i++)
    cout << i << "、" << id[i] << endl;
  cout << "请选择你需要查询的成员身份:";
  cin >> need_att;
  cout << "查询到的人员名单:" << endl;
  tall = 0; 
  for (i = 0; i < total; i++)
    if (person[i].att == need_att)
     {
      tall++;
      cout << person[i].fn << " " << person[i].ln <<endl;
     }
  cout << "总计找到符合条件人员" << tall << "名" << endl;
  system("pause");
}

void addressBookType::print_all()                                               // 8、输出所有资料,用以检验程序 
 {
  for (i = 0; i < total; i++)
    person[i].out();
  system("pause"); 
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -