如何计算每次阅读炭的数量
How to count the number of a char each time you read it in?
我是C 类中的学生。我需要分配的帮助。
"Read from a text file called salaries.txt that contains the following format:
M321232.34
F43234.34
M23432.23
M191929.34
字母'm'和'f'代表性别,数字代表他们的薪水。每次阅读时都计算男性数量,并且将每个男性薪水加到累积的总乳液中。做同样的事情女性。在循环结束时计算平均女性工资和平均男性薪水 - 显示您的结果并确定两个平均值中的哪一个更大。"
我如何计算男性人数并添加每个薪水?
这就是我所拥有的:
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;
}
- 不要以为文件已成功打开。使用
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;
}
我假设问题是使用该方法时的字符没有与整数分开读取: fin>>gender>>salary;
一个简单的解决方案是同时使用<string>
和<sstream>
类。
而不是:
fin>>gender>>salary;
有两个新变量:
std::string input;
和std::stringstream stream;
并使用此代码段来读取文件中的数据:
fin>>输入;性别=输入[0];
for (int character{1}; character < input.length(); character++) { stream.put(input[character]); } stream >> salary;
字符串流基本上像iostream或FileStream一样,除了它没有执行任何侧面功能,因此更有效。
它使您可以将数据放回流中,并在删除字符后纯粹是整数。
您正在打开文件,但是" do"只会读取数据所以。"做"1)逐行读取文件。例如:(第一行)M321232.342)为" M"定义字符。3)将行读取在缓冲区中,然后在线路中找到字符。例如: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)最后一个平均..您的程序已经准备就绪。
这是我的方法:
- 使用双重变量存储男性薪水的总和,以及一个可存储男性薪金数量的INT变量。女性相同。
- 按行读取文件(检查我的示例)。
- 对于每一行,请检查第一个字母。如果是
'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);
将字符串转换为双重。
我们不能将"M321232.34"
作为参数传递,因为这会引发无效的参数异常。我们需要从第二个字符到最后(即仅数字)。
因此,我们可以通过构造一个新字符串来构造原始字符串的子字符串,从第二个字符到最后一个字符串,该字符串将从原始字符串 1的开始开始,并将在结束时结束原始字符串,像这样:
std::string(line.begin() + 1, line.end())
因此,这将给出"321232.34"
,这是std::stod()
的一个很好的参数,允许该函数成功返回参数字符串的双重数。
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 递归函数计算序列中的平方和(并输出过程)
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- 在计算中使用二的幂有多有利可图
- 如何计算文件中的"columns"数?
- 计算排序向量的向量中唯一值的计数
- 如何使用 std::累积在 C++ 中计算总和立方体
- 使用Qt C++计算类似Git的SHA1哈希
- OpenCV C++.快速计算混淆矩阵
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- C++如何计算用户输入的数字中的偶数位数
- 如何计算数据类型的范围,例如int
- 类似枚举的计算常量
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 计算缩放多边形的比例,得到给定的多边形面积
- 在C++中如何在没有pow的情况下进行基础计算
- 计算平均值,不包括上次得分
- C++ openmp 并行计算计算错误的结果