C ++指针和结构

问题描述 投票:-1回答:1

我必须执行以下任务:

  1. 阅读有关文件person.txt个别人的信息(见下文)和存储阵列p。设置配偶指针每个人先NULL值。
  2. 执行对MaryTom娶操作。您可以通过设置自己的配偶指针指向对方(彼此的店面地址)结婚的两个人。
  3. 在这里你需要打印的阵列p指出每个人的可变打印出数组p内容。如果一个人的配偶指针是一个NULL值,然后打印Not Married,否则打印配偶的名字。该程序的输出如下所示。确保您的输出是一样的。

我可以做的(1),阅读文本文件person.txt,它具有以下内容:

Mary        012-35678905    20000
John        010-87630221    16000
Alice       012-90028765    9000
Tom         019-76239028    30000
Pam         017-32237609    32000

但我不知道该怎么办(2)和(3)。

这是我迄今所做的基础上,提供了这个问题,并且我不应该更改模板:

#include <iostream>    //>>>>>>> This part is the template given >>>>>>>
#include <cstdlib>     //
#include <fstream>     //
                       //
using namespace std;   //
                       //
struct person          //
{                      //
char name[30];         //
char phone[15];        //
double money;          //
person *spouse;        //
};                     //
                       //
int main()             //
{                      //
person *p[10];         //<<<<<<<< This is the end of the template part <<<  

ifstream inFile;
inFile.open("person.txt");

if (inFile.fail())
{
    cout << "Error in opening the file!" << endl;
    exit(1);
}

char name[30], phone[15];
int money;
int number = 5;

for (int i = 0; i < number; i++)
{
    inFile >> name >> phone >> money;
    cout << "Name:" << name << endl;
    cout << "Phone:" << phone << endl;
    cout << "Money:" << money << endl;
    cout << "Spouse Name:" << endl;
    cout << endl;
}

cin.get();

system("pause");
return 0;
}

预期结果应该是这样的:

Name: Mary
Phone Number:012-35678905
Money: 20000
Spouse Name:Tom

Name: John
Phone Number:010-87630221
Money: 16000
Spouse Name: Not Married

...
c++ arrays pointers struct iostream
1个回答
1
投票

Be aware that this exercise shows outdated use of C++

首先你的阵列p你有点忘了使用。 p[10]是10.但10什么的阵列? person*的,所以指针到其它人。

这是非常老式的C ++。如果你按照互联网上的课程,应立即更换,因为现在,我们可以使用vector<person>stringnullptr。如果它是一个类当然,你别无选择,让我们去...

Some hints, based on what you have already done

首先简化了读循环,不要忘了指针设置为NULL作为问题要求:

for (int i = 0; i < number; i++)
{
    person *r = new person;                     // allocate a new person 
    inFile >> r->name >> r->phone >> r->money;  // read data into the new person
    r->spouse = NULL;                           // initialize the poitner
    p[i] = r;                                   // store the pointer in the array 
}

你已经几乎具有打印部分(3)。你必须把它从你的阅读循环移动到一个新的循环,从阵列打印,并解决已婚人士的特殊情况:

for (int i = 0; i < number; i++)
{
    cout << "Name:" << p[i]->name << endl;
    cout << "Phone:" << p[i]->phone << endl;
    cout << "Money:" << p[i]->money << endl;
    cout << "Spouse:" ;
    if (p[i]->spouse==NULL) {
         cout << "Not married" <<endl; 
    }
    else {
         cout << p[i]->spouse->name <<endl; 
    }
    cout << endl;
}

Now something to do on your own

现在关于结婚嫁与汤姆(2)。这是更加细腻。我不会为你做,因为现在你拥有你所需要完成的功课。但总的原则是:

  • 创建两个指针spouse1spouse2并进行初始化为NULL。
  • 循环通过该阵列以发现人是Tom和哪一个是Marry,并更新相关的指针(例如spouse1 = p[i];
  • 在循环结束时,检查我们发现,夫妻双方(两个指针不是NULL了,而且两个指针是不同的,因为你不能嫁给别人同喜/她的自我)
  • 如果它的确定,那么就嫁给他们:spouse1->spouse=spouse2; spouse2->spouse=spouse1;

最后,结束程序之前,需要解除分配阵列中的所有指针(与载体,你就不必在意这一点)。

Further improvements needed

你仍然需要提高阅读循环,使其更具活力。因为在现实中,你不知道有多少行是在文本文件中。因此,开始与number=0和尽可能长的时间读取数据,每次递增number,但如果回采无法再阅读,或者如果到达阵列的最大尺寸。

© www.soinside.com 2019 - 2024. All rights reserved.