個人記賬軟體(C++ & mysql)

DW_Deven發表於2015-10-12

一、功能

  1. 個人賬單資訊的增刪查改
  2. 個人使用者的註冊和登入
  3. 管理員管理所有使用者的賬戶資訊

二、平臺

  • Linux(CentOs 7)

三、功能展示

  • 進入介面
    這裡寫圖片描述

  • 註冊使用者
    這裡寫圖片描述

  • 登入使用者

  • 這裡寫圖片描述

  • 記賬功能
    這裡寫圖片描述

  • 查賬功能
    這裡寫圖片描述

  • 改賬功能
    這裡寫圖片描述

  • 刪賬功能
    這裡寫圖片描述

四、程式碼展示

#include <iostream>  
#include <stdio.h>  
#include <mysql.h>
#include <stdlib.h>
#include <string.h>  
using namespace std;
MYSQL *mysql_api;
MYSQL_RES *result;
MYSQL_ROW row;
char Username[50]="\0";
char Password[50]="\0";
string Dest;
char dest[100];
int chose;

int main(int argc,char *argv[])  
{
    void SignIn();
    void SignUp();
    void ShowMen1();
    void ShowMen2();
    ShowMen1();
    cin>>chose;
    while(chose)
    {
        switch(chose)
        {
            case 1:SignIn();break;
            case 2:SignUp();break;
            case 3:exit(0);break;
            default:cout<<"輸入有誤,請重新輸入"<<endl;break;
        }
        cin>>chose;
    }
    return 0;  
}  

void ShowMen1()
{
    printf("==============================\n");
    printf("|     歡迎使用記賬軟體       |\n");
    printf("==============================\n");
    printf("==============================\n");
    printf("|         1.登入             |\n");
    printf("|         2.註冊             |\n");
    printf("|         3.退出             |\n");
    printf("==============================\n");
}

int ShowMen2()
{   void write_item();
    void check_item();
    void update_item();
    void delete_item();
    printf("==============================\n");
    printf("|       請選擇使用功能       |\n");
    printf("==============================\n");
    printf("==============================\n");
    printf("|         1.記賬             |\n");
    printf("|         2.查賬             |\n");
    printf("|         3.改賬             |\n");
    printf("|         4.刪賬             |\n");
    printf("|         5.退出             |\n");
    printf("==============================\n");
    cin>>chose;
    while(chose)
    {
        switch(chose)
        {
            case 1:write_item();break;
            case 2:check_item();break;
            case 3:update_item();break;
            case 4:delete_item();break;
            case 5:exit(0);break;
            default:cout<<"輸入有誤,請重新輸入"<<endl;break;
        }
    cin>>chose;
    }
}

void Require()
{   
    void quit();
    printf("==============================\n");
    printf("|       是否返回主選單       |\n");
    printf("==============================\n");
    printf("==============================\n");
    printf("|           1.是             |\n");
    printf("|       2.否,直接退出       |\n");
    printf("==============================\n");
    cin>>chose;
    while(chose)
    {
        switch(chose)
        {
            case 1:ShowMen2();break;
            case 2:quit();break;
            default:cout<<"輸入有誤,請重新輸入"<<endl;break;
        }
        cin>>chose;
    }
}

void quit()
{
    mysql_close(mysql_api);
    exit(0);    
}

void write_item()
{
    void Require();
    string dates;
    Dest="insert into ";
    Dest=Dest+Username;
    Dest=Dest+"(DATES,num,type) values('";
    cout<<"請輸入消費日期"<<endl;
    cout<<"year-month-dates"<<endl;
    cin>>dates;
    Dest=Dest+dates;
    Dest=Dest+"','";
    cout<<"請輸入消費數額"<<endl;
    cin>>dates;
    Dest=Dest+dates;
    Dest=Dest+"','";
    cout<<"請輸入消費型別"<<endl;
    cin>>dates;
    Dest=Dest+dates;
    Dest=Dest+"')";
    strcpy(dest,Dest.c_str());
    mysql_query(mysql_api,dest);
    printf("==============================\n");
    printf("|          新增成功          |\n");
    printf("==============================\n");
    Require();
}

void delete_item()
{
    char sql0[50];
    Dest="delete from ";
    Dest=Dest+Username;
    printf("==============================\n");
    printf("|     請選擇待刪資料的日期   |\n");
    printf("|   (year — month — date)  |\n");
    printf("==============================\n");
    cin>>sql0;
    Dest = Dest+" where DATES='";
    Dest = Dest+sql0;
    Dest = Dest+"'";
    strcpy(dest,Dest.c_str());
    mysql_query(mysql_api,dest);
    printf("==============================\n");
    printf("|          刪除成功          |\n");
    printf("==============================\n");
    Require();
}

