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

Simple beginner C++, increment and decrement questions

本文关键字:问题 初学者 C++ 简单      更新时间:2023-10-16

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

请更好地表述您的问题。你的意思是什么

有一个方程,我必须递减"theChar"方程,或者 带有"int var"的那个 -2

你的意思是:

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

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

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

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的左侧,因此此时a3覆盖;

char c = 'B';
c = c - 1;

c在此代码末尾的值为 'A'。有一些幕后魔术正在发生。字符也是数字。那么当我'B'存储到计算机实际存储66变量中时,真正会发生什么。你可以在这里阅读。当 i 递减 1 时,该值从 66 递减为 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;
}