首页 > 编程语言 >实验2 类和对象_基础编程1

实验2 类和对象_基础编程1

时间:2024-10-25 22:59:24浏览次数:1  
标签:const 对象 double 编程 int Complex 实验 Fraction include

1. 实验任务1

t.h源代码:

 1 #pragma once
 2 #include <string>
 3 
 4 // 类T: 声明
 5 class T {
 6 public:
 7     // 对象属性、方法
 8     T(int x = 0, int y = 0);   // 普通构造函数
 9     T(const T& t);             // 复制构造函数
10     T(T&& t);                  // 移动构造函数
11     ~T();                      // 析构函数
12 
13     void adjust(int ratio);    // 按系数成倍调整数据
14     void display() const;      // 以(m1, m2)形式显示T类对象信息
15     // 类属性、方法
16     static int get_cnt();               // 显示当前T类对象总数
17     static const std::string doc;       // 类T的描述信息
18     static const int max_cnt;           // 类T对象上限
19 
20 private:
21     int m1, m2;
22     static int cnt;         // 当前T类对象数目
23     friend void func();     // 类T友元函数声明
24 };
25 
26 // 普通函数声明
27 void func();

t.cpp源代码:

 1 // 类T: 实现
 2 // 普通函数实现
 3 
 4 #include "t.h"
 5 #include <iostream>
 6 #include <string>
 7 
 8 using std::cout;
 9 using std::endl;
10 using std::string;
11 
12 // static成员数据类外初始化
13 const std::string T::doc{ "a simple class sample" };   // 类T的描述信息
14 const int T::max_cnt = 999;                            // 类T对象上限
15 int T::cnt = 0;                           //private    // 当前T类对象数目
16 
17 
18 // 对象方法
19 // 普通构造函数
20 T::T(int x, int y) : m1{ x }, m2{ y } {
21     ++cnt;
22     cout << "T constructor called.\n";
23 }
24 // 复制构造函数
25 T::T(const T& t) : m1{ t.m1 }, m2{ t.m2 } {
26     ++cnt;
27     cout << "T copy constructor called.\n";
28 }
29 // 移动构造函数
30 T::T(T&& t) : m1{ t.m1 }, m2{ t.m2 } {
31     ++cnt;
32     cout << "T move constructor called.\n";
33 }
34 // 析构函数
35 T::~T() {
36     --cnt;
37     cout << "T destructor called.\n";
38 }
39 // 按系数成倍调整数据
40 void T::adjust(int ratio) {
41     m1 *= ratio;
42     m2 *= ratio;
43 }
44 // 以(m1, m2)形式显示T类对象信息
45 void T::display() const {
46     cout << "(" << m1 << ", " << m2 << ")";
47 }
48 
49 // 类方法
50 // 显示当前T类对象总数
51 int T::get_cnt() {
52     return cnt;
53 }
54 
55 // 友元
56 void func() {
57     T t5(42);
58     t5.m2 = 2049;
59     cout << "t5 = "; t5.display(); cout << endl;
60 }

task1.cpp源代码:

 1 #include "t.h"
 2 #include <iostream>
 3 
 4 using std::cout;
 5 using std::endl;
 6 
 7 void test();
 8 
 9 int main() {
10     test();
11     cout << "\nmain: \n";
12     cout << "T objects'current count: " << T::get_cnt() << endl;
13 }
14 
15 void test() {
16     cout << "test class T: \n";
17     cout << "T info: " << T::doc << endl;
18     cout << "T objects'max count: " << T::max_cnt << endl;
19     cout << "T objects'current count: " << T::get_cnt() << endl << endl;
20 
21 
22     T t1;
23     cout << "t1 = "; t1.display(); cout << endl;
24 
25     T t2(3, 4);
26     cout << "t2 = "; t2.display(); cout << endl;
27 
28     T t3(t2);
29     t3.adjust(2);
30     cout << "t3 = "; t3.display(); cout << endl;
31 
32     T t4(std::move(t2));
33     cout << "t3 = "; t4.display(); cout << endl;
34 
35     cout << "T objects'current count: " << T::get_cnt() << endl;
36 
37     func();
38 }

task1运行截图:

问题1:t.h中,普通函数func作为类X的友元,在类的内部声明了友元关系。在类外部,去掉line27,重新编译,是否能正确运行。如果能,回答说明可以去掉line27。如果不能,以截图形式给出编译报错信息,分析可能的原因。

