C++ Primer 5.3.2~6.2.3部分节练习

来源:互联网 发布:java定时任务管理系统 编辑:程序博客网 时间:2024/06/06 03:51

定义函数的源文件Chapter6.cpp:

#include "stdafx.h"#include "Chapter6.h"#include <string>using namespace std;size_t count_calls()//源文件中的函数声明{static size_t cnt = 0;return ++cnt;}//该函数通过引用的形式交换两个Int的值void RefSwap(int &a, int &b){int temp = a;a = b;b = temp;}//该函数用于交换两个int类型的值void PointerSwap(int *a,int *b){int temp = *a;//使用变量存储a的地址指向的值,而不是指针,这样当a改变时,temp不会改变*a = *b;*b = temp;//错误代码:/*int *temp = a;*a = *b;//注意*temp等于a的值,改变a的时候也改变了temp*b = *temp;*/}//通过引用的形式将int改为0void Reset(int &num){num = 0;}//该函数判断一个string对象中是否有大写字母bool HasUpper(const string& str){for (auto temp : str)//const char{if (isupper(temp)){return true;}}return false;}//该函数将一个string中的字母全部变成大写string GetUpper(string& str){for (char& temp : str){if (islower(temp)){temp=toupper(temp);}}return str;}

头文件chapter6.h:

//头文件#include <string>//一般在头文件中声明函数,而在源文件中定义函数size_t count_calls();//声明函数void PointerSwap(int *a, int *b);void Reset(int &num);void RefSwap(int &a, int &b);bool HasUpper(const std::string& str);std::string GetUpper(std::string& str);

关于SaleItem的定义和声明:

#pragma onceclass SalesItem //类声明{public:SalesItem();~SalesItem();SalesItem(int _value);int value;};



main:

// ConsoleApplication1.cpp: 定义控制台应用程序的入口点。#include "stdafx.h"#include <iostream>//<>直接从编译器自带的函数库中寻找文件#include <string>//使用字符串需要包含<string> #include <vector>#include "SalesItem.h"//""先从自定义的文件中找 ,如果找不到在从函数库中寻找文件#include "Chapter6.h"//将函数写在头文件中,并包含进来//命名空间:using namespace std;using std::vector;//函数声明:void func(int);//size_t count_calls();int main(){/*vector<int> ivec{1, 2, 3, 4, 5};int arr[5];auto p = ivec.begin();int *t = arr;//首地址int i = 0;while (p < ivec.end()){*t++ = *p++;}for (int temp : arr){cout << temp << endl;}*//*int arr[5] = { 1,2,3,4,5 };cout << sizeof arr << endl;int *p = arr;cout << sizeof p << endl;cout << sizeof *p << endl;*///----------------------5.3.2 节练习 ----------------------//----------------------练习 5.9 ----------------------/*string str = "abcdefg";int cnt = 0;for (auto p = str.begin(); p !=str.end(); p++) {switch (*p)//可以使用下标的形式访问字符串{case 'a':case 'e':case 'i':case 'o':case 'u':cnt++;break;default:break;}}cout << cnt << endl;*///----------------------5.6 异常 ----------------------//下列代码声明两个SalesItem对象,并给他们的value属性赋值,接着进行比较,如果两个item的value不一样,//则用throw关键字抛出一个runtime_error类型的异常,并在catch块中捕获该异常,调用what()方法可以获取异常的相关信息/*SalesItem item_1,item_2;item_1.value = 1;item_2.value = 2;try{if (item_1.value != item_2.value){throw runtime_error("value is not same");}}catch (runtime_error err)//负责处理runtime_error类型的异常{cout << err.what() << endl;cout << "触发异常" << endl;}*//*int a = 1,b = 0;try{cout << a / b << endl;}catch (exception err){cout << err.what() << endl;}*///---------------------- 第6章 函数----------------------//下列代码用于演示局部静态对象/*for (int i = 0; i < 10; i++){cout <<count_calls() << endl;}cout <<"last:"<< count_calls() << endl;*///---------------------- 6.2.1 节练习----------------------//练习6.20/*int a = 2, b = 4;PointerSwap(&a, &b);cout << a << endl;cout << b << endl;*///---------------------- 6.2.2 节练习----------------------//练习6.11/*int num = 10;Reset(num);cout << num << endl;*///练习6.12/*int a = 1, b = 2;RefSwap(a, b);cout << a << endl;cout << b << endl;*///---------------------- 6.2.3 节练习----------------------//练习6.17string s1 = "hellO World";string s2 = "hello world";cout << boolalpha << HasUpper(s1) << endl;cout << boolalpha << HasUpper(s2) << endl;cout << boolalpha << GetUpper(s2) << endl;system("pause");return 0;}void func(int n){cout << n << endl;}


原创粉丝点击