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

程序设计实验6

时间:2024-12-18 21:43:10浏览次数:5  
标签:index major int score 实验 程序设计 include size

任务4

task4.cpp

 1 #include <iostream>
 2 #include "Vector.hpp"
 3 
 4 void test1() {
 5     using namespace std;
 6 
 7     int n;
 8     cout << "Enter n: ";
 9     cin >> n;
10     
11     Vector<double> x1(n);
12     for(auto i = 0; i < n; ++i)
13         x1.at(i) = i * 0.7;
14 
15     cout << "x1: "; output(x1);
16 
17     Vector<int> x2(n, 42);
18     const Vector<int> x3(x2);
19 
20     cout << "x2: "; output(x2);
21     cout << "x3: "; output(x3);
22 
23     x2.at(0) = 77;
24     x2.at(1) = 777;
25     cout << "x2: "; output(x2);
26     cout << "x3: "; output(x3);
27 }
28 
29 void test2() {
30     using namespace std;
31 
32     int n, index;
33     while(cout << "Enter n and index: ", cin >> n >> index) {
34         try {
35             Vector<int> v(n, n);
36             v.at(index) = -999;
37             cout << "v: "; output(v);
38         }
39         catch (const exception &e) {
40             cout << e.what() << endl;
41         }
42     }
43 }
44 
45 int main() {
46     cout << "测试1: 模板类接口测试\n";
47     test1();
48 
49     cout << "\n测试2: 模板类异常处理测试\n";
50     test2();
51 }
task4.cpp

Vector.hpp

 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 }
Vecotr.hpp

代码运行截图:

任务5

task5.cpp

 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     cout<<"文件导出成功!\n"; 
48     return 0;
49 }
task5.cp

代码运行截图:

 

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

相关文章

  • 实验6
    实验4#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voidou......
  • 实验6 模板类、文件I/O与异常处理
    实验四vector.hpp#pragmaonce#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{private:intsize;T*ptr;public:Vector(intsize,intvalue=0):size{size}{......
  • 实验6
    任务41#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count;......
  • 谭浩强C程序设计课后习题(第3章)
    本章结合介绍最简单的程序,系统的介绍了编写程序的各项要素,有了这些基础,就可以开始编写程序了。习 题1.假如我国国民生产总值的年增长率为7%,计算10年后我国国民生产总值与现在相比增长多少百分比。计算公式为r为年增长率,n为年数,p为与现在相比的倍数。具体代码:#include<......
  • 实验六 模板类、文件I/O和异常处理
    1、实验任务一Complex.hpp#pragmaonce#include<iostream>#include<stdexcept>//声明//////////////////////////////////////////////////////复数模板类声明template<typenameT>classComplex{public:Complex(Tr=0,Ti=0);Complex(co......
  • 实验6
    任务4源代码#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;......
  • 实验6
    任务4:源代码:1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count;......
  • 实验6 模板类、文件I/O和异常处理
    实验任务4:1#pragmaonce23#include<iostream>4#include<stdexcept>56usingnamespacestd;78template<typenameT>9classVector{10public:11Vector(intn,Tvalue=0);12~Vector();13Vec......
  • 文献解读:采用波浪前缘的风电机组翼型后缘降噪实验研究
    题目:采用波浪前缘的风电机组翼型后缘降噪实验研究关键词:风能;风力发电机;噪音控制;波浪前缘;尾缘噪声1中文摘要  旋转叶片产生的气动噪声是制约现代风力机快速发展的重要因素。在各种噪声源中,翼型尾缘噪声对风力机噪声的贡献最大。  本文在半消声室内进行了仿生正弦波形前......
  • 实验6
    Task4.c1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count;......