[C++primer]第一章,第二章,第三章课后习题

第一章

1.3

#include<iostream>
using namespace std;

int main()
{
	cout << "hello world" << endl;

	system("pause");
	return 0;
}

1.4

#include<iostream>
using namespace std;

int main()
{
	int v1 = 0;
	int v2 = 0;

	cin >> v1 >> v2;

	cout << v1 << "与" << v2 <<"积为:" << v1*v2 << endl;

	system("pause");
	return 0;
}

1.6

不合法 封号

1.8

#include<iostream>
using namespace std;

int main()
{
	cout << "/*";
	cout << "*/";
	//cout <</*"*/"*/;错误会认为是不完整字符,在封号前加“就对了
	cout <</*"*/"/*"/*”*/;

	system("pause");
	return 0;
}

1.9

#include<iostream>
using namespace std;

int main()
{
	int sum = 0;
	int v = 50;
	while (v <= 100)
	{
		sum += v;
		++v;
	}
	cout << "50到100和为:" << sum << endl;

	system("pause");
	return 0;
}

1.10

#include<iostream>
using namespace std;

int main()
{
	int v = 10;
	cout << "10-0之间的整数为:" << endl;
	while (v >= 0)
	{
		cout << v<<" ";
		v--;
	}
	cout << endl;

	system("pause");
	return 0;
}

1.11

#include<iostream>
using namespace std;

int main()
{
	int v1 = 0;
	int v2 = 0;
	cout << "请输入两个数" << endl;
	cin >> v1 >> v2;
	cout << "这两个数之间整数为:" << endl;
	if (v1 > v2)
	{
		while (v1 >= v2)
		{
			cout << v2 << " ";
			v2++;
		}
	}
	else
	{
		while (v2 >= v1)
		{
			cout << v1 << " ";
			v1++;
		}
	}
	cout << endl;

	system("pause");
	return 0;
}

1.12

-100到100整数相加 sum=0

1.13

#include<iostream>
using namespace std;

void test1()
{
	int sum = 0;
	for (int i = 50; i <= 100; i++)
	{
		sum += i;
	}
	cout << "test1 sum= " << sum << endl;
}
void test2()
{
	cout << "test2 ";
	for (int i = 10; i >= 0; i--)
	{
		cout << i << " ";
	}
	cout << endl;
}
void test3()
{
	int v1 = 0;
	int v2 = 0;
	cout << "请输入两个数" << endl;
	cin >> v1 >> v2;
	cout << "这两个数之间整数为:" << endl;
	if (v1 > v2)
	{
		for (int i = v2; i <= v1; i++)
		{
			cout << i << " ";
		}
	}
	else
	{
		for (int i = v1; i <= v2; i++)
		{
			cout << i << " ";
		}
	}
	cout << endl;
}
int main()
{
	test1();
	test2();
	test3();

	system("pause");
	return 0;
}

1.16

#include<iostream>
using namespace std;

int main()
{
	int sum = 0, value = 0;

	cout << "输入一组数 ,按ctrl+z结束" << endl;
	/*while (cin >> value)
		sum += value;
	cout << sum << endl;*/

	for (; cin >> value;)
	{
		sum += value;
	}
	cout << sum << endl;


	system("pause");
	return 0;
}

1.18

#include<iostream>
using namespace std;

int main()
{
	int currval = 0;
	int val = 0;
	int count = 1;
	if (cin >> currval)
	{
		while (cin >> val)
		{
			if (currval == val)
			{
				count++;
			}
			else
			{
				cout << currval << " 出现了 " << count << " 次" << endl;
				count = 1;
				currval = val;
			}
		}
		cout << currval << " 出现了 " << count << " 次" << endl;
	}

	system("pause");
	return 0;
}

1.19

#include<iostream>
using namespace std;

int main()
{
	int v1 = 0;
	int v2 = 0;
	cout << "请输入两个数" << endl;
	cin >> v1 >> v2;

	if (v1 < v2)
	{
		for (int i = v2; i >= v1; i--)
		{
			cout << i << "  ";
		}
	}
	else
	{
		for (int i = v1; i >= v2; i--)
		{
			cout << i << " ";
		}
	}
	cout << endl;

	system("pause");
	return 0;
}

1.20

#pragma once


#ifndef SALESITEM_H
// we're here only if SALESITEM_H has not yet been defined 
#define SALESITEM_H

//#include "Version_test.h" 

// Definition of Sales_item class and related functions goes here
#include <iostream>
#include <string>

