Skip to content

Commit 52dec9a

Browse files
committed
11_manage_memory
1 parent 22409d7 commit 52dec9a

File tree

7 files changed

+229
-2
lines changed

7 files changed

+229
-2
lines changed

10_define_new_class/10_define_new_class.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ string PF_grade(double grade)
7171

7272

7373

74-
int main()
74+
int dnc()
7575
{
7676
vector<Student_info_dnc> students;
7777
Student_info_dnc record;
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// Created by Daisy on 2020/12/14.
3+
//
4+
5+
#include <string>
6+
#include <vector>
7+
8+
#ifndef ACCLERATEDCPP_10_DEFINE_NEW_CLASS_H
9+
#define ACCLERATEDCPP_10_DEFINE_NEW_CLASS_H
10+
11+
#endif //ACCLERATEDCPP_10_DEFINE_NEW_CLASS_H
12+
13+
class Student_info_dnc{
14+
private:
15+
std::string n;
16+
double midterm, final;
17+
std::vector<double> homework;
18+
std::istream& readhw(std::istream&);
19+
20+
public:
21+
std::istream& read(std::istream&);
22+
double grade() const;
23+
std::string name() const {return n;}
24+
bool valid() const;
25+
Student_info_dnc(): midterm(0),final(0) {};
26+
Student_info_dnc(std::istream&) ;
27+
};
28+
29+
bool compare_dnc(const Student_info_dnc&, const Student_info_dnc&);
30+
std::string PF_grade(double );

11_manage_memory.cpp

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
//
2+
// Created by Daisy on 2020/12/14.
3+
//
4+
5+
#include <vector>
6+
#include <string>
7+
#include <random>
8+
#include <iostream>
9+
#include <fstream>
10+
#include <cfloat>
11+
#include <cstring>
12+
#include <algorithm>
13+
#include <list>
14+
15+
using namespace std;
16+
typedef double (*analysis_fp)(const std::vector<double >&);
17+
18+
analysis_fp get_analysis_ptr(){
19+
analysis_fp pointer= nullptr;
20+
int* p = new int(42);
21+
++ *p;
22+
cout << *p << endl;
23+
delete p;
24+
int* pp = new int[20];
25+
cout << pp[0] << endl;
26+
vector<int> v(pp, pp+20);
27+
delete [] pp;
28+
29+
return pointer;
30+
}
31+
32+
string letter_grade(double grade)
33+
{
34+
static const double numbers[] =
35+
{97,94,90,87,84,80,77,74,70,60,0};
36+
37+
static const char *letters[] =
38+
{"A+","A","A-","B+","B","B-","C+","C","C-","D","F"};
39+
40+
static const std::size_t ngrades = sizeof(numbers) / sizeof(*numbers);
41+
42+
for (std::size_t i = 0; i < ngrades; ++i)
43+
{
44+
if (grade>=numbers[i])
45+
return letters[i];
46+
};
47+
return "?\?\?";
48+
}
49+
50+
void gradetoletter()
51+
{
52+
random_device rd;
53+
mt19937 mt(rd());
54+
uniform_int_distribution<int> dist(0,100);
55+
for(int i=0; i<30; ++i)
56+
{
57+
int grade = dist(mt);
58+
cout << "The grade is: " << grade
59+
<< ", And the letter grade is: "
60+
<< letter_grade(grade) << endl;
61+
}
62+
}
63+
64+
int passparamsTomain(int argc, char** argv) // 指针数组的指针
65+
{
66+
if (argc>1)
67+
{
68+
int i;
69+
for(i=1; i< argc-1; ++i )
70+
cout << argv[i] << " ";
71+
cout << argv[i] << endl;
72+
}
73+
return 0;
74+
}
75+
76+
int copyin()
77+
{
78+
ifstream infile(R"(E:\Projects\Accleratedcpp\in.txt)");
79+
ofstream outfile(R"(E:\Projects\Accleratedcpp\out.txt)");
80+
string s;
81+
while (getline(infile, s))
82+
outfile << s << endl;
83+
cout << "gfdgdsss{" << endl;
84+
return 0;
85+
}
86+
87+
int copymultiin(int argc, char ** argv)
88+
{
89+
int failcnt = 0;
90+
ofstream outfile(R"(E:\Projects\Accleratedcpp\out.txt)");
91+
for (int i= 1; i<argc; ++i)
92+
{
93+
ifstream in(argv[i]);
94+
if(in)
95+
{
96+
string s;
97+
while(getline(in, s))
98+
outfile << s << endl;
99+
}else
100+
{
101+
cerr << "cannot open file" << argv[i] << endl;
102+
++ failcnt;
103+
}
104+
}
105+
return failcnt;
106+
}
107+
108+
const char* duplicate_chars(const char * p)
109+
{
110+
size_t length = strlen(p) + 1;
111+
char* result = new char[length];
112+
copy(p,p+length, result);
113+
return result;
114+
}
115+
116+
int testduplicate()
117+
{
118+
const char letters[] =
119+
{'a','v','A','B','C','e','d','D','F','\0'};
120+
const char* p = duplicate_chars(letters);
121+
for(std::size_t i =0; i < sizeof(letters)/sizeof((*letters)); ++i)
122+
{
123+
cout << *(p+i)<< endl;
124+
}
125+
cout << letters << endl;
126+
}
127+
128+
129+
130+
template <class T,class M>
131+
M median(T v)
132+
{
133+
std::size_t size = sizeof(v)/sizeof(*v);
134+
if (size == 0)
135+
throw domain_error("median of an empty vector");
136+
137+
sort(v, v+size);
138+
std::size_t mid = size / 2;
139+
140+
return size %2 ==0 ? &((*(v+mid) + *(v+mid+1)) / 2) : v+mid;
141+
}
142+
143+
int main()
144+
{
145+
int numbers[] =
146+
{3,2,4,67,3,1,2,4};
147+
int a = median(*numbers);
148+
cout << a << endl;
149+
for(std::size_t i =0; i < sizeof(numbers)/sizeof((*numbers)); ++i)
150+
{
151+
cout << *(numbers+i)<< endl;
152+
}
153+
154+
vector<double> nums(numbers, numbers+8);
155+
double b = median(&nums);
156+
cout << b << endl;
157+
std::list<double > student;
158+
student.sort();
159+
return 0;
160+
}
161+

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ project(Accleratedcpp)
33

44
set(CMAKE_CXX_STANDARD 11)
55

6-
add_executable(Accleratedcpp "5_organize program_data/original/pmain.cpp" "1_Hello World.cpp" 2_chars.cpp "3_loop and count.cpp" "4_use batch data.cpp" "5_organize program_data/original/grade.h" "5_organize program_data/original/Student_info.h" "5_organize program_data/original/Student_info_funcs.cpp" "5_organize program_data/original/median_funcs.cpp" "5_organize program_data/original/median.h" "5_organize program_data/original/grade_funcs.cpp" "7_Use library/use library.cpp" "5_organize program_data/memOptim/main.cpp" "5_organize program_data/memOptim/grade.h" "5_organize program_data/memOptim/grade_funcs.cpp" "5_organize program_data/memOptim/Student_info.h" "5_organize program_data/memOptim/Student_info_funcs.cpp" "5_organize program_data/main.cpp" "6_UseIterator extract_fail.cpp" "7_Use library/find_url.cpp" "7_Use library/StudentGrades.cpp" "7_Use library/6-1.cpp" "7_Use library/using library.h" 8_CorrelatedContainer.cpp "7_Use library/StudentGrade.h" 8_MakeaSentence.cpp 9_Generics.cpp 10_define_new_class/10_define_new_class.cpp 10_define_new_class/10_define_new_class.h)
6+
add_executable(Accleratedcpp "5_organize program_data/original/pmain.cpp" "1_Hello World.cpp" 2_chars.cpp "3_loop and count.cpp" "4_use batch data.cpp" "5_organize program_data/original/grade.h" "5_organize program_data/original/Student_info.h" "5_organize program_data/original/Student_info_funcs.cpp" "5_organize program_data/original/median_funcs.cpp" "5_organize program_data/original/median.h" "5_organize program_data/original/grade_funcs.cpp" "7_Use library/use library.cpp" "5_organize program_data/memOptim/main.cpp" "5_organize program_data/memOptim/grade.h" "5_organize program_data/memOptim/grade_funcs.cpp" "5_organize program_data/memOptim/Student_info.h" "5_organize program_data/memOptim/Student_info_funcs.cpp" "5_organize program_data/main.cpp" "6_UseIterator extract_fail.cpp" "7_Use library/find_url.cpp" "7_Use library/StudentGrades.cpp" "7_Use library/6-1.cpp" "7_Use library/using library.h" 8_CorrelatedContainer.cpp "7_Use library/StudentGrade.h" 8_MakeaSentence.cpp 9_Generics.cpp 10_define_new_class/10_define_new_class.cpp 10_define_new_class/10_define_new_class.h 11_manage_memory.cpp 10_define_new_class/10_define_new_class.h)

in.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
I would like to eat a bunch of fruits.
2+
I would like to eat a bunch of fruits.
3+
I would like to eat a bunch of fruits.
4+
I would like to eat a bunch of fruits.
5+
I would like to eat a bunch of fruits.
6+
I would like to eat a bunch of fruits.
7+
I would like to eat a bunch of fruits.
8+
I would like to eat a bunch of fruits.
9+
I would like to eat a bunch of fruits.

in2.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
I would like to eat a bunch of food.
2+
I would like to eat a bunch of food.
3+
I would like to eat a bunch of food.
4+
I would like to eat a bunch of food.
5+
I would like to eat a bunch of food.
6+
I would like to eat a bunch of food.
7+
I would like to eat a bunch of food.
8+
I would like to eat a bunch of food.
9+
I would like to eat a bunch of food.

out.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
I would like to eat a bunch of fruits.
2+
I would like to eat a bunch of fruits.
3+
I would like to eat a bunch of fruits.
4+
I would like to eat a bunch of fruits.
5+
I would like to eat a bunch of fruits.
6+
I would like to eat a bunch of fruits.
7+
I would like to eat a bunch of fruits.
8+
I would like to eat a bunch of fruits.
9+
I would like to eat a bunch of fruits.
10+
I would like to eat a bunch of food.
11+
I would like to eat a bunch of food.
12+
I would like to eat a bunch of food.
13+
I would like to eat a bunch of food.
14+
I would like to eat a bunch of food.
15+
I would like to eat a bunch of food.
16+
I would like to eat a bunch of food.
17+
I would like to eat a bunch of food.
18+
I would like to eat a bunch of food.

0 commit comments

Comments
 (0)