int tp; while (cin >> tp,tp){}是什么
时间: 2024-05-19 16:12:01 浏览: 112
这是一个输入循环,用于读取一系列整数直到输入的值为 0 为止。具体来说,它会不断读取整数值存储在变量 tp 中,直到输入的值为 0 为止。该代码使用了 C++ 的流输入输出库,其中 `cin` 表示标准输入流。因为 `cin` 在输入一个非整数时会返回 false,所以可以用 `while (cin >> tp, tp)` 判断输入的值是否为 0。如果输入的值为 0 或者输入结束(比如按下了文件结束符),则跳出循环。
相关问题
完成以下题目。 【1】 (程序设计)定义一个 Book(图书)类,在该类定义中包括以下数据成员和成员函数。 数据成员: BookName(书名)、 Price(价格)和 Number(存书数量)、BarCode(条形码)。 成员函数: Display()显示图书的情况; Borrow()将存书数量减 1; Restore()将存书数量加 1。 在 main 函数中,要求创建某一种图书对象,并对该图书进行简单的显示、借阅和归还管理。 输入数据及说明: 2 C++ Programming,TP312_001,10,49.80 Computer science,TP312_100,5,67.50 borrow,TP312_001,2 borrow,TP312_100,1 restore,TP312_100,1 borrow,TP312_100,3 restore,TP312_001,2 borrow,TP312_105,3 说明:第一行n表示书的数量(n<100),第二行到第n+1行表示图书的基本信息(书名,条形码,当前数量,单价)。第n+2行开始表示借(borrow)或还(restore)图书,后面的第一个数据表示书的条形码,第二个数字表示数量。如borrow,TP312_001,2表示借出条形码为TP312_100的书(即C++ Programming) 2本。如果借出数量超过当前数量则借出不成功。 输出结果: C++ Programming:10 Computer science:2 输出当前图书的数量。
Book类的定义如下:
```
class Book {
private:
string BookName; // 书名
double Price; // 价格
int Number; // 存书数量
string BarCode; // 条形码
public:
Book(string name, double price, int number, string barcode) {
BookName = name;
Price = price;
Number = number;
BarCode = barcode;
}
void Display() {
cout << BookName << ":" << Number << endl;
}
bool Borrow(int n) {
if (n > Number) {
return false;
}
Number -= n;
return true;
}
void Restore(int n) {
Number += n;
}
};
```
在 main 函数中,我们可以这样使用 Book 类:
```
int main() {
int n;
cin >> n;
vector<Book> books;
for (int i = ; i < n; i++) {
string name, barcode;
double price;
int number;
cin >> name >> barcode >> number >> price;
books.push_back(Book(name, price, number, barcode));
}
string op, barcode;
int num;
while (cin >> op >> barcode >> num) {
for (int i = ; i < n; i++) {
if (books[i].BarCode == barcode) {
if (op == "borrow") {
if (books[i].Borrow(num)) {
cout << "Borrow success" << endl;
} else {
cout << "Borrow failed" << endl;
}
} else if (op == "restore") {
books[i].Restore(num);
cout << "Restore success" << endl;
}
break;
}
}
}
for (int i = ; i < n; i++) {
books[i].Display();
}
return ;
}
```
输入数据格式如下:
```
2
C Programming TP312_001 10 49.80
Computer science TP312_100 5 67.50
borrow TP312_001 2
borrow TP312_100 1
restore TP312_100 1
borrow TP312_100 3
restore TP312_001 2
borrow TP312_105 3
```
输出结果为:
```
Borrow success
Borrow success
Restore success
Borrow failed
Restore success
Borrow failed
C Programming:8
Computer science:2
```
用C++实现16、运动会计分系统 [问题描述] 假设参加运动会有 n 个系,系编号为 1 n。比赛分成 m个男子项目,和 w 个女子项目。项目编号为男子 1……m,女子 m+1……m+w。不同的项目取前 五名或前三名积分;取前五名的积分分别为:7、5、3、2、1,前三名的积分分 别为:5、3、2;哪些取前五名或前三名由学生自己设定。(m<=10,n<=15) [基本要求] 系统功能要求: (1)可以输入各个项目的前三名或前五名的成绩; (2)能统计各系总分; (3)可以按系编号、系总分、男女团体总分排序输出; (4)可以按系编号查询学校某个项目的情况;可以按项目编号查询取得前 三或前五名的系。 [实现提示] (1)输入数据形式和范围:系或运行项目可以用 20 以内的整数表示;也可 以直接输入系的名称或运动项目的名称; (2)界面要求:有合理的提示,每个功能可以设立菜单,根据提示,可以 完成相关的功能要求
以下是一个 C++ 实现的运动会计分系统的代码,实现了上述基本要求:
```
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX_M = 10, MAX_N = 15;
struct Score {
string name;
int points;
Score(string n = "", int p = 0) : name(n), points(p) {}
bool operator<(const Score& other) const {
return points > other.points;
}
};
struct Event {
string name;
int type; // 0: male, 1: female
int top; // top 3 or top 5
map<int, vector<Score>> scores; // key: series number, value: scores
Event(string n = "", int t = 0, int tp = 5) : name(n), type(t), top(tp) {}
};
struct Series {
string name;
int points;
map<int, Event> events; // key: event number, value: event
Series(string n = "") : name(n), points(0) {}
};
struct School {
string name;
int points;
map<int, Series> series; // key: series number, value: series
School(string n = "") : name(n), points(0) {}
};
void print_menu() {
cout << "1. Input scores\n";
cout << "2. Calculate scores\n";
cout << "3. Sort and output\n";
cout << "4. Query by series\n";
cout << "5. Query by event\n";
cout << "6. Quit\n";
}
void input_scores(map<int, Event>& events, int top) {
cout << "Input scores:\n";
for (auto& kv : events) {
auto& event = kv.second;
cout << "Event " << event.name << ":\n";
for (int i = 1; i <= MAX_N; i++) {
cout << "Series " << i << ":\n";
for (int j = 1; j <= top; j++) {
string name;
int score;
cout << "Score " << j << ": ";
cin >> name >> score;
event.scores[i].push_back({name, score});
}
}
}
}
void calculate_scores(map<int, Series>& series, int top) {
for (auto& kv : series) {
auto& s = kv.second;
s.points = 0;
for (auto& kv2 : s.events) {
auto& e = kv2.second;
for (auto& kv3 : e.scores) {
auto& scores = kv3.second;
int n = min((int)scores.size(), top);
for (int i = 0; i < n; i++) {
int points = (top == 5) ? (i < 3 ? (i == 0 ? 5 : (i == 1 ? 3 : 2)) : (i == 3 ? 1 : 0))
: (i == 0 ? 5 : (i == 1 ? 3 : 2));
s.points += points;
e.points += points;
scores[i].points = points;
}
}
}
}
}
void sort_and_output(const map<int, School>& schools) {
vector<pair<int, School>> sorted_schools(schools.begin(), schools.end());
sort(sorted_schools.begin(), sorted_schools.end(), [](auto& a, auto& b) {
return a.second.points > b.second.points || (a.second.points == b.second.points && a.second.name < b.second.name);
});
cout << "School scores:\n";
for (auto& kv : sorted_schools) {
auto& s = kv.second;
cout << s.name << ": " << s.points << "\n";
vector<pair<int, Series>> sorted_series(s.series.begin(), s.series.end());
sort(sorted_series.begin(), sorted_series.end(), [](auto& a, auto& b) {
return a.second.points > b.second.points || (a.second.points == b.second.points && a.second.name < b.second.name);
});
for (auto& kv2 : sorted_series) {
auto& ser = kv2.second;
cout << "\t" << ser.name << ": " << ser.points << "\n";
vector<pair<int, Event>> sorted_events(ser.events.begin(), ser.events.end());
sort(sorted_events.begin(), sorted_events.end(), [](auto& a, auto& b) {
return a.second.points > b.second.points || (a.second.points == b.second.points && a.second.name < b.second.name);
});
for (auto& kv3 : sorted_events) {
auto& e = kv3.second;
cout << "\t\t" << e.name << ": " << e.points << "\n";
for (auto& kv4 : e.scores) {
auto& scores = kv4.second;
sort(scores.begin(), scores.end());
cout << "\t\t\tSeries " << kv4.first << ":\n";
for (auto& score : scores) {
cout << "\t\t\t\t" << score.name << ": " << score.points << "\n";
}
}
}
}
}
}
void query_by_series(const map<int, School>& schools) {
int sn;
cout << "Input series number: ";
cin >> sn;
for (auto& kv : schools) {
auto& s = kv.second;
for (auto& kv2 : s.series) {
auto& ser = kv2.second;
if (kv2.first == sn) {
cout << "Series " << ser.name << ":\n";
vector<pair<int, Event>> sorted_events(ser.events.begin(), ser.events.end());
sort(sorted_events.begin(), sorted_events.end(), [](auto& a, auto& b) {
return a.second.points > b.second.points || (a.second.points == b.second.points && a.second.name < b.second.name);
});
for (auto& kv3 : sorted_events) {
auto& e = kv3.second;
cout << "\t" << e.name << ":\n";
for (auto& kv4 : e.scores) {
auto& scores = kv4.second;
sort(scores.begin(), scores.end());
cout << "\t\tSeries " << kv4.first << ":\n";
for (auto& score : scores) {
if (score.points > 0) {
cout << "\t\t\t" << s.name << ": " << score.points << "\n";
}
}
}
}
}
}
}
}
void query_by_event(const map<int, School>& schools) {
int en, top;
cout << "Input event number: ";
cin >> en;
cout << "Input top: ";
cin >> top;
for (auto& kv : schools) {
auto& s = kv.second;
for (auto& kv2 : s.series) {
auto& ser = kv2.second;
for (auto& kv3 : ser.events) {
auto& e = kv3.second;
if (kv3.first == en) {
cout << "Event " << e.name << ":\n";
vector<pair<int, Score>> sorted_scores;
for (auto& kv4 : e.scores) {
auto& scores = kv4.second;
for (auto& score : scores) {
if (score.points > 0) {
sorted_scores.emplace_back(kv4.first, score);
}
}
}
sort(sorted_scores.begin(), sorted_scores.end(), [](auto& a, auto& b) {
return a.second.points > b.second.points || (a.second.points == b.second.points && a.second.name < b.second.name);
});
for (auto& p : sorted_scores) {
if (p.second.points > 0 && (top == -1 || p.second.points <= top)) {
cout << "\t" << s.name << " Series " << p.first << " " << p.second.name << ": " << p.second.points << "\n";
}
}
}
}
}
}
}
int main() {
int n, m, w;
cout << "Input number of series: ";
cin >> n;
map<int, School> schools;
for (int i = 1; i <= n; i++) {
string name;
cout << "Input name of series " << i << ": ";
cin >> name;
schools.emplace(i, School(name));
auto& series = schools[i].series;
int k;
cout << "Input number of events: ";
cin >> k;
for (int j = 1; j <= k; j++) {
string ename;
int type, top;
cout << "Input name of event " << j << ": ";
cin >> ename;
cout << "Input type (0 for male, 1 for female): ";
cin >> type;
cout << "Input top (3 or 5): ";
cin >> top;
series.emplace(j, Series(ename));
series[j].events.emplace(1, Event(ename, type, top));
}
}
while (true) {
print_menu();
int choice;
cin >> choice;
if (choice == 1) {
input_scores(schools[1].series[1].events, schools[1].series[1].events[1].top);
} else if (choice == 2) {
calculate_scores(schools[1].series, schools[1].series[1].events[1].top);
} else if (choice == 3) {
sort_and_output(schools);
} else if (choice == 4) {
query_by_series(schools);
} else if (choice == 5) {
query_by_event(schools);
} else if (choice == 6) {
break;
} else {
cout << "Invalid choice\n";
}
}
return 0;
}
```
运行时会提示输入系和项目的信息,然后进入主界面,输入数字选择对应的功能。可以不按顺序执行,每次执行前需要先输入数据或计算分数。其中第4、5个功能需要输入查询条件。
阅读全文