吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3024|回复: 2
收起左侧

[其他转载] 吃完饭水一帖:百度知道网友C++题目解答

[复制链接]
yysniper 发表于 2015-4-18 17:41
该题目是百度知道网友发的,具体问题在http://zhidao.baidu.com/question/1047760868141148939。题目不是很难比较基础,我就写了下,吃完饭没事我就发上来水一下。
题目(英文的):
COSC 1410
Spring 2015
Assignment 9: C-Strings, String Objects, and Classes
[1] Objectives: The main purpose of this assignment is to make sure you are familiar with C-string,
String class, and classes in general. The old C-strings are being used a lot in the existing applications, so
it is important to know how it works. In addition, it is important that you know how to use functions to
structure your program.
[2] Description: In this assignment you are going to build a computer application for car dealerships.
This application needs to keep track of all the cars the dealership has at the moment. The user of the
application should be able display all cars, the cars with prices within a range, and all the cars made by a
give company.
a) Define a class for “Auto”, with the following members:
• make, array of character of size 20,
• model, a string,
• year, a 4-digeit number
• color, a string,
• price, double,
• setAll(string make, string model, string color , int year, double price);
• getPrice(), return the price of the car;
• getMake() return the make of the car;
• display() display all the information about the car;
b) Define another class called “Dealer”, with the following members:
• Cars, An array of Autos (up to 100 in size).
• noOfCars, The number of cars currently in the dealership.
• addCar(), to insert a new car to the array
• getNoOfCars(), return the number of cars stored in the array
• displayAll(), print all the cars
• displayBetween(double min, double max), display all the cars with prices between min and
max.
• displayByMake(char mk[]), display all the cars that has make value equal to mk.
Your main menu should look like: (This is the final menu that I used. The sample output used a slightly
different menu).
********** Class Deealership ******* Total cars = 9
1. Display all cars
2. Display all cars within price range
3. Display all cars with specific make
4. Exit
The difficulty to implement this program is the lack of "file input" at this time. So we wrote such a
function for your use. A template for this assignment will be provided. You don't have to understand the
detail steps of the function (yet) but have to know how to call it. This is probably a good exercise for you
to go through such a practice at this stage. Keep in mind that this is my way of getting around the fileinput issue. This may not be the best way to do the work once we get to Ch. 12. As much as I hate it, I
have to put one variable at the global level to simplify the code. There is no requirement to keep the
inventory in any particular order. If you have extra time, you may try to sort them in increasing order of
the Make (for your personal satisfaction).
[3] Input: Menu-driven interactive. A test data will be stored in a file called "prog9in.txt" for you to test
the program. Each car record is stored in a separate line. Within a line, all fields are separated by one or
more blanks. The function getRecord() provided will return all five fields one at a time. It will also return
true or false depending on whether there is more data in the file. Because you have to define the auto
classes, it is not possible for the function to return an auto object.
[4] Output: A sample output is shown below. First name and last name should be put together properly.
File "prog9in.txt" opened.
Make Model Color Year Price
---------------------------------------------
Toyota Camry red 2002 4000
Ford Taurus green 2004 3500
Nissan Altima grey 2009 7000
Ford Focus white 2001 2000
mercedes C-Class black 2010 20000
Toyota Yaris blue 2007 6500
Nissan sentra white 2014 15000
Toyota corolla green 2007 7000
Honda civic white 2006 6500
********** Class Dealer ******* Total cars = 9
1. Display all cars
2. Display all cars within price range
3. Display all cars with specific make
4. Exit
>> 2
Enter the minimum amount you want to spend: 4000
Enter the maximum amount you can pay: 7000
Make Model Color Year Price
---------------------------------------------
Toyota Camry red 2002 4000
Nissan Altima grey 2009 7000
Toyota Yaris blue 2007 6500
Toyota corolla green 2007 7000
Honda civic white 2006 6500
********** Class Dealer ******* Total cars = 9
1. 1. Display all cars
2. Display all cars within price range
3. Display all cars with specific make
4. Exit
>> 3
Enter the make you are looking for : Toyota
Make Model Color Year Price ---------------------------------------------
Toyota Camry red 2002 4000
Toyota Yaris blue 2007 6500
Toyota corolla green 2007 7000
********** Class Dealer ******* Total cars = 9
1. Display all cars
2. Display all cars within price range
3. Display all cars with specific make
4. Exit
>>
[5] Restrictions and Suggestions:
• Must define the two classes mentioned.
• The user input can be in either case.
• Since this is the first exercise that you are using strings, you are required to use both
strings. For the auto and dealership classes, use string objects. In the getRecord(), string
should be implemented as C-strings.
• Do not use global variables other than the one I listed and constants.
• Your main function should consist of primarily function calls. To make sure you are using
functions properly.
[6] Due Date: Wednesday, April 15, 2015.


发源码:
首先main函数:
[C++] 纯文本查看 复制代码
#include <iostream>
#include <iomanip>
#include "auto.h"
#include "dealer.h"

using namespace std;