void update_item()
{   
    string num;
    char sql0[50];
    Dest="update ";
    Dest=Dest+Username;
    cout<<Username<<endl;
    printf("==============================\n");
    printf("|     請選擇更新資料的日期   |\n");
    printf("|      (year—month—date)   |\n");
    printf("==============================\n");
    cin>>sql0;
    Dest = Dest+" set num='";
    cout<<"請輸入修改的數額"<<endl;
    cin>>num;
    Dest = Dest+num;
    Dest = Dest+"' where DATES='";
    Dest = Dest+sql0;
    Dest = Dest+"'";
    strcpy(dest,Dest.c_str());
    mysql_query(mysql_api,dest);
    printf("==============================\n");
    printf("|          更新成功          |\n");
    printf("==============================\n");
    Require();
}

void check_item()
{   Dest="select * from ";
    Dest=Dest+Username;
    strcpy(dest,Dest.c_str());
    mysql_query(mysql_api,dest);
    result=mysql_store_result(mysql_api);
    if (mysql_num_rows(result)!=0)
    {
        printf("============================\n");
        printf("|         查詢結果           |\n");
        printf("=============================\n");
        printf("=============================\n");
        printf("編號|   日期  | 種類 | 數目 |\n");
        printf("=============================\n");
        while ((row=mysql_fetch_row(result)))
        {
            cout<<row[0]<<"|"<<row[1]<<"|"<<row[2]<<"|"<<row[3]<<endl;  
            printf("==============================\n");
        }           
    }
    else 
    {
        printf("==============================\n");
        printf("|          無記錄            |\n");
        printf("==============================\n");
    }
    mysql_free_result(result);
    Require();
}

void SignIn()
{   
    void ShowMen2();
    mysql_api = mysql_init(NULL);
    cout<<"請輸入使用者名稱"<<endl;
    cin>>Username;
    cout<<"請輸入密碼"<<endl;
    cin>>Password;
    mysql_api = mysql_real_connect(mysql_api,"localhost",Username,Password,"money",0,NULL,0);       
    while (!mysql_api)
    {   
        cout<<"賬號或密碼錯誤!"<<endl;
        cout<<"請重新輸入使用者名稱"<<endl;
        cin>>Username;
        cout<<"請重新輸入密碼"<<endl;
        cin>>Password;
        mysql_api = mysql_init(NULL);  
        mysql_api = mysql_real_connect(mysql_api,"localhost",Username,Password,"money",0,NULL,0);       
    }
    ShowMen2();
}

void SignUp()
{
    void LogIn();
    void SignIn();
    char dest[100]; 
    mysql_api = mysql_init(0);    
    mysql_api = mysql_real_connect(mysql_api,"localhost","root","","money",0,NULL,0);       
    cout<<"請輸入使用者名稱(20字元以內)"<<endl;
    cin>>Username;
    while (strlen(Username)>20)
    {
        cout<<"位數超過限制,請重新輸入!"<<endl;
        cin>>Username;
    }
    cout<<"請輸入密碼(20字元以內)"<<endl;
    cin>>Password;
    while (strlen(Password)>20)
    {
        cout<<"位數超過限制,請重新輸入!"<<endl;
        cin>>Password;
    }

    Dest="create USER ";
    Dest = Dest+Username;
    Dest = Dest+ " IDENTIFIED BY '";
    Dest = Dest+Password;
    Dest = Dest+ "';";
    strcpy(dest,Dest.c_str());
    mysql_query(mysql_api,dest);
    Dest="create table ";
    Dest = Dest+Username;
    Dest = Dest+" (ID int auto_increment primary key,DATES DATE,num double,type varchar(50))";
    strcpy(dest,Dest.c_str());
    mysql_query(mysql_api,dest);
    Dest="grant all on money.";
    Dest = Dest+Username;
    Dest = Dest+" to ";
    Dest = Dest+Username;
    strcpy(dest,Dest.c_str());
    mysql_query(mysql_api,dest);
    mysql_close(mysql_api);
    cout<<"註冊成功"<<endl;
    printf("==============================\n");
    printf("|       是否開始使用軟體     |\n");
    printf("==============================\n");
    printf("==============================\n");
    printf("|           1.是             |\n");
    printf("|       2.否(退出)         |\n");
    printf("==============================\n");
    cin>>chose;
    while(chose)
    {
        switch(chose)
        {
            case 1:LogIn();break;
            case 2:exit(0);break;
            default:cout<<"輸入有誤,請重新輸入"<<endl;break;
        }
        cin>>chose;
    }
}

void LogIn()
{
    void ShowMen2();
    mysql_api = mysql_real_connect(mysql_api,"localhost",Username,Password,"money",0,NULL,0);       
    ShowMen2();
}

五、優化方向

相關文章