我觉得我放入结构中的输入代码可以压缩,关于如何在保持代码简短的同时保持数据个性化的任何建议?

I feel like the input code I put into my struct can be condensed, any suggestions on how to keep the data individual while keeping the code short?

本文关键字:代码 任何建 个性化 数据 结构 输入 压缩 于如何      更新时间:2023-10-16

我一直在试图弄清楚如何将信息输入到我的结构中,但我想压缩代码。

#include <iostream>
using namespace std;
struct Employee //The whole program is going to revolve around this struct
{
char first[10], last[10];
float Hours_Worked, Hourly_Rate, Fed_Tax_Rate, St_Tax_Rate;
}Kobe, Lebron, Larry, Michael; //Struct declarations

这里的代码就是我所说的代码。我的首选设计是使用循环 4 次的 for 循环,但我需要个人信息。

int main()
{
Employee Kobe;
cout << "First name: ";
cin >> Kobe.first;
cout << "Last name: ";
cin >> Kobe.last;
cout << "Hours worked: ";
cin >> Kobe.Hours_Worked;
cout << "Federal Tax Rate: ";
cin >> Kobe.Fed_Tax_Rate;
cout << "State Tax Rate: ";
cin >> Kobe.St_Tax_Rate;
Employee Lebron;
cout << "First name: ";
cin >> Lebron.first;
cout << "Last name: ";
cin >> Lebron.last;
cout << "Hours worked: ";
cin >> Lebron.Hours_Worked;
cout << "Federal Tax Rate: ";
cin >> Lebron.Fed_Tax_Rate;
cout << "State Tax Rate: ";
cin >> Lebron.St_Tax_Rate;
Employee Larry;
cout << "First name: ";
cin >> Larry.first;
cout << "Last name: ";
cin >> Larry.last;
cout << "Hours worked: ";
cin >> Larry.Hours_Worked;
cout << "Federal Tax Rate: ";
cin >> Larry.Fed_Tax_Rate;
cout << "State Tax Rate: ";
cin >> Larry.St_Tax_Rate;
Employee Michael;
cout << "First name: ";
cin >> Michael.first;
cout << "Last name: ";
cin >> Michael.last;
cout << "Hours worked: ";
cin >> Michael.Hours_Worked;
cout << "Federal Tax Rate: ";
cin >> Michael.Fed_Tax_Rate;
cout << "State Tax Rate: ";
cin >> Michael.St_Tax_Rate;

return 0;
}

使用输入方法定义一个Employee以获取输入

struct Employee 
{
char first[10], last[10];
float Hours_Worked, Hourly_Rate, Fed_Tax_Rate, St_Tax_Rate;
bool getinput(std::istream & in, 
std::ostream & out);
};

然后实现此方法

bool Employee::getinput(std::istream & in, 
std::ostream & out);
{
out << "First name: ";
in >> first;
out << "Last name: ";
in >> last;
out << "Hours worked: ";
in >> Hours_Worked;
out << "Federal Tax Rate: ";
in >> Fed_Tax_Rate;
out << "State Tax Rate: ";
in >> St_Tax_Rate;
return in.good(); //always good to know if the input succeeded
}

然后调用该方法

Employee Kobe;
Kobe.getinput(cin, cout);
Employee Lebron;
Lebron.getinput(cin, cout);
Employee Larry;
Larry.getinput(cin, cout);
Employee Michael;
Michael.getinput(cin, cout);

cincout以抽象形式传入,以便您可以在不同的输入流(例如网络套接字(上调用getinput

使用Employee数组和for循环来避免编写重复的代码:

#include <iostream>
using namespace std;
struct Employee
{
char first[10];
char last[10];
float Hours_Worked;
float Hourly_Rate;
float Fed_Tax_Rate;
float St_Tax_Rate;
};
int main()
{
Employee employees[4];
for (auto &e : employees) {
cout << "First name: ";
cin >> e.first;
cout << "Last name: ";
cin >> e.last;
cout << "Hours worked: ";
cin >> e.Hours_Worked;
cout << "Hourly rate: ";
cin >> e.Hourly_Rate;
cout << "Federal Tax Rate: ";
cin >> e.Fed_Tax_Rate;
cout << "State Tax Rate: ";
cin >> e.St_Tax_Rate;
}
for (auto const &e : employees)
cout << e.first << ' ' << e.last << 'n' << e.Hours_Worked << " / "
<< e.Hourly_Rate << " / " << e.Fed_Tax_Rate << " / " << e.St_Tax_Rate << "nn";
}

如果您(还(不熟悉基于范围的for循环,您当然可以使用传统的循环,例如

for(size_t i{}; i < sizeof(employees)/sizeof(*employees); ++i)
// and access eployees[i] in the loops body.
相关文章: