关于通讯录

#include<iostream>
using namespace std;
#define max 1000
#include<string.h>
//显示菜单界面;
void showmenu()
{
    cout << "*************************" << endl;
    cout << "*****  1,添加联系人 *****" << endl;
    cout << "*****  2,显示联系人 *****" << endl;
    cout << "*****  3,删除联系人 *****" << endl;
    cout << "*****  4,查找联系人 *****" << endl;
    cout << "*****  5,修改联系人 *****" << endl;
    cout << "*****  6,清空联系人 *****" << endl;
    cout << "*****  0,退出通讯录 *****" << endl;
    cout << "*************************" << endl;
}
struct person
{
    string name;
    int sex;//1,男 2女;
    int age;
    string number;
    string home;
};
struct addressbooks
{
    struct person arrange[max];//保存数组;
    int size;//记录个数
};
//1,添加联系人
void addperson(addressbooks* abs)
{
    //判断通讯录是否满了
    if (abs->size == max)
    {
        cout << "通讯已满,无法添加" << endl;
        return;
    }
    else
    {
        //添加联系人

        //姓名
        string name;
        cout << "请输入姓名" << endl;
        cin >> name;
        abs->arrange[abs->size].name = name;
        //年龄,电话,性别。
        cout << "请输入性别" << endl;
        cout << "1---男" << endl;
        cout << "2---女" << endl;
        int sex = 0;
        while (true) {
            cin >> sex;//不是1就会退出循环
            if (sex == 1 || sex == 2)
            {
                abs->arrange[abs->size].sex = sex;
                break;
            }
            cout << "输入有误,请重新输入" << endl;
        }
        cout << "请输入年龄" << endl;
        int age = 0;
        cin >> age;
        abs->arrange[abs->size].age = age;
        cout << "输入电话" << endl;
        string phone;
        cin >> phone;
        abs->arrange[abs->size].number = phone;
        cout << "请输入地址" << endl;
        string address;
        cin >> address;
        abs->arrange[abs->size].home = address;
        abs->size++;
        cout << "添加成功" << endl;
        system("pause");
        system("cls");//清屏
    }
}
//2,显示联系人
void showperson(addressbooks* abs)//为零提示为空//不为0,显示信息
{
    if (abs->size==0)//判断人数是不是0;
    {
        cout << "当前为空" << endl;
    }
    else
    {
        for (int i = 0; i < abs->size; i++)
        {
            cout << "姓名:" << abs->arrange[i].name <<"\t";
            cout << "性别:" <<( abs->arrange[i].sex==1?"男":"女") <<"\t";
            cout << "年龄:" << abs->arrange[i].age <<"\t";
            cout << "电话:" << abs->arrange[i].number<<"\t";
            cout << "住址:" << abs->arrange[i].home << endl;
        }
    }
    system("pause"); 
    system("cls");
}//检测联系人是否存在
int isexit(addressbooks* abs, string name)
{
    for (int i = 0; i < abs->size; i++)
    {
        if (abs->arrange[i].name == name)
        {
            return i;//找到了,返回下标; 
        }
    }
    return -1;
}

void outperson(addressbooks *abs)
{
    cout << "请输入你要删除的联系人" << endl;
    string name;
    cin >> name;
    isexit(abs, name);
    //ret==-1//没有这个人
    int ret = isexit(abs, name);
    if (ret != -1)
    {
        //找到人 
        for (int i = ret; i < abs->size; i++)
        {
            abs->arrange[i] = abs->arrange[i + 1];
        }
        abs->size--;
        cout << "删除成功" << endl;
    }
    system("pause");
    system("cls");
    
}
void findperson(addressbooks *abs)
{
    cout << "请输入查找的纤细人" << endl;
    string name;
    cin >> name;
    //判断联系人是否存在
    int ret = isexit(abs, name);
        if (ret!= -1)
        {
            cout << "姓名" << abs->arrange[ret].name<< "\t";
            cout << "性别" << abs->arrange[ret].sex << "\t";
            cout << "年龄" << abs->arrange[ret].age<< "\t";
            cout << "电话" << abs->arrange[ret].number << "\t";
            cout << "住址" << abs->arrange[ret].home << "\t";
        }
        else
        {
            cout << "查无此人" << endl;
        }
        system("pause");
        system("cls");
}
void changeperson(addressbooks* abs)
{

    cout << "请输入修改联系人" << endl;
    string name;
    cin >> name;
    int ret = isexit(abs, name);
    if (ret != -1)
    {
        string name;
        cout << "请输入新的姓名" << endl;
        cin >> name;
        abs->arrange[ret].name = name;
        cout << "请新的输入性别" << endl;
        cout << "1------男" << endl;
        cout << "2------女" << endl;
        int sex = 0;
        while (true) {
            cin >> sex;
            if (sex == 1 || sex == 2)
            {
                abs->arrange[ret].sex = sex;
                break;
            }
            cout << "输入有误" << endl;
        }
        cout << "请输入新的年龄" << endl;
        int age;
        cin >> age;
        abs->arrange[ret].age = age;
        cout << "请输入新的电话" << endl;
        string number;
        cin >> number;
        abs->arrange[ret].number = number;
        cout << "请输入新的地址" << endl;
        string home;
        cin >> home;
        abs->arrange[ret].home = home;
        cout << "修改成功" << endl;

    }
    else
    {
        cout << "查无此人" << endl;

    }
    system("pause");
    system("cls");
}
void killallperson(addressbooks* abs)
{
    abs->size = 0;
    cout << "已经清空" << endl;
    system("pause");
    system("cls");
}
int main()
{//创建通讯录变量
    struct addressbooks abs;
    abs.size = 0;
    int select = 0;
    while (true) {
        showmenu();
        cin >> select;
        switch (select)
        {
        case 1://添加联系人
            addperson(&abs);//利用地址传递
            break;
        case 2://显示联系人
            showperson(&abs);
            break;
        case 3://删除联系人 {
        /*{
            cout << "请输入删除联系人的姓名" << endl;
            string name;
            cin >> name;
            isexit(&abs, name);
            if (isexit(&abs, name) == -1)
            {
                cout << "查无此人" << endl;
            }
            else
            {
                cout << "找到此人" << endl;
            }
            break; }*/
            outperson(&abs);
            break;
        case 4://查找联系人
            findperson(&abs);
            break;
        case 5://修改联系人
            changeperson(&abs);
            break;
        case 6://清空联系人
            killallperson(&abs);
            break;
        case 0://退出通讯录
            cout << "欢迎下次使用" << endl;
            return 0;
        default:
            cout << "请输入对应的数字" << endl;
            break;
        }
    }
    
    system("pause");
    return 0;
}

