在网上看到的一个C++的小项目,我自己码了一遍,然后记录下我的理解以及像我这种菜鸟在整个过程中产生的问题。
我将我知道的尽可能详细的写下来,如有错误请联系我哈,QQ:920209178。
原文地址:https://blog.csdn.net/qq_33832591/article/details/78076816
环境:win10,Microsoft Visual Studio 2017
1 /*---------------------------------------------------------------------------------------- 2 ---------原文地址:https://blog.csdn.net/qq_33832591/article/details/78076816------------- 3 ------------------------------------------------------------------------------------------ 4 */ 5 #include <iostream> 6 #include <string.h> 7 using namespace std; 8 #define MAX 100 //宏定义 9 10 11 //学生的出生日期类 12 class CDate 13 { 14 public: 15 CDate(int y = 0, int m = 0, int d = 0); 16 //重载<运算符 17 bool operator < (CDate d); 18 //重载非成员函数和成员函数两者的参数会有变化,详情请百度 19 //重载流运算符 20 friend istream & operator >> (istream &in, CDate &d); 21 friend ostream & operator << (ostream &out, CDate &d); 22 23 friend bool CheckValid(CDate d); 24 friend bool LeapYear(int year); 25 26 void setDate(int y, int m, int d); 27 ~CDate(); 28 29 private: 30 unsigned short int year, month, day; 31 }; 32 33 CDate::CDate(int y, int m, int d) :year(y), month(m), day(d) 34 { 35 //构造函数后面加':'---->初始化参数列表 36 //设置日期 37 } 38 39 CDate::~CDate() 40 { 41 //~函数名----->析构函数 42 } 43 void CDate::setDate(int y, int m, int d) 44 { 45 year = y; 46 month = m; 47 day = d; 48 } 49 50 //--------定义成员函数,就是那个流运算符的重载函数 51 istream &operator >> (istream &in, CDate &d) 52 { 53 char ch1, ch2; 54 cout << "请输入日期(格式:YYYY-MM-DD):"; 55 while (1) 56 { 57 cin >> d.year >> ch1 >> d.month >> ch2 >> d.day; 58 if (ch1 == '-' && ch2 == '-') 59 if (CheckValid(d)) break; 60 cerr << "时间格式不正确\n"; 61 } 62 return cin; 63 } 64 65 ostream &operator << (ostream &out, CDate &d) 66 { 67 out << d.year << "年" << d.month << "月" << d.day << "日"; 68 return out; 69 } 70 71 //用处:日期判断 ------ < 运算符重载函数定义 72 bool CDate::operator < (CDate d) 73 { 74 if (year < d.year) return true; 75 if (month < d.month) return true; 76 if (day < d.day) return true; 77 if (year > d.year) return false; 78 if (month > d.month) return false; 79 return false; 80 } 81 82 //检查是否为闰年 83 bool LeapYear(int year) 84 { 85 //能被4整除而不能被100整除. 86 //能被400整除. 87 //就是闰年 88 if (year % 4 == 0 && year % 100 || year % 400 == 0) 89 return true; 90 else 91 return false; 92 } 93 94 //检查用户输入的日期的合法性 95 bool CheckValid(CDate d) 96 { 97 int n; 98 if (d.month < 1 || d.month>12) return false; 99 if (d.day < 1) return false; 100 n = 31; 101 switch (d.month) 102 { 103 case 2: 104 if (LeapYear(d.year))//判断是否为闰年 105 n = 29; 106 else 107 n = 28; 108 break; 109 case 4: 110 case 6: 111 case 9: 112 case 11: 113 n = 30; 114 break; 115 } 116 if (d.day > n) return false;//遇到return,函数结束(?) 117 return true; 118 } 119 120 121 //-------------------------------------------------- 122 //-----------------定义学生信息类------------------- 123 class CStudent 124 { 125 public: 126 static int num;//学生人数 127 CStudent(); 128 129 void InputData(); 130 friend void Sort();//排序 131 friend void FindName();//按姓名查询 132 friend void Statistic();//按性别统计 133 friend void display();//显示全部信息 134 135 136 ~CStudent(); 137 138 private: 139 char* name;//姓名 140 bool sex;//性别 141 CDate date;//出生日期,CDate类对象为数据成员 142 }stu[MAX];//对象数组 143 144 int CStudent::num = 0;//这里定义了学生信息类的num变量的值,代表学生编号(数量?) 145 146 CStudent::CStudent() 147 { 148 } 149 150 CStudent::~CStudent() 151 { 152 } 153 154 //输入学生信息功能函数 155 void CStudent::InputData() 156 { 157 int p; 158 char s[41]; 159 cout << "请输入学生信息(NO." << num << "):\n"; 160 cout << "姓名:"; 161 cin >> s; 162 name = new char[strlen(s) + 1]; 163 strcpy(name, s);//strcpy函数:复制字符串,遇到'\0'为止 164 cout << "性别(输入1-男,输入2-女):"; 165 cin >> p; 166 if (p) sex = true;//sex的值为1或0,一般除了0以外的都代表true 167 else sex = false; 168 cin >> date; 169 cout << endl; 170 } 171 172 //友元函数,根据出生日期data从小到大排序 173 void Sort() 174 { 175 int i, j, p, num; 176 char* tn; 177 bool ts; 178 CDate td; 179 num = CStudent::num;//前面定义了num变量值为0,所以当学生数量为1或者0的时候不会调用下面for循环的语句 180 for (i = 1; i < num; i++) 181 { 182 p = i; 183 for (j = i + 1; j <= num; j++) 184 if (stu[i].date < stu[p].date) p = j; 185 if (p == i) continue; 186 //排序,姓名 187 tn = stu[i].name; 188 stu[i].name = stu[p].name; 189 stu[p].name = tn; 190 //排序,性别 191 ts = stu[i].sex; 192 stu[i].sex = stu[p].sex; 193 stu[p].sex = ts; 194 //排序,出生日期 195 td = stu[i].date; 196 stu[i].date = stu[p].date; 197 stu[p].date = td; 198 } 199 } 200 //根据学生姓名查询学生信息函数 201 void FindName() 202 { 203 char name[41]; 204 int i, num; 205 cout << "请输入姓名:"; 206 cin >> name; 207 num = CStudent::num; 208 for (i = 1; i <= num; i++) 209 if (strcmp(stu[i].name, name) == 0) break; 210 //strcmp函数:比较两个参数,相同返回0,参数1>参数2,返回正数;参数1<参数2,返回负数 211 if (i < num) 212 { 213 cout << "查无此人" << endl << endl; 214 return; 215 } 216 217 cout << "姓名:" << stu[i].name << endl; 218 cout << "性别:"; 219 if (stu[i].sex) 220 cout << "男" << endl; 221 else 222 cout << "女" << endl; 223 cout << endl; 224 } 225 226 //统计各性别人数 227 void Statistic() 228 { 229 //int i, num, s1, s0; 230 //我的显示错误:使用未初始化的变量:s1,s0 231 //给这两个值初始化 232 int i, num, s1=0, s0=0; 233 num = CStudent::num; 234 for (i = 1; i <= num; i++) 235 { 236 if (stu[i].sex == 1) s1++; 237 else s0++; 238 } 239 cout << "-----男生人数:" << s1 << "\n----女生人数:" << s0 << endl << endl; 240 } 241 242 void display() 243 { 244 int i, num; 245 num = CStudent::num; 246 for (i = 1; i <= num; i++) 247 { 248 cout << stu[i].name << "\t"; 249 if (stu[i].sex == 1) cout << "男"; 250 else cout << "女"; 251 cout << "\t" << stu[i].date << endl; 252 } 253 } 254 255 256 //----------------------------------------------- 257 //--------------------主函数--------------------- 258 int main() 259 { 260 //char* menu[] = { "","输入信息","排序","按姓名查询", 261 // "按性别统计", "显示全部信息", "推出"}; 262 //这个按照原作者代码我的显示错误(VS2017),我加了个const约束 263 const char* menu[] = { "","输入信息","排序","按姓名查询", 264 "按性别统计","显示全部信息","退出" }; 265 int i, p; 266 bool end; 267 end = false; 268 while (!end) 269 { 270 for (i = 1; i < 7; i++) 271 cout << i << " " << menu[i] << endl; 272 cin >> p; 273 switch (p) 274 { 275 case 1: 276 CStudent::num++; 277 stu[CStudent::num].InputData(); 278 break; 279 case 2: 280 Sort(); 281 break; 282 case 3: 283 FindName(); 284 break; 285 case 4: 286 Statistic(); 287 break; 288 case 5: 289 display(); 290 break; 291 case 6: 292 end = true; 293 break; 294 } 295 } 296 return 0; 297 }
-------------------------------------------------------------------------
再进行本地调试的时候有个错误:C4996 ‘strcpy’: This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
在网上找到了解决方案:
项目 –> 项目属性 –> c/c++ –> 预处理器定义
添加: _CRT_SECURE_NO_WARNINGS
成功
--------------------------------------------------------------------------
如果我的理解有错误请一定要联系我,谢谢了!