设计一个电话簿管理程序

.1数据结构设计:采用数组(维数)、指针、结构体等哪一种进行数据表达形式,简述原因.

主要采用结构体和函数来构成程序。此程序是一个电话簿管理系统,存入的信息互相关联但类型不同,适合使用结构体来建立一个新的数据结构,单独存放不同的联系人信息。函数被用来执行系统中的十个功能,共同服务于主函数,简洁直观。其中还用了一维数组表示联系人信息,起到简化程序的作用,最后编写了文件便于保存此电话簿管理系统,可长期使用

.2算法设计:如查找采用折半法、排序采用冒泡法等给出算法的主要思想。

按姓名/电话号码排序时采用冒泡排序,在姓名排序:每比较一次将的信息调到后面让的在前在电话号码排序:每比较一次将的信息调到后面让的在前。

采用了模块化的设计方法,根据程序模块的功能,将它划分成若干子模块,通过函数来实现程序中的子模块,使用一个模块完成一个功能,注意了模块的独立性。将一个大任务分干成若干小任务,这样每一个子任务就相对简单。

主函数中采用switch选择结构,简洁明了,实现多个功能的使用。

运行结果截图:

1.菜单:

2.添加联系人:

3.修改联系人:

4.显示全部联系人:

5.按名字查询:

6.按号码查询:

7.按姓名显示:

8.删除联系人:

9.保存文件:

10.退出系统:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
struct person
{
	char work[50];		//工作单位 
	char num[20];		//手机号
	char name[20];		//姓名
	char sex[10];		//性别 
	char QQ[20];		//QQ号
	char email[20];		//邮箱
	char address[50];	//地址
};

int menu()
{
	int choice;
	system("cls");//清屏 
 	printf("\t\t\t\t----------------------------------------------------------\n");
    printf("\t\t\t\t|                 欢迎使用电话簿管理系统                 |\n");
    printf("\t\t\t\t----------------------------------------------------------\n");
    printf("\t\t\t\t|                   1-新增联系人                         |\n");
    printf("\t\t\t\t----------------------------------------------------------\n");
    printf("\t\t\t\t|                   2-删除联系人                         |\n");
    printf("\t\t\t\t----------------------------------------------------------\n");
    printf("\t\t\t\t|                   3-修改联系人                         |\n");
    printf("\t\t\t\t----------------------------------------------------------\n");
    printf("\t\t\t\t|                   4-按名字查询联系人                   |\n");
    printf("\t\t\t\t----------------------------------------------------------\n");
    printf("\t\t\t\t|                   5-按号码查询联系人                   |\n");
    printf("\t\t\t\t----------------------------------------------------------\n");
    printf("\t\t\t\t|                   6-按姓名显示                         |\n");
    printf("\t\t\t\t----------------------------------------------------------\n");
    printf("\t\t\t\t|                   7-按号码显示                         |\n");
    printf("\t\t\t\t----------------------------------------------------------\n");
    printf("\t\t\t\t|                   8-显示全部联系人                     |\n");
     printf("\t\t\t\t---------------------------------------------------------\n");
    printf("\t\t\t\t|                   9-保存文件                           |\n");
    printf("\t\t\t\t----------------------------------------------------------\n");
    printf("\t\t\t\t|                   0-退出程序                           |\n");
    printf("\t\t\t\t----------------------------------------------------------\n");
	printf("\t\t\t\t请选择功能:");
	scanf("%d",&choice);
	while(choice>9||choice<0)//判断是否输入正确
	{
		printf("请重新选择:");
		scanf("%d",&choice);
	}
	return choice;
}

int add_person(struct person arr[],int n)		//添加联系人函数 
{
	system("cls");				//清屏 
	printf("请输入联系人姓名:");
	scanf("%s",arr[n].name);
	printf("请输入联系人工作单位:");
	scanf("%s",arr[n].work);
	printf("请输入联系人手机号:");
	scanf("%s",arr[n].num);
	printf("请输入联系人性别:");
	scanf("%s",arr[n].sex); 
	printf("请输入联系人QQ号:");
	scanf("%s",arr[n].QQ);
	printf("请输入联系人邮箱:");
	scanf("%s",arr[n].email);
	printf("请输入联系人地址:");
	scanf("%s",arr[n].address);
	printf("添加成功!\n");
	system("pause");			//暂停界面 
	return n+1;
}

