数据类型之记录(record)..With XXX do begin... end;

本文详细介绍了Pascal语言中记录类型的定义与使用方法,并通过示例展示了如何利用with语句简化记录字段的操作。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
type
  MailingListRecord = record   { 声明记录用关键字record}
    FirstName: string;
    LastName: string;
    Address: string;
    City: string;
    State: string;
    Zip: Integer;
  end;
var
  MLRecord: MailingListRecord;  { 定义一个该记录的变量}
begin
  { 当使用记录时,用小圆点来访问它的字段}
  MLRecord.FirstName := 'Bruce';
  MLRecord.LastName := 'Reisdorph';
  MLRecord.Address := '123 Inspiration Pt.';
  MLRecord.City := 'Merced';
  MLRecord.State := 'CA';
  MLRecord.Zip := 99999;
 
  { 使用with语句来简化上面的字段的输入}
  { with表示“用这个对象'MLRecord'做下列”}
  with MLRecord do
  begin
    FirstName := 'Bruce';
    LastName := 'Reisdorph';
    Address := '123 Inspiration Pt.';
    City := 'Merced';
    State := 'CA';
    Zip := 99999;
  end;
  { with可以减少键入量,同时也使程序可读性更强}
end;

转载于:https://www.cnblogs.com/BillLei/p/4286061.html

