Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

visual studio2022 在运行以下程序时会出现异常,请修改#include<iostream> #include<fstream> #include…

  1. User

    visual studio2022 在运行以下程序时会出现异常,请修改#include #include #include ; using namespace std; class student { int id; string name; int score; public: student(int i = 0, string n = "\0", int s = 0); void show(); bool operator<(student stu); bool operator!=(student stu); void input(int i = 0, string n = "\0", int s = 0); friend ostream& operator<<(ostream& out, student stu); void operator=(const student stu); void putkey(int i) { id = i; }; }; void student:: operator=(const student stu) { id = stu.id; name = stu.name; score = stu.score; } ostream& operator<<(ostream& out, student stu) { out << stu.id << '\t' << stu.name << '\t' << stu.score << endl; return out; } void student::input(int i, string n, int s) { id = i; name = n; score = s; } student::student(int i, string n, int s) { id = i; name = n; score = s; } void student::show() { cout << "id= " << id << " name= " << name << " score= " << score << endl; } bool student:: operator<(student stu) { return(score < stu.score); } bool student:: operator!=(student stu) { return (id != stu.id); } template class List; template class Node { T info; Node* link; public: Node(); Node(const T& i); friend class List; }; template Node::Node() { link = NULL; } templateNode::Node(const T& i) { info = i; link = NULL; } templateclass List { Node* head, * tail; public: List(); List(List& lis); ~List(); void MakeEmpty(); Node* CreatNode(T data); void InsertFront(Node* p); void PrintList(); void InsertRear(Node* p); Node* Find(T inf); Node* DeleteNode(Node* p); int Length(); void InsertOrder(Node* p); void Reverse(); }; templatevoid List::Reverse() { Node* temp , * p = head, q=head->link; head->link = tail; while (tail != q) { temp = q; while (temp != tail) { p = temp; temp = temp->link; } tail->link = p; tail = p; tail->link = NULL; } } templatevoid List::InsertOrder(Node p) { Node* temp = head->link, * p2 = head; while (temp != NULL&& temp->info < p->info) { p2 = temp->link; temp = temp->link; } if (temp!=NULL&&p->info < temp->info) { p2->link = p; p->link = temp; } else { tail->link = p; tail = p; tail->link = NULL; } } template int List::Length() { int i = 0; Node* temp = head->link; while (temp != 0) { i++; temp=temp->link; } return i; } template Node* List::DeleteNode(Node* p) { Node* temp = head, t=head; while (temp != NULL && temp->link != p) { t = temp->link; temp = temp->link; } if (temp->link == p) { t->link = p->link; delete p; } return head; } template Node List::Find(T inf) { Node* temp = head->link; while (temp != NULL&&temp->info != inf)temp=temp->link; return temp; } template void List::PrintList() { Node* temp = head->link; while (temp!= 0) { cout << temp->info; temp = temp->link; } cout << endl; } templatevoid List:: InsertRear(Node* p){ tail->link = p; tail = p; p->link = NULL; } template void List::InsertFront(Node* p) { p->link = head->link; head->link = p; if (head == tail) tail = p; } template Node* List::CreatNode(T data) { Node* temp = new Node(data); return temp; } /template List::List(List& lis) { head = tail = new Node(); Node iter = lis.head->link; // 原链表中的第一个结点 while (iter != NULL) { Node* newNode = new Node(iter->info); // 复制原结点数据 InsertRear(newNode); // 插入到新链表尾部 iter = iter->link; // 遍历下一个结点 } }/ template List::List(List& lis) { head = tail = new Node(); Node temp = lis.head->link; while (temp != NULL) { Node* pnew = CreatNode(temp->info); InsertFront(pnew); temp=temp->link; } } template List::List() { head = tail = new Node(); } templatevoid List::MakeEmpty() { Node* temp; while (head->link != NULL) { temp = head->link; head->link = temp->link; delete temp; } tail = head; } templateList::~List() { MakeEmpty(); delete head; }

    void main() { student st; Node* pnode; List list1, list2; //读入文件中数据建立链表list1,list2; ifstream ifile; ifile.open("F:\C++\c++\5 10\studata.txt"); int i, s; char n[10]; while (ifile.eof() == 0) { ifile >> i >> n >> s; st.input(i, n, s); pnode = list1.CreatNode(st); list1.InsertFront(pnode); } ifile.close(); ifile.open("F:\C++\c++\5 10\studata.txt"); while (ifile.eof() == 0) { ifile >> i >> n >> s; st.input(i, n, s); pnode = list2.CreatNode(st); list2.InsertRear(pnode); } ifile.close(); //list1,list2 建立成功,输出查看。 list1.PrintList(); list2.PrintList(); cout << "用list构造list3\n"; List list3(list1); //拷贝构造函数先建立一个头结点,再以原链表的各结点的数据域来构造新链表的各对应结点。 list3.PrintList(); cout << "请输入删除结点的学号:\n"; int number; cin >> number; st.putkey(number); pnode = list1.Find(st);

    if (pnode) {
    	pnode = list1.DeleteNode(pnode);
    	list1.PrintList();
    	cout << "list1 长度:" << list1.Length() << endl;
    }
    else cout << "未找到!\n";
    
    cout << "清空list1,按分数从大到小的顺序重建list1" << endl;
    list1.MakeEmpty();
    
    ifile.clear(0);
    ifile.seekg(0);
    
    //读入文件数据,重建list1
    ifile.open("F:\\C++\\c++\\5 10\\studata.txt");
    while (ifile.eof() == 0) {
    	ifile >> i >> n >> s;
    	student st(i, n, s);
    	pnode = list1.CreatNode(st);
    	list1.InsertOrder(pnode);
    }
    list1.PrintList();
    ifile.close();
    cout << "将链表list1逆序\n";
    //要求不删除原结点,也不另建一个链表来取代,
    // 而是通过改变指针域的链接方向来逆转链表。
    
    list1.Reverse();
    list1.PrintList();
    
    cout << "用list3赋值给list1\n";
    list1 = list3;
    list1.PrintList();
    

    }

  2. Assistant

    在运行该程序时出现异常的原因可能是因为程序中使用了带命名空间的iostream头文件,应该使用using namespace std语句来引入命名空间。修改后的代码如下:

    #include #include #include using namespace std;

    class student { int id; string name; int score; public: student(int i = 0, string n = "\0", int s = 0); void show(); bool operator<(student stu); bool operator!=(student stu); void input(int i = 0, string n = "\0", int s = 0); friend ostream& operator<<(ostream& out, student stu); void operator=(const student stu); void putkey(int i) { id = i; }; };

    void student:: operator=(const student stu) { id = stu.id; name = stu.name; score = stu.score; }

    ostream& operator<<(ostream& out, student stu) { out << stu.id << '\t' << stu.name << '\t' << stu.score << endl; return out; }

    void student::input(int i, string n, int s) { id = i; name = n; score = s; }

    student::student(int i, string n, int s) { id = i; name = n; score = s; }

    void student::show() { cout << "id= " << id << " name= " << name << " score= " << score << endl; }

    bool student:: operator<(student stu) { return(score < stu.score); }

    bool student:: operator!=(student stu) { return (id != stu.id); }

    template class List; template class Node { T info; Node* link; public: Node(); Node(const T& i); friend class List; };

    template Node::Node() { link = NULL; }

    templateNode::Node(const T& i) { info = i; link = NULL; }

    templateclass List { Node* head, * tail; public: List(); List(List& lis); ~List(); void MakeEmpty(); Node* CreatNode(T data); void InsertFront(Node* p); void PrintList(); void InsertRear(Node* p); Node* Find(T inf); Node* DeleteNode(Node* p); int Length(); void InsertOrder(Node* p); void Reverse(); };

    templatevoid List::Reverse() { Node* temp, * p = head, * q = head->link; head->link = tail; while (tail != q) { temp = q; while (temp != tail) { p = temp; temp = temp->link; } tail->link = p; tail = p; tail->link = NULL; } }

    templatevoid List::InsertOrder(Node* p) { Node* temp = head->link, * p2 = head; while (temp != NULL && temp->info < p->info) { p2 = temp->link; temp = temp->link; } if (temp != NULL && p->info < temp->info) { p2->link = p; p->link = temp; } else { tail->link = p; tail = p; tail->link = NULL; } }

    template int List::Length() { int i = 0; Node* temp = head->link; while (temp != 0) { i++; temp = temp->link; } return i; }

    template Node* List::DeleteNode(Node* p) { Node* temp = head, * t = head; while (temp != NULL && temp->link != p) { t = temp->link; temp = temp->link; } if (temp->link == p) { t->link = p->link; delete p; } return head; }

    template Node* List::Find(T inf) { Node* temp = head->link; while (temp != NULL && temp->info != inf)temp = temp->link; return temp; }

    template void List::PrintList() { Node* temp = head->link; while (temp != 0) { cout << temp->info; temp = temp->link; } cout << endl; }

    templatevoid List:: InsertRear(Node* p) { tail->link = p; tail = p; p->link = NULL; }

    template void List::InsertFront(Node* p) { p->link = head->link; head->link = p; if (head == tail) tail = p; }

    template Node* List::CreatNode(T data) { Node* temp = new Node(data); return temp; }

    template List::List(List& lis) { head = tail = new Node(); Node* temp = lis.head->link; while (temp != NULL) { Node* pnew = CreatNode(temp->info); InsertFront(pnew); temp = temp->link; } }

    template List::List() { head = tail = new Node(); }

    templatevoid List::MakeEmpty() { Node* temp; while (head->link != NULL) { temp = head->link; head->link = temp->link; delete temp; } tail = head; }

    templateList::~List() { MakeEmpty(); delete head; }

    void main() { student st; Node* pnode; List list1, list2; //读入文件中数据建立链表list1,list2; ifstream ifile; ifile.open("F:\C++\c++\5 10\studata.txt"); int i, s; char n[10]; while (ifile.eof() == 0) { ifile >> i >> n >> s; st.input(i, n, s); pnode = list1.CreatNode(st); list1.InsertFront(pnode); } ifile.close(); ifile.open("F:\C++\c++\5 10\studata.txt"); while (ifile.eof() == 0) { ifile >> i >> n >> s; st.input(i, n, s); pnode = list2.CreatNode(st); list2.InsertRear(pnode); } ifile.close(); //list1,list2 建立成功,输出查看。 list1.PrintList(); list2.PrintList(); cout << "用list构造list3\n"; List list3(list1); //拷贝构造函数先建立一个头结点,再以原链表的各结点的数据域来构造新链表的各对应结点。 list3.PrintList(); cout << "请输入删除结点的学号:\n"; int number; cin >> number; st.putkey(number); pnode = list1.Find(st);

    if (pnode) {
        pnode = list1.DeleteNode(pnode);
        list1.PrintList();
        cout << "list1 长度:" << list1.Length() << endl;
    }
    else cout << "未找到!\n";
    
    cout << "清空list1,按分数从大到小的顺序重建list1" << endl;
    list1.MakeEmpty();
    
    ifile.clear(0);
    ifile.seekg(0);
    
    //读入文件数据,重建list1
    ifile.open("F:\\C++\\c++\\5 10\\studata.txt");
    while (ifile.eof() == 0) {
        ifile >> i >> n >> s;
        student st(i, n, s);
        pnode = list1.CreatNode(st);
        list1.InsertOrder(pnode);
    }
    list1.PrintList();
    ifile.close();
    cout << "将链表list1逆序\n";
    
    list1.Reverse();
    list1.PrintList();
    
    cout << "用list3赋值给list1\n";
    list1 = list3;
    list1.PrintList();
    

    }