回答:不能正确运行。 在这里,如果对代码进行些许修改(VS2022):func()声明改为func(T t)(定义及实现中做相同改动,这样改动并不会改变输出结果),可以发现,如果去掉类外部对func的声明,代码依旧可以正常运行!但如果将“T t”改为“int a”,那么代码无法正确运行。所以可以做出以下推断:在某些情况下,编译器可能会对未显式声明的函数进行隐式声明,这里的某些情况可能是函数的参数中存在当前类的元素。但采用分文件编译的方式,即类的定义在一个头文件中,而友元函数的定义在另一个源文件中,在类所在的头文件中额外对友元函数进行声明是一个好习惯,不仅代码可读性增强,其编译的逻辑也更加完整。

问题2:t.h中,line8-11给出了各种构造函数、析构函数。总结各种构造函数的功能,以及它们与析构函数的调用时机。

回答:普通构造函数T(int x = 0, int y = 0):用于创建T类的对象,可以接受两个整数参数xy,如果不提供参数则使用默认值 0,将参数值分别赋值给对象的成员变量以初始化对象。

           复制构造函数T(const T& t):当通过一个已存在的T对象创建新对象时被调用,确保新对象与原对象具有相同的状态。

           移动构造函数T(T&& t):右值引用来实现资源的转移。

           调用时机:调用构造函数->结束->调用析构函数,其中析构的顺序与构造的顺序相反。

问题3:t.cpp中,line13-15,调整到t.h,重新编译,程序能否正确编译运行。

回答:不能将静态成员变量的初始化放在头文件中。除非使用内联说明符,但使用内联变量至少需要C++17 。

 

2. 实验任务2

Complex.h源代码:

 1 #pragma once
 2 #include <string>
 3 #ifndef COMPLEX_H
 4 #define COMPLEX_H
 5 
 6 class Complex {
 7 public:
 8     // 公有常量,用于对自定义复数类 Complex 做描述说明
 9     static const std::string doc;
10     // 构造函数
11     Complex();
12     Complex(double realPart);
13     Complex(double realPart, double imagPart);
14     Complex(const Complex& other);
15     // 获取实部和虚部
16     double get_real() const;
17     double get_imag() const;
18     // 加法操作
19     void add(const Complex& other);
20 
21 private:
22     double real;
23     double imag;
24 
25 public:
26     // 友元函数声明
27     friend Complex add(const Complex& c1, const Complex& c2);
28     friend bool is_equal(const Complex& c1, const Complex& c2);
29     friend bool is_not_equal(const Complex& c1, const Complex& c2);
30     friend void output(const Complex& c);
31     friend double abs(const Complex& c);
32 };
33 
34 #endif

Complex.cpp源代码:

 1 #include <iostream>
 2 #include <cmath>
 3 #include "Complex.h"
 4 
 5 //公有信息描述
 6 const std::string Complex::doc = "a simplified complex class";
 7 
 8 Complex::Complex() : real{ 0 }, imag{ 0 } {}
 9 Complex::Complex(double realPart) : real{ realPart }, imag{ 0 } {}
10 Complex::Complex(double realPart, double imagPart) : real{ realPart }, imag{ imagPart } {}
11 Complex::Complex(const Complex& other) : real{ other.real }, imag{ other.imag } {}
12 
13 double Complex::get_real() const {
14     return real;
15 }
16 
17 double Complex::get_imag() const {
18     return imag;
19 }
20 
21 void Complex::add(const Complex& other) {
22     real += other.real;
23     imag += other.imag;
24 }
25 
26 //友元函数实现
27 Complex add(const Complex& c1, const Complex& c2) {
28     return Complex(c1.real + c2.real, c1.imag + c2.imag);
29 }
30 
31 bool is_equal(const Complex& c1, const Complex& c2) {
32     return (c1.real == c2.real && c1.imag == c2.imag);
33 }
34 
35 bool is_not_equal(const Complex& c1, const Complex& c2) {
36     return!(is_equal(c1, c2));
37 }
38 
39 void output(const Complex& c) {
40     std::cout << c.real << (c.imag >= 0 ? " + " : " - ") << std::abs(c.imag) << "i" ;
41 }
42 
43 double abs(const Complex& c) {
44     return std::sqrt(c.real * c.real + c.imag * c.imag);
45 }

 task2.cpp源代码:

 1 #include "Complex.h"
 2 #include <iostream>
 3 
 4 using std::cout;
 5 using std::endl;
 6 using std::boolalpha;
 7 
 8 void test() {
 9     cout << "类成员测试: " << endl;
10     cout << Complex::doc << endl;
11 
12     cout << endl;
13 
14     cout << "Complex对象测试: " << endl;
15     Complex c1;
16     Complex c2(3, -4);
17     const Complex c3(3.5);
18     Complex c4(c3);
19 
20     cout << "c1 = "; output(c1); cout << endl;
21     cout << "c2 = "; output(c2); cout << endl;
22     cout << "c3 = "; output(c3); cout << endl;
23     cout << "c4 = "; output(c4); cout << endl;
24     cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl;
25 
26     cout << endl;
27 
28     cout << "复数运算测试: " << endl;
29     cout << "abs(c2) = " << abs(c2) << endl;
30     c1.add(c2);
31     cout << "c1 += c2, c1 = "; output(c1); cout << endl;
32     cout << boolalpha;
33     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
34     cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl;
35     c4 = add(c2, c3);
36     cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl;
37 }
38 
39 int main() {
40     test();
41 }