class Sales_item {
    // these declarations are explained section 7.2.1, p. 270 
    // and in chapter 14, pages 557, 558, 561
    friend std::istream& operator>>(std::istream&, Sales_item&);
    friend std::ostream& operator<<(std::ostream&, const Sales_item&);
    friend bool operator<(const Sales_item&, const Sales_item&);
    friend bool
        operator==(const Sales_item&, const Sales_item&);
public:
    // constructors are explained in section 7.1.4, pages 262 - 265
    // default constructor needed to initialize members of built-in type
#if defined(IN_CLASS_INITS) && defined(DEFAULT_FCNS)
    Sales_item() = default;
#else
    Sales_item() : units_sold(0), revenue(0.0) { }
#endif
    Sales_item(const std::string& book) :
        bookNo(book), units_sold(0), revenue(0.0) { }
    Sales_item(std::istream& is) { is >> *this; }
public:
    // operations on Sales_item objects
    // member binary operator: left-hand operand bound to implicit this pointer
    Sales_item& operator+=(const Sales_item&);

    // operations on Sales_item objects
    std::string isbn() const { return bookNo; }
    double avg_price() const;
    // private members as before
private:
    std::string bookNo;      // implicitly initialized to the empty string
#ifdef IN_CLASS_INITS
    unsigned units_sold = 0; // explicitly initialized
    double revenue = 0.0;
#else
    unsigned units_sold;
    double revenue;
#endif
};

// used in chapter 10
inlinen bool compareIsbn(const Sales_item& lhs, const Sales_item& rhs)
{
    return lhs.isbn() == rhs.isbn();
}

// nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&);

inline bool
operator==(const Sales_item& lhs, const Sales_item& rhs)
{
    // must be made a friend of Sales_item
    return lhs.units_sold == rhs.units_sold &&
        lhs.revenue == rhs.revenue &&
        lhs.isbn() == rhs.isbn();
}

inline bool
operator!=(const Sales_item& lhs, const Sales_item& rhs)
{
    return !(lhs == rhs); // != defined in terms of operator==
}

// assumes that both objects refer to the same ISBN
Sales_item& Sales_item::operator+=(const Sales_item& rhs)
{
    units_sold += rhs.units_sold;
    revenue += rhs.revenue;
    return *this;
}

// assumes that both objects refer to the same ISBN
Sales_item
operator+(const Sales_item& lhs, const Sales_item& rhs)
{
    Sales_item ret(lhs);  // copy (|lhs|) into a local object that we'll return
    ret += rhs;           // add in the contents of (|rhs|) 
    return ret;           // return (|ret|) by value
}

std::istream&
operator>>(std::istream& in, Sales_item& s)
{
    double price;
    in >> s.bookNo >> s.units_sold >> price;
    // check that the inputs succeeded
    if (in)
        s.revenue = s.units_sold * price;
    else
        s = Sales_item();  // input failed: reset object to default state
    return in;
}

std::ostream&
operator<<(std::ostream& out, const Sales_item& s)
{
    out << s.isbn() << " " << s.units_sold << " "
        << s.revenue << " " << s.avg_price();
    return out;
}

double Sales_item::avg_price() const
{
    if (units_sold)
        return revenue / units_sold;
    else
        return 0;
}
#endif


#include<iostream>
using namespace std;
#include"Sales_item.h"

int main()
{
	Sales_item book;
	cout << "请输入销售记录" << endl;

	while (cin >> book)
	{
		cout << "isbn ,本数,销售额,平均售价" << book << endl;
	}

	system("pause");
	return 0;
}  

1.21

#include<iostream>
using namespace std;
#include"Sales_item.h"

int main()
{
	Sales_item book1;
	Sales_item book2;
	Sales_item book3;

	cout << "请输入销售记录" << endl;
	cin >> book1 >> book2;
	if(compareIsbn(book1,book2))
	{
		book3 = book1 + book2;
		cout << "isbn ,本数,销售额,平均售价" << book3 << endl;
	}
    else
    {
        cout<<"isbn不同"<<endl;
    }

	system("pause");
	return 0;
}  

1.22

#include<iostream>
using namespace std;
#include"Sales_item.h"

int main()
{
	Sales_item allbook;
	Sales_item book;
	cout << "请输入" << endl;
	if (cin >> allbook)
	{
		while (cin >> book)
		{
			if (compareIsbn(allbook, book))
			{
				allbook += book;
			}
			else
			{
				cout << "isbn不同" << endl;
				return -1;
			}
		}
		cout << "isbn  本数 销售额 平均售价" << allbook << endl;
	}
	else
	{
		cout << "没有数据" << endl;
		return -1;
	}
	system("pause");
	return 0;
}  

1.23

#include<iostream>
using namespace std;
#include"Sales_item.h"

