Sunday, January 1, 2012

C++ [泡沫排序] [ 模擬提款機]

//第一題:

// CPPTest.cpp : 定義主控台應用程式的進入點。

//

#include "stdafx.h"
#include<iomanip>
#include<iostream>
#include<stdlib.h>
#include<time.h>

using namespace std;

void BubbleSort(int num)
{

    int* ptr = new int[num];

    for (int i = 0; i < num; i ++)
    {
        cout << "Enter number " << i+1 << " > " ;

        cin >> ptr[i];
    }

    int temp;

    for (int i = 0; i < num; i ++)

    {

        for (int j = i+1; j < num ; j++)

        {

            if (ptr[i] > ptr[j])

            {

                temp = ptr[i];

                ptr[i]=ptr[j];

                ptr[j]=temp;

            }

        }

   

    }

    cout << "The sorted numbers in ascending order are: \n";

    for (int i = 0; i < num; i ++)

    {

        cout << ptr[i];

        cout << " ";

    }

}

int _tmain(int argc, _TCHAR* argv[])

{

   

    int num;

    cout << "Enter the number of inegers: ";

    cin >> num;
   
    BubbleSort(num);

    system("pause");

    return 0;

}

-----

//第二題:

// CPPTest.cpp : 定義主控台應用程式的進入點。

//

#include "stdafx.h"
#include<iomanip>
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<cstring>

#define size 15

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

    int money;

    int deposition, withdrawal;

    int choice;

    fstream file;

    while(true)

    {

        cout << "(1)Balance Check  \n";
        cout << "(2)Withdraw       \n";
        cout << "(3)Deposit      \n";
        cout << "(4)Clear      \n";
        cout << "(5)Exit      \n";
        cout << "Please choose a service: ";

        cin >> choice;

        if (choice == 1)
        {        ifstream fin("money.txt", ios::in);

                fin >> money;
                cout << "The balance in your account is : "  

                     << money << " dollars.\n\n";
        }

        else if (choice == 2)
        {

            ifstream fin("money.txt");

            fin >> money;

            cout << "Enter the amount of money you withdraw: ";

            cin >> withdrawal;

            if (withdrawal <= money)

            {

                ofstream fout("money.txt", ios::trunc);

                fout << money - withdrawal;

                cout << "The balance in your account now is : "  

                     <<  money - withdrawal << " dollars\n\n";

            }

            else
            {

                cout << "The balance in your account now is only " 

                     << money << " dollars and not enough to withdraw " 

                     << withdrawal << " dollars.\n\n";
            }

        }

        else if (choice == 3)
        {

                ifstream fin("money.txt");

                fin >> money;

                cout << "Enter the amount of money you deposit: ";

                cin >> deposition;

                ofstream fout("money.txt", ios::trunc);

                fout << money + deposition;

                cout << "The balance in your account now is : "  

                      <<  money + deposition << " dollars.\n\n";

        }

        else if (choice == 4)
        {

            ofstream fout("money.txt", ios::trunc);

            fout << 0;

            cout << "The balance in your account now is 0 dollar.\n\n";

       

        }

        else if (choice == 5 )
        {
            break;
        }

    } "pause");
    return 0;

}

1 comment:

  1. C++ [泡沫排序] [ 模擬提款機題目]

    題目1[泡沫排序]

    設計一個可輸入指定個數的整數,依泡沫排序法顯示出由小到大順序之資料,程式要能重複執行。

    程式2: [ 模擬提款機題目]

    設計一個可模擬提款機的程式,包含餘額查詢、提款、存款、清除(即將餘額歸0) 、離開等五功能,請將存款餘額存入c:\money.txt內,程式要能重複執行,且要能顯示提款時之餘額不足錯誤訊息。

    昨天本來要在奇摩知識回答這題目,

    http://tw.knowledge.yahoo.com/question/question?qid=1511123002668

    不過寫了第1題左右就發現被回答了,

    但現在還是發表在這,當練習

    ReplyDelete