English 中文(简体)
我如何避免使用阵列?
原标题:How do I avoid using arrays?
  • 时间:2011-08-10 00:06:08
  •  标签:
  • c++

我的教授们希望我们不使用像这样的阵列或矢量来起草一份方案:

起草一项方案,利用计算和印刷在停车场停泊的每家 n的停车费。

停车率:

  • a parking garage charges a $5.00 minimum fee to park for up to five hours.
  • the garage charges an additional $0.50 per hour for each hour or part thereof in the excess of five hours
  • the maximum charge for any given 24hr period is $10.00. Assume that no car parks longer that 24 hours at a time.

You should enter the hours parked for each customer. Your program should print the results in a neat tabular format and should calculate and print the total of your receipts.

方案产出应如此:

car------Hours------Charge

1--------2.00--------$5.00 

2--------5.00--------$5.00

3--------5.30--------$5.50

etc.

total: 3---12.30----$15.50

我只设法达到这一目的:

include <iostream>
include <conio.h>
include <cmath>
include <iomanip>
using namespace std;
double calculate(double);
int main()
{
    double hours,charge;
    int finish;
    double sumhours;
    sumhours=0;
    finish=0;
    charge=0;
    int cars;
    cars=0;

    do
    {
        cout<<"Enter the number of hours the vehicle has been parked: "<<endl;
        cin>>hours;
        cars++;
        sumhours+=hours;
        finish=cin.get();
        if(hours>24)
        {
            cout<<"enter a time below 24hrs."<<endl;
            cars--; 
            sumhours=sumhours-hours;
        }
    }
        while(finish!=EOF);

        double total=calculate(hours);
        cout<<total<<": "<<(cars-1)<<": "<<sumhours;


    while(!_kbhit());
    return 0;
}

double calculate(double time)
{
    double calculate=0;
    double fees;

    if(time<=5)
        return 5;
     if(time>15)
        return 10;

     time=ceil(time);
     fees=5+(.5*(time-5));

    return calculate;

}
最佳回答

每一种迭代都产生相关产出,但没有将其流到<条码>上:cout/code>。 相反,将其移至std:stringstream 之后,在结尾处对<代码>表示反对:cout。 数学只能靠保持对投入价值的不断积累来实现。

This, of course, assumes that using a std::stringstream is not considered "cheating" in the context of this homework exercise.

问题回答

Since this is homework, here is an algorithm:
1. Print header.
2. Clear running total variables.
3. While not end of file
3.1 read a record.
3.2 print record contents
3.3 add record field values to running total variables (Hint! Hint!)
3.4. end-while
4. print out running total variables.

您可能需要与running Total变量做一些额外的计算,特别是平均数。

www.un.org/Depts/DGACM/index_spanish.htm Edit 1: Example of aîd alltable

int sum = 0; // This is the running total variable.
const unsigned int QUANTITY = 23;
for (unsigned int i = 0; i < QUANTITY; ++i)
{
    cout << "Adding " << i << " to sum.
";
    sum += i;
}
cout << "Sum is: " << sum << "
";
cout.flush();

In this example, the data i is not stored only used. The sum variable is a running total. Look for similarities in your assignment.

www.un.org/Depts/DGACM/index_spanish.htm Edit 2: Exampleing end of submissions on cin

char reply =  n ;
while (tolower(reply) !=  y )
{
   cout << "Do you want to quit? (y/n)";
   cout.flush();
   cin >> reply;
   cin.ignore(1000,  
 ); // Eat up newline.
}
cout << "Thanks for the answer.
";
cout.flush();

由于你可以使用阵列或病媒,我认为,在处理每辆汽车时,你应当印制停车数据。 Pseudocode:

While more cars:
    Read data for next car
    Calculate cost
    Print data
    Add to running totals
End while
Print totals

You can try storing your values in a linked list structure instead of an array. Linked lists work great for dynamic storage. Try this tutorial, http://www.cprogramming.com/tutorial/lesson15.html

我当时建议采用一种宽松的方法,这种方法首先接受投入,询问是否有更多的投入。 如果有更多的投入,那么它就自称。 如果没有更多的投入,它就将目前的汽车投入使用,然后将迄今在结构中增加的一笔款项返还。

这种方法的唯一问题是,它会生产出反投入的汽车,但如果没有一阵列或档案可以节约。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签