int main()
{
	Sales_item book1;
	Sales_item book2;
	int num = 1;
	cout << "请输入" << endl;
	if (cin >> book1)
	{
		while (cin >> book2)
		{
			if (compareIsbn(book1, book2))
			{
				++num;
			}
			else
			{
				cout << "isbn为" << book1.isbn() <<"数量为" << num << endl;
				num = 1;
				book1 = book2;
			}
		}
		cout << "isbn为" << book1.isbn() << "数量为" << num << endl;
	}
	else
	{
		cout << "没有数据" << endl;
		return -1;
	}
	system("pause");
	return 0;
}  

1.25

#include<iostream>
using namespace std;
#include"Sales_item.h"

int main()
{
	Sales_item total;
	Sales_item trams;
	cout << "请输入:" << endl;
	if (cin >> total)
	{
		while (cin >> trams)
		{
			if (compareIsbn(total, trams))
			{
				total += trams;
			}
			else
			{
				cout << "isbn  本数 销售额 平均售价" << total << endl;
				total = trams;
			}
		}
		cout << "isbn  本数 销售额 平均售价" << total << endl;
	}
	else
	{
		cout << "没有数据" << endl;
		return -1;
	}
	system("pause");
	return 0;
}  

第二章

2.2

double

2.3

#include<iostream>
using namespace std;

int main()
{
	unsigned u = 10, u2 = 42;
	cout << u2 - u << endl;
	cout << u - u2 << endl;

	int i = 10, i2 = 42;
	cout << i2 - i << endl;
	cout << i - i2 << endl;
	cout << i - u << endl;
	cout << u - i << endl;

	system("pause");
	return 0;
}

2.8

#include<iostream>
using namespace std;

int main()
{
	cout << "2\x4d\n";
	cout << "2\tM\n";

	system("pause");
	return 0;
}

2.18

#include<iostream>
using namespace std;

int main()
{
	int i = 5, j = 10;
	int* p = &i;
	cout << p << " " << *p << endl;

	p = &j;
	cout << p << " " << *p << endl;
	
	j = 30;
	cout << p << " " << *p << endl;
	
	system("pause");
	return 0;
}

2.34

#include<iostream>
using namespace std;

int main()
{
	int i = 0, & r = i;
	auto a = r;
	const int ci = i, & cr = ci;
	auto b = ci;
	auto c = cr;
	auto d = &i;
	auto e = &ci;
	auto& g = ci;
	cout << a << " " << b << " " << c << " " << d << " " << e << " " << g << endl;

	a = 42;
	b = 42;
	c = 42;
	//d = 42;
	//e = 42;
	//g = 42;
	cout << a << " " << b << " " << c << " " << d << " " << e << " " << g << endl;

	system("pause");
	return 0;
}

2.35

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

int main()
{
	const int i = 42;
	auto j = i;
	const auto& k = i;
	auto* p = &i;
	const auto j2 = i, & k2 = i;
	cout << typeid(i).name() << endl;
	cout << typeid(j).name() << endl;
	cout << typeid(k).name() << endl;
	cout << typeid(p).name() << endl;
	cout << typeid(j2).name() << endl;
	cout << typeid(k2).name() << endl;

	system("pause");
	return 0;
}

2.38

struct Sales_data
{
	string bookno;
	int units_sold = 0;
	double sellingprice = 0;
	double saleprice = 0;
};

2.41

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

class Sales_data
{
    friend istream& operator>>(std::istream&, Sales_data&);
    friend ostream& operator<<(std::ostream&, const  Sales_data&);
    friend bool operator<(const  Sales_data&, const  Sales_data&);
    friend bool operator==(const  Sales_data&, const  Sales_data&);

public:
	Sales_data() = default;
	Sales_data(const std::string& book) : bookNo(book), units_sold(0), sellingprice (0.0), saleprice (0.0), discount (0.0) {}
	Sales_data(std::istream& is) 
    {
        is >> *this; 
    }

public: 
    Sales_data& operator+=(const Sales_data&);
    string isbn() const 
    {
        return bookNo; 
    }

private:
	string bookNo;
	int units_sold = 0;
	double sellingprice = 0.0;
	double saleprice = 0.0;
	double discount = 0.0;
};

inline bool compareIsbn(const Sales_data& lhs, const Sales_data& rhs)
{
	return lhs.isbn() == rhs.isbn();
}

inline bool operator==(const Sales_data& lhs, const Sales_data& rhs)
{
    return lhs.units_sold == rhs.units_sold &&
        lhs.sellingprice == rhs.sellingprice &&
        lhs.saleprice==rhs.saleprice &&
        lhs.isbn() == rhs.isbn();
}

inline bool operator!=(const Sales_data& lhs, const Sales_data& rhs)
{
    return !(lhs == rhs); 
}

