C++版本一
/*
*@Author: STZG
*@Language: C++
*/
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<fstream>
using namespace std;
struct list {
int id; /* 标识这个元素方便查找 */
char name[100];
char tele[20];
char sex[10];
int data; /* 链表中包含的元素 */
int num;
struct list *next; /* 指向下一个链表的指针 */
};
int all = 0;
struct list *list_head = NULL;
struct list *lists = NULL;
struct list temp_list;
int list_id = 0;
void list_add(struct list **head, struct list *list){
struct list *temp;
if(NULL == *head){
*head = list;
(*head)->next = NULL;
}else{
temp = *head;
while(temp)
{
if(NULL == temp->next)
{
temp->next = list;
list->next = NULL;
}
temp = temp->next;
}
}
}
void out(struct list* obj){
cout<<"姓名:"<<obj->name<<"\t"<<"性别:"<<obj->sex<<"\t"<<"联系方式:"<<obj->tele<<endl;
}
void list_print(struct list **head){
struct list *temp;
temp = *head;
if(temp==NULL){
cout<<"通讯录为空!"<<endl;
return;
}
cout<<"通讯录全部记录如下:"<<endl;
while(temp){
out(temp);
temp = temp->next;
}
}
void list_fileprint(struct list **head,ofstream &outfile){
struct list *temp;
temp = *head;
if(temp==NULL){
outfile<<"通讯录为空!"<<endl;
return;
}
while(temp){
outfile<<"姓名:"<<temp->name<<"\t"<<"性别:"<<temp->sex<<"\t"<<"联系方式:"<<temp->tele<<endl;
temp = temp->next;
}
}
int list_query(struct list **head,int id_que){
struct list *temp;
temp = *head;
if(NULL == temp){
cout<<"通讯录为空!"<<endl;
return -1;
}else{
if(id_que== temp->id){
out(temp);
return temp->id;
}
while(temp->next){
temp = temp->next;
if(id_que == temp->id){
out(temp);
return temp->id;
}
}
return -1;
}
return -1;
}
int list_namequery(struct list **head,char *name_que){
struct list *temp;
temp = *head;
if(NULL == temp){
cout<<"通讯录为空!"<<endl;
return -1;
}else{
if(strcmp(name_que,temp->name)==0 ){
out(temp);
return temp->id;
}
while(temp->next){
temp = temp->next;
if(strcmp(name_que,temp->name)==0){
out(temp);
return temp->id;
}
}
return -1;
}
return -1;
}
int list_del(struct list **head, int id_del){
struct list *temp, *p;
temp = *head;
if(NULL == temp){
cout<<"通讯录为空!"<<endl;
return -1;
}else{
if(id_del== temp->id){
*head = temp->next;
return 0;
}
while(temp->next){
p = temp;
temp = temp->next;
if(id_del == temp->id){
p->next = temp->next;
return 0;
}
}
return 0;
}
return -1;
}
void err(){
cout<<"----无效操作----"<<endl;
}
void memu(){
cout<<"---请选择操作---"<<endl;
cout<<"1、添加记录"<<endl;
cout<<"2、查询记录"<<endl;
cout<<"3、查看全部"<<endl;
cout<<"4、删除记录"<<endl;
cout<<"5、修改记录"<<endl;
cout<<"6、离线存储"<<endl;
cout<<"7、退出系统"<<endl;
cout<<"----------------"<<endl;
}
void add(){
all++;
cout<<"正在新建ID:"<<++list_id<<"的记录"<<endl;
cout<<"请输入姓名:"<<endl;
cin>>lists[all].name;
cout<<"请输入性别:"<<endl;
cin>>lists[all].sex;
cout<<"请输***系方式:"<<endl;
cin>>lists[all].tele;
lists[all].id = list_id;
cout<<"-----存储中-----"<<endl;
list_add(&list_head, &lists[all]);
cout<<"----存储完成----"<<endl;
}
int allquery(){
int op;
cout<<"1、ID"<<endl;
cout<<"2、姓名"<<endl;
cin>>op;
int result;
switch(op){
case 1:
int id;
cout<<"----请输入ID----"<<endl;
cin>>id;
result=list_query(&list_head,id);
break;
case 2:
char name[100];
cout<<"---请输入姓名---"<<endl;
cin>>name;
result=list_namequery(&list_head,name);
break;
default:err();break;
}
return result;
}
void query(){
cout<<"-请选择查询方式-"<<endl;
if(allquery()==-1){
cout<<"----查无此人----"<<endl;
}
}
void del(){
cout<<"-请选择删除方式-"<<endl;
int result=allquery();
if(result==-1){
cout<<"----查无此人----"<<endl;
}else{
cout<<"----正在删除----"<<endl;
list_del(&list_head,result);
cout<<"----删除完成----"<<endl;
}
}
void print(){
list_print(&list_head);
cout<<"----查询完成----"<<endl;
}
void updata(){
cout<<"-请选择更新方式-"<<endl;
int result=allquery();
if(result==-1){
cout<<"----查无此人----"<<endl;
}else{
cout<<"--请输入新信息--"<<endl;
cout<<"请输入姓名:"<<endl;
cin>>lists[result].name;
cout<<"请输入性别:"<<endl;
cin>>lists[result].sex;
cout<<"请输***系方式:"<<endl;
cin>>lists[result].tele;
cout<<"----更新完成----"<<endl;
}
}
void save(){
cout<<"----建立文件----"<<endl;
ofstream outfile("list.dat");
cout<<"----写入文件----"<<endl;
list_fileprint(&list_head,outfile);
cout<<"----关闭文件----"<<endl;
outfile.close();
cout<<"----导出完成----"<<endl;
}
void exit(){
cout<<"----退出系统----"<<endl;
exit(0);
}
bool applymemory(){
lists = (struct list*)malloc(sizeof(struct list) * 20);
if(NULL == lists){
printf("malloc error!\n");
return 0;
}
return 1;
}
void init(){
cout<<"---正在初始化---"<<endl;
cout<<"---建立通讯录---"<<endl;
cout<<"----申请存储----"<<endl;
if(applymemory()){
cout<<"--申请存储成功--"<<endl;
}else{
exit();
}
cout<<"---初始化完成---"<<endl;
cout<<"------欢迎------"<<endl;
}
int main()
{
init();
while(1){
memu();
int op;
cin>>op;
switch(op){
case 1:add();break;
case 2:query();break;
case 3:print();break;
case 4:del();break;
case 5:updata();break;
case 6:save();break;
case 7:exit();break;
default:err();break;
}
}
//cout << "Hello world!" << endl;
return 0;
}
C++版本二
// ConsoleApplication.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
//#include "pch.h"
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
typedef struct Person { //定义联系人信息结构体
string name;
string address;
string telephone;
}DataType;
typedef struct Node { //定义链表节点结构体
DataType data;
struct Node *next;
}Node;
Node *getptr(Node *head, int position) //获取链表指定位置元素
{
Node *p = head;
if (p == NULL || position == 0) //p==NULL表示链表为空
{
return head;
}
for (int i = 0; p != NULL && i < position; i++)
{
p = p->next;
}
return p;
}
int getSize(Node *head) //获取单链表中的节点个数
{
int size = 0;
Node *p = head;
while (p != NULL)
{
size++;
p = p->next;
}
return size;
}
bool insert(Node **head, int position, DataType d) //在单链表指定位置插入元素
{
//注意上面的形参一定要使用指针的指针
//因为head本身存放的就是地址,只有这样才能对存放head的实际地址进行操作
if (position<0 || position>getSize(*head))
{
return false;
}
Node *node = (Node *)malloc(sizeof(Node));
/*node->data = d;*/
memcpy(&node->data, &d, sizeof(node->data));
node->next = NULL;
if (position == 0)
{
node->next = *head;
*head = node;
return true;
}
Node *p = getptr(*head, position - 1);
Node *r = p->next;
node->next = r;
p->next = node;
return true;
}
bool erase(Node **head, int position) //在单链表指定位置删除元素
{
//注意上面的形参一定要使用指针的指针
//因为head本身存放的就是地址,只有这样才能对存放head的实际地址进行操作
if (position < 0 || position >= getSize(*head))
{
return false;
}
Node *p = *head;
if (position == 0)
{
*head = (*head)->next;
free(p);
p = NULL;
return true;
}
p = getptr(*head, position - 1);
Node *q = p->next;
p->next = q->next;
free(p);
q = NULL;
return true;
}
void reverse(Node **head) //单链表的反转函数
{
Node *p = *head;
Node *q = p->next;
if (q == NULL)
{
return;
}
Node *r = q->next;
if (p == *head)
{
p->next = NULL;
}
while (true)
{
q->next = p;
if (r == NULL)
{
*head = q;
break;
}
else
{
p = q;
q = r;
r = r->next;
}
}
}
void trave(Node *head, void(*fun)(DataType)) //单链表的遍历函数
{
Node *p = head;
while (p != NULL)
{
fun(p->data);
p = p->next;
}
}
void print(DataType d) //用于打印的函数,供其他函数调用
{
cout << "姓名:" << d.name << endl;
cout << "地址:" << d.address << endl;
cout << "电话:" << d.telephone << endl;
}
void write(DataType *d)
{
cout << "姓名:";
getline(cin, d->name);
cout << "地址:";
getline(cin, d->address);
cout << "电话:";
getline(cin, d->telephone);
cout << d->name << endl;
cout << d->address << endl;
cout << d->telephone << endl;
/*cout << "姓名:";
cin >> d->name;
cout << "地址:";
cin >> d->address;
cout << "电话:";
cin >> d->telephone;*/
}
int main()
{
cout << "欢迎使用浙江理工大学通讯录管理系统" << endl;
int index;
Node *head = NULL;
DataType temp;
do {
cout << "请选择您需要进行的操作:" << endl;
cout << "1.新建联系人" << endl;
cout << "2.删除联系人" << endl;
cout << "3.修改联系人" << endl;
cout << "4.查找联系人" << endl;
cout << "5.按编辑时间正序查看通讯录" << endl;
cout << "6.按编辑时间倒序查看通讯录" << endl;
cout << "7.根据姓名查找联系人" << endl;
cout << "8.根据电话查找联系人" << endl;
cout << "9.退出通讯录" << endl;
cout << "当前节点个数:" << getSize(head) << endl;
cin >> index;
getchar();
switch (index)
{
case 1:
write(&temp);
insert(&head, getSize(head), temp);
cout << "联系人创建成功!" << endl;
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
trave(head, print);
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
default:
break;
}
cout << endl;
} while (index != 9);
cout << "谢谢使用!" << endl;
return 0;
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门提示:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
C++版本三
类与对象版本
main.cpp
/*
*@Author: STZG
*@Language: C++
*/
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<fstream>
#include"DemoSystem.h"
using namespace std;
int main()
{
DemoSystem Demo;
Demo.init();
Demo.run();
//cout << "Hello world!" << endl;
return 0;
}
DemoSystem.cpp
#include "DemoSystem.h"
DemoSystem::DemoSystem()
{
op=0;
all = 0;
list_head = NULL;
lists = NULL;
list_id = 0;
//ctor
}
DemoSystem::~DemoSystem()
{
//dtor
}
void DemoSystem::list_add(struct list **head, struct list *list){
struct list *temp;
if(NULL == *head){
*head = list;
(*head)->next = NULL;
}else{
temp = *head;
while(temp)
{
if(NULL == temp->next)
{
temp->next = list;
list->next = NULL;
}
temp = temp->next;
}
}
}
void DemoSystem::out(struct list* obj){
cout<<"姓名:"<<obj->name<<"\t"<<"性别:"<<obj->sex<<"\t"<<"联系方式:"<<obj->tele<<endl;
}
void DemoSystem::list_print(struct list **head){
struct list *temp;
temp = *head;
if(temp==NULL){
cout<<"通讯录为空!"<<endl;
return;
}
cout<<"通讯录全部记录如下:"<<endl;
while(temp){
out(temp);
temp = temp->next;
}
}
void DemoSystem::list_fileprint(struct list **head,ofstream &outfile){
struct list *temp;
temp = *head;
if(temp==NULL){
outfile<<"通讯录为空!"<<endl;
return;
}
while(temp){
outfile<<"姓名:"<<temp->name<<"\t"<<"性别:"<<temp->sex<<"\t"<<"联系方式:"<<temp->tele<<endl;
temp = temp->next;
}
}
int DemoSystem::list_query(struct list **head,int id_que){
struct list *temp;
temp = *head;
if(NULL == temp){
cout<<"通讯录为空!"<<endl;
return -1;
}else{
if(id_que== temp->id){
out(temp);
return temp->id;
}
while(temp->next){
temp = temp->next;
if(id_que == temp->id){
out(temp);
return temp->id;
}
}
return -1;
}
return -1;
}
int DemoSystem::list_namequery(struct list **head,char *name_que){
struct list *temp;
temp = *head;
if(NULL == temp){
cout<<"通讯录为空!"<<endl;
return -1;
}else{
if(strcmp(name_que,temp->name)==0 ){
out(temp);
return temp->id;
}
while(temp->next){
temp = temp->next;
if(strcmp(name_que,temp->name)==0){
out(temp);
return temp->id;
}
}
return -1;
}
return -1;
}
int DemoSystem::list_del(struct list **head, int id_del){
struct list *temp, *p;
temp = *head;
if(NULL == temp){
cout<<"通讯录为空!"<<endl;
return -1;
}else{
if(id_del== temp->id){
*head = temp->next;
return 0;
}
while(temp->next){
p = temp;
temp = temp->next;
if(id_del == temp->id){
p->next = temp->next;
return 0;
}
}
return 0;
}
return -1;
}
void DemoSystem::err(){
cout<<"----无效操作----"<<endl;
}
void DemoSystem::memu(){
cout<<"---请选择操作---"<<endl;
cout<<"1、添加记录"<<endl;
cout<<"2、查询记录"<<endl;
cout<<"3、查看全部"<<endl;
cout<<"4、删除记录"<<endl;
cout<<"5、修改记录"<<endl;
cout<<"6、离线存储"<<endl;
cout<<"7、退出系统"<<endl;
cout<<"----------------"<<endl;
}
void DemoSystem::add(){
all++;
cout<<"正在新建ID:"<<++list_id<<"的记录"<<endl;
cout<<"请输入姓名:"<<endl;
cin>>lists[all].name;
cout<<"请输入性别:"<<endl;
cin>>lists[all].sex;
cout<<"请输***系方式:"<<endl;
cin>>lists[all].tele;
lists[all].id = list_id;
cout<<"-----存储中-----"<<endl;
list_add(&list_head, &lists[all]);
cout<<"----存储完成----"<<endl;
}
int DemoSystem::allquery(){
int op;
cout<<"1、ID"<<endl;
cout<<"2、姓名"<<endl;
cin>>op;
int result;
switch(op){
case 1:
int id;
cout<<"----请输入ID----"<<endl;
cin>>id;
result=list_query(&list_head,id);
break;
case 2:
char name[100];
cout<<"---请输入姓名---"<<endl;
cin>>name;
result=list_namequery(&list_head,name);
break;
default:err();break;
}
return result;
}
void DemoSystem::query(){
cout<<"-请选择查询方式-"<<endl;
if(allquery()==-1){
cout<<"----查无此人----"<<endl;
}
}
void DemoSystem::del(){
cout<<"-请选择删除方式-"<<endl;
int result=allquery();
if(result==-1){
cout<<"----查无此人----"<<endl;
}else{
cout<<"----正在删除----"<<endl;
list_del(&list_head,result);
cout<<"----删除完成----"<<endl;
}
}
void DemoSystem::print(){
list_print(&list_head);
cout<<"----查询完成----"<<endl;
}
void DemoSystem::updata(){
cout<<"-请选择更新方式-"<<endl;
int result=allquery();
if(result==-1){
cout<<"----查无此人----"<<endl;
}else{
cout<<"--请输入新信息--"<<endl;
cout<<"请输入姓名:"<<endl;
cin>>lists[result].name;
cout<<"请输入性别:"<<endl;
cin>>lists[result].sex;
cout<<"请输***系方式:"<<endl;
cin>>lists[result].tele;
cout<<"----更新完成----"<<endl;
}
}
void DemoSystem::save(){
cout<<"----建立文件----"<<endl;
ofstream outfile("list.dat");
cout<<"----写入文件----"<<endl;
list_fileprint(&list_head,outfile);
cout<<"----关闭文件----"<<endl;
outfile.close();
cout<<"----导出完成----"<<endl;
}
void DemoSystem::exit(){
cout<<"----退出系统----"<<endl;
std::exit(0);
}
bool DemoSystem::applymemory(){
lists = (struct list*)malloc(sizeof(struct list) * 20);
if(NULL == lists){
printf("malloc error!\n");
return 0;
}
return 1;
}
void DemoSystem::init(){
cout<<"---正在初始化---"<<endl;
cout<<"---建立通讯录---"<<endl;
cout<<"----申请存储----"<<endl;
if(applymemory()){
cout<<"--申请存储成功--"<<endl;
}else{
exit();
}
cout<<"---初始化完成---"<<endl;
cout<<"------欢迎------"<<endl;
}
void DemoSystem::setop(int x){
op=x;
}
int DemoSystem::getop(){
return op;
}
void DemoSystem::run(){
while(1){
memu();
cin>>op;
switch(op){
case 1:add();break;
case 2:query();break;
case 3:print();break;
case 4:del();break;
case 5:updata();break;
case 6:save();break;
case 7:exit();break;
default:err();break;
}
}
}
DemoSystem.h
#ifndef DEMOSYSTEM_H
#define DEMOSYSTEM_H
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<fstream>
using namespace std;
struct list {
int id; /* 标识这个元素方便查找 */
char name[100];
char tele[20];
char sex[10];
int data; /* 链表中包含的元素 */
int num;
struct list *next; /* 指向下一个链表的指针 */
};
class DemoSystem
{
public:
DemoSystem();
virtual ~DemoSystem();
void list_add(struct list **head, struct list *list);
void out(struct list* obj);
void list_print(struct list **head);
void list_fileprint(struct list **head,ofstream &outfile);
int list_query(struct list **head,int id_que);
int list_namequery(struct list **head,char *name_que);
int list_del(struct list **head, int id_del);
void err();
void memu();
void add();
int allquery();
void query();
void del();
void print();
void updata();
void save();
void exit();
bool applymemory();
void init();
int getop();
void setop(int x);
void run();
protected:
private:
int op;
int all;
struct list *list_head;
struct list *lists;
struct list temp_list;
int list_id;
};
#endif // DEMOSYSTEM_H