gpt4 book ai didi

c++ - 如果我将所有这些放在不同的功能中是个好主意吗?

转载 作者:太空宇宙 更新时间:2023-11-04 13:27:31 25 4
gpt4 key购买 nike

所以我正在做这个期末项目。老师说我们现在应该使用函数。他最近向我们介绍的。我的问题是将每个选项都放入函数中是否是个好主意?函数用于组织和代码重用,对吗?或者我错过了功能点。 XD

我的意思是为选项 1 创建一个功能,即添加帐户。而不是将其放在主要功能中。到目前为止,我只为选项 3,4 和 5 制作了一个功能。它们是搜索、查看和删除功能

#include <iostream>
#include <string>
using namespace std;

bool accountSearch(int searchParameter);
void viewList();
void deleteFromList(int delParameter);

int option, numberOfAccounts = 0, accountNumSearch, index, deleteAccount;

struct personAccount
{
int currentBalance, accountNumber, pin;
string lastname, firstname, middlename;
};

personAccount account[20];

int main()
{
do{
cout << "[1] Add Account" << endl
<< "[2] Edit Account" << endl
<< "[3] Search Account" << endl
<< "[4] View Account" << endl
<< "[5] Delete Account" << endl
<< "[6] Inquire" << endl
<< "[7] Change Pin Number" << endl
<< "[8] Withdraw" << endl
<< "[9] Deposit" << endl
<< "[10] View Transactions" << endl
<< "[11] Exit" << endl << endl
<< "Option [1-11]: ";

cin >> option; cout << endl;

if(option == 1)
{
if(numberOfAccounts != 20)
{
cout << "Account Number: ";
cin >> account[numberOfAccounts].accountNumber;
cout << "PIN: ";
cin >> account[numberOfAccounts].pin;
cout << "Lastname: ";
cin >> account[numberOfAccounts].lastname;
cout << "Firstname: ";
cin >> account[numberOfAccounts].firstname;
cout << "Middlename: ";
cin >> account[numberOfAccounts].middlename;
account[numberOfAccounts].currentBalance = 0;
++numberOfAccounts;
cout << endl;
}
else
{
cout << "The list is full!\n\n";
}
}
else if(option == 2)
{
if(numberOfAccounts != 0)
{
cout << "Account Number: ";
cin >> accountNumSearch;
if(accountSearch(accountNumSearch))
{
cout << "PIN: ";
cin >> account[index].pin;
cout << "Lastname: ";
cin >> account[index].lastname;
cout << "First name: ";
cin >> account[index].firstname;
cout << "Middlename: ";
cin >> account[index].middlename;
cout << endl;
}
else
{
cout << "Account not found!\n\n";
}

}
else
{
cout << "The list is empty!\n\n";
}
}
else if(option == 3)
{
if(numberOfAccounts != 0)
{
cout << "Enter account number to search: ";
cin >> accountNumSearch;
if(accountSearch(accountNumSearch))
{
cout << "Found at index " << index << "\n\n";
}
else
{
cout << "Not found!\n\n";
}
}
else
{
cout << "The list is empty!\n\n";
}
}
else if(option == 4)
{
if(numberOfAccounts != 0)
{
viewList();
}
else
{
cout << "The list is empty!\n\n";
}
}
else if(option == 5)
{
if(numberOfAccounts != 0)
{
cout << "Account Number: ";
cin >> deleteAccount;
deleteFromList(deleteAccount);
}
}

}while(option != 11);
}

bool accountSearch(int searchParameter)
{
bool found = 0;
for(int i = 0; i < numberOfAccounts; i++)
{
index = i;
if (account[i].accountNumber == searchParameter)
{
found = 1;
break;
}
}

if(found)
{
return 1;
}
else
{
return 0;
}
}

void viewList()
{
for(int i = 0; i < numberOfAccounts; i++)
{
cout << "Account Number: " << account[i].accountNumber << endl
<< "Lastname: " << account[i].lastname << endl
<< "Firstname: " << account[i].firstname << endl
<< "Middlename: " << account[i].middlename << endl
<< "Current Balance: " << account[i].currentBalance << "\n\n";
}
}

void deleteFromList(int delParameter)
{
if(accountSearch(deleteAccount))
{
for(int i = index; i < numberOfAccounts; i++)
{
account[i] = account[i+1];
}

--numberOfAccounts;
cout << "Deleted Done\n";
}
else
{
cout << "Account not found!\n";
}
}

这还没有完成,但是你有什么想提或建议的吗?

最佳答案

是的,您应该单独编写函数,作为程序员,这是常见的良好做法,您(和其他人)将更容易阅读、遵循和理解您的代码。

因此,如果您的选项做不同的事情,它们应该有自己的功能。 (更像是可取的,最后由你决定)

关于c++ - 如果我将所有这些放在不同的功能中是个好主意吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32829599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com