void print_person(struct person arr[],int n)//输出函数 
{
	int i;
	printf("%-15s%-15s%-15s%-15s%-15s%-15s%-15s\n","姓名","工作单位","手机号","性别","QQ","邮箱","地址");
	for(i=0;i<n;i++)
	{
		printf("%-15s",arr[i].name);
		printf("%-15s",arr[i].work);
		printf("%-15s",arr[i].num);
		printf("%-15s",arr[i].sex);
		printf("%-15s",arr[i].QQ);
		printf("%-15s",arr[i].email);
		printf("%-15s",arr[i].address);
		printf("\n");
	}
}

int delete_person(struct person arr[],int n)//删除联系人函数 
{
	int i,j;
	char temp[20];
	system("cls");
	printf("请输入要删除的联系人:");
	scanf("%s",temp);
	for(i=0;i<n;i++)
	{
		if(!strcmp(temp,arr[i].name))
		break;
	} 
	if(i==n)
	{
		printf("没有该联系人!");
		system("pause");//暂停 
		return n;
	}
	print_person(&arr[i],1);
	printf("是否删除'Y'or'N':");
	scanf("%s",&temp);
	if(strcmp(temp,"y")==0||strcmp(temp,"Y")==0)
	{
		for(j=i;j<n;j++)
		{
			arr[j]=arr[j+1];
		}
		printf("删除成功!\n");
	}
	system("pause");
	return n-1;
	printf("取消删除\n");
	system("pause");
	return n;
}
void modify_person(struct person arr[],int n)//修改联系人函数 
{
	int i;
	char temp[20];
	struct person arr0;			//临时储存 
	system("cls");
	printf("请输入要修改的联系人:");
	scanf("%s",temp);
	for(i=0;i<n;i++)
	{
		if(strcmp(temp,arr[i].name)==0)
		break; 
	}
	if(i==n)
	{
		printf("无该联系人!");
		system("pause");
		return ; 
	}
	print_person(&arr[i],1);
	printf("请输入联系人工作地址:");
	scanf("%s",arr0.work);
	printf("请输入联系人手机号:");
	scanf("%s",arr0.num);
	printf("请输入联系人性别:");
	scanf("%s",arr0.sex); 
	printf("请输入联系人QQ号:");
	scanf("%s",arr0.QQ);
	printf("请输入联系人邮箱:");
	scanf("%s",arr0.email);
	printf("请输入联系人地址:");
	scanf("%s",arr0.address);
	printf("是否确认修改Y/N?:  ");
	scanf("%s",temp);
	if(strcmp(temp,"y")==0||strcmp(temp,"Y")==0)
	{
		strcpy(arr0.name,arr[i].name);
		arr[i]=arr0;
		printf("修改成功!\n");
		system("pause");
		return ;
	}
	else
	{
		printf("已取消修改!");
		system("pause");
		return ; 
	} 
}
void search_person_name(struct person arr[],int n)//查询联系人函数 
{
	 int i;
	 char temp[20];
	 system ("cls");
	 printf("请输入要查询的联系人:");
	 scanf("%s",temp);
	 for(i=0;i<n;i++)
	 {
	 	if(strcmp(arr[i].name,temp)==0)
	 	{
			break;
		}
	 }
	 if(i==n)
	 {	
	 	printf("未查找到该联系人!\n");
	 	system("pause");
		return ; 
	 }
	 print_person(&arr[i],1);
	 system("pause");
	 return;
}
void search_person_num(struct person arr[],int n)
{
	int i;
	char temp[20];
	system("cls");
	printf("请输入要查询的电话号码:");
	scanf("%s",temp);
	for(i=0;i<n;i++)
	{
		if(strcmp(arr[i].num,temp)==0)
		break;
	}
	if(i==n)
	{
		printf("未查找到该联系人!\n");
		system("pause");
	 	return;
	}
	print_person(&arr[i],1);
	system("pause");
}