#include <iostream> #include <fstream> #include <vector> #include <string> #include <algorithm> #include <iomanip> using namespace std; enum AbsenceType { LATE = 1, LEAVE_EARLY, ASK_FOR_LEAVE, CUT_CLASS }; struct Record { string date; int classNumber; string className; string studentName; AbsenceType type; string toString() const { return date + "#" + to_string(classNumber) + "#" + className + "#" + studentName + "#" + to_string(type); } void print() const { cout << date << " " << classNumber << "\t " << left << setw(15) << className << "\t" << left << setw(18) << studentName << " "; switch(type) { case LATE: cout << "late"; break; case LEAVE_EARLY: cout << "leave early"; break; case ASK_FOR_LEAVE: cout << "ask for leave"; break; case CUT_CLASS: cout << "cut a class"; break; } cout << endl; } }; class AttendanceSystem { private: vector<Record> records; bool isValidDate(const string& date) { if (date.length() != 6) return false; int year = stoi(date.substr(0, 2)); int month = stoi(date.substr(2, 2)); int day = stoi(date.substr(4, 2)); if (year < 0 || year > 99) return false; if (month < 1 || month > 12) return false; const int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); int maxDay = daysInMonth[month]; if (month == 2 && isLeapYear) { maxDay = 29; } return day >= 1 && day <= maxDay; } void saveToFile() { ofstream out("data.txt"); for (const auto& record : records) { out << record.toString() << endl; } out << "!" << endl; } void loadFromFile() { ifstream in("data.txt"); if (!in) return; records.clear(); string line; while (getline(in, line) && line != "!") { size_t pos = 0; Record record; pos = line.find('#'); record.date = line.substr(0, pos); line.erase(0, pos + 1); pos = line.find('#'); record.classNumber = stoi(line.substr(0, pos)); line.erase(0, pos + 1); pos = line.find('#'); record.className = line.substr(0, pos); line.erase(0, pos + 1); pos = line.find('#'); record.studentName = line.substr(0, pos); line.erase(0, pos + 1); record.type = static_cast<AbsenceType>(stoi(line)); records.push_back(record); } } public: AttendanceSystem() { loadFromFile(); } void showAllRecords() { if (records.empty()) { cout << "No records found.\n"; return; } cout << "\nN date ordinal name of class\tname of student\t type\n"; for (size_t i = 0; i < records.size(); ++i) { cout << i << " "; records[i].print(); } } void addRecord() { Record newRecord; do { cout << "Date (YYMMDD): "; cin >> newRecord.date; } while (!isValidDate(newRecord.date)); do { cout << "Class number (1-10): "; cin >> newRecord.classNumber; } while (newRecord.classNumber < 1 || newRecord.classNumber > 10); cout << "Class name: "; cin >> newRecord.className; cout << "Student name: "; cin >> newRecord.studentName; int type; do { cout << "Absence type (1-late, 2-leave early, 3-ask for leave, 4-cut class): "; cin >> type; } while (type < 1 || type > 4); newRecord.type = static_cast<AbsenceType>(type); auto it = find_if(records.begin(), records.end(), [&](const Record& r) { return r.date == newRecord.date && r.classNumber == newRecord.classNumber && r.className == newRecord.className && r.studentName == newRecord.studentName && r.type == newRecord.type; }); if (it != records.end()) { char choice; cout << "This record already exists. Add anyway? (y/n): "; cin >> choice; if (tolower(choice) != 'y') return; } records.push_back(newRecord); saveToFile(); } void searchStudent() { if (records.empty()) { cout << "No records found.\n"; return; } string name; cout << "Enter student name to search: "; cin >> name; bool found = false; for (size_t i = 0; i < records.size(); ++i) { if (records[i].studentName == name) { if (!found) { cout << "\nN date ordinal name of class\tname of student\t type\n"; found = true; } cout << i << " "; records[i].print(); } } if (!found) { cout << "No records found for student " << name << endl; } } void editRecord() { if (records.empty()) { cout << "No records found.\n"; return; } showAllRecords(); size_t index; cout << "Enter record number to edit: "; cin >> index; if (index >= records.size()) { cout << "Invalid record number.\n"; return; } Record& record = records[index]; int choice; cout << "What to edit?\n" << "1. Date\n2. Class number\n3. Class name\n4. Student name\n5. Type\n6. All\nChoice: "; cin >> choice; if (choice == 1 || choice == 6) { do { cout << "New date (YYMMDD): "; cin >> record.date; } while (!isValidDate(record.date)); } if (choice == 2 || choice == 6) { do { cout << "New class number (1-10): "; cin >> record.classNumber; } while (record.classNumber < 1 || record.classNumber > 10); } if (choice == 3 || choice == 6) { cout << "New class name: "; cin >> record.className; } if (choice == 4 || choice == 6) { cout << "New student name: "; cin >> record.studentName; } if (choice == 5 || choice == 6) { int type; do { cout << "New type (1-late, 2-leave early, 3-ask for leave, 4-cut class): "; cin >> type; } while (type < 1 || type > 4); record.type = static_cast<AbsenceType>(type); } saveToFile(); } void deleteRecord() { if (records.empty()) { cout << "No records found.\n"; return; } showAllRecords(); int choice; cout << "Enter record number to delete (-1 to delete all): "; cin >> choice; if (choice == -1) { records.clear(); } else if (choice >= 0 && static_cast<size_t>(choice) < records.size()) { records.erase(records.begin() + choice); } else { cout << "Invalid choice.\n"; return; } saveToFile(); } void generateStatistics() { if (records.empty()) { cout << "No records found.\n"; return; } string startDate, endDate; do { cout << "Enter date range (YYMMDD-YYMMDD): "; cin >> startDate >> endDate; } while (!isValidDate(startDate) || !isValidDate(endDate)); vector<Record> filtered; copy_if(records.begin(), records.end(), back_inserter(filtered), [&](const Record& r) { return r.date >= startDate && r.date <= endDate && r.type == CUT_CLASS; }); if (filtered.empty()) { cout << "No records found in this period.\n"; return; } cout << "\nStatistics from " << startDate << " to " << endDate << ":\n"; cout << "1. By student\n2. By class\nChoice: "; int choice; cin >> choice; if (choice == 1) { vector<pair<string, int>> counts; for (const auto& r : filtered) { auto it = find_if(counts.begin(), counts.end(), [&](const pair<string, int>& p) { return p.first == r.studentName; }); if (it != counts.end()) { it->second++; } else { counts.emplace_back(r.studentName, 1); } } sort(counts.begin(), counts.end(), [](const pair<string, int>& a, const pair<string, int>& b) { return b.second < a.second; }); cout << "Student name Count\n"; for (const auto& p : counts) { cout << left << setw(20) << p.first << p.second << endl; } } else if (choice == 2) { vector<pair<string, int>> counts; for (const auto& r : filtered) { auto it = find_if(counts.begin(), counts.end(), [&](const pair<string, int>& p) { return p.first == r.className; }); if (it != counts.end()) { it->second++; } else { counts.emplace_back(r.className, 1); } } sort(counts.begin(), counts.end(), [](const pair<string, int>& a, const pair<string, int>& b) { return b.second < a.second; }); cout << "Class name Count\n"; for (const auto& p : counts) { cout << left << setw(20) << p.first << p.second << endl; } } } }; #include<windows.h> int main() { SetConsoleOutputCP(65001); AttendanceSystem system; while (true) { cout << "\n学生考勤管理系统\n" << "1. 显示所有记录\n" << "2. 添加新记录\n" << "3. 搜索学生\n" << "4. 编辑记录\n" << "5. 删除记录\n" << "6. 生成统计信息\n" << "7. 退出\n" << "退出: "; int choice; cin >> choice; switch (choice) { case 1: system.showAllRecords(); break; case 2: system.addRecord(); break; case 3: system.searchStudent(); break; case 4: system.editRecord(); break; case 5: system.deleteRecord(); break; case 6: system.generateStatistics(); break; case 7: return 0; default: cout << "Invalid choice.\n"; } cout << "\nContinue? (y/n): "; char cont; cin >> cont; if (tolower(cont) != 'y') break; } return 0; } 给我生成一个这个代码的程序设计报告
最新发布
06-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值