void MainClass::search(){ if(this->study->listLong() > 0){ cout << "请输入要查询学生的电话号或者姓名,输入“0”退出" << endl; string TNAME; cin >> TNAME; flushstd; if (TNAME == "0") { return; } if (strspn(TNAME.data(), "0123456789") == TNAME.length()) { //判断输入的是学号还是姓名 Study* temp = this->study->headerList(); while(temp->nextList() != nullptr){ //学号执行 temp = temp->nextList(); if (temp->studyPhone == TNAME) { // DIRECTORY(true); this->putstu(temp); return; } } } else { bool state = false; Study* temp = this->study->headerList(); while(temp->nextList() != nullptr){ //姓名执行 temp = temp->nextList(); if (temp->studyName == TNAME) { state = true; // DIRECTORY(true); this->putstu(temp); } } if (state == true) { return; } } cout << "没有此学生" << endl; return; }else{} cout << "无数据" << endl; return;
时间: 2024-02-10 07:22:00 浏览: 59
这段代码是一个C++程序中的一个成员函数,函数名为`search()`,主要用于在学生信息列表中查询学生信息。下面是这段代码的具体分析:
1. 首先,通过`if`语句判断学生信息列表中是否有学生信息。如果有学生信息,则打印提示信息,要求用户输入要查询的学生的电话号码或姓名,如果用户输入的是0,则直接退出查询。
2. 接下来,通过`strspn`函数和`if`语句来判断用户输入的是学生的电话号码还是姓名。如果是电话号码,则通过`while`循环遍历学生信息列表中的每一个学生,如果找到了与用户输入电话号码相符的学生,则将该学生的信息输出并返回。
3. 如果用户输入的是姓名,则也通过`while`循环遍历学生信息列表中的每一个学生,如果找到了与用户输入姓名相符的学生,则将该学生的信息输出,并将状态标记为`true`。
4. 最后,如果找到了与用户输入相符的学生,则直接返回;如果没有找到,则打印提示信息,并返回。
需要说明的是,这段代码中使用了一些自定义的类和函数,如`Study`类、`listLong()`函数、`headerList()`函数等,这些类和函数的具体实现需要结合程序的其他部分进行分析。
相关问题
#include<iostream> #include <string> using namespace std; class Person {protected:string name; int age; public: Person(string n,int a):name(n),age(a){} virtual void display() /*虚函数显示信息,实现多态性*/ {cout<<"姓名:"<<name<<endl; cout<<"年龄:"<<age<<endl;} }; class Teacher:public Person {private:string title; string course; public: Teacher(string n,int a,string t,string c):Person(n,a){title=t;course=c;} void display() {Person::display(); cout<<"职称:"<<title<<endl; cout<<"担任课程:"<<course<<endl;} }; class Information:virtual public Person {private:double height,weight; int id; public: Information(string n,int a,int i,double h,double w):Person(n,a),id(i),height(h),weight(w){} virtual void display() {Person::display(); cout<<"学号:"<<id<<endl; cout<<"身高:"<<height<<endl; cout<<"体重:"<<weight<<endl;} }; class Study:public Information {private:string major; int score; public: static int countS; /*静态成员数据说明*/ Study(string n,int a,int i,double h,double w,string m,int s):Person(n,a),Information(n,a,i,h,w),major(m),score(s){countS++;} void display() {Information::display(); cout<<"专业:"<<major<<endl; cout<<"分数:"<<score<<endl;} friend int operator-(Study c1,Study c2); /*友元函数,重载运算符实现分数差计算*/ void displays(){cout<<score;} }; int Study::countS = 0; int operator-(Study c1,Study c2) {return c1.score-c2.score;} int main() {Teacher A("文渊",45,"导师","计算机"); A.display(); cout<<endl; Study B("子博",19,226201324,178,65,"计算机科学与技术",90); Study C("皓轩",19,226203355,176,60,"计算机科学与技术",78); B.display(); cout<<endl; C.display(); cout<<endl; cout<<"有"<<Study::countS<<"个学生"<<endl; cout<<"分数相差"<<B-C<<endl; }找出错误
代码中没有明显的错误,但是可以进行一些改进和优化:
1. 在类 Person 的构造函数中,应该使用成员初始化列表来对成员变量进行初始化,而不是在函数体中进行赋值操作。
2. 在类 Teacher 和类 Information 的构造函数中,应该将参数传递给父类的构造函数进行初始化,而不是在函数体中进行赋值操作。
3. 在类 Study 中,应该使用虚继承的方式继承类 Information,以避免多次继承导致的资源浪费和二义性问题。
4. 在函数 operator- 中,应该将参数声明为常量引用,以避免不必要的拷贝操作。
5. 在类 Study 中,应该将静态成员数据 countS 声明为私有的,以避免被外部访问和修改。
6. 在类 Study 中,应该为成员函数 displays 加上 const 关键字,以表示该函数不会修改对象的状态。
7. 在类 Person 中,应该将 display 函数声明为纯虚函数,以使得该类成为抽象类,不能被实例化。
改进后的代码如下:
设计class:student,根据你的需求分析,定义数据成员和成员函数,类型、数量和功能无限制,要求: class中需要至少分别定义1个static和const成员; 至少3种构造函数:不带参数的构造函数,带参数有默认值的构造函数,拷贝构造函数以及你需要的其他构造函数; 析构函数:自定义析构函数; 使用友元函数,实现合适的功能; 编写测试程序main(),测试你定义的class:student中的所有功能
以下是一个参考的实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
// 定义静态变量和常量
static int num_students;
const string school_name;
// 构造函数
Student() : school_name("ABC School") {
num_students++;
cout << "调用无参构造函数" << endl;
}
Student(string name, int age = 18, float score = 0.0) : name(name), age(age), score(score), school_name("ABC School"){
num_students++;
cout << "调用带参数的构造函数" << endl;
}
Student(const Student &other) : name(other.name), age(other.age), score(other.score), school_name("ABC School") {
num_students++;
cout << "调用拷贝构造函数" << endl;
}
// 析构函数
~Student() {
num_students--;
cout << "调用析构函数" << endl;
}
// 成员函数
void set_name(string name) {
this->name = name;
}
void set_age(int age) {
this->age = age;
}
void set_score(float score) {
this->score = score;
}
void study() {
cout << name << "正在学习" << endl;
}
// 友元函数
friend void print_student_info(const Student &stu);
private:
string name;
int age;
float score;
};
// 定义静态变量
int Student::num_students = 0;
// 友元函数
void print_student_info(const Student &stu) {
cout << "姓名:" << stu.name << endl;
cout << "年龄:" << stu.age << endl;
cout << "成绩:" << stu.score << endl;
}
int main() {
Student s1; // 调用无参构造函数
s1.set_name("张三");
s1.set_age(20);
s1.set_score(90.5);
Student s2("李四"); // 调用带参数的构造函数
s2.set_age(21);
s2.set_score(88.5);
Student s3(s1); // 调用拷贝构造函数
print_student_info(s1);
print_student_info(s2);
print_student_info(s3);
cout << "学生人数:" << Student::num_students << endl;
return 0;
}
```
程序输出:
```
调用无参构造函数
调用带参数的构造函数
调用拷贝构造函数
姓名:张三
年龄:20
成绩:90.5
姓名:李四
年龄:21
成绩:88.5
姓名:张三
年龄:20
成绩:90.5
学生人数:3
调用析构函数
调用析构函数
调用析构函数
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)