简单的初学者C ++,递增和递减问题

时间:2015-01-31 10:23:47

标签: c++

我正在使用C ++,并且我有一定的增量和减量,但我有一个等式,我必须递减theChar等式,或者int var递减-2,我不知道它的代码。

2 个答案:

答案 0 :(得分:1)

请更好地制定您的问题。

是什么意思?
  

我有一个等式,我必须减少“theChar”等式,或者   带有“int var”by -2

的那个

你的意思是:

char x = 'a';
x = x + 3; //now x is 'd'
int var = 10; 
var -= 2; //equal to var = var -2; 

答案 1 :(得分:-1)

方程式不是方程式数学意义上的方程式。

=符号告诉计算机将右侧的内容存储在左侧的变量中。

int a;
a = 5;

这会将5存储到a一次。

int a, b;
a = 5;
b = a;
a = 6;

b仍然是5,因为它存储时会从a复制。 a更改时b未重新计算,但保持不变。

int a;
a = 5;
a = a - 2;

a现在正在减少2,因为a设置为5,当计算右侧(a - 2)时,它被计算为3 a。完成后,它会被写入左侧,恰好是a,所以此时3会被char c = 'B'; c = c - 1; 覆盖;

c

'A'在此代码末尾的值为'B'。幕后有一些魔术在继续。 字符也是数字。那么当我将66存储到计算机实际存储的变量66中时会发生什么。您可以阅读here。当我递减1时,该值从65递减到65。数字'A'的字符恰好是#include <iostream> using namespace std; int main() { int a, b; char c; //cout is like a friend that you give something to put on the console // << means give this to cout cout << "Hello World!" << endl; //endl is a new line character cout << endl << "Setting a, b" << endl; a = 5; b = a; cout << "Value of a is " << a << ". " << "Value of b is " << b << "." << endl; cout << endl << "Changing a" << endl; a = 3; cout << "Value of a is " << a << ". " << "Value of b is " << b << "." << endl; cout << endl << "Adding to a" << endl; a = a + 3; cout << "Value of a is " << a << ". " << "Value of b is " << b << "." << endl; cout << endl << "Playing around with characters" << endl; c = 'B'; cout << "Character c is " << c << ". " << "Number stored for c is actually " << (int)c << "." << endl; c = c + 1; cout << "Character c is " << c << ". " << "Number stored for c is actually " << (int)c << "." << endl; c = 70; cout << "Character c is " << c << ". " << "Number stored for c is actually " << (int)c << "." << endl; }

我在评论中读到,您无法将其全部放入程序中。 我继续为你写了一段代码片段。

{{1}}