将任务拆分为函数

时间:2014-04-21 02:54:50

标签: c++ function arguments

我需要在此代码中包含4个带有参数的函数,但我不能想到一种方法来合并任何有参数的方法。 Aren函数参数通常与整数一起用于计算?我可以为此代码创建哪些函数示例?如果这很容易,请原谅我是相当新的。

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

ofstream outFile;
ifstream inFile;

const int MAXCHAR = 101;
const int MAXLINE = 256;



struct task
{
    char course[MAXCHAR];
    char desc[MAXCHAR];
    char date[MAXCHAR];
};

int main()
{

    task track[MAXLINE];
    bool quit = false;
    while (quit == false)
    {

        char choice;
        cout << "Welcome to my Task List: \n";
        cout << "<a> to add task\n";
        cout << "<s> to show the task list\n";
        cout << "<f> to find a task by course name\n";
        cout << "<q> to quit\n";
        cin >> choice;
        cin.ignore(100, '\n');

        if (choice == 'a' || choice == 'A')
        {
            int count = 0;
            outFile.open("tasks.txt", fstream::app);

            cout << "Enter Course Name (less than 101 characters): ";
            cin.get(track[count].course, MAXCHAR, '\n');
            cin.clear();
            cin.ignore(100, '\n');

            cout << "Enter Task Description (less than 101 characters): ";
            cin.get(track[count].desc, MAXCHAR, '\n');
            cin.clear();
            cin.ignore(100, '\n');

            cout << "Enter due date (mm/dd/yyyy): ";
            cin.get(track[count].date, MAXCHAR, '\n');
            cin.clear();
            cin.ignore(100, '\n');

            char confirm;
            cout << "\nAre you sure you want to add " << track[count].course << ";" << track[count].desc << ";" 
                 << track[count].date << "? (y/n)";
            cin >> confirm;
            if (confirm == 'y' || confirm == 'Y')
            {
                cin.clear();
                cin.ignore(100, '\n');
                outFile << track[count].course << ";" << track[count].desc << ";" << track[count].date << "\n";
                cout << "Task has been added\n";
                count++;
            }
            else if (confirm == 'n' || confirm == 'N')
            {
                cin.clear();
                cin.ignore(100, '\n');
            }
            outFile.close();
        }
        else if (choice == 's' || choice == 'S')
        {
            int count = 0;
            inFile.open("tasks.txt");

            while (inFile)
            {
                inFile.getline(track[count].course, MAXLINE, ';');
                inFile.getline(track[count].desc, MAXLINE, ';');
                inFile.getline(track[count].date, MAXLINE, '\n');
                if (inFile)
                {
                    cout << track[count].course << ";" << track[count].desc << ";" 
                         << track[count].date << "\n";
                    count++;
                }
            }

            inFile.close();
            cin.clear();

        }

        else if (choice == 'f' || choice == 'F')
        {
        int count = 0;
            char course[MAXCHAR];
            cout << "Enter Course Name: ";
            cin >> course;

            inFile.open("tasks.txt");
            while (inFile)
            {
                inFile.getline(track[count].course, MAXLINE, ';');
                inFile.getline(track[count].desc, MAXLINE, ';');
                inFile.getline(track[count].date, MAXLINE, '\n');
                if (strcmp(track[count].course, course) == 0)
                {
                    cout << track[count].course <<  ";" << track[count].desc 
                         << track[count].date << "\n";

                    count++;
                }


            }
            inFile.close();

        }
        else if (choice == 'q' || choice == 'Q')
        {
            quit = true;
        }

    }

}

2 个答案:

答案 0 :(得分:0)

方法的重点在于分割代码,以便更容易阅读。考虑代码中可以自包含的部分,然后将它们分成自己的函数。

您的代码的一个基本示例可能是:

if (choice == 'a' || choice == 'A')
   DoStuff_a(<params>);
else if (choice == 's' || choice == 'S')
   DoStuff_s(<params>);    

        //...

else if (choice == 'f' || choice == 'F')
   DoStuff_f(<params>);  
else if (choice == 'q' || choice == 'Q')
   quit = true;

有很多方法可以隔离您的代码(例如GetInput()GetFileData()等) - 对此问题的讨论可以be found here。请记住,功能设计主要取决于个人偏好,(b)即使对于main() 也应该被视为

许多开发人员坚信main()应该是95%+ 函数调用,而且很少“工作代码”。

答案 1 :(得分:0)

你对'a'和'A','s'和'S','f'和'F'的情况没有多少共同之处。因此,我认为您不能编写一个函数来将所有功能合并到一个函数中,然后基于参数执行操作。但是,您可以通过将所有3种不同的情况包含在一个不带参数的函数中并返回void来使代码更具可读性:

void Case_s_S()
{
    int count = 0;
    inFile.open("tasks.txt");

    while (inFile)
    {
        inFile.getline(track[count].course, MAXLINE, ';');
        inFile.getline(track[count].desc, MAXLINE, ';');
        inFile.getline(track[count].date, MAXLINE, '\n');
        if (inFile)
        {
            cout << track[count].course << ";" << track[count].desc << ";" 
                 << track[count].date << "\n";
            count++;
        }
    }

    inFile.close();
    cin.clear();
}

我可以给你一个更多的建议,让你的“主”更整洁,就是利用函数指针。你可以查找定义的std :: function。你可以做的是有一个chars和std :: functions的映射,它们看起来像std :: map&gt;。然后你可以指定'a'和'A'的键指向函数Case_a_A的值,依此类推。这样,当用户按下该键时,您只需转到该键并调用其值,这将是该函数。