C++编程思想第二章例题笔记

来源:互联网 发布:淘宝店铺布局管理 编辑:程序博客网 时间:2024/05/21 07:49

2.2.1.1:

/*************************************************************************> File Name: 2.2.1.1.c> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 20时05分20秒 ************************************************************************/#include <stdio.h>//在c语言中,空参数代表可以接受任意参数(任意数目,任意类型)int func(){return 3;}int main(int argc, char *argv[]){int a = func(2, 3);int b = func(2.3);printf("%d %d\n", a, b);return 0;}

/*************************************************************************> File Name: 2.2.1.1.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 20时07分59秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;//在c++中,空参数意味着不带参数的函数int func(){return 3;}int main(int argc, char *argv[]){//报错//int a = func(1);//int b = func(2);int a = func();int b = func();cout << a << " " << b << endl;return 0;}

2.2.1.4:

/*************************************************************************> File Name: 2.2.1.4.c> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 20时35分40秒 ************************************************************************/#include <stdio.h>int add(int, int);int main(int argc, char *argv[]){printf("%d\n", add(1, 2));return 0;}//c语言在函数定义时,参数要求有标示符,c++则不要求//int add(int, int)int add(int a, int b){return a + b;}

/*************************************************************************> File Name: 2.2.1.4_declare.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 20时23分48秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;//extern关键字对于变量来说表明只声明,而不定义(分配存储空间)//而对于函数来说,则是多余的,可选的// Declaration & definition examplesextern int i;// Declaration without definitionextern float f(float);// Function declarationfloat b;// Declaration & definitionfloat f(float a)// Definition{return a + 1.0f;}int i;// Definitionint h(int x)// Declaration & definition{return x + 1;}int main(int argc, char *argv[]){b = 1.0;i = 2;f(b);h(i);return 0;}

2.3.4:

/*************************************************************************> File Name: 2.3.4_hello.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 20时55分45秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;//在c++语言中,mian函数总是返回int类型int main(int argc, char *argv[]){cout << "Hello, World! I am " << 8 << " Today!" << endl;return 0;}
2.4:

/*************************************************************************> File Name: 2.4_stream.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 20时59分50秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;int main(int argc, char *argv[]){// Specifying formats whit manipulatorscout << "a number in decimal: " << dec << 15 << endl;cout << "in octal: " << oct << 15 << endl;cout << "in hex: " << hex << 15 << endl;//浮点数的格式由编译器自动确定cout << "a floating-poing number: " << 3.14159 << endl;//显式类型转换(case),任何字符都能转换成char类型cout << "non-printing char (escape): " << char(27) << endl;return 0;}


2.4.1:

/*************************************************************************> File Name: 2.4.1_concat.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 21时06分45秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;//可以实现字符数组的拼接(character array concatenation)//如果两个加引号的字符数组邻接,并且它们之间没有任何标点,//编译器就会把这些字符数组连接成单个字符数组。int main(int argc, char *argv[]){cout << "This is far too long to put on a ""single line but it can be broken up with ""no ill effects\nas long as there is no ""punctuation separating adjacent character ""arrays.\n";return 0;}

2.4.2:

/*************************************************************************> File Name: 2.4.2_numconv.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 21时14分26秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;// Converts decimal to octal and hexint main(int argc, char *argv[]){int number;cout << "Enter a decimal number: ";cin >> number;cout << "value in octal = 0" << oct << number << endl;cout << "value in hex = 0x" << hex << number << endl;return 0;}

2.5:

/*************************************************************************> File Name: 2.5_hellostrings.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 21时24分39秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;//string对象使用示例int main(int argc, char *argv[]){string s1, s2;// Empty stringsstring s3 = "Hello, World.";// Initializedstring s4("I am");// Also initializeds2 = "Today";// Assigning to a strings1 = s3 + " " + s4;// Combining stringss1 += " 8 ";// Appending to a stringcout << s1 + s2 + "!" << endl;return 0;}

2.6:

/*************************************************************************> File Name: 2.6_fillstring.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 21时32分55秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <vector>#include <sstream>#include <fstream>// 文件读写#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;//string具有动态性,不必担心string的内存分配//string会自动扩展以保存新的输入int main(int argc, char *argv[]){ifstream in("2.6_fillstring.cpp");string s, line;while (getline(in, line)) {s += line + "\n";}cout << s << endl;return 0;}

/*************************************************************************> File Name: 2.6_scopy.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 21时28分26秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <vector>#include <sstream>#include <fstream>//文件读写#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;// Copy one file to another, a line at a timeint main(int argc, char *argv[]){ifstream in("2.6_scopy.cpp");// Open for readingofstream out("2.6_scopy.cpp~");// Open for writing string s;while (getline(in, s)) {// Discards newline charout << s << "\n";// ... must add it back}return 0;}

2.7:

/*************************************************************************> File Name: 2.7_intvector.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 22时00分48秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;//vector容器可以看成动态扩展的数组,push_back()函数将元素加入到vector的尾部//size()函数返回vector容器的元素个数,并且vector重载了[]操作符,使得可以像数组//一样访问vector中的元素int main(int argc, char *argv[]){vi v;for (int i = 0; i < 10; ++i) {v.pb(i);}for (int i = 0; i < sz(v); ++i) {cout << v[i] << ", ";}cout << endl;for (int i = 0; i < sz(v); ++i) {v[i] = v[i] * 10;//赋值}for (int i = 0; i < sz(v); ++i) {cout << v[i] << ", ";}cout << endl;return 0;}

/*************************************************************************> File Name: 2.7_fillvector.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 21时54分03秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <vector>#include <sstream>#include <fstream>//文件读写#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;// Copy an entire file into a vector of stringint main(int argc, char *argv[]){vector<string> v;ifstream in("2.7_fillvector.cpp");string line;while (getline(in, line)) {v.pb(line);}for (int i = 0; i < sz(v); ++i) {cout << i << ": " << v[i] << endl;}return 0;}

/*************************************************************************> File Name: 2.7_getwords.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 21时57分26秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <vector>#include <sstream>#include <fstream>//文件读写#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;int main(int argc, char *argv[]){vector<string> words;ifstream in("2.7_getwords.cpp");string word;//以空白来区分单词,可以使用更复杂的方式分割输入while (in >> word) {words.pb(word);}for (int i = 0; i < sz(words); ++i) {cout << words[i] << endl;}return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 厦门中考居住证没满三年怎么办 海归落户过了两年期怎么办 借呗学历填错了怎么办 上海落户应届生分不够怎么办 应届生落户分数不够72分怎么办? 上海应届生落户时间延误怎么办 南京市区户口签江宁怎么办 深圳公司集体户口离职后怎么办 济南本地户口不符合入学条件怎么办 上海住亲戚家怎么办居住证 政府卖非农户口怎么办 90年代买了户口怎么办 上海应届大学生积分不够怎么办 广州居住证回执单丢了怎么办 惠阳居住证回执单丢了怎么办 南京居住证换地方了怎么办 买家退回的商品有问题怎么办 农转农户口手续怎么办 原房东不迁户口我怎么办 户主信息页掉了怎么办 户主变了户口本首页怎么办 大人户口迁走小孩户口怎么办 网银转账处理中怎么办 教育部学籍在线验证报告有错怎么办 验证码连续输入三次错误怎么办 交通运输监察大队截车了怎么办 平安安康续保没成功怎么办 危险品经营许可证到期了怎么办 郑万350渝万怎么办 厂里饭堂的饭好难吃怎么办 学校的食堂饭菜不好不卫生怎么办 亲戚借钱我真没有怎么办 榴莲肉酸了吃了怎么办 亲戚赖在家里住怎么办 食堂饭菜味道差该怎么办 被监视居住公安打电话睡着了怎么办 鱼缺氧浮上水面怎么办 车载低音炮有电流声怎么办 925纯银变黑了怎么办 银子放久了变黑怎么办 高铁票网上售空怎么办