Sales_data& Sales_data::operator+=(const Sales_data& rhs)
{
    units_sold += rhs.units_sold;
    saleprice = (rhs.saleprice * units_sold + saleprice * units_sold) / (rhs.units_sold + units_sold);
    sellingprice = (rhs.sellingprice * units_sold + sellingprice * units_sold) / (rhs.units_sold + units_sold);
    if (sellingprice != 0)
    {
        discount = saleprice / sellingprice;
    }
    return *this;
}

Sales_data operator+(const Sales_data& lhs, const Sales_data& rhs)
{
    Sales_data ret(lhs);  
    ret += rhs;            
    return ret;          
}

istream& operator>>(std::istream& in, Sales_data& s)
{
    double price;
    in >> s.bookNo >> s.units_sold >> s.sellingprice >> s.saleprice;

    if (in && s.sellingprice != 0)
        s.discount = s.saleprice / s.sellingprice;
    else
        s = Sales_data();  // input failed: reset object to default state
    return in;
}

ostream& operator<<(std::ostream& out, const Sales_data& s)
{
    out << s.isbn() << " " << s.units_sold << " " << s.sellingprice << " " << s.saleprice << " " <<s.discount;
    return out;
}


int main()
{
	//1
    Sales_data book;
    cout << "请输入销售记录" << endl;
    while (cin>>book)
    {
        cout << "isbn 本数 原价 实价 折扣" << book << endl;
    }

    //2
    Sales_data book1;
    Sales_data book2;
    Sales_data book3;
    cout << "请输入两条记录" << endl;
    cin >> book1;
    cin >> book2;
    if (compareIsbn(book1,book2))
    {
        book3 = book1 + book2;
        cout << "isbn 本数 原价 实价 折扣" << book3 << endl;

    }
    else
    {
        cout << "isbn不相同" << endl;
    }

    //3
    Sales_data book4;
    Sales_data total;
    cout << "请输入多条记录" << endl;
    if (cin >> total)
    {
        while (cin >> book4)
        {
            if (compareIsbn(total, book4))
            {
                total += book4;
            }
            else
            {
                cout << "isbn不相同" << endl;
                return -1;
            }
        }
        cout << "isbn 本数 原价 实价 折扣" << total << endl;
    }
    else
    {
        cout << "无数据" << endl;
        return -1;
    }

    //4.
    Sales_data book5;
    Sales_data book6;
    int sum = 1;
    cout << "请输入多条记录" << endl;
    if (cin >> book5)
    {
        while (cin >> book6)
        {
            if (compareIsbn(book5, book6))
            {
                sum++;
            }
            else
            {
                cout << "isbn为: " << book5.isbn() << "数量为:" << sum << endl;
                sum = 1;
                book5 = book6;
            }
        }
        cout << "isbn为: " << book5.isbn() << "数量为:" << sum << endl;
    }
    else
    {
        cout << "无数据" << endl;
        return -1;
    }


	system("pause");
	return 0;
}

2.42

.h

//#pragma once 第二种方法代替ifndef

#ifndef SALES_DATA_H
#define SALES_DATA_H

#include<iostream>
#include<string>

class Sales_data
{
    friend istream& operator>>(std::istream&, Sales_data&);
    friend ostream& operator<<(std::ostream&, const  Sales_data&);
    friend bool operator<(const  Sales_data&, const  Sales_data&);
    friend bool operator==(const  Sales_data&, const  Sales_data&);

public:
    Sales_data() = default;
    Sales_data(const std::string& book) : bookNo(book), units_sold(0), sellingprice(0.0), saleprice(0.0), discount(0.0) {}
    Sales_data(std::istream& is)
    {
        is >> *this;
    }

public:
    Sales_data& operator+=(const Sales_data&);
    string isbn() const
    {
        return bookNo;
    }

private:
    string bookNo;
    int units_sold = 0;
    double sellingprice = 0.0;
    double saleprice = 0.0;
    double discount = 0.0;
};

inline bool compareIsbn(const Sales_data& lhs, const Sales_data& rhs)
{
    return lhs.isbn() == rhs.isbn();
}

inline bool operator==(const Sales_data& lhs, const Sales_data& rhs)
{
    return lhs.units_sold == rhs.units_sold &&
        lhs.sellingprice == rhs.sellingprice &&
        lhs.saleprice == rhs.saleprice &&
        lhs.isbn() == rhs.isbn();
}

inline bool operator!=(const Sales_data& lhs, const Sales_data& rhs)
{
    return !(lhs == rhs);
}

Sales_data& Sales_data::operator+=(const Sales_data& rhs)
{
    units_sold += rhs.units_sold;
    saleprice = (rhs.saleprice * rhs.units_sold + saleprice * units_sold) / (rhs.units_sold + units_sold);
    sellingprice = (rhs.sellingprice * rhs.units_sold + sellingprice * units_sold) / (rhs.units_sold + units_sold);
    if (sellingprice != 0)
    {
        discount = saleprice / sellingprice;
    }
    return *this;
}

