第3类:您好,变量!

因此,我们简要介绍了Hello World,可以稍微了解一些C ++代码。 但是到处看看代码,是时候编写我们自己的代码了! 为了从基础开始,我们将学习一些数据类型,例如整数,双精度数,字符串和流。 一旦知道如何使用这些非常简单的工具,我们就可以开始编写一些有用的程序。

Stream是程序员在谈论输入或输出时所用的惯用词。 现在,假设我要求您输入一个数字,然后输入8490。您在键盘上键入此数字,然后按Enter。 不知何故,这个数字进入了我的程序,我可以将其用于计算或其他需要的东西。 想象一下,这些信息以流的形式从计算机键盘通过键盘传输到我的程序-一种数字信息流,几乎没有数据。 好,所以没有鱼。 但是您能想象现在是什么流吗? 同样,如果我告诉我的程序输出“ Hello World!”字样,则该信息必须经过另一个流-从我的程序到您的监视器。 两种类型的流称为输入流和输出流。 回顾Hello World,您能确定输出流是什么吗?

如果您猜对了,那是正确的! 这是C ++程序中的标准输出设备-它已为我们(程序员)预先定义,因此易于使用。 我们需要做的就是包括必要的头文件<iostream>(对于Input / Output STREAM),并说我们正在使用称为std的一组变量。 对应的预定义输入流变量称为cin-用于Common INput。

为了使用输入,您需要在计算机中留出一定的空间来存储用户为您提供的值。 这个房间称为变量。 如果您在学校学习过代数,那么您已经知道变量的基础知识。 变量可以保存任何值,程序运行的方式取决于此值。 用户可以输入一个整数值,您的程序将输出该数字的平方。 显然,程序无法知道用户将输入什么,因此我们必须使用变量才能使我们的代码适用于许多情况。但是,要使用变量,我们必须了解一些简单的数据类型。 简单数据类型有以下三种形式:整数,浮点数和枚举。 稍后我们将担心枚举数据类型。

整体数据类型有几种形式。 以下是数据类型的全名,其后是其C ++昵称,然后是描述:

整数(int):整数数据类型。 整数是任何整数,例如5、2000或-32。 但是,整数变量不能包含十进制数,例如43.34。 整数数据类型非常通用,通常可以容纳-2147483648和2147483647之间的任何数字。因此,我们将最常使用整数作为数字值。 我说“通常”是因为整数的具体大小由不同的系统确定。 我使用的系统(Windows XP)具有4字节整数(-2147483648至2147483647),但是某些系统使用2字节整数(-32,768至32,767)。

为了找出您的系统使用哪个int,请在您的C ++编译器中运行以下简短程序:

#include <limits>
#include <iostream> 
int main() {
    std::cout << "Int values range from " << INT_MIN << " to " << INT_MAX << std::endl;
    system("PAUSE");
    return 0;
}
Long(长整数):另一种整数数据类型。 此数据类型向您的系统指定您要使用4字节int类型,其值介于-2147483648至2147483647之间。

短(短):与长类型相似,但指定2个字节的整数(值-32,768至32,767)。

布尔(布尔):如果您已经学习了预演算类,那么您将熟悉布尔表达式。 布尔变量仅包含两个可能的值之一-true或false。 布尔变量用于使用if ...语句和循环来控制程序的某些方面。 我们很快将讨论这些结构。

字符(字符):信不信由你,您已经熟悉字符数据类型。 字符是任何单个文本单元-例如'e'或反斜杠'\',甚至是句点。 由于输出中仅使用255个标准字符,因此计算机将这些数据保存为-128到127之间的整数。 现在,由于计算机将字符存储为数字,因此会出现一些不规则现象。 字符“ A”不是1-实际上是65。此外,我们可以在字符变量中添加数字以更改其表示的符号-如果我在前面的“ A”中添加3,则char变量将为68或“ D”。 最后,字符值“ 5”不是5,因此在使用字符保存数字时要小心。

