c++程序设计语言第三章练习

6.6.1

int i = 0;
while(i < max_length)
{
    if(*input_line == '?')
        quset_count++;
}

6.6.2

	(a = (((b + (c * d)) << 2) & 8))
    (a & (077 != 3))
    ((a == b) || ((a == c) && (c < 5)))
    (c = (x != 0))
    ((0 <= i) < 7)
    ((f(1, 2)) + 3)
    (a = ((((- 1) +) + (b --)) - 5))
    (a = (b == (c++)))
    (a = (b = (c = 0)))
    ((a[4][2]) *= (* b)) ? c : ((* d) * 2)
    ((a - b), (c = d))

6.6.3

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main(void)
{
    map<string, int> vec;
    string key;
    int num;
    for(int i = 0; i < 3; i++)
    {
        cout << "输入名字:";
        getline(cin, key);
        cout << "输入数字:";
        (cin >> num).get();
        vec[key] = num;
    }
    int sum = 0;
    for(auto it : vec)
    {
        cout << it.first << ' ' << it.second << endl;
        sum += it.second;
    }
    cout << "平均值为:" << sum / 3 << endl;
    return 0;
}

6.6.4

#include <iostream>
using namespace std;
int main(void)
{
    cout << (0 & 1) << endl
         << (0 | 1) << endl
         << (0 ^ 1) << endl
         << (0 << 1) << endl
         << (0 >> 1) << endl
         << (~0) << ' ' << (~1) << endl;
    return 0;
}

6.6.5
不是很清楚他题在说什么,还是c++ primer or c++primer plus好一点

6.6.6
比如字符集问题直接使用ascii码值而不使用字符,这个似乎比较多 但一下举10个害真举不出来

6.6.7
网上搜了下求值顺序未定义的意思
就是比如: a + b 先算a或者先算b的问题 这是由编译器决定的

6.6.8
用vscode试了下,会有个警告 运行之后无响应结束程序.上下溢的话是会从相应方向的溢出到相反的最大值,就是上溢会从数据最小值重新开始计数

6.6.9

	(*(p++))//后缀++并不会导致值的直接改变
    (*(--p))
    (++(a--))//从右向左
    ((int*)(p->m))
    (*(p.m))
    (*(a[i]))

6.6.10

strlen参数是一个c风格的字符串返回无符号整数
strcpy参数是二个c风格字符串,前者是待复制的,后者是目标,返回前者指针
strcmp参数是二个c风格字符串,根据字符的ascii比较,返回值0表示相等1表示前者大于后者,-1表示后者大于前者
6.6.11
提示error:expected primary-expression before ‘=’ token
6.6.12

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main(void)
{
    map<string, int> vec;
    string key;
    int num;
    for(int i = 0; i < 3; i++)
    {
        cout << "输入名字:";
        getline(cin, key);
        cout << "输入数字:";
        (cin >> num).get();
        vec[key] = num;
    }
    int sum = 0;
    int zjz = 0, i = 0;
    if(vec.size() % 2 == 0)
    {
        for(auto it : vec)
        {
            if(i == vec.size() / 2 || i == vec.size() / 2 + 1)
            {
                zjz += it.second;
            }
            cout << it.first << ' ' << it.second << endl;
            sum += it.second;
            i++;
        }
        zjz /= 2;
    }
    else
    {
        for(auto it : vec)
        {
            if(i == vec.size() / 2)
            {
                zjz += it.second;
            }
            cout << it.first << ' ' << it.second << endl;
            sum += it.second;
            i++;
        }
    }
    cout << "平均值为:" << sum / 3 << " 中间值为:" << zjz << endl;
    return 0;
}

6.6.13

#include <iostream>
#include <cstring>
using namespace std;
char * cat(const char *, const char *);
int main(void)
{
    char * s = cat("cs", "xswl");
    cout << s;
    delete [] s;
    return 0;
}
char * cat(const char * s1, const char * s2)
{
    char * temp = new char[strlen(s1) + strlen(s2) + 1];
    int i;
    for(i = 0; s1[i]; i++)
    {
        temp[i] = s1[i];
    }
    for(int j = 0; s2[j]; j++, i++)
    {
        temp[i] = s2[j];
    }
    return temp;
}

6.6.14

#include <iostream>
#include <cstring>
using namespace std;
void rev(char *);
int main(void)
{
    char temp[] = "Hello";
    rev(temp);
    cout << temp << endl;
    return 0;
}
void rev(char * p)
{
    char ch;
    for(int i = 0, j = strlen(p) - 1; i < strlen(p) / 2; i++, j--)
    {
        ch = p[i];
        p[i] = p[j];
        p[j] = ch;
    }
}

6.6.15
针不太清楚在做什么 这个语法
6.6.16

int atoi(const char * s)
{
    int sum = 0;
    if(s[0] == '0' && s[1] == 'x')
    {
        for(int i = 2; i < strlen(s); i++)
        {
            sum += (s[i] - '0') * pow(16, (strlen(s) - i - 1));
        }
    }
    else if(s[0] == '0')
    {
        for(int i = 1; i < strlen(s); i++)
        {
            sum += (s[i] - '0') * pow(8, (strlen(s) - i - 1));
        }
    }
    else
    {
        for(int i = 0; i < strlen(s); i++)
        {
            sum += (s[i] - '0') * (strlen(s) - i);
        }
    }
    return sum;
}

6.6.17

char * itoa(int n, char * a)
{
    int count = 0;
    int temp = n;
    while(temp % 10)
    {
        temp /= 10;
        count++;
    }
    for(int i = count - 1; i >= 0; i--)
    {
        a[i] = n % 10 + '0';
        n /= 10;
    }
    a[count] = '\0';
    return a;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值