void sort_person_name(struct person arr[],int n)//按姓名排序函数 
{
	int i,j;
	struct person temp;
	system("cls");
	for(i=0;i<n-1;i++)
	{
		for(j=0;j<n-i-1;j++)
		{
			if(strcmp(arr[j+1].name,arr[j].name)<0)
			{
				temp=arr[j];
				arr[j]=arr[j+1];
				arr[j+1]=temp; 
			}
		}
	}
	
	return;
}

void sort_person_num(struct person arr[],int n)//按电话号码排序函数 
{
	int i,j;
	struct person temp;
	system("cls");
	for(i=0;i<n-1;i++)
	{
		for(j=0;j<n-i-1;j++)
		{
			if(strcmp(arr[j+1].num,arr[j].num)<0);
			{
				temp=arr[j];
				arr[j]=arr[j+1];
				arr[j+1]=temp;
			}
		}
	}
	return;
}

void save_file(struct person arr[],int n)//写入文件函数 
{
	int i;
	FILE *fp;
	fp=fopen("电话簿管理系统.dat","w+");
	if(fp==NULL)
	{
		return;
	} 
	for(i=0;i<n;i++)//依次写入到文件
	{
		fwrite(&arr[i],sizeof(struct person),1,fp);
	}
	fclose(fp);
}

int read_file(struct person arr[])//读文件函数 
{
	int i=0;
	FILE* fp;
	fp=fopen("电话簿管理系统.dat","r+");
	if(fp==NULL)
	{
		return 0;
	}
	while(fread(&arr[i++],sizeof(struct person),1,fp));
	fclose(fp);
	if(i>=1)
	{
		return i-1;
	}
	return 0;
}

int main()//主函数 
{
	system("color 9F");//改变颜色函数,9代表浅蓝色,F代表白色 
	int choice;
	int num=0;						//记录结构体数组长度 
	struct person per[100];			//定义结构体数组 
	num=read_file(per);
	do
	{
		choice=menu();
		switch(choice)
		{
		case 1:
			num=add_person(per,num);		//添加 num=n
			break;
		case 2:
			delete_person(per,num);			//删除
			break;
		case 3:
			modify_person(per,num);			//修改
			break;
		case 4:
			search_person_name(per,num);	//按名字查找
			break;
		case 5:
			search_person_num(per,num);		//按号码查找
			break;
		case 6:
			sort_person_name(per,num);		//对名字排序
			print_person(per,num);
			system("pause");
			break;
		case 7:
			sort_person_num(per,num);		//对号码排序
			print_person(per,num);
			system("pause");
			break;
		case 8:
			system("cls");
			print_person(per,num);
			system("pause");
			break;
		case 9:
			system("cls");
			save_file(per,num);
			printf("保存成功!");
			system("pause");
			break; 
		case 0:
			printf("\t谢谢使用!\n");
            exit(0);
		}
	}
		while(choice!=0);
	return 0;
}



















#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;

class Book { //定义书籍类
private:
    string name; //书名
    string author; //作者
    double price; //价格
    int stock; //库存
public:
    Book(string n, string a, double p, int s) : name(n), author(a), price(p), stock(s) {}

    string getName() const { return name; }
    string getAuthor() const { return author; }
    double getPrice() const { return price; }
    int getStock() const { return stock; }
    void buyBook() { stock--; } //购买书籍时库存-1
};

class Buyer { //定义用户类
private:
    string name; //用户名
    int level; //用户等级:1-普通会员;2-黄金会员;3-钻石会员
    double discount; //用户购书折扣
public:
    Buyer(string n, int l, double d) : name(n), level(l), discount(d) {}

    string getName() const { return name; }
    int getLevel() const { return level; }
    double getDiscount() const { return discount; }
};

