C++指针和结构
C++ Pointer and Structure
我必须完成以下任务:
- 读取文件
person.txt
中有关个人的信息(请参阅下文)并存储到数组p
中。首先将每个人的配偶指针设置为NULL
值 - 对
Mary
和Tom
执行合并操作。你可以通过将两个人的配偶指针设置为指向对方(对方的商店地址)来结婚 - 打印出数组
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的过时使用++
首先是您有点忘记使用的数组p
。CCD_ 11是一个10的数组。但是10个呢?person*
的指针也是如此。
这是非常老式的C++。如果你在网上学习课程,请立即更改,因为现在我们会使用vector<person>
、string
和nullptr
。如果这是一门课堂课程,你别无选择,所以让我们继续…
一些提示,基于你已经做过的事情
首先简化读取循环,不要忘记按照问题中的要求将指针设置为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)结婚的事。这更微妙。我不会为你做这件事,因为现在你已经拥有了完成家庭作业所需的一切。但总的原则是:
- 创建两个指针
spouse1
和spouse2
,并将它们初始化为NULL - 循环遍历数组,找出哪个人是
Tom
,哪个是Marry
,并更新相关指针(例如spouse1 = p[i];
) - 在循环的最后,检查我们是否找到了配偶双方(两个指针都不再为NULL,而且两个指针不同,因为你不能嫁给有自我的人)
- 如果可以的话,就嫁给他们吧:
spouse1->spouse=spouse2; spouse2->spouse=spouse1;
最后,在结束程序之前,您需要释放数组中的所有指针(使用向量,您不必关心这一点)。
需要进一步改进
你仍然需要改进你的阅读循环,使其更有活力。因为实际上,您不知道文本文件中有多少行。因此,从number=0
开始,尽可能长时间地读取数据,每次递增number
,但如果无法再读取,或者达到阵列的最大大小,则停止。
相关文章:
- 如何在 C# 中映射双 C 结构指针?
- C++:映射结构中的结构指针
- C++:添加新结构时,结构指针向量中的所有元素都会更新
- 类的静态结构指针声明在C++
- 在两个.cpp文件之间定义全局类/结构指针
- 返回对常量结构(指针类型)成员的引用:明显的左值到右值转换
- 是否可以将无符号字符数组reinterpret_cast到仅包含C++中无符号字符成员的结构指针
- 如何在地图中使用结构指针
- 结构指针在"新"调用上给出已分配的地址?
- 铸造结构指针指向C 的工会指针
- c++ 结构指针在初始化为 NULL 时无法读取内存
- 函数中的结构指针的地址为0x1
- C 用结构指针初始化结构
- C 结构指针铸成数组Interop C#
- 方法中不允许访问结构指针
- 如何打印出结构指针
- MPI_Op_create:候选功能不可行.自定义结构指针不能解释为空指针
- 尝试将结构指针传递给类时出错
- 初始化结构指针的值
- 使用 pybind11 将自定义结构(指针向量)从 C++ 传递到 python 的方法是什么?