15 Variables and cin for input

学习用C++开发你的第一个游戏(英文)

在这里插入图片描述

# include <iostream>
# include <string>      // 引入string 库
using namespace std; // 引入命名空间std,之后的cout 和 endl 就不用再加上std:: 了
int main()
{
    // 介绍游戏
    constexpr int WORLD_LENGTH = 5; // 定义一个变量让单词的长度可变
    cout << "Welocm to Bulls and Cows, a fun word game." << endl;
    cout << "Can you guess the " << WORLD_LENGTH;
    cout << " letter isogram I'm thinking of?\n";
    cout << endl;

    // 让玩家猜一个
    cout << "输入你猜测的结果: ";
    string Guess = " ";
    cin >> Guess;

    // 让猜测继续
    cout << "你猜测的是:" << Guess << endl;

	// 让玩家猜一个
	cout << "输入你猜测的结果: ";
	cin >> Guess;

	// 让猜测继续
	cout << "你猜测的是:" << Guess << endl;

    cout << endl;
    return 0;
}

运行:
在这里插入图片描述

Create a class called Rational for performing arithmetic with fractions. Use integer variables to represent the private data of the class – the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should store the fraction in reduced form. For example, the fraction would be stored in the object as 1 in the numerator and 2 in the denominator. In order to compute the reduced form, you need to write a reduction function which uses the Euclidean algorithm to get the greatest common divisor (GCD) of the numerator and denominator first and then divides GCD to get the reduced numerator and denominator. Provide public member functions that perform each of the following tasks:(a) (5%) Subtract a Rational number from the other Rational number. The result should be stored in reduced form. (b) (5%) Divide a Rational number by the other Rational number. The result should be stored in reduced form. (c) (5%) Print Rational numbers in the form a/b, where a is the numerator and b is the denominator. (d) (5%) Compare two Rational numbers to make sure which one is smaller or they are equal. (1 for the first number, 2 for the second number and 0 if they are equal) Please also write a main function to prompt the user to input two Rational numbers (5%). Subtract one number from the other from these two numbers using (a) and then print the result using (c). Divide one number from the other from these two numbers using (b) and then print the result using (c). Compare these two Rational numbers using (d) and indicate which one is smaller or they are equal. 用c++寫,並用using namespace std;
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值