|
| 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 | + |
0 commit comments