cin.good函数的用法
时间: 2023-09-05 17:07:18 浏览: 139
`cin.good()` 是 C++ 标准库提供的流状态查询函数之一,用于判断输入流是否处于“好”的状态,即是否可以继续读取。如果输入流处于“好”的状态,`cin.good()` 返回 `true`;否则返回 `false`。
一般来说,`cin.good()` 在读取数据时用于判断输入是否合法,可以结合 `while` 循环使用,例如:
```cpp
int x;
while (cin >> x && x < 10) {
// ...
}
```
其中,如果输入不为整数或者输入的整数大于等于 10,`cin.good()` 将返回 `false`,`while` 循环结束,程序继续执行下一行代码。如果输入合法,`cin.good()` 将返回 `true`,`while` 循环继续执行。
相关问题
运用贪心算法,每次挑选目前单价最高且重量未超过背包剩余容量的物品。补全以下代码#include <iostream.h> #include <stdio.h> typedef struct { char name[16]; // 名称 double p; // 价值 double w; // 重量 double r; // 单价 }good; int main() { int n, i, j, k; good *G, temp; double C; // 存储当前背包容量 double value; // 背包当前所装物品总价 freopen("Knapsack.in", "r", stdin); freopen("Knapsack.out", "w", stdout); cin>>n; cin>>C; G = new good[n]; for(i=0; i<n; i++) { cin>>G[i].name; cin>>G[i].p; cin>>G[i].w; G[i].r=G[i].p/G[i].w; } //************************************************ //================================================= cout<<"Total price: "<<((int)(100*value+0.5))*0.01<<endl; return 0; }
补全的代码如下:
```c++
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef struct {
char name[16]; // 名称
double p; // 价值
double w; // 重量
double r; // 单价
} good;
// 比较函数,用于排序
bool cmp(good a, good b) {
return a.r > b.r;
}
int main() {
int n, i, j, k;
good *G, temp;
double C; // 存储当前背包容量
double value = 0; // 背包当前所装物品总价
freopen("Knapsack.in", "r", stdin);
freopen("Knapsack.out", "w", stdout);
cin >> n;
cin >> C;
G = new good[n];
for(i = 0; i < n; i++) {
cin >> G[i].name;
cin >> G[i].p;
cin >> G[i].w;
G[i].r = G[i].p / G[i].w;
}
// 按照单价从高到低排序
sort(G, G + n, cmp);
// 按照贪心算法选择物品
for(i = 0; i < n; i++) {
if(G[i].w <= C) { // 若当前物品可以放入背包
C -= G[i].w; // 更新背包容量
value += G[i].p; // 更新背包所装物品总价
} else { // 若当前物品无法放入背包
value += G[i].r * C; // 将背包剩余容量全部用于装当前物品
break;
}
}
// 输出结果
cout << "Total price: " << ((int)(100 * value + 0.5)) * 0.01 << endl;
return 0;
}
```
代码中使用了 `sort` 函数进行排序,需要添加头文件 `algorithm`。在选择物品时,如果当前物品无法放入背包,则将背包剩余容量全部用于装当前物品,这是贪心算法的核心思想之一。最后输出结果时,使用了四舍五入的方法,即将数值乘以 100 后加上 0.5,再取整数部分,最后除以 100。
利用C++,不用类函数编程实现散列表的应用(可根据实际情况进行调整), 选择合适散列函数和冲突处理方法,利用文件方式储存在D盘,编程实现QQ账户的申请与登录。有账号可以实现登录,没有询问是否注册新的账号并保存在文件
以下是一个简单的散列表的实现,用于QQ账户的申请与登录:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
// 定义哈希表大小
const int TABLE_SIZE = 100;
// 定义哈希表节点结构体
struct HashNode {
string key;
string value;
HashNode* next;
HashNode(string k, string v) : key(k), value(v), next(NULL) {}
};
// 定义哈希表类
class HashTable {
private:
vector<HashNode*> table;
int hash(string key);
public:
HashTable();
~HashTable();
bool contains(string key);
void put(string key, string value);
string get(string key);
};
// 构造函数,初始化哈希表
HashTable::HashTable() {
table = vector<HashNode*>(TABLE_SIZE, NULL);
}
// 析构函数,释放哈希表内存
HashTable::~HashTable() {
for (int i = 0; i < TABLE_SIZE; i++) {
HashNode* node = table[i];
while (node != NULL) {
HashNode* temp = node;
node = node->next;
delete temp;
}
}
}
// 哈希函数,根据key计算hash值
int HashTable::hash(string key) {
int sum = 0;
for (int i = 0; i < key.length(); i++) {
sum += key[i];
}
return sum % TABLE_SIZE;
}
// 判断哈希表中是否存在某个key
bool HashTable::contains(string key) {
int index = hash(key);
HashNode* node = table[index];
while (node != NULL) {
if (node->key == key) {
return true;
}
node = node->next;
}
return false;
}
// 往哈希表中添加一个键值对
void HashTable::put(string key, string value) {
int index = hash(key);
HashNode* node = table[index];
while (node != NULL) {
if (node->key == key) {
node->value = value;
return;
}
node = node->next;
}
HashNode* newNode = new HashNode(key, value);
newNode->next = table[index];
table[index] = newNode;
}
// 根据key获取哈希表中对应的value
string HashTable::get(string key) {
int index = hash(key);
HashNode* node = table[index];
while (node != NULL) {
if (node->key == key) {
return node->value;
}
node = node->next;
}
return "";
}
// 检查文件是否存在
bool fileExists(string filename) {
ifstream file(filename);
return file.good();
}
int main() {
HashTable ht;
// 检查文件是否存在,如果存在就把账户信息读入哈希表
if (fileExists("accounts.txt")) {
ifstream infile("accounts.txt");
string line;
while (getline(infile, line)) {
int pos = line.find(",");
string username = line.substr(0, pos);
string password = line.substr(pos + 1);
ht.put(username, password);
}
}
bool loggedIn = false;
while (!loggedIn) {
string username;
string password;
// 登录
cout << "请输入用户名:";
cin >> username;
if (ht.contains(username)) {
cout << "请输入密码:";
cin >> password;
if (ht.get(username) == password) {
cout << "登录成功!" << endl;
loggedIn = true;
}
else {
cout << "密码错误!" << endl;
}
}
else {
// 注册新账号
cout << "账号不存在,是否注册新账号?(y/n)";
char c;
cin >> c;
if (c == 'y') {
cout << "请输入密码:";
cin >> password;
ht.put(username, password);
ofstream outfile("accounts.txt", ios::app);
outfile << username << "," << password << endl;
outfile.close();
cout << "注册成功!" << endl;
loggedIn = true;
}
else {
cout << "请重新输入用户名!" << endl;
}
}
}
return 0;
}
```
该程序使用了一个简单的哈希函数,将用户名作为key,密码作为value,存入哈希表中。在程序启动时,它会检查是否存在储存账户信息的文件,如果存在就把文件中的账户信息读入哈希表中。然后,它会提示用户输入用户名和密码,如果已经存在该用户名,则检查密码是否正确,如果密码正确,则登录成功,程序结束。如果不存在该用户名,则提示用户是否注册新账号,如果输入“y”,则要求用户输入密码,并把用户名和密码存入哈希表和文件中,然后登录成功,程序结束。如果输入“n”,则要求用户重新输入用户名。