如何计算每次阅读炭的数量

How to count the number of a char each time you read it in?

本文关键字:何计算 计算      更新时间:2023-10-16

我是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;
}
  1. 不要以为文件已成功打开。使用if ( !is )...
  2. 不要使用eof()。在提取成功时阅读:while ( is >>...
  3. 在读取整个文件后,必须显示结果: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)最后一个平均..您的程序已经准备就绪。

这是我的方法:

  1. 使用双重变量存储男性薪水的总和,以及一个可存储男性薪金数量的INT变量。女性相同。
  2. 按行读取文件(检查我的示例)。
  3. 对于每一行,请检查第一个字母。如果是'M',请增加男性计数器,并将其工资加到男性总薪水中。女性相同,当您遇到字母'F'
  4. 通过计数器潜水总和计算男性薪水的平均值。女性相同。
  5. 比较平均和打印消息。

完整的代码示例:

#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()的一个很好的参数,允许该函数成功返回参数字符串的双重数。