【问题标题】:C++ file handling and calculationsC++ 文件处理和计算
【发布时间】:2010-04-11 13:32:55
【问题描述】:

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

我的讲师给了我任务:

您所在的公司会收到一份文本格式的月度报告。该报告包含 以下信息。

  • 部门名称
  • 部门主管姓名
  • 月
  • 当月最低消费
  • 当月最高消费

您的程序是从用户那里获取输入文件的名称。实现一个结构来表示数据:

文件读入程序后,为用户打印以下统计信息:

  • 按月列出每月最低支出的部门
  • 按月列出哪个部门的最低支出

将信息写入名为“MaxMin.txt”的文件中

然后做一个处理,使Department Name、Head of Department Name、Minimum 支出和最大支出根据月份写入单独的文件,例如一月、二月、 三月等。

这是给我的文本文件:

工程法案 2000 年 1 月 15000 IT 杰克 Jan 300 20000 HR 吉尔 Jan 1500 10000 工程账单 2 月 5000 45000 IT 杰克 2 月 4500 7000 HR 吉尔 2 月 5600 60000 工程法案 3 月 5000 45000 IT 杰克三月 4500 7000 HR 吉尔 3 月 5600 60000 工程账单 4 月 5000 45000 IT 杰克 四月 4500 7000 HR 吉尔 4 月 5600 60000 工程法案 2000 年 5 月 15000 IT 杰克 300 年 5 月 20000 HR 吉尔 5 月 1500 10000 工程法案 2000 年 6 月 15000 IT 杰克 Jun 300 20000 HR Jill Jun 1500 10000

这是我编写的 c++ 代码

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;}
}

【问题讨论】:

  • 尝试再次粘贴您的代码,然后将其缩进四个空格(使用按钮)。这可以防止 HTML 解析搞砸它。另外,请正确缩进代码本身。
  • 我帮助您对整个帖子进行格式化,使其具有可读性并符合 SO 约定。然后你几乎用你的编辑删除了它。那很愚蠢。我建议回滚到我的版本,然后添加您的更改。这将增加人们努力阅读您的帖子并为您提供帮助的机会。
  • 顺便说一句,从代码中删除无数注释行并正确缩进也是如此。
  • 请再试一次。在编辑器中将制表符转换为空格,复制+粘贴,然后单击代码按钮。另外,您的计划在 6 月之后会发生什么?
  • 抱歉,peter torok,我不小心这样做了,抱歉,我已经回滚了

标签: c++


【解决方案1】:

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

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);
}

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

首先让它工作,然后改进它。如果这是一门大学水平的课程,您的教授可能希望您想出一个比 O(n^2) 更好的解决方案。

【讨论】:

    【解决方案2】:

    我建议您的第一件事是提高程序的易读性。尝试添加将为您执行简单操作的函数,例如,将单行输入读取到结构中(顺便说一句,结构中的输入中还有一个字段)。

    在你需要的地方声明你的变量,预先声明你需要的一切是没有意义的,你甚至可以在你实际使用它的时候忘记它是什么。使用简化代码的数据结构(而不​​是 N min_month,max_month 变量只使用按月索引的最小值/最大值的向量;使用向量而不是数组;如果您发现需要查找表,请考虑使用映射...)

    但首先,用笔和纸来实现您的算法。我有一位教授曾经说过,在你知道如何用笔和纸实现一个过程之前,甚至考虑在计算机中实现它是没有意义的,因为它只会更快地失败并且更难诊断。

    【讨论】:

    • 顺便说一句:如果你能拿到 Accelerated C++ 的副本,我会开始阅读它...它有一些与你想要的类似的示例,并提供了一种惯用的方法
    猜你喜欢
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 2013-03-01
    相关资源
    最近更新 更多