gpt4 book ai didi

C++ 文件处理和计算

转载 作者:行者123 更新时间:2023-11-28 03:58:00 24 4
gpt4 key购买 nike

我的程序快要完成了,但我有一个问题似乎无法解决。我如何获得最低消费?

我的讲师给我的任务是:

The company you work at receives a monthly report in a text format. The report contains the following information.

  • Department Name
  • Head of Department Name
  • Month
  • Minimum spending of the month
  • Maximum spending of the month

Your program is to obtain the name of the input file from the user. Implement a structure to represent the data:

Once the file has been read into your program, print out the following statistics for the user:

  • List which department has the minimum spending per month by month
  • List which department has the minimum spending by month by month

Write the information into a file called “MaxMin.txt”

Then do a processing so that the Department Name, Head of Department Name, Minimum spending and Maximum spending are written to separate files based on the month, eg Jan, Feb, March and so on.

这是我要使用的文本文件:

Engineering Bill Jan 2000 15000IT  Jack Jan 300 20000HR  Jill Jan 1500 10000Engineering  Bill Feb 5000 45000IT  Jack Feb 4500 7000HR  Jill Feb 5600 60000Engineering  Bill Mar 5000 45000IT  Jack Mar 4500 7000HR  Jill Mar 5600 60000Engineering  Bill Apr 5000 45000IT  Jack Apr 4500 7000HR  Jill Apr 5600 60000Engineering Bill May 2000 15000IT  Jack May 300 20000HR  Jill May 1500 10000Engineering Bill Jun 2000 15000IT  Jack Jun 300 20000HR  Jill Jun 1500    10000

and here's the c++ code I've written

ue#include <iostream>
#include <fstring>
#include <string>

using namespace std;

struct Record
{
string depName;
string head;
string month;
float max;
float min;
string name;
}
myRecord[19];

