【发布时间】:2013-10-22 02:28:09
【问题描述】:
我正在学习 C++,我需要创建结构 Airplane 并使用它。
我的结构Airplane.h
#include "stdafx.h"
using namespace std;
struct Airplane {
string destination;
int number;
string type;
};
这是我的代码
#include "stdafx.h"
#include "Airplane.h"
string SetDestination(int n);
string SetType(int n);
void PrintAirplaneList(Airplane * &airplaneList, int n, string title);
void SortByDestination (Airplane *&airplaneList, int n);
void FindAirplanesAndPrint(Airplane *&airplaneList, int n, string type);
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
srand((unsigned)time(NULL));
int n;
cout << "Input n = ";
cin >> n;
Airplane * airplaneList = new Airplane[n];
for (int i = 0; i < n; ++i)
{
airplaneList[i].destination = SetDestination(rand()%5);
airplaneList[i].number = rand()%9001 + 1000;
airplaneList[i].type = SetType(rand()%3);
}
PrintAirplaneList(airplaneList, n, "List:");
SortByDestination (airplaneList, n);
PrintAirplaneList(airplaneList, n, "Sorted list (by destination):");
string type;
cout << "Input type: ";
getline(cin, type);
FindAirplanesAndPrint(airplaneList, n, type);
delete [] airplaneList;
system("PAUSE");
return 0;
}
string SetDestination (int n)
{
string destination;
switch(n){
case 0: destination = "Tokio"; break;
case 1: destination = "Amsterdam"; break;
case 2: destination = "Moscow"; break;
case 3: destination = "Philadelphia"; break;
case 4: destination = "San Diego"; break;
default: destination = "Unknown city"; break;
}
return destination;
}
string SetType (int n)
{
string type;
switch(n){
case 0: type = "passenger"; break;
case 1: type = "cargo"; break;
case 2: type = "post"; break;
default: type = "unknown type"; break;
}
return type;
}
void PrintAirplaneList(Airplane *&airplaneList, int n, string title)
{
cout << "\n";
cout << title << "\n\n";
for (int i = 0; i < n; ++i)
{
cout << "Destination: " << airplaneList[i].destination << "\n";
cout << "Number: " << airplaneList[i].number << "\n";
cout << "Type: " << airplaneList[i].type << "\n\n";
}
}
void SortByDestination (Airplane *&airplaneList, int n)
{
for (int i = 0; i < n - 1; ++i)
{
for (int j = 0; j < n -1; ++j)
{
if(airplaneList[j + 1].destination > airplaneList[j].destination) continue;
Airplane tempAirplane = airplaneList[j];
airplaneList[j] = airplaneList[j + 1];
airplaneList[j + 1] = tempAirplane;
}
}
}
void FindAirplanesAndPrint(Airplane *&airplaneList, int n, string type) {
cout << "Type - " << type << "\n";
int count = 0;
for (int i = 0; i < n; ++i)
{
if (airplaneList[i].type == type)
{
cout << "Destination: " << airplaneList[i].destination << "\n";
cout << "Number: " << airplaneList[i].number << "\n";
++count;
}
}
if (count == 0)
{
cout << "Not found\n";
}
}
我有两个问题。
1. 无法输入类型
string type;
cout << "Input type: ";
getline(cin, type);
FindAirplanesAndPrint(airplaneList, n, type);
我的函数FindAirplanesAndPrint 开始工作,没有任何类型值。如何让我的程序获得价值?
2. 如何获取函数中动态数组的大小?因为似乎每个函数中数组n的传递大小是错误的方式。
【问题讨论】:
-
你不能通过 C++ 中动态分配数组的大小来计算。您必须自己跟踪大小,方法是为其保留一个变量、使用标准库容器或编写自己的动态数组包装器。
-
使用 std::vector 或 std:;array 然后 size 方法会做你想要的。
-
你确定这是 C++ 吗?当你有
classes 时我看到structs,当你有std::vector或std::array时我看到数组。我的意思是,如果您使用 C++,则无需以复杂(且更容易出错)的方式进行操作。 -
“我有两个问题。” -- 那你为什么只发一个帖子?每个帖子问一个问题。