Sales_data operator+(const Sales_data& lhs, const Sales_data& rhs)
{
    Sales_data ret(lhs);
    ret += rhs;
    return ret;
}

istream& operator>>(std::istream& in, Sales_data& s)
{
    double price;
    in >> s.bookNo >> s.units_sold >> s.sellingprice >> s.saleprice;

    if (in && s.sellingprice != 0)
        s.discount = s.saleprice / s.sellingprice;
    else
        s = Sales_data();  // input failed: reset object to default state
    return in;
}

ostream& operator<<(std::ostream& out, const Sales_data& s)
{
    out << s.isbn() << " " << s.units_sold << " " << s.sellingprice << " " << s.saleprice << " " << s.discount;
    return out;
}

#endif // !SALES_DATA_H

.cpp

#include<iostream>
using namespace std;
#include"Sales_item.h"

int main()
{
	Sales_item total;
	Sales_item trams;
	cout << "请输入:" << endl;
	if (cin >> total)
	{
		while (cin >> trams)
		{
			if (compareIsbn(total, trams))
			{
				total += trams;
			}
			else
			{
				cout << "isbn  本数 销售额 平均售价" << total << endl;
				total = trams;
			}
		}
		cout << "isbn  本数 销售额 平均售价" << total << endl;
	}
	else
	{
		cout << "没有数据" << endl;
		return -1;
	}
	system("pause");
	return 0;
}  

第三章

3.2

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

int main()
{
	string line;
	while (getline(cin, line))
	{
		cout << line << endl;
	}

	system("pause");
	return 0;
}
#include<iostream>
using namespace std;
#include<string>

int main()
{
	string line;
	while (cin>>line)
	{
		cout << line << endl;
	}

	system("pause");
	return 0;
}

3.4

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

int main()
{
	string s1;
	string s2;
	cin >> s1;
	cin >> s2;

	if (s1 == s2)
	{
		cout << "相等" << endl;
	}
	else if (s1 < s2)
	{
		cout << "s1<s2" << endl;
		cout << s2 << endl;
	}
	else
	{
		cout << "s1>s2" << endl;
		cout << s1 << endl;
	}
	
	system("pause");
	return 0;
}
#include<iostream>
using namespace std;
#include<string>

int main()
{
	string s1;
	string s2;
	cin >> s1;
	cin >> s2;
	auto len1 = s1.size();
	auto len2 = s2.size();
	if (len1 == len2)
	{
		cout << "相等" << endl;
	}
	else if (len1 < len2)
	{
		cout << "s1比s2短" << endl;
		cout << s2 << endl;
	}
	else
	{
		cout << "s1比s2长" << endl;
		cout << s1 << endl;
	}
	
	system("pause");
	return 0;
}

3.5

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

int main()
{
	string s1;
	string s2;
    string s3;
    
	while (cin >> s1)
	{
		s2 += s1;
        cout<<"继续y,停止n"<<endl;
        cin>>s3;
        if(s3=="y")
        {
            cout<<"输入下一串字符串"<<endl;
        }
        else
        {
            break;
        }
	}
	cout << s2 << endl;

	system("pause");
	return 0;
}
#include<iostream>
using namespace std;
#include<string>

int main()
{
	string s1;
	string s2;
	string s3;

	while (cin >> s1)
	{
		if (!s2.size())
		{
			s2 += s1;
		}
		else
		{
			s2 += s1;
			s2 += " ";
			cout << "继续y,停止n" << endl;
			cin >> s3;
			if (s3 == "y")
			{
				cout << "输入下一串字符串" << endl;
			}
			else
			{
				break;
			}
		}
		
	}
	cout << s2 << endl;

	system("pause");
	return 0;
}

3.6

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

int main()
{
	string s1;
	getline(cin, s1);
	for (auto &c : s1)
	{
		c = 'X';
	}
	cout << s1 << endl;

	system("pause");
	return 0;
}

3.7

auto会自动识别为char

3.8

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

int main()
{
	string s1;
	getline(cin, s1);
	decltype(s1.size()) i = 0;
	while (i != s1.size())
	{
		s1[i] = 'X';
		i++;
	}
	cout << s1 << endl;
	system("pause");
	return 0;
}
#include<iostream>
using namespace std;
#include<string>

int main()
{
	string s1;
	getline(cin, s1);
	decltype(s1.size()) i = 0;
	for(;i<s1.size();i++)
	{
		s1[i] = 'X';
	}
	cout << s1 << endl;
	system("pause");
	return 0;
}

