我是c ++课程的学生。我需要帮助完成作业。
"Read from a text file called salaries.txt that contains the following format:
M321232.34
F43234.34
M23432.23
M191929.34
字母“M”和“F”代表性别,数字代表 他们的薪水。每次读入时都计算男性的数量,和 将每个男性工资添加到累计的totalMaleSalary。做同样的事情 女性。在循环结束时计算女性的平均工资 和男性平均工资 - 显示您的结果,并确定 两个平均值中哪一个更大。“
我如何计算男性人数并加上每个薪水?
这就是我所拥有的:
int main(){
int male=0, female=0;
double salary,totalMaleSalary=0,totalFemaleSalary=0;
char gender;
ifstream fin;
fin.open("salary.txt");
do{
fin>>gender>>salary;
if(gender=='M'){
male=male+1;
totalMaleSalary=salary+totalMaleSalary;
}
if(gender=='F'){
female=female+1;
totalFemaleSalary=salary+totalFemaleSalary;
}
cout<<"Number of Males is "<<male<<endl;
cout<<"Number of Females is "<<female<<endl;
cout<<"Total Male Salary is "<<totalMaleSalary<<endl;
cout<<"Total Female Salary is "<<totalFemaleSalary<<endl;
cout<<"Average Male salary is "<<totalMaleSalary/male<<endl;
cout<<"Average Female salary is "<<totalMaleSalary/female<<endl;
}while(!fin.eof());
fin.close();
return 0;
}
答案 0 :(得分:0)
我假设问题是在使用方法时字符没有与整数分开读取:fin>>gender>>salary;
一个简单的解决方案是同时使用<string>
和<sstream>
类。
而不是:
fin>>gender>>salary;
有两个新变量:
std::string input;
和std::stringstream stream;
并使用此代码段读取文件中的数据:
fin >> input;gender = input[0];
for (int character{1}; character < input.length(); character++) { stream.put(input[character]); } stream >> salary;
字符串流基本上像iostream或filestream一样,但它不执行任何辅助功能,因此效率更高。
它使您能够将数据放回流中,并在删除开头的字符后将其读取为纯整数。
答案 1 :(得分:0)
您正在打开文件,但“do”只会读取一次数据,最后它会转到while并读取其余数据直到文件末尾,避免这样做。 “做”
1)逐行读取文件中的数据。
例如:(第1行)M321232.34
2)为“M”定义一个char。
3)将读取的行存储在缓冲区中并找到该行中的char。
例:char * buf = malloc(10 + 1)M321232.34
4)一旦找到“M”或“F”,则计算它并增加其值。
例:“M”被发现countm ++;或“F”被发现countf ++;
5)如果找到“M”,则在另一个缓冲区中复制除“M”之外的数据并将其转换为整数并添加它。
例:发现M321232.34“M”,然后msum = msum + 34;
6)否则发现“F”复制除了“F”的数据在另一个缓冲区中并将其转换为整数并添加它。
例如:找到F321232.34“F”,然后fsum = fsum + 34;
7)最后做平均..你的程序准备好了。谢谢
答案 2 :(得分:0)
这是我的方法:
'M'
,则增加男性计数器,并将其工资加到男性工资总额中。当遇到字母'F'
时,女性也是如此。完整代码示例:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <numeric>
int main(void) {
std::ifstream infile("salaries.txt");
std::string line;
double male_salaries = 0.0;
int males_count = 0;
double female_salaries = 0.0;
int females_count = 0;
while (std::getline(infile, line)) {
std::cout << line << std::endl;
if(line[0] == 'M') {
male_salaries += std::stod(std::string(line.begin() + 1, line.end()));
males_count++;
} else if(line[0] == 'F') {
female_salaries += std::stod(std::string(line.begin() + 1, line.end()));
females_count++;
} else
std::cout << "Something went wrong with: " << line << std::endl;
}
double m_sal_avg = male_salaries / males_count;
double f_sal_avg = female_salaries / females_count;
if(m_sal_avg > f_sal_avg)
std::cout << "Male salary average is greaten than the one of the females.\n";
else if(m_sal_avg < f_sal_avg)
std::cout << "Female salary average is greaten than the one of the males.\n";
else
std::cout << "Averages are equal.\n";
return 0;
}
输出:
男性工资平均值比女性高。
附录:
这一行:
std::stod(std::string(line.begin() + 1, line.end()));
使用double stod (const string& str, size_t* idx = 0);
将字符串转换为double。
我们不能将"M321232.34"
作为参数传递,因为这将抛出无效的参数异常。我们需要从第二个字符到最后一个字符串(即仅限数字)。
出于这个原因,我们可以通过构造一个新的字符串来构造原始字符串的子字符串,从第二个字符到最后一个字符串,该字符串将从原始字符串+ 1的开头开始,并将在结束时结束。原始字符串,如下所示:
std::string(line.begin() + 1, line.end())
因此,这将给"321232.34"
std::stod()
,这是$(document).ready(function(){
$('#chat-scroll').animate({scrollTop: $('#chat-scroll')[0].scrollHeight}, 2000);
});
的一个很好的参数,允许函数成功返回参数字符串的双精度数。
答案 3 :(得分:0)
if ( !is )...
。 eof()
。在提取成功时读取:while ( is >>...
。cout <<...
必须在循环外。您的固定计划:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
std::ifstream is( "salary.txt" );
if ( !is )
return -1; // failed to open
int male = 0, female = 0;
double salary, totalMaleSalary = 0, totalFemaleSalary = 0;
char gender;
while ( is >> gender >> salary )
{
if ( gender == 'M' )
{
male = male + 1;
totalMaleSalary = salary + totalMaleSalary;
}
else if ( gender == 'F' )
{
female = female + 1;
totalFemaleSalary = salary + totalFemaleSalary;
}
else
return -2; // unknown
};
cout << "Number of Males is " << male << endl;
cout << "Number of Females is " << female << endl;
cout << "Total Male Salary is " << totalMaleSalary << endl;
cout << "Total Female Salary is " << totalFemaleSalary << endl;
cout << "Average Male salary is " << totalMaleSalary / male << endl;
cout << "Average Female salary is " << totalMaleSalary / female << endl;
return 0;
}