我正在使用C ++,并且我有一定的增量和减量,但我有一个等式,我必须递减theChar
等式,或者int var
递减-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}}