task2运行截图:

 

3. 实验任务3

task3.cpp源代码:

 1 #include <iostream>
 2 #include <complex>
 3 
 4 using std::cout;
 5 using std::endl;
 6 using std::boolalpha;
 7 using std::complex;
 8 
 9 void test() {
10     cout << "标准库模板类comple测试: " << endl;
11     complex<double> c1;
12     //声明一个名为c1的复数对象,由于没有显式初始化,其初始值由complex类的默认构造函数决定,通常实部和虚部都初始化为 0
13     complex<double> c2(3, -4);
14     //创建一个实部为 3,虚部为 -4 的复数对象c2
15     const complex<double> c3(3.5);
16     //声明一个常量复数对象c3,实部初始化为 3.5,虚部初始化为 0
17     complex<double> c4(c3);
18     //使用拷贝构造函数创建一个新的复数对象c4,初始值与c3相同
19 
20     cout << "c1 = " << c1 << endl;
21     cout << "c2 = " << c2 << endl;
22     cout << "c3 = " << c3 << endl;
23     cout << "c4 = " << c4 << endl;
24     cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() << endl;
25     cout << endl;
26 
27     cout << "复数运算测试: " << endl;
28     cout << "abs(c2) = " << abs(c2) << endl;
29     c1 += c2;
30     cout << "c1 += c2, c1 = " << c1 << endl;
31     cout << boolalpha;
32     //设置布尔值的输出格式,使其以 “true” 或 “false” 的形式输出
33     cout << "c1 == c2 : " << (c1 == c2) << endl;
34     cout << "c1 != c3 : " << (c1 != c3) << endl;
35     c4 = c2 + c3;
36     cout << "c4 = c2 + c3, c4 = " << c4 << endl;
37 }
38 
39 int main() {
40     test();
41 }

task3运行截图:

complex模板类:#include <complex>,通常使用complex<double>表示实部和虚部为双精度浮点数的复数类型,也可以使用其他数值类型如floatlong double等;支持基本的算术运算,包括加法、减法、乘法、除法;可以进行赋值运算,如c1=c2将一个复数赋值给另一个复数;支持复合赋值运算,如c1+=c2、c1-=c2;提供了一些数学函数,如计算复数的模(abs函数);通过成员函数real()imag()可以轻松获取复数的实部和虚部;可以直接使用流输出操作符<<输出复数对象。

 

4. 实验任务4

Fraction.h源代码:

 1 #pragma once
 2 #include <string>
 3 #ifndef Fraction_h
 4 #define Fraction_h
 5 
 6 class Fraction
 7 {
 8 public:
 9     //公有常量,用于对自定义分数类 Fraction 做描述说明
10     static const std::string doc;
11     //构造函数
12     Fraction();
13     Fraction(int uppart, int downpart = 1);
14     Fraction(const Fraction& other);
15 
16     //获取分子分母
17     int get_up() const;
18     int get_down() const;
19     //求负运算
20     Fraction negative() const;
21 
22 private:
23     int up, down;
24 
25 public:
26     //友元函数
27     friend void output(Fraction f);
28     friend Fraction add(const Fraction& f1, const Fraction& f2);
29     friend Fraction sub(const Fraction& f1, const Fraction& f2);
30     friend Fraction mul(const Fraction& f1, const Fraction& f2);
31     friend Fraction div(const Fraction& f1, const Fraction& f2);
32 };
33 
34 #endif

