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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| #include<iostream> #include<fstream> #include<cstdlib> #include<string>
using namespace std;
struct Person { int m_Id; string m_Name; int m_Age; int m_Wage;
Person(int i, string n, int a, int w): m_Id(i), m_Name(n), m_Age(a), m_Wage(w) { } void PrintInfo() { cout << "===================" << endl; cout << "号码:\t" << this -> m_Id << endl; cout << "名字:\t" << this -> m_Name << endl; cout << "年龄:\t" << this -> m_Age << endl; cout << "工资:\t" << this -> m_Wage << endl; cout << "===================" << endl; } };
Person *p[3] = { new Person(101, "小黄", 23, 3100), new Person(102, "中黄", 41, 6600), new Person(105, "大黄", 55, 7000),
};
int main() { int i, j; Person *temp; for(i = 0; i < 3; i++) { for(j = i + 1; j < 3; j++) { if(p[i] -> m_Id > p[j] -> m_Id) { temp = p[i]; p[i] = p[j]; p[j] = temp; } } }
ofstream outfile1("f.dat", ios :: out); if(!outfile1) { cerr << "f.dat 打开错误" << endl; exit(1); } for(i = 0; i < 3; i++) { outfile1 << p[i] -> m_Id << " " << p[i] -> m_Name << " " << p[i] -> m_Age << " " << p[i] -> m_Wage << endl; } outfile1.close();
cout << "请输入2个职工信息:"; ofstream outfile2("f.dat", ios :: out | ios :: app); if(!outfile2) { cerr << "f.dat 打开错误" << endl; exit(1); } temp = new Person(0, "", 0, 0); cin >> temp -> m_Id >> temp -> m_Name >> temp -> m_Age >> temp -> m_Wage; outfile2 << temp -> m_Id << " " << temp -> m_Name << " " << temp -> m_Age << " " << temp -> m_Wage << endl; cin >> temp -> m_Id >> temp -> m_Name >> temp -> m_Age >> temp -> m_Wage; outfile2 << temp -> m_Id << " " << temp -> m_Name << " " << temp -> m_Age << " " << temp -> m_Wage << endl; outfile2.close();
ifstream infile1("f.dat", ios :: in); if(!infile1) { cerr << "f.dat 打开错误" << endl; exit(1); } for(i = 0; i < 5; i++) { infile1 >> temp -> m_Id >> temp -> m_Name >> temp -> m_Age >> temp -> m_Wage; temp -> PrintInfo(); } infile1.close();
int id; cout << "请输入一个号码:"; cin >> id; bool isFound = false; ifstream infile2("f.dat", ios :: in); if(!infile2) { cerr << "f.dat 打开错误" << endl; exit(1); } for(i = 0; i < 5; i++) { infile2 >> temp -> m_Id >> temp -> m_Name >> temp -> m_Age >> temp -> m_Wage; if(temp -> m_Id == id) { temp -> PrintInfo(); isFound = true; break; } } if(!isFound) { cerr << "没有找到这个人" << endl; } infile2.close();
return 0; }
|