1. 程式人生 > >C語言自制找零錢程式

C語言自制找零錢程式


程式的執行大致如上圖一樣。

接下來程式碼:

#include<stdio.h>
#define SIZE 1000//事先定義結構體陣列的大小
int numberOne,numberTwo,i,j,k=0,numberThree;
char num,num1,num2,num3;
struct note
{
    double sum;
    double money[100];
    double customerMoney;
    double balance;
    int numberK;
} s[SIZE],s1[SIZE];

calcultor()//用來算應該找多少的零錢;
{
    FILE *fp;
    scanf("%*c",&num3);
    for(i=0;; i++)
    {
        k++;
        s[i].sum=0;
        printf("請輸入你消費的金錢:\n");
        for(j=0;; j++)
        {
            //因為不知道顧客買幾次所以用字元輸入,以換行為程式截止
            scanf("%c",&num);
            if(num=='\n') break;
            if(num==' ')
            {
                continue;
            }
            else
            {
                s[i].money[j]=num-'0';//將顧客每次輸入的金額存入陣列中
                s[i].sum=s[i].sum+s[i].money[j];//最後在計算陣列的總和
            }

        }
        printf("請輸入顧客給的金錢:\n");
        //將顧客給的錢及消費的總額存入到結構體中,以及最後找的錢
        scanf("%lf",&s[i].customerMoney);
        printf("你消費的總金額為:%0.2lf\n",s[i].sum);
        s[i].balance=s[i].customerMoney-s[i].sum;
        printf("找您:%0.2lf\n",s[i].balance);
        scanf("%*c",&num1);
        printf("請輸入數字:\n");

        printf("1:繼續    2:結束\n");
        scanf("%d",&numberOne);
        if(numberOne==2)
        {
            s[i].numberK=k;
            break;
        }

        else
        {
            scanf("%*c",&num2);

        }
    }
    if((fp=fopen("cost1.txt","a"))==NULL)//將檔案指標關聯到檔案
    {
        printf("cannot open file\n");
        return;
    }
    for(i=0; i<k; i++) //將2個學生的資訊輸出到檔案
        fprintf(fp,"%0.2lf       %0.2lf     %0.2lf\n",
                s[i].customerMoney,s[i].sum,s[i].balance);
    fclose(fp);//關閉檔案
}
read()
{
    int n;
    FILE *fp;
    if((fp=fopen("cost1.txt","r"))==NULL)
    {
        printf("cannot open file\n");
        return;
    }

    for(i=0; i<1000; i++) //此時引用個新的結構體將從檔案中讀取的存入另一個
        fscanf(fp,"%lf%lf%lf",
               &s1[i].customerMoney,&s1[i].sum,&s1[i].balance);

    fclose(fp);//關閉檔案
}
inquiry()///查詢1級選單
{
    read();
    int a;
    printf("        請選擇數字\n");
    printf("++++++++++++++++++++++++++++\n");
    printf("+    @1:按照消費金額       +\n");
    printf("+                          +\n");
    printf("+    @2:按找顧客給的錢     +\n");
    printf("+                          +\n");
    printf("+    @3:完整消費記錄       +\n");
    printf("+                          +\n");
    printf("+    @4:返回主選單         +\n");
    printf("++++++++++++++++++++++++++++\n");
    scanf("%d",&a);
    if(a==1)
    {
       inquirySum();
    }
     else if(a==2)
    {
        inquiryCustomerMoney();
    }
    else if(a==3)
    {
        Array();
    } else if(a==4)
    {
        main();
    }
}
inquirySum()///按照顧客消費的金額
{
    int a;
    printf("        請選擇數字\n");
    printf("============================\n");
    printf("   &1:按照金額的大小        \n");
    printf("                            \n");
    printf("   &2:按照時間的順序        \n");
    printf("                            \n");
    printf("   &3:返回上一層選單        \n");
    printf("                            \n");
    printf("   &4:返回主選單            \n");
    printf("============================\n");
    scanf("%d",&a);
    //printf2();
    if(a==1)
    {
        ArraySum();
        printf1();
        inquirySum();
    }
    else if(a==2)
    {
       ArraySumTime();
       printf1();
       inquirySum();
    }
    else if(a==3)
    {
        printf1();
        inquiry();
    }
    else if(a==4)
    {
        printf1();
        main();
    }

}
inquiryCustomerMoney()///按照顧客給的錢
{
    int a;
    printf("        請選擇數字\n");
    printf("============================\n");
    printf("   &1:按照金額的大小        \n");
    printf("                            \n");
    printf("   &2:按照時間的順序        \n");
    printf("                            \n");
    printf("   &3:返回上一層選單        \n");
    printf("                            \n");
    printf("   &4:返回主選單            \n");
    printf("============================\n");
    scanf("%d",&a);
    //printf2();
    if(a==1)
    {
        ArrayCustomerMoney();
        printf1();
        inquiryCustomerMoney();
    }
    else if(a==2)
    {
       ArrayCustomerMoneyTime();
       printf1();
       inquiryCustomerMoney();
    }
    else if(a==3)
    {
        printf1();
        inquiry();
    }
    else if(a==4)
    {
        printf1();
        main();
    }

}
int langding()
{
    int a,b;
    printf("     歡迎來到本程式\n");
    printf("****************************\n");
    printf("*         1:進入           *\n");
    printf("*                          *\n");
    printf("*                          *\n");
    printf("*  2:查詢         3:退出   *\n");
    printf("*                          *\n");
    printf("*                          *\n");
    printf("****************************\n");
    printf("請選擇數字:\n");
    scanf("%d",&a);
    return a;
}
print()
{
    printf("--------------------------------\n");
}
printf1()
{
    printf("******************************\n");
}
printf2()
{
    printf("_______________________________\n");
}
first1()///初次呼叫時的東西
{
    int a,b;
    int i=0;
    FILE *fp;
    char str[7],str1[7],str2[7];
    printf("      請設定您的密碼\n");
    printf("****************************\n");
    printf("*                          *\n");
    printf("*                          *\n");
    printf("*                          *\n");
    printf("*  1:進入         2:退出   *\n");
    printf("*                          *\n");
    printf("*                          *\n");
    printf("****************************\n");
    printf("請選擇數字:\n");
    scanf("%d%*c",&a);
    if(a==1)
    {
        printf("請第一次輸入你的密碼:");
        gets(str);
        printf("請第二次輸入你的密碼:");
        gets(str1);
        ///進行兩次的密碼比較
        if(strcmp(str,str1)==0)
        {

            ///通過這個將密碼存入
            if((fp=fopen("code.txt","w"))==NULL)
            {
                printf("can't open file\n");
                exit(0);
            }
            fputs(str,fp);
            fclose(fp);
            main();
        }
        else
        {
            ///密碼錯誤時用for迴圈來繼續輸入
            printf("您的密碼錯誤,請重新輸入:");
            gets(str1);
            for(i=0;; i++)
            {
                if(strcmp(str,str1)==0)
                {
                    if((fp=fopen("code.txt","w"))==NULL)
                    {
                        printf("can't open file\n");
                        exit(0);
                    }
                    fputs(str,fp);
                    fclose(fp);
                    break;
                }
                else
                {
                    printf("您的密碼錯誤,請重新輸入:");
                    gets(str1);
                }
            }
            main();
        }
    }
}
LandingQuery()///查詢登入
{
    int a,b,c;
    int i=0;
    FILE *fp;
    char str[7],str1[7],str3[5]= {'q','u','i','t'},t;
    if((fp=fopen("code.txt","r"))==NULL)
    {
        printf("can't open file\n");
        exit(0);
    }
    while(!feof(fp))  //讀取檔案
    {
        fgets(str1,128,fp);
    }

    printf("      請輸入您的密碼\n");
    printf("----------------------------\n");
    scanf("%c",&t);
    gets(str);
    if(strcmp(str,str1)==0)
    {
        ///呼叫所查詢的函式,
        read();
        inquiry();
        print();
        main();
    }
    else
    {
        printf("    您的密碼錯誤\n");
        printf("++++++++++++++++++++++++++++\n");
        printf("+                          +\n");
        printf("+   @1:重新輸入            +\n");
        printf("+--------------------------+\n");
        printf("+   @2:返回主選單          +\n");
        printf("+                          +\n");
        printf("++++++++++++++++++++++++++++\n");
        scanf("%d%*c",&a);
        if(a==1)
        {
            printf("請輸入密碼:");
            gets(str);
            for(i=0;; i++)
            {
                c=0;
                if(strcmp(str,str1)==0)
                {
                    break;
                }
                else
                {
                    printf("您的密碼錯誤,請重新輸入:\n");
                    printf("^^^^^^^^^^^^^^^^^^^^^^^^^\n");
                    printf("溫馨提示:如果你輸入quit,將會回到主選單\n");
                    gets(str);
                    printf("___________________________\n");
                    ///引用c來判斷這個迴圈啥時結束
                    if(strcmp(str,str3)==0)
                    {
                        c=1;
                        break;
                    }
                }
            }///當密碼數對後,停止後直接呼叫相應的函式
            if(c==1)
            {
                main();
            }
            else
            {
                read();
                inquiry();
                print();
                main();
            }
        }
        else
        {
            ///呼叫主選單的函式
            main();
        }
    }

}
int Index()
{
    ///對結構體中的記錄進行查詢
    int i,j,index;
    index=0;
    read();
    for(i=0;; i++)
    {
        index++;
        if(s1[i].customerMoney==0)
            break;
    }
    return index-1;
}
ArrayCustomerMoney()///顧客給的錢
{
    ///主要用來實現排序
    int i,j,h;
    double t;
    h=Index();
    for(i=0; i<h-1; i++)
    {
        for(j=0; j<h-i-1; j++)
        {
            if(s1[j].customerMoney>s1[j+1].customerMoney)
            {
                t=s1[j].customerMoney;
                s1[j].customerMoney=s1[j+1].customerMoney;
                s1[j+1].customerMoney=t;
            }
        }
    }
    printf("============================\n");
    for(i=0; i<h; i++)
    {
        printf("%d %0.2lf\n",i+1,s1[i].customerMoney);
    }
}
ArraySum()///消費總額
{
    ///主要用來實現排序
    int i,j,h;
    double t;
    h=Index();
    for(i=0; i<h-1; i++)
    {
        for(j=0; j<h-i-1; j++)
        {
            if(s1[j].sum>s1[j+1].sum)
            {
                t=s1[j].sum;
                s1[j].sum=s1[j+1].sum;
                s1[j+1].sum=t;
            }
        }
    }
    printf("============================\n");
    for(i=0; i<h; i++)
    {
        printf("%d %0.2lf\n",i+1,s1[i].sum);
    }
}
Array()///完整的排序
{
    int n;
    printf("       你想要查詢的所有記錄:\n");
    printf("序號 顧客給的錢 消費總額    找錢 \n");
    for(i=0; i<1000; i++)
    {
        if(s1[i].customerMoney==0.00&&s1[i].sum==0.00) break;
        if(i<=9)
        {
            printf(" %d:  %0.2lf       %0.2lf     %0.2lf\n",i,
                   s1[i].customerMoney,s1[i].sum,s1[i].balance);
        }
        else
        {
            printf("%d:   %0.2lf       %0.2lf     %0.2lf\n",i,
                   s1[i].customerMoney,s1[i].sum,s1[i].balance);
        }
    }
}
ArrayCustomerMoneyTime()///顧客給的錢按時間排序
{
    print();
    int h;
    h=Index();
    for(i=0; i<h; i++)
    {
        printf(" %d:  %0.2lf\n",i+1,s1[i].customerMoney);
    }
}
ArraySumTime()///消費總額按時間排序
{
    print();
    int h;
    h=Index();
    for(i=0; i<h; i++)
    {
        printf(" %d:  %0.2lf\n",i+1,s1[i].sum);
    }
}
int main()
{
    FILE *fp;///定義指標
    ///讀取檔案將他與原先的密碼進行比較;
    char str1[8],str3[8]= {'0','0','0','0','0','0'};
    char ch;
    if((fp=fopen("code.txt","r"))==NULL)
    {
        printf("can't open file\n");
        exit(0);
    }
    while(!feof(fp))  //讀取檔案
    {
        fgets(str1,128,fp);
    }
    ///進行檔案比較;
    if(strcmp(str1,str3)==0)
    {
        first1();
    }
    else
    {
        read();
        numberThree=langding();
        if(numberThree==1)
        {
            calcultor();//呼叫計算的函式
            print();
            main();//呼叫自身的函式
        }
        else if(numberThree==2)
        {
            LandingQuery();
        }
    }
    return 0;
}