好的,这是一个关于通讯录管理系统的 JSP 程序,可以实现添加、删除、编辑和查询联系人信息的功能。首先创建一个 contact.jsp 文件,并在文件中编写以下代码: <html> <head> <title>通讯录管理系统</title> </head> <body> <% // 定义一个数组,用于保存联系人信息 String[][] contacts = new String[100][3]; // 记录当前联系人数量 int count = 0; // 获取请求参数 String action = request.getParameter("action"); String name = request.getParameter("name"); String phone = request.getParameter("phone"); String address = request.getParameter("address"); // 如果 action 参数为空,则显示联系人列表 if (action == null) { %> <h1>通讯录管理系统</h1> <table border="1"> <tr> <th>姓名</th> <th>电话</th> <th>地址</th> <th>操作</th> </tr> <% for (int i = 0; i < count; i++) { %> <tr> <td><%= contacts[i][0] %></td> <td><%= contacts[i][1] %></td> <td><%= contacts[i][2] %></td> <td> <a href="contact.jsp?action=edit&id=<%= i %>">编辑</a> | <a href="contact.jsp?action=delete&id=<%= i %>">删除</a> </td> </tr> <% } %> </table> <a href="contact.jsp?action=add">添加联系人</a> <% } else if (action.equals("add")) { // 如果 action 参数为 add,则添加联系人 %> <h1>添加联系人</h1> <form method="post" action="contact.jsp"> <label for="name">姓名:</label> <input type="text" name="name" id="name" /><br /> <label for="phone">电话:</label> <input type="text" name="phone" id="phone" /><br /> <label for="address">地址:</label> <input type="text" name="address" id="address" /><br /> <input type="submit" value="添加" /> </form> <% } else if (action.equals("edit")) { // 如果 action 参数为 edit,则编辑联系人 int id = Integer.parseInt(request.getParameter("id")); %> <h1>编辑联系人</h1> <form method="post" action="contact.jsp"> <label for="name">姓名:</label> <input type="text" name="name" id="name" value="<%= contacts[id][0] %>" /><br /> <label for="phone">电话:</label> <input type="text" name="phone" id="phone" value="<%= contacts[id][1] %>" /><br /> <label for="address">地址:</label> <input type="text" name="address" id="address" value="<%= contacts[id][2] %>" /><br /> <input type="hidden" name="id" value="<%= id %>" /> <input type="submit" value="保存" /> </form> <% } else if (action.equals("delete")) { // 如果 action 参数为 delete,则删除联系人 int id = Integer.parseInt(request.getParameter("id")); for (int i = id; i < count - 1; i++) { contacts[i][0] = contacts[i + 1][0]; contacts[i][1] = contacts[i + 1][1]; contacts[i][2] = contacts[i + 1][2]; } count--; %> <h1>联系人已删除。</h1> <a href="contact.jsp">返回</a> <% } else if (action.equals("save")) { // 如果 action 参数为 save,则保存联系人 int id = Integer.parseInt(request.getParameter("id")); contacts[id][0] = name; contacts[id][1] = phone; contacts[id][2] = address; %> <h1>联系人已保存。</h1> <a href="contact.jsp">返回</a> <% } else { // 如果 action 参数不是上述几种情况,则说明是保存联系人的表单提交 // 获取表单中的 id 参数,如果为空,则表示是添加联系人的表单提交 String idStr = request.getParameter("id"); if (idStr == null) { // 添加联系人 contacts[count][0] = name; contacts[count][1] = phone; contacts[count][2] = address; count++; %> <h1>联系人已添加。</h1> <a href="contact.jsp">返回</a> <% } else { // 编辑联系人 int id = Integer.parseInt(idStr); contacts[id][0] = name; contacts[id][1] = phone; contacts[id][2] = address; %> <h1>联系人已保存。</h1> <a href="contact.jsp">返回</a> <% } } %> </body> </html> 通过对代码的解析,我们可以看到,这个程序定义了一个二维数组,用于保存联系人的姓名、电话和地址信息。在页面中,提供了一个联系人列表,以及按钮来添加、编辑和删除联系人。根据请求参数的不同,程序会执行不同的分支代码来实现相应的功能。通过这个程序,用户可以方便地管理自己的通讯录信息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值