浮点数据类型填补了最后的空白-保留十进制数字。 浮点数据类型也可以保存整数,但是该数据类型可以保存32.0,而不是保存32。

浮点数(float):通用的十进制数字类型。 此数据类型可以保存-3.4E38和3.4E38之间的任何值(即-3.4 * 10 ^ 38和2.4 * 10 ^ 38)。 浮点变量最多可在任何变量中包含6或7个有效数字。 因此,如果我想将值3.141592653589 ...存储在浮点中,则该变量将仅保留3.141592-足够接近以用于常见目的,而不能用于极其精确的数学目的。 尽管浮点数据类型具有通用性,但C ++程序员通常使用...

Double(double):与浮点数据类型一样,double保留一个十进制数字,但是double的范围是-1.7E308到1.7E308。 有很多价值! 此外,双精度型最多可容纳15个有效数字。 在上面的示例中,这就是我输入的所有数字以及更多数字! 显然,double数据类型比它的对应类型更加准确(因此很有用)-因此,在需要十进制数的任何时候,我们都会使用double。

一种最终的数据类型称为字符串。

字符串(字符串):用于保存单词或文本的字符集合。 在“ Hello World”中,“ Hello,World!” 代码的一部分是字符串。 您能看到它是如何收集字符的吗? 首先是字符“ H”,然后是“ e”,然后是“ l”,依此类推。

现在,为了使用变量,我们必须首先声明它。 以下是一些变量声明的示例,以及一些说明如何将值设置为变量的语句:

int x; // Integer variable called x
long numOfBoxes; // Long variable called numOfBoxes
bool isDone; // Boolean variable called isDone
char decision; // Character variable called decision
string someText;
float pi;
double betterPI; 
x = 32; // Any time we use x, the value 32 is used instead.  This is called intialization,  
as it is the first value set into x.
isDone = false; // Boolean variables can be set to either true or false - these are reserved words in C++.  Anytime you use true or false, the C++ compiler knows what you mean.  NOTE: False and True are different than false and true, since C++ is case-sensitive. 
decision = 'Y'; // In order to let C++ know that Y is a character, we enclose the letter in single-quotation marks.
someText = "This is a sample string."; // Like the character, we need to let C++ know that the following text is a string.  To do so, we enclose the text in double-quotation marks.
pi = 3.141592;
betterPI = 3.141592653589; 
int y = 16; // This is the same as two statements - one to declare y, and another to set y to 16.  We can type it like this, however, to save some time.
现在,我们可以使用一些与变量有关的新功能,例如获取用户输入并在一行中声明多个变量。
int a = 0, b = 1; // Declares a and b, both integer types, and sets them to 0 and 1, respectively. 
cin >> a; // This will wait for the user to enter in a value, and then store that value inside a.  Note that, since the user is inputting to a, any previous information a held is lost.  That is, a no longer holds 0 - that 0 is gone forever. 
double q = 0.0; // Same as initializing to 0, but use a decimal number with doubles in order to remind yourself that it is a decimal. 
bool amICool = true, amILame = false; 
cin >> b >> q; // This waits for two values from the user.  The first goes into b, and whatever remains goes into q.
现在用cin解释一些不合规定之处。

cin在确定输入时会跳过所有空格字符。 空格字符是空格(''),换行符(表示为'\ n',并在您按下'enter'键时创建)或制表符(表示为'\ t')。 请注意,即使我们将Tab键和换行符表示为两个字符(反斜杠和字母),C ++仍将其视为一个字符。 这些称为转义序列,将在下一章进行说明。 由于cin跳过空格,因此它使用空格的位置等来确定值何时开始或停止。 例如,考虑以下示例:

// User is prompted for input
cin >> a; // User enters 34 and hits enter; 34 goes into a 
cin >> b; // User enters a space, then 4, then another space, then 7, then enter.  4 goes into b, 6 is still in the input stream
cin >> a; // The 6 from the last input goes into a, since it was still in the stream 
double m;
cin >> q >> m; // User enters 12.3, then a space, then 65.77, then hits enter.  q gets 12.3 and m gets 65.77. 
cin >> a >> q; // Inputting into two different data types!  Note that this is completely legal.  User enters 3, then a space, then 5; a gets 3, q gets 5.0 (since it is a double) 
cin >> a >> q; // User enters 34.05, then enter.  You may think this would be wrong, but it actually works.  Since a is an integer, cin looks for the first integer - namely, 34.  a gets 34, and q gets the leftover - 0.05. 
string myMessage;
cin >> myMessage; // User enters "Sunshine", "Sunshine" goes into myMessage;
cin >> someText; // User enters "The Rain in Spain."  Since cin skips whitespace, only "The"  
is stored in someText - the rest of the data is stored for later.
// If we want to get the whole line inside someText, we have to use a function called getline, as follows:
getline(cin, someText); // User enters "The Rain in Spain." and all this text is stored in someText.  getline gets all text from the start of the stream until the newline character.
cin >> decision; // User enters a; decision now stores 'a'
cin >> decision; // User enters abcd; decision now stores 'a', and 'bcd' is stored for later.
cin >> decision >> a; // User enters 25; decision gets '2', and a gets 5.
最后,简要说明输出。

为了输出,您必须像在Hello World中一样使用cout。 键入cout,然后键入插入操作(在C ++中称为<<),然后键入您想要显示的内容。 您可以显示任何这些简单的数据类型,因为C ++知道它们是什么以及如何使用它们。 您还可以通过将这些文本括在引号中来显示未存储在变量内的文本。 Hello World利用了此功能。 关于cout的最后一点说明是,必须通过<<操作符分隔不同的元素。 因此,如果要显示一些文本,则可以键入要显示的文本,使用最终引号将文本结尾,然后是<<,然后输入变量名,然后<<,然后endl以转到新的文本。线。 监视程序将显示您的文本,变量值,然后转到新行。 考虑一些简单输出的示例:

cout << "What's up?" << endl; // Moniter displays the text What's up?
cout << "5 + 3 = " << 5 + 3 << endl; // Moniter displayes 5 + 3 = 8.  The left side of the equation was enclosed in quotation marks, so no operations are performed on the numbers.  The right side, however, was not enclosed - C++ adds these numbers together to get 8, and displays 8.
cout << "a^2 = " << a * a << endl; // Moniter displays a^2 = , and then the value of a squared.  There is no ^ operation in C++, so if we want to, say, cube a, we must write a * a * a.  In C++, * means multiplicatiopn, instead of x or X.  Similarly, / means division.
cout << "Please enter an integer: "; // Displays the text, but does not go to a new line!  This looks good if you want to follow this display with a prompt for user input, as the user's input will appear on the same line.
string message = "Hello, World!";
cout << message << endl; // Moniter displays Hello, World! - the value stored in message.
希望对输出,输入和变量的简短说明不会使您完全困惑! 为了了解您是否了解基本知识,我希望您编写一个程序

执行以下操作:

1)声明3个整数变量a,b和c

2)显示一条消息,要求用户输入

3)提示用户输入a,b和c

4)显示一条包含a,b和c值的消息

5)声明2个字符串变量

6)显示一条消息,要求用户输入字符串

7)提示用户使用cin输入

8)使用getline提示用户输入第二个字符串

9)显示用户输入的文本

10)您程序的最后两行必须是:

系统(“ PAUSE”);

返回0;

使用下面的C ++程序的基本外壳开始使用:

#include <iostream>

使用名称空间std;

int main(){

//变量声明

//其他陈述

//显示语句

系统(“ PAUSE”);

返回0;

}

如果您想让我检查您的工作,请把您的代码生成给我。

直到下一次,当我们将研究更多的流函数和转义字符时!

From: https://bytes.com/topic/c/insights/738840-class-3-hello-variables

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值