Fraction.cpp源代码:

 1 #include <iostream>
 2 #include "Fraction.h"
 3 
 4 //公有常量
 5 const std::string Fraction::doc = "Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算.";
 6 //构造函数
 7 Fraction::Fraction() :up{ 0 }, down{ 0 } { }
 8 Fraction::Fraction(int uppart, int downpart) :up{ uppart }, down{ downpart } { }
 9 Fraction::Fraction(const Fraction& other):up{ other.up }, down{ other.down } { }
10 
11 //获取分子分母
12 int Fraction::get_up() const {
13     //化简
14     int num1 = abs(up), num2 = abs(down), temp;
15     while (num2 > 0) {
16         temp = num1 % num2;
17         num1 = num2;
18         num2 = temp;
19     }
20     int newup = up / num1;
21     return newup;
22 }
23 int Fraction::get_down() const {
24     //化简
25     int num1 = abs(up), num2 = abs(down), temp;
26     while (num2 > 0) {
27         temp = num1 % num2;
28         num1 = num2;
29         num2 = temp;
30     }
31     int newdown = down / num1;
32     return newdown;
33 }
34 
35 //求负运算
36 Fraction Fraction::negative() const {
37     return Fraction(-up, down);
38 }
39 
40 void output(Fraction f) {
41     //化简
42     int num1 = abs(f.up), num2 = abs(f.down), temp;
43     while (num2 > 0) {
44         temp = num1 % num2;
45         num1 = num2;
46         num2 = temp;
47     }
48     f.up /= num1, f.down /= num1;
49     //输出
50     if (f.down == 0) std::cout << "分母不能为0";
51     else if (f.up == 0) std::cout << "0";
52     else if (f.down == 1) std::cout << f.up;
53     else if (f.up * f.down < 0) std::cout << -abs(f.up) << "/" << abs(f.down);
54     else std::cout << abs(f.up) << "/" << abs(f.down);
55 }
56 
57 Fraction add(const Fraction& f1, const Fraction& f2) {
58     return Fraction(f1.up * f2.down + f2.up * f1.down, f1.down * f2.down);
59 }
60 
61 Fraction sub(const Fraction& f1, const Fraction& f2) {
62     return Fraction(f1.up * f2.down - f2.up * f1.down, f1.down * f2.down);
63 }
64 
65 Fraction mul(const Fraction& f1, const Fraction& f2) {
66     return Fraction(f1.up * f2.up, f1.down * f2.down);
67 }
68 
69 Fraction div(const Fraction& f1, const Fraction& f2) {
70     return Fraction(f1.up * f2.down, f1.down * f2.up);
71 }

task4.cpp源代码:

 1 #include "Fraction.h"
 2 #include <iostream>
 3 
 4 using std::cout;
 5 using std::endl;
 6 
 7 
 8 void test1() {
 9     cout << "Fraction类测试: " << endl;
10     cout << Fraction::doc << endl << endl;
11 
12     Fraction f1(5);
13     Fraction f2(3, -4), f3(-18, 12);
14     Fraction f4(f3);
15     cout << "f1 = "; output(f1); cout << endl;
16     cout << "f2 = "; output(f2); cout << endl;
17     cout << "f3 = "; output(f3); cout << endl;
18     cout << "f4 = "; output(f4); cout << endl;
19 
20     Fraction f5(f4.negative());
21     cout << "f5 = "; output(f5); cout << endl;
22     cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl;
23 
24     cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl;
25     cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl;
26     cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl;
27     cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl;
28     cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl;
29 }
30 
31 void test2() {
32     Fraction f6(42, 55), f7(0, 3);
33     cout << "f6 = "; output(f6); cout << endl;
34     cout << "f7 = "; output(f7); cout << endl;
35     cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl;
36 }
37 
38 int main() {
39     cout << "测试1: Fraction类基础功能测试\n";
40     test1();
41 
42     cout << "\n测试2: 分母为0测试: \n";
43     test2();
44 }

 task4运行截图:

 

