C++ C++;实现不受'影响的功能;不要利用全球宣言

C++ C++;实现不受'影响的功能;不要利用全球宣言,c++,function,vector,C++,Function,Vector,我的代码已在运行,请参见: 现在,我的函数可以工作,但使用了我在全局范围内声明的信息,我想,我想将它们转换为第11-15行所示的格式,但我不确定如何转换它们。简单地说,我正在尝试转换 “无效添加县选举文件” 格式为 “无效添加县选举文件(常量字符串、常量向量&、常量向量&、常量向量&、常量向量&、常量向量&)” 我不知道从哪里开始,甚至不知道如何开始 有没有人能帮我一下,告诉我第一个函数是如何实现的,这样我就可以全面实现它了 谢谢大家 将变量和函数存储在类中,重载运算符并创建函数来访问这些变量

我的代码已在运行,请参见:

现在,我的函数可以工作,但使用了我在全局范围内声明的信息,我想,我想将它们转换为第11-15行所示的格式,但我不确定如何转换它们。简单地说,我正在尝试转换

“无效添加县选举文件”

格式为

“无效添加县选举文件(常量字符串、常量向量&、常量向量&、常量向量&、常量向量&、常量向量&)”

我不知道从哪里开始,甚至不知道如何开始

有没有人能帮我一下,告诉我第一个函数是如何实现的,这样我就可以全面实现它了

谢谢大家

  • 将变量和函数存储在类中,重载运算符并创建函数来访问这些变量
  • int main()
    中声明所有变量,并设置要传递给每个函数的参数,例如

    作废打印结果()
    修改为

    void打印结果(std::vector vec、int nCount等)

  • 与第一个类似,创建一个包含所有数据成员的结构,然后将该结构(按ref)传递到每个函数中


您的函数声明应该如下所示:

void add_county_election_file(const string, vector<int>&, vector<string>..);
void add_county_election_file(const string, vector<string>&, vector<int>&, vector<int&, vector<int>&);
add_county_election_file(countyname, countyNameVector, countyNCount, countyFCount, countyOCount);
void add_country_election_文件(常量字符串、向量和向量..);
确保向量模板的参数列表是正确的(这是您放置的类型)

然后将函数的实现与声明相匹配:

   void add_county_election_file(const string, vector<int>&, vector<string>..){...}
void add_country_election_文件(常量字符串、向量和向量..){…}
现在使用main中的apppropriate arguemtns调用函数:

string s;
vector<int> arg;
vector<string> sv;
void someFunction (s, arg, sv ...);
字符串s;
向量arg;
向量sv;
无效函数(s、arg、sv…);

OOP实现这一点的方法是创建一个名为ElectionInfo的类,其中:

以下是其成员字段:

vector <string> countyNameVector;
vector <int> countyNCount;
vector <int> countyFCount;
vector <int> countyOCount;
int NCount;
int FCount;
int OCount;
int NTotal;
int FTotal;
int OTotal;
通过这种方式,您根本不必传递对向量的引用,而是可以执行以下操作:

ElectionInfo an_elect_info;
char selection = get_menu_choice();

// some if-statements to decide which of the following to call:

an_elect_info.add_county_election_file(county_name);
an_elect_info.search_county(county_name);
an_elect_info.print_results();
但是,如果您希望继续使用当前的功能方法:

在主方法内声明并初始化以下内容:

vector <string> countyNameVector;
vector <int> countyNCount;
vector <int> countyFCount;
vector <int> countyOCount;
int NCount;
int FCount;
int OCount;
int NTotal;
int FTotal;
int OTotal;

对象是通过引用自动传递的。

我认为您所声明的函数是正确的
void add_country_election_文件(常量字符串、向量&、向量&、…)

因此,您只需使用必需的参数调用上述函数,因为现在您没有传递参数,并且您当前的定义不接受任何参数


作为一种良好的实践,在
int main()
函数中,您可以使用
开关
,而不是使用
if-else

