阿稳自修室P41:读取数字的循环

前言

当要求用户输入数字,用户就是不输入数字,输入字母符号的话,之前写的代码会出现崩溃报错,因为我们没有设计对该种情况的处理方法,今天的课程就教了我们该怎么样处理。

CIN返回false

当int a; cin >> a;,但是输入的是一个字符时,cin会因为输入类型错误返回false值,并使cin失效,可以借助这个机制实现上述功能。

cin.clear()可以恢复cin,再通过cin.get()把缓存区输入的其他的字符取出,就可以进行新一轮输入

习题1

统计鱼的重量,按q中止输入

#include <iostream>
const int Max = 5;
int main()
{
	using namespace std;
	double fish[Max];
	cout << "pls enter the weights of your fish.\n";
	cout << "you may enter up to " << Max << " fish <q to terminate>.\n";
	cout << "fish #1: ";
	int i = 0;
	while (i< Max && cin >> fish[i])
	{
		if (++i < Max)
			cout << "fish #" << i + 1 << ": ";
	}
	double total = 0.0;
	for (int j = 0; j < i; j++)
	{
		total += fish[j];
	}
	if (i == 0)
		cout << "No fish\n";
	else
		cout << total / i << "= average weight of " << i << " fish\n";
	cout << "Done.\n";
}

习题2

统计高尔夫个数,输入错误可以重新输入

#include <iostream>

const int Max = 5;

int main() {
	using namespace std;
	int golf[Max];
	cout << "Pls enter your golf scores.\n";
	cout << "You must enter " << Max << " rounds.\n";
	int i;
	for (i = 0; i < Max; i++)
	{
		cout << "round #" << i + 1 << ": ";
		while (!(cin >> golf[i]))
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Pls enter a number: ";
		}
	}
	double total = 0.0;
	for (i = 0; i < Max; i++)
		total += golf[i];

	cout << total / Max << " = average score " << Max << "rounds\n";
	return 0;

}

课后习题

可以提前退出的高尔夫

以习题2为基础,加上习题1的功能:即统计高尔夫的分数,如果输错了可以重复输入,但是如果输入的是q则直接中止输入。

例如:

Pls enter your golf scores.
You must enter 5 rounds.
round #1: 20
round #2: w
Pls enter a number: 33
round #3: q
10.6 = average score 5rounds

桂桂答题

LEETCODE二分查找

课后习题答案

可以提前退出的高尔夫
#include <iostream>

const int Max = 5;

int main() {
	using namespace std;
	int golf[Max] = { 0 };  //初始化
	cout << "Pls enter your golf scores.\n";
	cout << "You must enter " << Max << " rounds.\n";
	int i;
	for (i = 0; i < Max; i++)
	{
		cout << "round #" << i + 1 << ": ";
		while (!(cin >> golf[i]))
		{
			cin.clear();
			if (cin.get() == 'q')//判断是否输入为q
			{
				i = Max;
				break;
			}
			while (cin.get() != '\n')
				continue;
			cout << "Pls enter a number: ";
		}
	}
	double total = 0.0;
	for (i = 0; i < Max; i++)
		total += golf[i];

	cout << total / Max << " = average score " << Max << "rounds\n";
	return 0;

}
二分查找
class Solution {
public:
    int search(vector<int>& nums, int target) {
        int left = 0;
        int right = nums.size() - 1; // 定义target在左闭右闭的区间里,[left, right]
        while (left <= right) { // 当left==right,区间[left, right]依然有效,所以用 <=
            int middle = left + ((right - left) / 2);// 防止溢出 等同于(left + right)/2
            if (nums[middle] > target) {
                right = middle - 1; // target 在左区间,所以[left, middle - 1]
            } else if (nums[middle] < target) {
                left = middle + 1; // target 在右区间,所以[middle + 1, right]
            } else { // nums[middle] == target
                return middle; // 数组中找到目标值,直接返回下标
            }
        }
        // 未找到目标值
        return -1;
    }
};
上一篇
下一篇