学生信息
头文件
1 #pragma once 2 #ifndef ZMR_H 3 #define ZMR_H 4 #include <iostream> 5 #include <fstream> 6 using namespace std; 7 const int MAX = 100; 8 enum zone_type { EST, CST, MST, PST, EDT, CDT, MDT, PDT }; 9 class time 10 { 11 public: 12 time() {} 13 time(int h, int m, int s) :hour(h), minute(m), second(s) {} 14 void set(int h, int m, int s); 15 void show()const; 16 public: 17 int hour; 18 int minute; 19 int second; 20 }; 21 void time::set(int h, int m, int s) 22 { 23 hour = h; 24 minute = m; 25 second = s; 26 } 27 void time::show()const 28 { 29 cout << hour << ":" << minute << ":" << second << " "; 30 } 31 32 class ext_time :public time 33 { 34 public: 35 ext_time(int h, int m, int s, zone_type z) :time(h, m, s), zone(z) {} 36 ext_time(); 37 void set(int h, int m, int s, zone_type z); 38 void show()const; 39 private: 40 zone_type zone; 41 }; 42 ext_time::ext_time() 43 { 44 zone = EST; 45 } 46 void ext_time::set(int h, int m, int s, zone_type z) 47 { 48 time::set(h, m, s); 49 zone = z; 50 } 51 void ext_time::show()const 52 { 53 static string zones[8]{ "EST","CST","MST","PST","EDT","CDT","MDT","PDT" }; 54 cout << hour << ":" << minute << ":" << second << " "; 55 cout << " " << zones[zone] << " "; 56 } 57 58 class Student 59 { 60 public: 61 Student(string num, string name, int h,int m,int s,zone_type z); 62 Student(){} 63 ~Student() {} 64 void show_student(); 65 public: 66 string Snum; 67 string Sname; 68 ext_time Stime; 69 }; 70 void Student::show_student() 71 { 72 cout << "学号:" << Snum << " " << "姓名:" << Sname << " " << "登记时间:"; 73 Stime.show(); 74 cout << endl; 75 } 76 Student::Student(string num, string name, int h,int m,int s,zone_type z) 77 { 78 Stime.set(h, m, s,z); 79 Snum = num; 80 Sname = name; 81 } 82 83 class Student_table 84 { 85 public: 86 Student_table(); 87 bool is_empty(); 88 bool is_full(); 89 int get_len(); 90 void show_stu(); 91 void search_stu(string num); 92 void delete_stu(string num); 93 void add_stu(string num, string name, int h, int m, int s, zone_type z); 94 void alter_stu(string num); 95 void write(); 96 void read(); 97 private: 98 int length; 99 class Student list[MAX]; 100 }; 101 Student_table::Student_table() 102 { 103 length = 0; 104 } 105 bool Student_table::is_empty() 106 { 107 if (length == 0) 108 return true; 109 else return false; 110 } 111 bool Student_table::is_full() 112 { 113 if (length == MAX) 114 return true; 115 else return false; 116 } 117 int Student_table::get_len() 118 { 119 return length; 120 } 121 void Student_table::show_stu() 122 { 123 cout << "登记学生数:" << get_len() <<endl; 124 for (int i = 0; i != length; i++) 125 { 126 list[i].show_student(); 127 } 128 } 129 void Student_table::search_stu(string num) 130 { 131 for (int i = 0;i!=length; i++) 132 { 133 if (num == list[i].Snum) 134 { 135 list[i].show_student(); 136 break; 137 } 138 } 139 } 140 void Student_table::delete_stu(string num) 141 { 142 int pos; 143 for (int i = 0; i != length; i++) 144 { 145 if (num == list[i].Snum) 146 { 147 pos = i; 148 break; 149 } 150 } 151 for (int i = pos; i != length; i++) 152 { 153 list[i] = list[i + 1]; 154 } 155 length--; 156 } 157 void Student_table::add_stu(string num, string name, int h, int m, int s, zone_type z) 158 { 159 class Student temp(num, name, h, m, s, z); 160 list[length] = temp; 161 length++; 162 } 163 void Student_table::alter_stu(string num) 164 { 165 for(int i=0;i!=length;i++) 166 { 167 if (list[i].Snum == num) 168 { 169 while (1) 170 { 171 cout << "输入要是修改的项:1.学号,2.姓名,3.登记时间,4.结束修改" << endl; 172 int order; 173 cin >> order; 174 if (order == 1) 175 { 176 string num; 177 cout << "修改为:"; 178 cin >> num; 179 list[i].Snum = num; 180 } 181 else if (order == 2) 182 { 183 string name; 184 cout << "修改为:"; 185 cin >> name; 186 list[i].Sname = name; 187 } 188 else if (order == 3) 189 { 190 int h, m, s; 191 cout << "修改为:" <<endl; 192 cout << "时:"; 193 cin >> h; 194 cout << "分:"; 195 cin >> m; 196 cout << "秒:"; 197 cin >> s; 198 list[i].Stime.set(h, m, s,EST); 199 } 200 else 201 { 202 break; 203 } 204 } 205 } 206 } 207 } 208 void Student_table::write() 209 { 210 ofstream fout; 211 fout.open("information.txt",ios::app); 212 if (!fout.fail()) 213 { 214 for (int i = 0; i != length; i++) 215 { 216 fout << "学号:" << list[i].Snum << " " << "姓名:" << list[i].Sname << " " << "登记时间:" << 217 list[i].Stime.hour << ":" << list[i].Stime.minute << ":" << list[i].Stime.second <<endl; 218 } 219 } 220 fout.close(); 221 } 222 #endif
源文件
1 #include <iostream> 2 #include "zmr.h" 3 using namespace std; 4 int main() 5 { 6 class Student_table Stable; 7 string num, name; 8 int h, m, s; 9 zone_type z = EST; 10 while (1) 11 { 12 cout << "选择功能:1.添加,2.删除,3.查找,4.修改,5.显示表,6.写入文件,8.退出程序"<<endl; 13 int order; 14 cin >> order; 15 if (order == 1) 16 { 17 cout << "学号:"; 18 cin >> num; 19 cout << "姓名:"; 20 cin >> name; 21 cout << "登记时间" << endl; 22 cout << "时:"; 23 cin >> h; 24 cout << "分:"; 25 cin >> m; 26 cout << "秒:"; 27 cin >> s; 28 Stable.add_stu(num, name, h, m, s, z); 29 cout << "添加完成" << endl; 30 } 31 else if (order == 2) 32 { 33 cout << "输入要删除学生的学号:"; 34 cin >> num; 35 Stable.delete_stu(num); 36 cout << "删除完成" << endl; 37 } 38 else if (order == 3) 39 { 40 cout << "输入要查找学生的学号:"; 41 cin >> num; 42 Stable.search_stu(num); 43 cout << endl; 44 } 45 else if (order == 4) 46 { 47 cout << "需要修改信息的学生学号:"; 48 cin >> num; 49 Stable.alter_stu(num); 50 cout << "修改完成" << endl; 51 } 52 else if (order == 5) 53 { 54 Stable.show_stu(); 55 cout << endl; 56 } 57 else if (order == 6) 58 { 59 Stable.write(); 60 cout << "读出完毕" << endl; 61 } 62 else break; 63 } 64 return 0; 65 }
浙公网安备 33010602011771号