int main(int argc, char** argv) {
    Auto *a[9];
    for (int i=0;i<9;i++)
        a[i]=new Auto();
    a[0]->setAll("Toyota","Camry","red",2002,4000);
    a[1]->setAll("Ford","Taurus","green",2004,3500);
    a[2]->setAll("Nissan","Altima","grey",2009, 7000);
    a[3]->setAll("Ford", "Focus","white", 2001, 2000);
    a[4]->setAll("mercedes", "C-Class", "black", 2010, 20000);
    a[5]->setAll("Toyota", "Yaris", "blue", 2007, 6500);
    a[6]->setAll("Nissan", "sentra", "white", 2014, 15000);
    a[7]->setAll("Toyota", "corolla", "green", 2007, 7000);
    a[8]->setAll("Honda", "civic", "white", 2006, 6500);

    Dealer *dealer=new Dealer();
    for(int i=0;i<9;i++)
    {
        dealer->addCar(a[i]);
    }

    int menunum;
    while(1)
    {
        cout<<"\
********** Class Dealer ******* Total cars = 9\n\
1. Display all cars\n\
2. Display all cars within price range\n\
3. Display all cars with specific make\n\
4. Exit\n\
>>";
        cin>>menunum;
        switch(menunum)
        {
            case 1:
                dealer->displayAll();
                break;
            case 2:
                int min,max;
                cout<<"Enter the minimum amount you want to spend: ";
                cin>>min;
                cout<<"Enter the maximum amount you can pay: ";
                cin>>max;
                dealer->displayBetween(min,max);
                break;
            case 3:
                char tmp[20];
                cout<<"Enter the make you are looking for : ";
                cin>>tmp;
                dealer->displayByMake(tmp);
                break;
            case 4:
                return 0;
        }
    }
    return 0;
}


类Auto的头文件:
[C++] 纯文本查看 复制代码
#ifndef AUTO_H
#define AUTO_H
#include <string>
using namespace std;
class Auto
{
	public:
		Auto();
	private:
		char m_make[20];
		string m_model;
		int m_year;
		string m_color;
		double m_price;
	public:
		void setAll(string make, string model, string color , int year, double price);
		double getPrice();
		char* getMake();
		void display();
};

#endif

类Auto的源文件:
[C++] 纯文本查看 复制代码
#include "auto.h"
#include <string>
#include <cstring>
#include "auto.h"
#include <iostream>
#include <iomanip>
using namespace std;

Auto::Auto()
{
}
void Auto::setAll(string make, string model, string color , int year, double price)
{
	for(int i=0;i<=strlen(make.c_str());i++)
	{
		m_make[i]=make[i];
	}
	
	m_model=model;
	m_year=year;
	m_color=color;
	m_price=price;
}

char* Auto::getMake()
{
	return m_make;
}

double Auto::getPrice()
{
	return m_price;
}

void Auto::display()
{
	cout<<setiosflags(ios::right)<<setw(10)<<m_make<<setw(10)<<m_model<<setw(10)<<m_color<<setw(10)<<m_year<<setw(10)<<m_price<<endl;
}


类Dealer头文件:
[C++] 纯文本查看 复制代码
#ifndef DEALER_H
#define DEALER_H
#include "auto.h"
class Dealer
{
	public:
		Dealer();
	private:
		Auto* Cars[100];
		int noOfCars;
	public:	
		void addCar(Auto* car);
		int getNoOfCars();
		void displayAll();
		void displayBetween(double min, double max);
		void displayByMake(char mk[]);
};

#endif


类Dealer源文件:
[C++] 纯文本查看 复制代码
#include "dealer.h"
#include <string>
#include <cstring>
#include <iostream>
#include <iomanip>

using namespace std;

Dealer::Dealer()
{
	noOfCars=0;
    for(int i=0;i<100;i++)
    {
        Cars[i]=NULL;
    }
}
void Dealer::addCar(Auto* car)
{
	for(int i=0;i<100;i++)
	{
        if(Cars[i]==NULL)
		{
			Cars[i]=car;
			break;
		}
	}
}

int Dealer::getNoOfCars()
{
	for(int i=0;i<100;i++)
	{
        if(Cars[i]==NULL)
		{
			return i;
		}
	}
}

void Dealer::displayAll()
{
	cout<<setiosflags(ios::right)<<setw(10)<<"Make"<<setw(10)<<"Model"<<setw(10)<<"Color"<<setw(10)<<"Year"<<setw(10)<<"Price"<<endl;
	cout<<"-------------------------------------------------------"<<endl;
	for (int i=0;i<100;i++)
	{

        if(Cars[i]==NULL)
        {
            break;
        }
        else
        {
			Cars[i]->display();
        }
	}
}

void Dealer::displayBetween(double min, double max)
{
	cout<<setiosflags(ios::right)<<setw(10)<<"Make"<<setw(10)<<"Model"<<setw(10)<<"Color"<<setw(10)<<"Year"<<setw(10)<<"Price"<<endl;
	cout<<"-------------------------------------------------------"<<endl;
	for (int i=0;i<100;i++)
	{
        if(Cars[i]!=NULL)
        {
            if(Cars[i]->getPrice()<max && Cars[i]->getPrice()>min)
                Cars[i]->display();
        }
        else
        {
            break;
        }
		
	}
}

void Dealer::displayByMake(char mk[])
{
	cout<<setiosflags(ios::right)<<setw(10)<<"Make"<<setw(10)<<"Model"<<setw(10)<<"Color"<<setw(10)<<"Year"<<setw(10)<<"Price"<<endl;
	cout<<"-------------------------------------------------------"<<endl;
	for (int i=0;i<100;i++)
	{
        if(Cars[i]!=NULL)
        {
            string s1(Cars[i]->getMake());
            string s2(mk);
            if(s1==s2)
                Cars[i]->display();
        }
        else
        {
            break;
        }
		
	}
}



发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

常黑屏 发表于 2015-4-18 17:54
纳尼。。。。压根看不懂的说。。。。怎么整
苏紫方璇 发表于 2015-4-18 23:09
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-14 18:49

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表