- 编写一个 C++ 程序,将正实数读入一个向量(以随机顺序)。 用一个
尾随值 -1。 将数字按升序排序并显示排序后的数字。
您的程序必须包括以下两个功能:
void sort(vector<float> & v);
void display(vector<float> & v);
Solution to Q1:
#include <iostream>
#include <vector>
using namespace std;
vector<float> numbers;
void sort(vector<float> & v);
void display(vector<float> & v);
int main() {
float temp;
temp = 99.9; // just to start the loop
while (temp > 0.0) {
cout << "Enter a positive real number (-1 to stop) ";
cin >> temp;
if (temp > 0.0) {
numbers.push_back(temp);
}
}
sort(numbers);
display(numbers);
}
void sort(vector<float> & v) {
int total, pass, i;
float temp;
total = v.size();
for (pass = 0; pass < total; pass++) {
for (i = 0; i < total - 1; i++) {
if (v[i] > v[i+1]) {
temp = v[i];
v[i] = v[i+1];
v[i+1] = temp;
}
}
}
}
void display(vector<float> & v) {
int total, i;
total = numbers.size();
for (i = 0; i < total; i++) {
cout << numbers[i] << ", ";
}
cout << endl;
}
- 在上面的排序程序中,将排序函数的第一行更改为:
void sort(vector v);
(请注意,您还必须更改函数定义中的第一行。)
不要更改任何其他代码。 运行程序。 解释会发生什么。
Solution to Q2:
向量未排序 – 这由显示屏显示。 发生这种情况是因为排序需要在向量中移动数据,即需要更改向量。 如果要更改参数,则必须使用 call-by-reference 将参数传递给函数。 通过删除 & 符号,我们停止了按引用调用的参数。 额外注意:参数不需要显示为按引用调用。 然而,许多 程序员通常对大型数据结构使用按引用调用,因为这意味着只有一个指针传递给函数。 如果删除 &(将其转换为按值调用),那么整个数据结构将被复制到函数中,这会浪费时间。
- 讨论你需要做什么来改变上面的排序程序,以便它使用数组
而不是向量。 这两个程序之间最大的区别是什么? 这是为了
仅讨论 - 无需实际使用数组编写程序。
Solution to Q3:
使用数组与使用向量非常相似。 您需要定义数组: float[500]; 唯一的最大区别是您需要猜测数组的最大大小。 在示例中 上面,最大大小的猜测是500。这可能不够大......
- 下面的程序输入银行帐户的数据,然后搜索以查找特定银行
帐户。 但是,该程序无法运行,因为其中有 6 个错误。 识别
错误并说明需要做什么来纠正每个错误。
#include <iostream>
#include <cstdio> // for getchar
#include <vector>
using namespace std;
class bank_account {
private:
int accnumber;
string name;
float balance;
public:
void loaddata();
void display();
};
void enteraccounts();
int main() {
int search, total, num, i;
enteraccounts();
cout << "Which account do you want to display? ";
cin >> search;
total = accounts.size();
for (i = 0; i < total; i++) {
num = accounts[i].getnum();
if (num != search) {
accounts[i].display();
}
}
}
void enteraccounts() {
// enter 3 accounts - this could be changed
int i;
bank_account temp;
for (i = 0; i < 3; i++) {
temp.loaddata();
}
}
//--------------- methods for the bank_account ------------
void loaddata() {
cout << "Enter the bank account number ";
cin >> accnumber;
cout << "Enter the name of the account ";
getline(cin, name);
cout << "Enter the balance, e.g. 32.45 ";
cin >> balance;
}
void bank_account::display() {
cout << "Data for Account " << accnumber << endl;
cout << "Name: " << name << endl;
cout << "Balance: $" << balance << endl << endl;
}
Solution to Q4:
1) method missing from the class, we need:
int getnum() { return accnumber; }
2) vector has not been defined, we need:
vector<bank_account> accounts;
3) incorrect test inside the if statement, it should be:
if (num == search) {
4) inside “enteraccounts”, data is not actually placed in the vector. Insert new line:
temp.loaddata();
accounts.push_back(temp);
5) method must have the class name before the function name, as follows:
void bank_account::loaddata() {
6) in method “loaddata”, entering a number (accnumber) before a string (name) will cause no data to be
entered for the string. Need to flush the input buffer with an extra line as follows:
cin >> accnumber;
getchar();
5.假设下面的程序有一个函数可以显示堆栈中的所有项(类似于
注释中的“显示和清空”函数)。 写下将显示的值和在
什么顺序。
stack s;
int main() {
s.push(27);
s.push(83);
s.pop();
s.push(56);
– call function here to display all items
}
Solution to Q5:
它将显示 56,然后是 27。56 是放置在堆栈中的最后一个项目,因此它是显示的第一个项目。 83 被放置在堆栈上,但随后被使用 pop 删除