首页 > 编程语言 >程序设计实验6

程序设计实验6

时间:2024-12-17 17:31:11浏览次数:7  
标签:index major int score 实验 程序设计 include size

实验任务1

 

实验任务2

实验任务3

实验任务4

 1 #pragma once
 2 #include<iostream>
 3 #include<stdexcept>
 4 #include<iomanip>
 5 using namespace std;
 6 template<typename T>
 7 class Vector {
 8 private:
 9 int size;
10 T* ptr;
11 public:
12 Vector(int size, int value = 0) :size{ size } {
13 if (size < 0) {
14 throw length_error("negative size");
15 }
16 ptr = new T[size];
17 for (int i = 0; i < size; i++) {
18 ptr[i] = value;
19 }
20 }
21 int get_size()const { return size; }
22 T& at(int index)const {
23 if (index < 0 || index >= size) throw out_of_range("index out of range");
24 return ptr[index];
25 }
26 T& operator[](int index)const {
27 if (index < 0 || index >= size) throw out_of_range("index out of range");
28 return ptr[index];
29 }
30 };
31 template<typename T1>
32 void output(const Vector<T1>& v) {
33 for (int i = 0; i < v.get_size();i++) {
34 cout << v[i] <<", ";
35 }
36 cout << endl;
37 }

实验任务5

 1 #include<iostream>
 2   #include<fstream>
 3   #include<algorithm>
 4   #include<vector>
 5   #include<iomanip>
 6   #include<string>
 7   using namespace std;
 8   struct stu
 9   {
10     string id;
11     string name;
12     string major;
13     int score;   
14     stu(string id,string name,string major,int score):id{id},name{name},major{major},score{score}{}
15  };
16  bool compare(stu x, stu y) {
17      if(x.major!=y.major)
18          return x.major < y.major;
19      else{
20          return x.score > y.score;
21      }
22  }
23  int main() {
24      ifstream input("data5.txt");
25      if (!input.is_open()) {
26          cout << "无法加载文件中的数据" << endl;
27          return 0;
28      }
29      string id, name, major;
30      int score;
31      vector<stu> v;
32      while (input >> id >> name >> major >> score) {
33          v.push_back(stu(id, name, major, score));
34      }
35      input.close();
36      sort(v.begin(), v.end(), compare);
37      ofstream output("ans5.txt");
38     if (!output.is_open()) {
39          cout << "文件加载失败" << endl;
40      }
41      for (auto i : v) {
42          cout << setiosflags(ios_base::left);
43          cout << setw(10) << i.id << setw(10) << i.name << setw(10) << i.major << setw(10) << i.score << endl;
44          output << i.id << "  " << i.name << "  " << i.major << "  " << i.score << endl;
45      }
46      output.close();
47      return 0;
48  }

 

标签:index,major,int,score,实验,程序设计,include,size
From: https://www.cnblogs.com/CK1NG/p/18612974

相关文章

  • 实验六
    任务4vector.hpp#pragmaonce#include<iostream>#include<stdexcept>usingstd::cout;usingstd::endl;template<typenameT>classVector{ public: Vector(ints); Vector(ints,Tv); Vector(constVector<T>&v); ~Vector();......
  • 实验六
    task4:代码:1#pragmaonce23#include<iostream>4#include<stdexcept>5#include<cmath>67usingnamespacestd;89template<typenameT>10classVector{11private:12intsize;13T*ptr;14......
  • 实验六
    实验任务四代码vector.hpp1#ifndefVECTOR_HPP2#defineVECTOR_HPP34#include<stdexcept>5#include<iostream>//确保可以使用std::cout和std::endl67template<typenameT>8classVector{9private:10T*data_;11......
  • 实验6 模板类、文件I/O和异常处理
    实验任务4:实验代码:1#pragmaonce2#include<iostream>3#include<stdexcept>4usingnamespacestd;5template<typenameT>6classVector{7public:8Vector(intn,constT&value=T()):size(n){9if(n<0......
  • 实验6
    task1task1.cpp#include"Complex.hpp"#include<iostream>#include<fstream>#include<stdexcept>voidtest1();voidtest2();intmain(){usingnamespacestd;cout<<"测试1:复数模板类测试"<<endl;......
  • 实验6 模板类、文件I/O和异常处理
    实验内容4Vector.hpp1#pragmaonce2#include<iostream>3#include<stdexcept>45usingnamespacestd;67template<typenameT>89classVector{10private:11intsize;12T*ptr;1314publi......
  • 实验六
    1#ifndefVECTOR_HPP2#defineVECTOR_HPP34#include<iostream>5#include<stdexcept>//用于异常处理67usingnamespacestd;89template<typenameT>10classVector{//类名修改为MyVector,避免与std::vector冲突11priva......
  • 实验 6
    实验6实验任务4//Vector.hpp#include<iostream>#include<stdexcept>#include<memory>template<typenameT>classVector{public://构造函数,动态指定大小explicitVector(intsize){if(size<0){throwstd......
  • 实验6
    实验任务4: 1#include<iostream>2#include<stdexcept>34usingnamespacestd;56template<typenameT>7classVector{8public:9Vector(intn);10Vector(intn,Tvalue);11Vector(constVector<T&g......
  • 实验6
    实验任务4Vector.hpp代码点击查看代码#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{public: Vector(intn); Vector(intn,Tvalue); Vector(constVector<T>&v); ~Vector(); intget_siz......