好的,所以我想发生的是程序会要求我输入我要修改的导师的ID,之后假设在这种情况下我想修改导师的地址和电话号码。我不确定如何处理这个问题,所以任何帮助将不胜感激!请引导我到正确的方向,谢谢!
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
//Has functions accepts input & prints output in date format
class date {
public:
int day, month, year;
char seperator = '/';
void enter() {
cin >> day >> seperator >> month >> seperator >> year;
}
void display() {
cout << day << seperator << month << seperator << year;
}
void reset() {
day = 0;
month = 0;
year = 0;
}
};
//Declaring tutor structure of Variables Globally (up to 100 records)
struct tutor {
tutor* back;
int tutorID;
string tutorName;
date dateJoined;
date dateTerminate;
float hourlyRate;
string phoneNum;
string address;
int tCentreCode;
string tCentreName;
string tCentreLocation;
int subjectCode;
string subjectName;
int rating;
tutor* next;
}*list, *newnode, *temp, *previous, *tail;
void generateSample() {
list = NULL;
newnode = new tutor;
newnode->tutorID = 1;
newnode->tutorName = "Yow Tew Thung";
newnode->address = "2, Jln 1/3, Bandar Sri Selamat";
newnode->phoneNum = "0121231542";
newnode->dateJoined.day = 15; newnode->dateJoined.month = 01; newnode->dateJoined.year = 2017;
newnode->dateTerminate.day = 10; newnode->dateTerminate.month = 11; newnode->dateTerminate.year = 2019;
newnode->tCentreCode = 4;
newnode->tCentreName = "eXcel Bukit Bintang 1";
newnode->tCentreLocation = "Bukit Bintang";
newnode->subjectCode = 4;
newnode->subjectName = "History";
newnode->rating = 5;
newnode->hourlyRate = 45;
list = tail = newnode;
newnode = new tutor;
newnode->tutorID = 2;
newnode->tutorName = "Syed Hasif";
newnode->address = "17, Jln Zainal Abidin 4, Suria";
newnode->phoneNum = "0189056841";
newnode->dateJoined.day = 05; newnode->dateJoined.month = 05; newnode->dateJoined.year = 2019;
newnode->dateTerminate.day = 02; newnode->dateTerminate.month = 11; newnode->dateTerminate.year = 2020;
newnode->tCentreCode = 1;
newnode->tCentreName = "eXcel Bukit Jalil 1";
newnode->tCentreLocation = "Bukit Jalil";
newnode->subjectCode = 2;
newnode->subjectName = "Bahasa Melayu";
newnode->rating = 3;
newnode->hourlyRate = 45;
newnode->back = tail;
tail->next = newnode;
tail = newnode;
newnode = new tutor;
newnode->tutorID = 3;
newnode->tutorName = "Chong Kun Li";
newnode->address = "34, Jalan 1/3Y, PJU8, Kiara";
newnode->phoneNum = "0189343221";
newnode->dateJoined.day = 05; newnode->dateJoined.month = 05; newnode->dateJoined.year = 2019;
newnode->dateTerminate.day = 02; newnode->dateTerminate.month = 11; newnode->dateTerminate.year = 2020;
newnode->tCentreCode = 2;
newnode->tCentreName = "eXcel Subang Jaya 1";
newnode->tCentreLocation = "Subang Jaya";
newnode->subjectCode = 3;
newnode->subjectName = "Mathematics";
newnode->rating = 3;
newnode->hourlyRate = 49;
newnode->back = tail;
tail->next = newnode;
tail = newnode;
newnode = new tutor;
newnode->tutorID = 4;
newnode->tutorName = "Sangeta Mahad";
newnode->address = "9, Jln 17J, Medan Keramat";
newnode->phoneNum = "0163457985";
newnode->dateJoined.day = 05; newnode->dateJoined.month = 05; newnode->dateJoined.year = 2019;
newnode->dateTerminate.day = 02; newnode->dateTerminate.month = 11; newnode->dateTerminate.year = 2020;
newnode->tCentreCode = 3;
newnode->tCentreName = "eXcel Subang Jaya 2";
newnode->tCentreLocation = "Subang Jaya";
newnode->subjectCode = 3;
newnode->subjectName = "Mathematics";
newnode->rating = 1;
newnode->hourlyRate = 40;
newnode->back = tail;
tail->next = newnode;
tail = newnode;
system("PAUSE");
system("cls");
}
我建议编写一个tutor * findTutorByName(const std::string &)
函数,该函数将导师的名字作为参数,然后遍历链表,寻找其tutorName
字段与该名称匹配的节点,如果找到这样的节点,它会返回指向该节点的指针。 如果找不到这样的节点,则应返回 NULL。
一旦你没有这样做,问题的其余部分只是调用tutor * t = findTutorByName(the_name_i_want)
的问题,如果t
不是 NULL 指针,则在t
中设置字段(例如t->phoneNum = "12345678";
(
(旁注:发布的代码有四次重复相同的 14 行代码序列。 每当您注意到类似代码的多次重复时,请将其视为有机会编写一个函数以将该代码统一到单个位置的信号,然后将重复代码的N 个实例替换为对该函数的调用。 从长远来看,这将为您节省很多痛苦,因为您只需在一个地方而不是N个地方修复错误并进行更改(
将 tutur 指针指向导师列表的起点并检查节点的 ID。如果节点是正确的 id,则修改指针,如果不移动到下一项。
或者从 id 创建地图到导师指针,并将 id 作为地图的键。