3.10

#include<iostream>
using namespace std;
#include<string>
#include<cctype>

int main()
{
	string s1;
	getline(cin, s1);
	decltype(s1.size()) i = 0;
	for(;i<s1.size();i++)
	{
		if (ispunct(s1[i]))
		{
			for (decltype(s1.size()) j = i; j < s1.size(); j++)
			{
				s1[j] = s1[j + 1];
			}
		}
	}
	cout << s1 << endl;

	system("pause");
	return 0;
}

可以看答案更简洁

3.14

#include<iostream>
using namespace std;
#include<vector>
#include<string>

int main()
{
	vector<int> v;
	int i = 0;
	string cont = "y";
	cout << "请输入整数" << endl;
	while (cin >> i)
	{
		v.push_back(i);
		cout << "继续添加y 停止n" << endl;
		cin >> cont;
		if (cont != "y")
		{
			break;
		}
	}
	for (auto c : v)
	{
		cout << c << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

3.15

#include<iostream>
using namespace std;
#include<vector>
#include<string>

int main()
{
	vector<string> v;
	string i = "";
	string cont = "y";
	cout << "请输入字符串" << endl;
	while (cin >> i)
	{
		v.push_back(i);
		cout << "继续添加y 停止n" << endl;
		cin >> cont;
		if (cont != "y")
		{
			break;
		}
	}
	for (auto c : v)
	{
		cout << c << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

3.16

#include<iostream>
using namespace std;
#include<vector>
#include<string>

int main()
{
	vector<int>v1;
	cout << "v1.size" << v1.size() << endl;
    if(v1.size()>0)
    {
        for (auto c : v1)
        {
            cout << c << " ";
        }
        cout << endl;
    }
    
	vector<int>v2(10);
	cout << "v2.size" << v2.size() << endl;
    if(v2.size()>0)
    {
        for (auto c : v2)
        {
            cout << c << " ";
        }
        cout << endl;
    }
    
	vector<int>v3(10, 42);
	cout << "v3.size" << v3.size() << endl;
    if(v3.size()>0)
    {
        for (auto c : v3)
        {
            cout << c << " ";
        }
        cout << endl;
    }
    
	vector<int>v4{ 10 };
	cout << "v4.size" << v4.size() << endl;
    if(v4.size()>0)
    {
        for (auto c : v4)
        {
            cout << c << " ";
        }
        cout << endl;
    }
    
	vector<int>v5{ 10,42 };
	cout << "v5.size" << v5.size() << endl;
    if(v5.size()>0)
    {
        for (auto c : v5)
        {
            cout << c << " ";
        }
        cout << endl;
    }
    
	vector<string>v6{ 10 };
	cout << "v6.size" << v6.size() << endl;
    if(v6.size()>0)
    {
        for (auto c : v6)
        {
            cout << c << " ";
        }
        cout << endl;
    }
	vector<string>v7{ 10,"hi" };
	cout << "v7.size" << v7.size() << endl;
    if(v7.size()>0)
    {
        for (auto c : v7)
        {
            cout << c << " ";
        }
        cout << endl;
    }
	system("pause");
	return 0;
}

3.17

#include<iostream>
using namespace std;
#include<vector>
#include<string>
#include<cctype>

int main()
{
	vector<string> v;
	string i = "";
	string cont = "y";
	cout << "请输入字符串" << endl;
	while (cin >> i)
	{
		v.push_back(i);
		cout << "继续添加y 停止n" << endl;
		cin >> cont;
		if (cont != "y")
		{
			break;
		}
	}
	for (auto& ci : v)
	{
		for (auto& zi : ci)
		{
			zi = toupper(zi);
		}
		cout << ci << endl;
	}
	cout << endl;
	system("pause");
	return 0;
}

3.20

#include<iostream>
using namespace std;
#include<vector>
#include<string>
#include<cctype>

int main()
{
	vector<int> v;
	int i = 0;
	string cont = "y";
	cout << "请输入整数" << endl;
	while (cin >> i)
	{
		v.push_back(i);
		cout << "继续添加y 停止n" << endl;
		cin >> cont;
		if (cont != "y")
		{
			break;
		}
	}
	for (decltype(v.size()) i = 0; i < v.size() - 1; i+=2)
	{
		cout << v[i] + v[i + 1] << endl;
	}
	if (v.size() % 2 != 0)
	{
		cout << v[v.size()-1] << endl;
	}
	
		
	
	cout << endl;
	system("pause");
	return 0;
}
#include<iostream>
using namespace std;
#include<vector>
#include<string>
#include<cctype>

int main()
{
	vector<int> v;
	int i = 0;
	string cont = "y";
	cout << "请输入整数" << endl;
	while (cin >> i)
	{
		v.push_back(i);
		cout << "继续添加y 停止n" << endl;
		cin >> cont;
		if (cont != "y")
		{
			break;
		}
	}
	for (decltype(v.size()) i = 0; i < v.size() / 2 ; i++)
	{
		cout << v[i] + v[v.size()-1-i] << endl;
	}
	if (v.size() % 2 != 0)
	{
		cout << v[v.size()/2] << endl;
	}
	
		
	
	cout << endl;
	system("pause");
	return 0;
}

3.21

#include<iostream>
using namespace std;
#include<vector>
#include<string>

int main()
{
	vector<int>v1;
	cout << "v1.size" << v1.size() << endl;
	if (v1.begin() != v1.end())
	{
		for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
		{
			cout << *it << " ";
		}
		cout << endl;
	}

	vector<int>v2(10);
	cout << "v2.size" << v2.size() << endl;
	if (v2.begin() != v2.end())
	{
		for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++)
		{
			cout << *it << " ";
		}
		cout << endl;
	}

	vector<int>v3(10, 42);
	cout << "v3.size" << v3.size() << endl;
	if (v3.begin() != v3.end())
	{
		for (vector<int>::iterator it = v3.begin(); it != v3.end(); it++)
		{
			cout << *it << " ";
		}
		cout << endl;
	}

	vector<int>v4{ 10 };
	cout << "v4.size" << v4.size() << endl;
	if (v4.begin() != v4.end())
	{
		for (vector<int>::iterator it = v4.begin(); it != v4.end(); it++)
		{
			cout << *it << " ";
		}
		cout << endl;
	}

	vector<int>v5{ 10,42 };
	cout << "v5.size" << v5.size() << endl;
	if (v5.begin() != v5.end())
	{
		for (vector<int>::iterator it = v5.begin(); it != v5.end(); it++)
		{
			cout << *it << " ";
		}
		cout << endl;
	}

	vector<string>v6{ 10 };
	cout << "v6.size" << v6.size() << endl;
	if (v6.begin() != v6.end())
	{
		for (vector<string>::iterator it = v6.begin(); it != v6.end(); it++)
		{
			cout << *it << " ";
		}
		cout << endl;
	}

	vector<string>v7{ 10,"hi" };
	cout << "v7.size" << v7.size() << endl;
	if (v7.begin() != v7.end())
	{
		for (auto it = v7.begin(); it != v7.end(); it++)
		{
			cout << *it << " ";
		}
		cout << endl;
	}

	system("pause");
	return 0;
}

3.22

#include<iostream>
using namespace std;
#include<vector>
#include<string>

int main()
{
	vector<string> text;
	string s;
	cout << "请输入字符串" << endl;
	while (getline(cin, s))
	{
		text.push_back(s);
	}

	for (auto it = text.begin(); it != text.end() && !it->empty(); it++)
	{
		for (auto it2 = it->begin(); it2 != it->end(); it2++)
		{
			*it2 = toupper(*it2);
		}
		cout << *it << endl;
	}
	

	system("pause");
	return 0;
}

3.23

#include<iostream>
using namespace std;
#include<vector>
#include<ctime>
#include<cstdlib>

int main()
{
	vector<int>v;
	srand((unsigned int)time(NULL));
	cout << "请输入整数" << endl;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(rand() % 1000);
	}
	
	for (auto it = v.begin(); it != v.end(); it++)
	{
		cout << *it << endl;
	}
	cout << "-----------" << endl;
	for (auto it = v.begin(); it != v.end(); it++)
	{
		*it *= 2;
		cout << *it << endl;
	}

	system("pause");
	return 0;
}

3.24

#include<iostream>
using namespace std;
#include<vector>
#include<string>
#include<cctype>

int main()
{
	vector<int> v;
	int i = 0;
	string cont = "y";
	cout << "请输入整数" << endl;
	while (cin >> i)
	{
		v.push_back(i);
		cout << "继续添加y 停止n" << endl;
		cin >> cont;
		if (cont != "y")
		{
			break;
		}
	}
	if (v.begin() == v.end())
	{
		cout << "bleak" << endl;
	}
	for (auto it=v.begin();it!=v.end()-1;it=it+2)
	{
		cout << *it + *(it+1) << endl;
	}
	if ((v.end() - v.begin()) % 2 != 0)
	{
		cout << *(v.end()-1) << endl;
	}



	cout << endl;
	system("pause");
	return 0;
}
#include<iostream>
using namespace std;
#include<vector>
#include<string>
#include<cctype>

int main()
{
	vector<int> v;
	int i = 0;
	string cont = "y";
	cout << "请输入整数" << endl;
	while (cin >> i)
	{
		v.push_back(i);
		cout << "继续添加y 停止n" << endl;
		cin >> cont;
		if (cont != "y")
		{
			break;
		}
	}
	if (v.begin() == v.end())
	{
		cout << "bleak" << endl;
	}
	for (auto it=v.begin();it!=v.begin()+(v.end()-v.begin())/2; it++)
	{
		cout << *it + *(v.begin()+(v.end() - it)-1) << endl;
	}
	if ((v.end() - v.begin()) % 2 != 0)
	{
		cout << *(v.begin() + (v.end() - v.begin()) / 2) << endl;
	}



	cout << endl;
	system("pause");
	return 0;
}

3.25

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

int main()
{
	vector<unsigned>v(11);
	unsigned grade;
	vector<unsigned>::iterator it = v.begin();

	while (cin >> grade)
	{
		if (grade <= 100)
		{
			*(it + grade/10)+=1;
		}

	}

	cout << "各个分数段人数分布:" << endl;
	for (auto its = v.begin(); its != v.end(); its++)
	{
		cout << *its << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

3.30

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

int main()
{
	const int sz = 10;
	int arr[sz];
	for (int i = 0; i < 10; i++)
	{
		arr[i] = i;
	}
	for (int val : arr)
	{
		cout << val << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

3.31

#include<iostream>
using namespace std;

int main()
{
	const int sz = 10;
	int arr[sz];
	int brr[sz];
	for (int i = 0; i < 10; i++)
	{
		arr[i] = i;
	}
	for (int i = 0; i < 10; i++)
	{
		brr[i] = arr[i];
	}

	for (int val : brr)
	{
		cout << val << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

3.32

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

int main()
{
	vector<int>v1;
	vector<int>v2;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	for (int i = 0; i < 10; i++)
	{
		v2.push_back(v1[i]);
	}

	for (auto val : v2)
	{
		cout << val << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

3.33

#include<iostream>
using namespace std;

int main()
{
	const int sz = 10;
	int arr[sz];

	for (int i = 0; i < sz; i++)
	{
		arr[i] = i;
	}
	cout << "数组中有数字为:" << endl;
	for (int j : arr)
	{
		cout << j << " ";
	}
	cout << endl;

	for (int *p = begin(arr); p != end(arr); p++)
	{
		*p = 0;
	}
	cout << "后数组中有数字为:" << endl;
	for (int j : arr)
	{
		cout << j << " ";
	}
	cout << endl;
}

3.34

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

int main()
{
	const int sz = 10;
	int arr1[sz], arr2[sz];
	srand((unsigned int)time(NULL));

	for (int i = 0; i < sz; i++)
	{
		arr1[i] = rand() % 10;
		arr2[i] = rand() % 10;
	}
	cout << "arr1:" << endl;
	for (int i = 0; i < sz; i++)
	{
		cout << arr1[i] << " ";
	}
	cout << endl;
	for (int i = 0; i < sz; i++)
	{
		cout << arr2[i] << " ";
	}
	cout << endl;

	for (int j = 0; j < sz; j++)
	{
		if (arr1[j] != arr2[j])
		{
			cout << "different" << endl;
			break;
		}
	}

}

也可以看书,用while语句也可以,可以用指针和库函数begin end;

3.41

#include<iostream>
using namespace std;
#include<cstring>
#include<vector>

int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9 };
	vector<int>v(begin(arr), end(arr));
	
	for (auto val : v)
	{
		cout << val << endl;
	}
	system("pause");
	return 0;
}

3.42

#include<iostream>
using namespace std;
#include<cstring>
#include<vector>

int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9 };
	vector<int>v(begin(arr1), end(arr1));

	for (auto val : v)
	{
		cout << val << endl;
	}
	
	int* arr2 = new int[v.size()];
	int i = 0;
	for (auto val : v)
	{
		arr2[i] = val;
		i++;
	}
	for (int j=0;j<v.size();j++)
	{
		cout << arr2[j] << endl;
	}

	system("pause");
	return 0;
}

3.43

#include<iostream>
using namespace std;

int main()
{
	int ia[3][4] = {
		{0,1,2,3},
		{4,5,6,7},
		{8,9,10,11}
	};
	for (int (&row)[4] : ia) {
		for (int &val : row) {
			cout << val << " ";
		}
		cout << endl;
	}
	cout << "--------" << endl;

	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 4; j++) {
			cout << ia[i][j] << " ";
		}
		cout << endl;
	}
	cout << "--------" << endl;

	for (int(*p)[4] = ia; p != end(ia); p++) {
		for (int* q = *p; q != end(*p); q++) {
			cout << *q << " ";
		}
		cout << endl;
	}

	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值