class Order { //定义订单类
private:
    string buyerName; //购买者用户名
    string bookName; //购买书籍名
    double originalPrice; //购买书籍原价
    double actualPrice; //购买书籍实际价格
public:
    Order(string b, string n, double op, double ap) : buyerName(b), bookName(n), originalPrice(op), actualPrice(ap) {}

    string getBuyerName() const { return buyerName; }
    string getBookName() const { return bookName; }
    double getOriginalPrice() const { return originalPrice; }
    double getActualPrice() const { return actualPrice; }
};

vector<Book> books; //存储书籍信息
vector<Buyer> buyers; //存储用户信息
vector<Order> orders; //存储订单信息

void initBooks() { //从文件中读取书籍信息
    ifstream ifs("books.txt");
    string name, author;
    double price;
    int stock;
    while (ifs >> name >> author >> price >> stock) {
        books.emplace_back(name, author, price, stock);
    }
    ifs.close();
}

void initBuyers() { //从文件中读取用户信息
    ifstream ifs("buyers.txt");
    string name;
    int level;
    double discount;
    while (ifs >> name >> level >> discount) {
        buyers.emplace_back(name, level, discount);
    }
    ifs.close();
}

void initOrders() { //从文件中读取订单信息
    ifstream ifs("orders.txt");
    string buyerName, bookName;
    double originalPrice, actualPrice;
    while (ifs >> buyerName >> bookName >> originalPrice >> actualPrice) {
        orders.emplace_back(buyerName, bookName, originalPrice, actualPrice);
    }
    ifs.close();
}

void writeOrdersToFile() { //将订单信息写入文件
    ofstream ofs("orders.txt");
    for (auto& order : orders) {
        ofs << order.getBuyerName() << " " << order.getBookName() << " " << order.getOriginalPrice() << " " << order.getActualPrice() << endl;
    }
    ofs.close();
}

int findBookByName(const string& name) { //根据书籍名查找书籍在books中的下标
    for (int i = 0; i < books.size(); i++) {
        if (books[i].getName() == name) {
            return i;
        }
    }
    return -1; //表示未找到
}

int findBuyerByName(const string& name) { //根据用户名查找用户在buyers中的下标
    for (int i = 0; i < buyers.size(); i++) {
        if (buyers[i].getName() == name) {
            return i;
        }
    }
    return -1; //表示未找到
}

void printBooks() { //打印所有书籍信息
    cout << "书名\t作者\t价格\t库存" << endl;
    for (auto& book : books) {
        cout << book.getName() << "\t" << book.getAuthor() << "\t" << book.getPrice() << "\t" << book.getStock() << endl;
    }
}

void printBuyers() { //打印所有用户信息
    cout << "用户名\t用户等级\t购书折扣" << endl;
    for (auto& buyer : buyers) {
        cout << buyer.getName() << "\t" << buyer.getLevel() << "\t" << buyer.getDiscount() << endl;
    }
}

void printOrders() { //打印所有订单信息
    cout << "购买者\t购买书籍\t原价\t实际价格" << endl;
    for (auto& order : orders) {
        cout << order.getBuyerName() << "\t" << order.getBookName() << "\t" << order.getOriginalPrice() << "\t" << order.getActualPrice() << endl;
    }
}

void login() { //登录用户信息
    string name;
    cout << "请输入用户名:";
    cin >> name;
    int index = findBuyerByName(name);
    if (index == -1) {
        cout << "该用户不存在!" << endl;
    }
    else {
        cout << "登录成功!" << endl;
    }
}