5. 实验任务5

 account.h源代码:

 1 #pragma once
 2 #ifndef ACCOUNT_H
 3 #define ACCOUNT_H
 4 class SavingsAccount {
 5     //储蓄账户类
 6 private:
 7     int id;                        //账号
 8     double balance;                //余额
 9     double rate;                   //年利率
10     int lastDate;                  //上次变更余额的时期
11     double accumulation;           //余额按日累加之和
12     static double total;
13     
14     //记录一笔账, date 为日期, amount 为金额,desc 为说明
15     void record(int date, double amount);
16     //获得到定日期为止的存款金额日紫积值
17     double accumulate(int date) const {
18         return accumulation + balance * (date - lastDate);
19     }
20 public:
21     //构造函数
22     SavingsAccount(int date, int id, double rate);
23     int getId() const { return id; }
24     double getBalance() const { return balance; }
25     double getRate() const { return rate; }
26     static double getTotal() { return total; }
27     void deposit(int date, double amount);              //存入现金
28     void withdraw(int date, double amount);             //取出现金
29     
30     //结算利息,每年 1 月 1 日调用一次该函数
31     void settle(int date);
32     //显示账户信息
33     void show() const;
34 };
35 
36 #endif //ACCOUNT_H

account.cpp源代码:

 1 #include "account.h"
 2 #include <cmath>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 double SavingsAccount::total = 0;
 7 //SavingsAccount成员函数的实现
 8 SavingsAccount::SavingsAccount(int date, int id, double rate): id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
 9     std::cout << date << "\t#" << id << " is created" << std::endl;
10 }
11 void SavingsAccount::record(int date, double amount) {
12     accumulation = accumulate(date);
13     lastDate = date;
14     amount = floor(amount * 100 + 0.5) / 100;     //保留小数点后两位
15     balance += amount;
16     total += amount;
17     std::cout << date << "\t#" << id << "\t" << amount << "\t"<< balance << std::endl;
18 }
19 void SavingsAccount::deposit(int date, double amount) {
20     record(date, amount);
21 }
22 void SavingsAccount::withdraw(int date, double amount) {
23     if (amount > getBalance()) {
24         std::cout << "Error: not enough money" << std::endl;
25     }
26     else {
27         record(date, -amount);
28     }
29 }
30 void SavingsAccount::settle(int date) {
31     double interest = accumulate(date) * rate / 365;        //计算年息
32     if (interest != 0) 
33         record(date, interest);
34     accumulation = 0;
35 }
36 void SavingsAccount::show() const {
37     std::cout << "#" << id << "\tBalance: " << balance;
38 }

 5_11.cpp源代码:

 1 #include "account.h"
 2 #include <iostream>
 3 using namespace std;
 4 int main() {
 5     //建立几个账户
 6     SavingsAccount sa0(1, 21325302, 0.015);
 7     SavingsAccount sal(1, 58320212, 0.015);
 8     //几笔账目
 9     sa0.deposit(5, 5000);
10     sal.deposit(25, 10000);
11     sa0.deposit(45, 5500);
12     sal.withdraw(60, 4000);
13     //开户后第 90 天到了银行的计息日,结算所有账户的年息
14     sa0.settle(90);
15     sal.settle(90);
16     //输出各个账户信息
17     sa0.show();
18     cout << endl;
19     sal.show();
20     cout << endl;
21     cout << "Total: " << SavingsAccount::getTotal() << endl;
22     return 0;
23 }

 task5运行截图:

代码短评:类的设计等都较为合理,一些const限定符的应用保证了代码的安全性;不过银行账户系统的用户也比较关注资金在未来何时将达到何值,所以加入查询未来资金成长值也比较有价值。   [实验总结] 1、complex模板类:#include <complex>,通常使用complex<double>表示实部和虚部为双精度浮点数的复数类型,也可以使用其他数值类型如floatlong double等;支持基本的算术运算,包括加法、减法、乘法、除法;可以进行赋值运算,如c1 = c2将一个复数赋值给另一个复数;支持复合赋值运算,如c1 += c2c1 -= c2;提供了一些数学函数,如计算复数的模(abs函数);通过成员函数real()imag()可以轻松获取复数的实部和虚部;可以直接使用流输出操作符<<输出复数对象。 2、友元函数的额外声明:在类所在的头文件中额外对友元函数进行声明是一个好习惯,不仅代码可读性增强,其编译的逻辑也更加完整。 3、floor函数:<cmath>头文件中的floor函数用于对一个浮点数进行向下取整操作,即返回不大于给定参数的最大整数。 原型为:double floor(double arg);返回值是一个 double 类型的整数。 task5中,amount = floor(amount * 100 + 0.5) / 100;作用是将变量amount进行处理,使其保留两位小数:amount * 100amount的值扩大 100 倍;amount * 100 + 0.5在扩大后的数值基础上加上 0.5,这一步通常是为了在进行向下取整操作时,能够更准确地四舍五入;floor(amount * 100 + 0.5)对上述结果进行向下取整操作,实现四舍五入到最接近的整数;最后再将结果除以 100,恢复到原来的数量级,此时amount的值就被处理为保留两位小数的形式。例如,如果amount原本是 3.14159,经过这个操作后会变为 3.14;如果amount是 3.145,处理后会变为 3.15。这种处理方式在需要对数值进行精确到特定小数位数的四舍五入时非常有用,比如在金融计算、数据处理等场景中。