int main ()
{
string line;
ofstream minmax,jan,feb,mar,apr,may,jun;
char a[50];
char b[50];
int i = 0,j,k;
float temp;
//float maxjan=myRecord[0].max,maxfeb=myRecord[0].max,maxmar=myRecord[0].max,maxapr=myRecord[0].max,maxmay=myRecord[0].max,maxjune=myRecord[0].max;
float minjan=myRecord[1].min,minfeb=myRecord[1].min,minmar=myRecord[1].min,minapr=myRecord[1].min,minmay=myRecord[1].min,minjune=myRecord[1].min;
float maxjan=0,maxfeb=0,maxmar=0,maxapr=0,maxmay=0,maxjune=0;
//float minjan=0,minfeb=0,minmar=0,minapr=0,minmay=0,minjune=0;
string maxjanDep,maxfebDep,maxmarDep,maxaprDep,maxmayDep,maxjunDep;
string minjanDep,minfebDep,minmarDep,minaprDep,minmayDep,minjunDep;
cout<<"Enter file name: ";
cin>>a;
ifstream myfile (a);
//minmax.open ("MaxMin.txt");

if (myfile.is_open()){
while (! myfile.eof()){
myfile>>myRecord[i].depName>>myRecord[i].head>>myRecord[i].month>>myRecord[i].min>>myRecord[i].max;
cout << myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl;
i++;
}
myfile.close();
}
else{
cout << "Unable to open file"<<endl;}

cout<<"Enter file name: ";
cin>>b;
ifstream myfile1 (b);
minmax.open ("MaxMin.txt");
jan.open ("Jan.txt");
feb.open ("Feb.txt");
mar.open ("March.txt");
apr.open ("April.txt");
may.open ("May.txt");
jun.open ("Jun.txt");
if (myfile1.is_open()){
while (! myfile1.eof()){
myfile1>>myRecord[i].depName>>myRecord[i].head>>myRecord[i].month>>myRecord[i].min>>myRecord[i].max;
if (myRecord[i].month == "Jan"){
jan<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl;

if (maxjan< myRecord[i].max){
maxjan=myRecord[i].max;
maxjanDep=myRecord[i].depName;}

for (k=1;k<=3;k++){
for (j=0;j<2;j++){
if (myRecord[j].min>myRecord[j+1].min){
temp=myRecord[j].min;
myRecord[j].min=myRecord[j+1].min;
myRecord[j+1].min=temp;
minjanDep=myRecord[j].depName;
}}}
}
if (myRecord[i].month == "Feb"){
feb<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl;
if (maxfeb< myRecord[i].max){
maxfeb=myRecord[i].max;
maxfebDep=myRecord[i].depName;}

for (k=1;k<=3;k++){
for (j=0;j<2;j++){
if (myRecord[j].min>myRecord[j+1].min){
temp=myRecord[j].min;
myRecord[j].min=myRecord[j+1].min;
myRecord[j+1].min=temp;
minfebDep=myRecord[j+1].depName;
}}}

}

if (myRecord[i].month == "Mar"){
mar<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl;
if (maxmar< myRecord[i].max){
maxmar=myRecord[i].max;
maxmarDep=myRecord[i].depName;}
if (myRecord[i].min<minmar){
minmar=myRecord[i].min;
minmarDep=myRecord[i].depName;
}}

if (myRecord[i].month == "Apr"){
apr<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl;
if (maxapr< myRecord[i].max){
maxapr=myRecord[i].max;
maxaprDep=myRecord[i].depName;}

if (minapr>myRecord[i].min){
minapr=myRecord[i].min;
minaprDep=myRecord[i].min;}
}

if (myRecord[i].month == "May"){
may<<myRecord[i].depName<<myRecord[i].head<<myRecord[i].month<<myRecord[i].min<<myRecord[i].max<<endl;
if (maxmay< myRecord[i].max){
maxmay=myRecord[i].max;
maxmayDep=myRecord[i].depName;}

if (minmay>myRecord[i].min){
minmay=myRecord[i].min;
minmayDep=myRecord[i].depName;}
}

if (myRecord[i].month == "Jun"){
jun<< myRecord[i].depName<<"\t"<<myRecord[i].head<<"\t"<<myRecord[i].month<<"\t"<<myRecord[i].min<<"\t"<<myRecord[i].max<<endl;
if (maxjune< myRecord[i].max){
maxjune=myRecord[i].max;
maxjunDep=myRecord[i].depName;}

if (minjune>myRecord[i].min){
minjune=myRecord[i].min;
minjunDep=myRecord[i].depName;}
}

i++;

myfile.close();
}
minmax<<"department that has maximum spending at jan "<<maxjanDep<<endl;
minmax<<"department that has minimum spending at jan "<<minjanDep<<endl;
minmax<<"department that has maximum spending at Feb "<<maxfebDep<<endl;
minmax<<"department that has minimum spending at Feb "<<minfebDep<<endl;
minmax<<"department that has maximum spending at March "<<maxmarDep<<endl;
minmax<<"department that has minimum spending at March "<<minmarDep<<endl;
minmax<<"department that has maximum spending at April "<<maxaprDep<<endl;
minmax<<"department that has minimum spending at April "<<minaprDep<<endl;
minmax<<"department that has maximum spending at May "<<maxmayDep<<endl;
minmax<<"department that has minimum spending at May "<<minmayDep<<endl;
minmax<<"department that has maximum spending at June "<<maxjunDep<<endl;
minmax<<"department that has minimum spending at June "<<minjunDep<<endl;
}

else{
cout << "Unable to open file"<<endl;}
}

最佳答案

我的第一个建议是教授想要一个记录列表 - 而不是在 19 中进行硬编码,而是使用一个 vector 并在您进行时填充它。

vector<Record> records;

我的第二个建议是使用一个循环来处理每个月。

vector<string> months;
months.push_back("Jan");
months.push_back("Feb");

for (int i = 0; i < months.size(); ++i) {
string month = months[i];
ReadInMonthBudget(&records, month); // Then use a function to read in each month file.
}

然后您可以收集您的统计数据:

for (int i = 0; i < months.size(); ++i) {
string month = months[i];
cout << "Min spender in month " << month << ": " << FindMinSpenderInMonth(records, month);
}

使用函数将使您的代码更易于理解(和编写),因为它将问题分解为更容易解决的更小的 block 。

先让它发挥作用,然后再改进它。如果这是大学水平的类(class),您的教授可能希望您提出一个比 O(n^2) 更好的解决方案。

关于C++ 文件处理和计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2617149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com