void buyBook() { //购买书籍
    string name;
    cout << "请输入要购买的书籍名:";
    cin >> name;
    int index = findBookByName(name);
    if (index == -1) {
        cout << "该书籍不存在!" << endl;
    }
    else if (books[index].getStock() == 0) {
        cout << "该书籍无库存!" << endl;
    }
    else {
        double price = books[index].getPrice();
        cout << "书籍价格为:" << price << "元" << endl;
        string buyerName;
        cout << "请输入购买者用户名:";
        cin >> buyerName;
        int buyerIndex = findBuyerByName(buyerName);
        if (buyerIndex == -1) {
            cout << "该用户不存在!" << endl;
        }
        else {
            double discount = buyers[buyerIndex].getDiscount();
            double actualPrice = 0;
            if (buyers[buyerIndex].getLevel() == 1) { //普通会员
                actualPrice = price;
            }
            else if (buyers[buyerIndex].getLevel() == 2) { //黄金会员
                if (discount == 0.7) {
                    actualPrice = price * 0.7;
                }
                else if (discount == 0.8) {
                    actualPrice = price * 0.8;
                }
                else if (discount == 0.85) {
                    actualPrice = price * 0.85;
                }
                else if (discount == 0.9) {
                    actualPrice = price * 0.9;
                }
                else if (discount == 0.95) {
                    actualPrice = price * 0.95;
                }
            }
            else if (buyers[buyerIndex].getLevel() == 3) { //钻石会员
                actualPrice = price * (1 - discount);
            }
            cout << "实际价格为:" << actualPrice << "元" << endl;
            books[index].buyBook(); //库存-1
            orders.emplace_back(buyerName, name, price, actualPrice); //加入订单
            writeOrdersToFile(); //将订单信息写入文件
        }
    }
}

void checkout() { //结账
    double totalPrice = 0;
    for (auto& order : orders) {
        totalPrice += order.getActualPrice();
    }
    cout << "总消费金额为:" << totalPrice << "元" << endl;
}

void refund() { //退货
    string buyerName, bookName;
    cout << "请输入购买者用户名:";
    cin >> buyerName;
    cout << "请输入要退货的书籍名:";
    cin >> bookName;
    int index = -1;
    for (int i = 0; i < orders.size(); i++) {
        if (orders[i].getBuyerName() == buyerName && orders[i].getBookName() == bookName) {
            index = i;
            break;
        }
    }
    if (index == -1) {
        cout << "该订单不存在!" << endl;
    }
    else {
        double actualPrice = orders[index].getActualPrice();
        orders.erase(orders.begin() + index); //删除订单
        writeOrdersToFile(); //将订单信息写入文件
        cout << "退货成功!" << endl;
    }
}

void activity() { //特色活动(买二送一)
    vector<Book> freeBooks;
    for (auto& book : books) { //寻找最便宜的三个书籍
        freeBooks.emplace_back(book);
        if (freeBooks.size() == 3) {
            sort(freeBooks.begin(), freeBooks.end(), [](Book& a, Book& b) { return a.getPrice() < b.getPrice(); });
            freeBooks.pop_back();
        }
    }
    double totalPrice = 0;
    for (auto& order : orders) {
        totalPrice += order.getActualPrice();
    }
    double freePrice = freeBooks[0].getPrice();
    cout << "买二送一活动:消费" << totalPrice << "元,赠送书籍《" << freeBooks[0].getName() << "》,价值" << freePrice << "元" << endl;
}

int main() {
    initBooks(); //从文件中读取书籍信息
    initBuyers(); //从文件中读取用户信息
    initOrders(); //从文件中读取订单信息

    int choice = 0;
    while (choice != 7) {
        cout << "网上购书系统" << endl;
        cout << "1. 登录" << endl;
        cout << "2. 查看书籍信息" << endl;
        cout << "3. 购买书籍" << endl;
        cout << "4. 结账" << endl;
        cout << "5. 查看消费记录" << endl;
        cout << "6. 退货" << endl;
        cout << "7. 特色活动" << endl;
        cout << "请选择:";
        cin >> choice;
        switch (choice) {
        case 1:
            login();
            break;
        case 2:
            printBooks();
            break;
        case 3:
            buyBook();
            break;
        case 4:
            checkout();
            break;
        case 5:
            printOrders();
            break;
        case 6:
            refund();
            break;
        case 7:
            activity();
            break;
        default:
            cout << "输入错误,请重新输入!" << endl;
            break;
        }
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值