【问题标题】:C++ Pointer and StructureC++ 指针和结构
【发布时间】:2019-02-10 10:09:18
【问题描述】:

我必须完成以下任务:

  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++ 中使用 namephone 的原始字符数组而不是 std::string?这对我来说简直是错误的。
  • "你可以通过设置他们的配偶指针指向对方(存储彼此的地址)来结婚两个人。" 这句话是#2的关键。您已经向我们展示了模板,但您尝试了什么?
  • 你尝试了什么?

标签: c++ arrays pointers struct iostream


【解决方案1】:

请注意,此练习显示了 C++ 的过时使用

首先到您有点忘记使用的数组pp[10] 是 10 个数组。但是 10 个是什么? person* 的,所以指向人的指针。

这是非常老式的 C++。如果您在互联网上学习课程,请立即更改,因为现在我们使用vector&lt;person&gt;stringnullptr。如果是班级课程,你别无选择,那就继续吧……

一些提示,基于你已经做过的事情

首先简化阅读循环,不要忘记按照问题的要求将指针设置为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;
}

现在有事自己做

现在关于在 (2) 中与 Marry 和 Tom 结婚。这个比较细腻。我不会为你做这件事,因为现在你拥有完成作业所需的一切。但总的原则是:

  • 创建两个指针spouse1spouse2 并将它们初始化为NULL。
  • 循环遍历数组找出哪个人是Tom,哪个人是Marry,并更新相关指针(例如spouse1 = p[i];
  • 在循环结束时,检查我们是否找到了配偶(两个指针都不再为 NULL,并且两个指针都不一样,因为你不能嫁给一个有自我的人)
  • 如果可以,那就嫁给他们吧:spouse1-&gt;spouse=spouse2; spouse2-&gt;spouse=spouse1;

最后,在你结束程序之前,你需要释放数组中的所有指针(有了向量,你就不必关心这个了)。

需要进一步改进

您仍然需要改进您的阅读循环,使其更具活力。因为实际上,您不知道文本文件中有多少行。因此,从number=0 开始并尽可能长时间地读取数据,每次递增number,但如果无法再读取,或者达到数组的最大大小,则停止。

【讨论】:

  • @LeslieWang 这里不需要高级指针。您只需创建一个循环,就像用于打印的循环一样。但不是打印 p[i]->name,而是将其与“Tom”或“Marry”进行比较(提示,您需要使用 strcmp() 进行比较)。现在尝试,因为尝试是最好的学习方式;-)
猜你喜欢
  • 1970-01-01
  • 2020-08-19
  • 1970-01-01
  • 1970-01-01
  • 2021-05-03
  • 2021-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多