我输出一个带有for
循环的二维数组,如下所示:
int output(int data[50][8], string names[50]) {
int amount = 0;
amount = fillAndDisplay(data, names);`
for (int i = 0; i < amount; i++) {
cout << names[i] << " ";
for (int j = 0; j < 8; j++) {
cout << data[i][j] << " ";
}
cout << endl;
}
return data, names;
}
在我的情况下, amount
等于5。
现在,我现在正在回复data
和names
,但它无效。我需要返回什么?另外,我应该使用什么数据类型的功能?我现在正在使用int
,但我不确定这是否正确。
这是我的完整代码。它正在读取的文件是:
5
Franks,Tom 2 3 8 3 6 3 5
Gates,Bill 8 8 3 0 8 2 0
Jordan,Michael 9 10 4 7 0 0 0
Bush,George 5 6 5 6 5 6 5
Heinke,Lonnie 7 3 8 7 2 5 7
以下是代码:
int fillAndDisplay(int data[50][8], string names[50]);
void sort(int data[50][8], string names[50]);
void output(int data[50][8], string names[50]);
int main()
{
int data[50][8];
string names[50];
int amount = 0;
amount = fillAndDisplay(data, names);
sort(data, names);
output(data, names);
system("pause");
return 0;
}
int fillAndDisplay(int data[50][8], string names[50]) {
int const TTL_HRS = 7;
ifstream fin;
fin.open("empdata.txt");
if (fin.fail()) {
cout << "ERROR";
}
int sum = 0;
int numOfNames;
fin >> numOfNames;
for (int i = 0; i < numOfNames; i++) {
fin >> names[i];
data[i][7] = 0;
for (int j = 0; j < 7; j++) {
fin >> data[i][j];
data[i][TTL_HRS] += data[i][j];
}
}
return numOfNames;
}
void sort(int data[50][8], string names[50]) {
int amount = 0;
int temp = 0;
bool hasSwapped = true;
string tempName;
amount = fillAndDisplay(data, names);
while (hasSwapped) {
hasSwapped = false;
for (int i = 0; i < amount - 1; ++i)
{
if (data[i][7] < data[i + 1][7]) {
for (int j = 0; j <= 7; j++) {
temp = data[i][j];
data[i][j] = data[i + 1][j];
data[i + 1][j] = temp;
}
tempName = names[i];
names[i] = names[i + 1];
names[i + 1] = tempName;
hasSwapped = true;
}
}
}
}
void output(int data[50][8], string names[50]) {
int amount = 0;
amount = fillAndDisplay(data, names);
for (int i = 0; i < amount; i++) {
cout << names[i] << " ";
for (int j = 0; j < 8; j++) {
cout << data[i][j] << " ";
}
cout << endl;
}
}
当打印出数组的代码在函数中打印出来时:
Franks,Tom 2 3 8 3 6 3 5 30
Gates,Bill 8 8 3 0 8 2 0 29
Jordan,Michael 9 10 4 7 0 0 0 30
Bush,George 5 6 5 6 5 6 5 38
Heinke,Lonnie 7 3 8 7 2 5 7 39
Press any key to continue . . .
但是当代码只在main中时会打印出来
Heinke,Lonnie 7 3 8 7 2 5 7 39
Bush,George 5 6 5 6 5 6 5 38
Jordan,Michael 9 10 4 7 0 0 0 30
Franks,Tom 2 3 8 3 6 3 5 30
Gates,Bill 8 8 3 0 8 2 0 29
Press any key to continue . . .
它应该打印出来。
答案 0 :(得分:-1)
这是一个界面功能。我不会返回任何内容,因为堆上没有任何更改或分配。 “Amount”在堆栈中,并在输出功能完成时被删除。如果我需要更改堆上的数据,我总是直接更改该特定对象而不是我不需要的已分配内存。
我会指出已经在堆上的那些值。复制堆栈上的数据没有意义,如果在对象中组织数据,则可以引用堆。这将允许您在软件扩展时轻松修复和进行更改。下面是我编写的代码,没有经过测试,但这个想法就在那里。您可以通过制作“金额”来进一步采取行动,这是一个可以访问私人数据以进行“数据”和“名称”等计算的成员。 myCompany-&GT; getAmount();更容易阅读,您可以随时使用它。
#ifndef MyHeaderFileH
#define MyHeaderFileH
class Company{
private:
int Size;
int ColCount;
int *data;
String *names;
//This can be public or private. I made it private to clean up my list.
bool withinRange(int i, int j = 0){
bool inRange = false;
if(i >= 0 && i < Size)
if(j >= 0 && j < ColCount)
inRange = true;
return inRange;
}
public:
//Retrieve data from the heap.
int getData(int i, int j){
int Data = 0;
if(withinRange(i, j))
Data = data[i][j];
return Data;
}
//UPDATE data on the heap.
void setData(int Row, int Col, int Data){
if(withinRange(i, j))
data[i][j] = Data;
}
//Retrieve data from the heap.
String getNames(int i){
String Name = "";
if(withinRange(i, j))
Name = names[i];
return Name;
}
//UPDATE data on the heap.
void setNames(i, String Name){
if(withinRange(i))
names[i] = Name;
}
int importFileData() {
int const TTL_HRS = 7;
ifstream fin;
fin.open("empdata.txt");
if (fin.fail()) {
cout << "ERROR";
}
int sum = 0;
int recordCount;
fin >> recordCount;
for (int i = 0; i < recordCount; i++) {
fin >> names[i].c_str(); //or try .w_str();
data[i][TTL_HRS] = 0;
for (int j = 0; j < TTL_HRS; j++) {
fin >> data[i][j];
//Make a member function that returns the totals for you.
//data[i][TTL_HRS] += data[i][j];
}
}
return recordCount;
}
//Default Constructor
Company(){
Size = 50;
ColCount = 8;
data = new int[Size][ColCount];
names = new String[Size];
}
/*If you know the size before you create the object you can use this constructor.*/
Company(int i, int j){
Size = i;
ColCount = j;
data = new int[Size][ColCount];
names = new String[Size];
}
/*Don't forget to free the memory. This will execute automatically once main() is finished.*/
~Company(){
delete[] data;
delete[] names;
}
};
extern Company *myCompany;
#endif
////////////////
//MyCPPFile.cpp
////////////////
void output();
Company *myCompany = new Company; //Calls constructor.
void main(){
output();
}
void output() {
for(int r=0; r < myCompany->Size(); r++){
cout << myCompany->getNames(r) << " ";
for(int c=0; c < myCompany->ColCount(); c++) //:P
cout << myCompany->getData(r, c) << " ";
cout << endl;
}
}
您可以将以下函数转换为成员函数,而不是将数据复制到堆栈中。您甚至不需要任何参数,因为您可以直接从对象访问私有数据。
public:
int fillAndDisplay();
void sort();
void output();
然后在你的cpp文件中使用它你会做类似的事情:
myCompany->sort();