C++ 在数组中查找 x 类型价格的总和
C++ Find the sum of type x's price in the array
我被困在这个程序的点上,我要计算所有 CarType 的价格总和。例如。福特的价格总和是__。数据是从名为input.dat的文件中提取的。我一生都无法弄清楚如何将某种汽车类型的所有元素分组并将总和相加,然后将总和存储到数组 carPriceSum 中。我了解如何在数组中找到连续元素的总和。任何提示或示例将不胜感激!
// carstats.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <conio.h> // I understand this is not best practice
#include <fstream>
using namespace std;
enum CarType
{
Ford,
Chevy,
Honda,
Toyota
};
struct CarCustomer
{
string firstName;
string lastName;
double price;
CarType carType;
};
void calcCarStats(CarCustomer arrCustomers[], int count, int carCount[], double carPriceSum[])
{
for(int index = 0; index < count; index++)
{
carCount[arrCustomers[index].carType]++;
carPriceSum[index] = arrCustomers[index].price;
// This is where I'm stuck
}
}
void displayCarTypeCounts(int carCount[], double carPriceSum[])
{
for(int index = Ford; index <= Toyota; index++)
{
cout << carCount[index] << " " << carPriceSum[index] << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int count = 0;
CarCustomer arrCustomers[100]; //Array of structs for the Struct CarCustomer
CarCustomer carCustomer;
int carCount[100] = {0};
double carPriceSum[100] = {0.0};
double carPriceAvg[100] = {0.0};
ifstream fin;
CarType carType; //CarType enum
fin.open("input.dat");
if(!fin)
{
cout << "Error opening file, check the file name" << endl;
_getch();
return -1;
}
while (!fin.eof())
{
int carTypeInt;
fin >> arrCustomers[count].firstName;
fin >> arrCustomers[count].lastName;
fin >> arrCustomers[count].price;
fin >> carTypeInt;
arrCustomers[count].carType = (CarType)carTypeInt;
count++;
}
fin.close();
calcCarStats(arrCustomers, count, carCount, carPriceSum);
displayCarTypeCounts(carCount, carPriceSum);
_getch();
return 0;
}
//input.dat
Joe Smith 5999.99 0
Mary Doe 23999.99 1
Joe Green 1999.99 1
Jim Smith 4999.99 2
Jane Green 3999.99 0
Mark Doe 9999.99 1
John Peters 7999.99 2
Jim Green 8999.99 3
Mindy Doe 3999.99 2
Janet Green 6999.99 1
Mork Doe 2999.99 3
Jane Smith 3999.99 3
John Roberts 15999.99 1
Mandy Doe 12999.99 0
Janet Smith 6999.99 0
Macy Doe 14999.99 1
我认为您只想在carPriceSum[n]
中添加"运行"总和:
将calcCarStats
从
carCount[arrCustomers[index].carType]++;
carPriceSum[index] = arrCustomers[index].price;
// This is where I'm stuck
自
CarType type = arrCustomers[index].carType;
carCount[type]++;
carPriceSum[type] += arrCustomers[index].price;
不相关的评论:
考虑更改读取 'eof()' 循环:
CarCustomer current; int carTypeInt; while (fin >> current.firstName >> current.lastName >> current.price >> carTypeInt) { current.carType = (CarType)carTypeInt; arrCustomers[count] = current; count++; }
考虑在
display
函数中打印实际的车型:ostream& operator<<(ostream& os, CarType ct) { switch(ct) { case Ford: return os << "Ford"; case Chevy: return os << "Chevy"; case Honda: return os << "Honda"; case Toyota: return os << "Toyota"; } return os << "Unknown"; } void displayCarTypeCounts(int carCount[], double carPriceSum[]) { for(int index = Ford; index <= Toyota; index++) { cout << (CarType)index << " " << carCount[index] << " " << carPriceSum[index] << endl; } }
考虑使用标准库容器(地图、矢量):观看演示 在 Coliru 上直播
// carstats.cpp : Defines the entry point for the console application. // #include <iostream> #include <iomanip> #include <string> #include <vector> #include <map> #include <fstream> using namespace std; enum CarType { Ford, Chevy, Honda, Toyota }; struct Stats { unsigned units_sold; double total_turnover; Stats() : units_sold(0), total_turnover(0.0) {} }; typedef map<CarType, Stats> Statistics; ostream& operator<<(ostream& os, CarType ct) { switch(ct) { case Ford: return os << "Ford"; case Chevy: return os << "Chevy"; case Honda: return os << "Honda"; case Toyota: return os << "Toyota"; } return os << "Unknown"; } struct CarCustomer { string firstName; string lastName; double price; CarType carType; }; Statistics calcCarStats(vector<CarCustomer> const& arrCustomers) { Statistics stats; for (auto& customer : arrCustomers) { auto& entry = stats[customer.carType]; entry.units_sold += 1; entry.total_turnover += customer.price; } return stats; } void displayCarTypeCounts(Statistics const& stats) { for (auto& entry: stats) cout << (CarType)entry.first << " " << entry.second.units_sold << " " << entry.second.total_turnover << endl; } int main() { vector<CarCustomer> arrCustomers; ifstream fin("input.dat"); if(!fin) { cout << "Error opening file, check the file name" << endl; return -1; } CarCustomer current; int carTypeInt; while (fin >> current.firstName >> current.lastName >> current.price >> carTypeInt) { current.carType = (CarType)carTypeInt; arrCustomers.push_back(current); } fin.close(); auto stats = calcCarStats(arrCustomers); displayCarTypeCounts(stats); }
最简单的方法(但不是最有效的)是遍历寻找特定车型的客户并累积您的总和。如果将该代码放在函数中,则可以为每种汽车类型调用它。
相关文章:
- C++默认情况下,指针类型数组的元素是否保证初始化为 nullptr?
- 类型数组?
- 如何在使用 make_unique<T[]>() 制作的模板类型数组上使用 std::fill?
- 将基本类型数组中的内存重新用于不同(但仍然是基本)类型的数组合法吗
- 在C++中返回基元类型数组(Java比较)
- 如何返回 QVariant 类型数组
- 如何在本机 c++ jni 中实现 java 类型数组
- 模板类型数组的模板类型数组
- 如何确定未知类型数组的索引是否为空
- 复杂类型数组的静态初始化
- 访问基类型数组成员(Int-to-type习惯用法)
- 如何将 char 类型数组传递给函数并将第一个字符数组复制到第二个图表数组并使用 c++ 进行比较
- 如何在C 中制作结构类型数组
- 如何创建结构类型数组的地图对象
- C++类类型数组
- C++中的Matlab类型数组
- 节点C++插件 - 当类型数组(Float32Array)作为参数传递时,如何访问它?
- 如何将二维字符类型数组逐行保存到 c 中的.txt文件中
- C++:用户定义的类型数组出错
- 在双精度类型数组中使用 memset()