标签:const,对象,double,编程,int,Complex,实验,Fraction,include
From: https://www.cnblogs.com/sunria/p/18494155

相关文章

  • 程序设计实验2
    任务1task1.cpp1#include"t.h"2#include<iostream>34usingstd::cout;5usingstd::endl;67voidtest();89intmain(){10test();11cout<<"\nmain:\n";12cout<<"Tobjects......
  • 编程小白如何成为大神?大学新生的最佳入门攻略
    编程已成为当代大学生的必备技能,但面对众多编程语言和学习资源,新生们常常感到迷茫。如何选择适合自己的编程语言?如何制定有效的学习计划?如何避免常见的学习陷阱?让我们一起探讨大学新生入门编程的最佳路径,为你的大学生活和未来职业发展打下坚实基础。刚入学的大学新生若想从编......
  • 编程语言有哪些
    编程语言主要包括以下类型:1、机器语言与汇编语言;2、系统编程语言;3、通用高级编程语言;4、领域特定语言(DSL);5、脚本语言;6、函数式编程语言;7、逻辑编程语言;8、并发编程语言。其中,*通用高级编程语言在当代的软件开发中应用最为广泛,这类语言旨在提高开发效率,降低硬件依赖,有助于开发者更......
  • 实验三
    实验任务11#include<stdio.h>2#include<stdlib.h>3charscore_to_grade(intscore);4intmain(){5intscore;6chargrade;78while(scanf("%d",&score)!=EOF){9grade=score_to_grade(score);1......
  • OOP实验二
    任务1源码:1#pragmaonce23#include<string>45//类T:声明6classT{7//对象属性、方法8public:9T(intx=0,inty=0);//普通构造函数10T(constT&t);//复制构造函数11T(T&&t);//移动构造函数12~T();......
  • CUDA编程学习 (1)——CUDA C介绍
    CUDA编程学习(1)——CUDAC介绍1.内存分配和数据移动API函数CUDA编程模型是一个异构模型,需要CPU和GPU协同工作。在CUDA中,host和device是两个重要的概念,我们用host指代CPU及其内存,而用device指代GPU及其内存。CUDA程序中既包含host程序,又包含device程序,它们分......
  • python编程基础
    @目录1.python中的变量和数据类型1.1变量1.2python基本数据类型1.3基本输入与输出输入(Input)输出(Output)基本输出打印多个参数格式化输出打印到文件1.4python中的运算符算术运算符比较运算符赋值运算符逻辑运算符位运算符成员运算符身份运算符2.python中的列表、元组、字典、集合2.1......
  • 实验3
    任务1代码#include<stdio.h>charscore_to_grade(intscore);intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);printf("分数:%d,等级:%c\n\n",score,grade);......
  • py网络工具编程
    从各项网络协议开始了解分析其用途以及攻击价值通过py构造数据包自动化的实现攻击过程scapy模块:该模块非常强大可以构造绝大部分数据包:上图构造了一个tcp的数据包其分片为零协议为tcp再次构造一个数据包通过调用show()查看默认构造数据包结构如何对数据包的属性值......
  • WINCC VBA编程练习10
    这一篇学习笔记在新浪博客发表过,这里再次记录一下。歇了一段时间没有做WINCC环境下VBA学习,今晚继续。新建下面的VBA脚本SubAddTrendctrl()'画面上添加趋势控件Dimi,objTrendSetobjTrend=ActiveDocument.HMIObjects.AddActiveXControl("trend","CCAxOnlineTrendContro......