,重构的基本过程应该在第一步只涉及代码分组和放置,并且应该只涉及编写新逻辑。使用这一原则,首先可以按照以下方式修改代码

string ReadInputString(const char* title)
{
    string s

    cout << title;
    cin >> s;

}

void add_county_election_file(const std::string& filename
    , std::vector<string>& countyNameVector
    , std::vector<int>& countyNCount
    , std::vector<int>& countyFCount
    , std::vector<int>& countyOCount
    )
{
        int NCount = 0;
        int FCount = 0;
        int OCount = 0;
        int NTotal = 0;
        int FTotal = 0;
        int OTotal = 0;
        char vote;
    std::ifstream input((filename).c_str());
    string countyName;
    if(input.is_open())
        {
        input >> countyName;
        countyNameVector.push_back(countyName);
        while(input >> vote)
                        {
                        if(vote == 'N' || vote == 'n')
                                {
                NCount = NCount + 1;
                }
                                else if(vote == 'F' || vote == 'f')
                                        {
                                        FCount = FCount + 1;
                                        }
                                        else
                                                {
                                                OCount = OCount + 1;
                                                }
                        }
            countyNCount.push_back(NCount);
            countyFCount.push_back(FCount);
            countyOCount.push_back(OCount);
        }
        cout << countyName << endl;
}

void add_county_election_file()
{
  string fn = ReadInputString("Enter the county file to process: ");
  add_county_election_file(fn,g_countyNameVector,g_countyNCount,g_countyFCount,g_countyOCount);
}
string ReadInputString(const char*title)
{
串
cout>s;
}
void add_country_election_文件(const std::string和filename)
,std::vector和countyNameVector
,std::vector和countyNCount
,std::vector和countyCount
,std::向量和计数
)
{
int NCount=0;
int FCount=0;
int-OCount=0;
int NTotal=0;
int-FTotal=0;
int-otottal=0;
字符投票;
std::ifstream输入((文件名).c_str());
字符串countyName;
if(input.is_open())
{
输入>>countyName;
countyName向量。向后推(countyName);
while(输入>>投票)
{
如果(投票='N'| |投票='N')
{
NCount=NCount+1;
}
else if(投票='F'| |投票='F')
{
FCount=FCount+1;
}
其他的
{
OCount=OCount+1;
}
}
CountyCount。推回(NCount);
CountyCount。推回(FCount);
countyOCount.向后推(OCount);
}
库特
void add_county_election_file(const string, vector<string>&, vector<int>&, vector<int&, vector<int>&);
add_county_election_file(countyname, countyNameVector, countyNCount, countyFCount, countyOCount);
string ReadInputString(const char* title)
{
    string s

    cout << title;
    cin >> s;

}

void add_county_election_file(const std::string& filename
    , std::vector<string>& countyNameVector
    , std::vector<int>& countyNCount
    , std::vector<int>& countyFCount
    , std::vector<int>& countyOCount
    )
{
        int NCount = 0;
        int FCount = 0;
        int OCount = 0;
        int NTotal = 0;
        int FTotal = 0;
        int OTotal = 0;
        char vote;
    std::ifstream input((filename).c_str());
    string countyName;
    if(input.is_open())
        {
        input >> countyName;
        countyNameVector.push_back(countyName);
        while(input >> vote)
                        {
                        if(vote == 'N' || vote == 'n')
                                {
                NCount = NCount + 1;
                }
                                else if(vote == 'F' || vote == 'f')
                                        {
                                        FCount = FCount + 1;
                                        }
                                        else
                                                {
                                                OCount = OCount + 1;
                                                }
                        }
            countyNCount.push_back(NCount);
            countyFCount.push_back(FCount);
            countyOCount.push_back(OCount);
        }
        cout << countyName << endl;
}

void add_county_election_file()
{
  string fn = ReadInputString("Enter the county file to process: ");
  add_county_election_file(fn,g_countyNameVector,g_countyNCount,g_countyFCount,g_countyOCount);
}