关于变量的讨论程序

程序如下:

#include <iostream>
#include <complex>
using namespace std;

//No initialization
void Task1()
{
    int num;
    cout << num << endl;
}

//Different data type
void Task2_1()
{
    int a = 2;
    float b = 2;
    cout << "a = " << a << endl;
    cout << "a / 4 = " << a / 4 << endl;
    cout << "b / 4 = " << b / 4 << endl;
}

//Data type calculation
void Task2_2()
{
    string str1 = "Prince Songkhla of University, ";
    string str2 = "Department of Computer Engineering";
    cout << "str1 = " << str1 << endl;
    cout << "str2 = " << str2 << endl;
    cout << "str1 + str2 = " << str1 + str2 << endl;
    
    complex <int> c1(3,4), c2(4,5);
    cout << "c1 = " << c1 << endl;
    cout << "c2 = " << c2 << endl;
    cout << "c1 + c2 = " << c1 + c2 << endl;
    cout << "c1 * c2 = " << c1 * c2 << endl;
}

//Memory address
void Task3()
{
    int n;
    float f;
    double d = 1.23;
    char c = '*';
    cout << "address of n: " << &n << endl;
    cout << "address of f: " << &f << endl;
    cout << "address of d: " << &d << endl;
    cout << "address of c: " << &c << endl;
}

//Address type
void Task4()
{
    int n;
    float f;
    double d = 1.23;
    char c = '*';
    
    int *pn = &n;
    float *pf = &f;
    double *pd = &d;
    char *pc = &c;
    
    cout << "address of n: " << &n << endl;
    cout << "address of f: " << &f << endl;
    cout << "address of d: " << &d << endl;
    cout << "address of c: " << &c << endl;
    cout << "pn: " << pn << endl;
    cout << "pf: " << pf << endl;
    cout << "pd: " << pd << endl;
    cout << "pc: " << pc << endl;
}

//Read/write memory
void Task5()
{
    int n;
    float f;
    double d = 1.23;
    char c = '*';
    
    int *pn = &n;
    float *pf = &f;
    double *pd = &d;
    char *pc = &c;
    
    *pn = 999;
    *pf = 888;
    *pd = 777;
    *pc = 'A'; 
    
    cout << "n: " << n << ", *pn = " << *pn << endl;
    cout << "f: " << f << ", *pf = " << *pf << endl;
    cout << "d: " << d << ", *pd = " << *pd << endl;
    cout << "c: " << c << ", *pc = " << *pc << endl;
}

//Memory calculation
void Task6()
{
    int n1 = 12, n2 = 87;
    char c1 = '9', c2 = 'B';

    cout << "n1: value = " << n1 << ", address = " << &n1 << endl;
    cout << "n2: value = " << n2 << ", address = " << &n2 << endl;
    cout << "c1: value = " << c1 << ", address = " << &c1 << endl;
    cout << "c2: value = " << c2 << ", address = " << &c2 << endl;
    
    int *pn = &n1;
    char *pc = &c1;
    
    cout << "pn: " << pn << endl;
    cout << "pn + 1: " << pn + 1 << endl;
    cout << "pn - 1: " << pn - 1 << endl;
    
    cout << "pc: " << pc << endl;
    cout << "pc + 1: " << pc + 1 << endl;
    cout << "pc - 1: " << pc - 1 << endl;
    
    *(pn - 1) = 367;
    cout << "n2 = " << n2 << endl;
    cin >> *(pn-1);
    cout << "n2 = " << n2 << endl;
 
    *(pn - 1) = 'K';
    cout << "c2 = " << c2 << endl;
    cin >> *(pc-1);
    cout << "c2 = " << c2 << endl;   
}

int main()
{
    //task1
    cout << "Task 1" << endl;
    Task1();
    cout << endl;

    //task2.1
    cout << "Task 2.1" << endl;
    Task2_1();
    cout << endl;
    
    //task2.2
    cout << "Task 2.2" << endl;
    Task2_2();
    cout << endl;
    
    //task3
    cout << "Task 3" << endl;
    Task3();
    cout << endl;
    
    //task4
    cout << "Task 4" << endl;
    Task4();
    cout << endl;
    
    //task5
    cout << "Task 5" << endl;
    Task5();
    cout << endl;
    
    //task6
    cout << "Task 6" << endl;
    Task6();
    cout << endl;
    
    return 0;
}


[XXXXXXX]$ g++ ccplus.cpp
[XXXXXXX]$ ./a.out

输出结果:

Task 1
32767

Task 2.1
a = 2
a / 4 = 0
b / 4 = 0.5

Task 2.2
str1 = Prince Songkhla of University, 
str2 = Department of Computer Engineering
str1 + str2 = Prince Songkhla of University, Department of Computer Engineering
c1 = (3,4)
c2 = (4,5)
c1 + c2 = (7,9)
c1 * c2 = (-8,31)

Task 3
address of n: 0x7fff556bba1c
address of f: 0x7fff556bba18
address of d: 0x7fff556bba10
address of c: *?G?z??????z?

Task 4
address of n: 0x7fff556bb9dc
address of f: 0x7fff556bb9d8
address of d: 0x7fff556bb9d0
address of c: *?G?z??????z?
pn: 0x7fff556bb9dc
pf: 0x7fff556bb9d8
pd: 0x7fff556bb9d0
pc: *?G?z??????z?

Task 5
n: 999, *pn = 999
f: 888, *pf = 888
d: 777, *pd = 777
c: A, *pc = A

Task 6
n1: value = 12, address = 0x7fff556bb97c
n2: value = 87, address = 0x7fff556bb978
c1: value = 9, address = 9W
c2: value = B, address = B9W
pn: 0x7fff556bb97c
pn + 1: 0x7fff556bb980
pn - 1: 0x7fff556bb978
pc: 9W
pc + 1: W
pc - 1: B9W
n2 = 367


关于 程序设计基石与实践 更多讨论与交流,敬请关注本博客和新浪微博 songzi_tea .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值