C++ C++;查找类型x'的和;数组中的价格是多少
我被困在这个程序中,我要计算所有类型的价格之和。 例如,福特汽车的总价格是\uu。数据从名为input.dat的文件中提取。 我一辈子都搞不清楚如何对某一车型的所有元素进行分组,将总和相加,然后将总和存储到数组carPriceSum中。我知道如何找到数组中连续元素的和。任何提示或例子都将不胜感激C++ C++;查找类型x'的和;数组中的价格是多少,c++,arrays,struct,enums,C++,Arrays,Struct,Enums,我被困在这个程序中,我要计算所有类型的价格之和。 例如,福特汽车的总价格是\uu。数据从名为input.dat的文件中提取。 我一辈子都搞不清楚如何对某一车型的所有元素进行分组,将总和相加,然后将总和存储到数组carPriceSum中。我知道如何找到数组中连续元素的和。任何提示或例子都将不胜感激 // carstats.cpp : Defines the entry point for the console application. // #include "stdafx.h" #i
// 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
//carstats.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
#包括
#包括
#包括//我理解这不是最佳做法
#包括
使用名称空间std;
枚举类型
{
河流浅水处
雪佛兰,
本田,
丰田
};
结构卡客户
{
字符串名;
字符串lastName;
双倍价格;
卡式卡式;
};
作废calcCarStats(CarCustomer arrcustomes[],整数计数,整数carCount[],双carPriceSum[])
{
for(int index=0;indexcarTypeInt;
arrcusters[count]。carType=(carType)carTypeInt;
计数++;
}
fin.close();
calcCarStats(Arrcusters、count、carCount、carPriceSum);
显示cartypecounts(carCount,carPriceSum);
_getch();
返回0;
}
//input.dat
乔·史密斯5999.99 0
玛丽·多伊23999.99 1
乔·格林1999.99 1
吉姆·史密斯4999.99 2
简·格林3999.99 0
马克·多伊9999.991
约翰·彼得斯7999.99 2
吉姆·格林8999.99 3
Mindy Doe 3999.99 2
珍妮特·格林6999.99 1
莫克·多伊2999.99 3
简·史密斯3999.99 3
约翰·罗伯茨15999.99 1
Mandy Doe 12999.99 0
珍妮特·史密斯6999.99 0
梅西能源14999.99 1
最简单的方法(但不是最有效的方法)是循环查找特定车型的客户,并累积您的总和。如果您将该代码放入函数中,则可以为每种车型调用该代码。我想您只需在carPriceSum[n]
中添加“正在运行”的总和:
更改calcCarStats
from
carCount[arrCustomers[index].carType]++;
carPriceSum[index] = arrCustomers[index].price;
// This is where I'm stuck
到
无关评论:
- 考虑更改读取的“eof()”循环:
CarCustomer current; int carTypeInt; while (fin >> current.firstName >> current.lastName >> current.price >> carTypeInt) { current.carType = (CarType)carTypeInt; arrCustomers[count] = current; count++; }
- 考虑在
功能中打印实际类型:显示
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; } }
ostream&operator您听说过使用调试器吗?还检查错误吗?注意:
是错误的,并且您不应该检查任何东西(每次提取)正在验证中。您需要为每种车型保留一个单独的总和…例如,将车型映射为累计总数,并在读取数据时更新。@WhozCraig,不仅如此,还更正了文件读取错误:while(!fin.eof())
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; } }
// 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); }