c++primer第五版课后练习答案(第六章)
2014-12-18 20:09
369 查看
chapter6_6.3
chapter6_6.4
chapter6_6.5
chapter6_6.7
chapter6_6.8
head.h头文件
chapter6_6.9
head.h头文件
fact.cpp
factMain.cc
chapter6_6.10
chapter6_6.12
chapter6_6.17
chapter6_6.21
chapter6_6.25
int fact(int val) { int ret = 1; while (val > 1) ret *= val--; return ret; }
chapter6_6.4
#include "stdafx.h"
#include <iostream>
using namespace std;
int fact(int val) { int ret = 1; while (val > 1) ret *= val--; return ret; }
int _tmain(int argc, _TCHAR* argv[])
{
int i;
cout << "请输入一个数:" << endl << "i=";
cin >> i;
cout << i<<"的阶乘="<<fact(i) << endl;
return 0;
}
chapter6_6.5
#include "stdafx.h" #include <iostream> using namespace std; template <class T> T fabs(T x) { if (x < 0) return -x; else return x; } int _tmain(int argc, _TCHAR* argv[]) { cout << fabs(-3.14) << endl; return 0; }
chapter6_6.7
#include "stdafx.h" #include <iostream> using namespace std; int call() { static int i = 0; return i++; } int _tmain(int argc, _TCHAR* argv[]) { for (int i = 0; i < 10;i++) cout<<call()<<endl; return 0; }
chapter6_6.8
head.h头文件
int fact(int val);
chapter6_6.9
head.h头文件
int fact(int val);
fact.cpp
#include "stdafx.h"
#include "head.h"
int fact(int val) { int ret = 1; while (val > 1) ret *= val--; return ret; }
factMain.cc
#include "stdafx.h" #include "head.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { cout << fact(3) << endl; return 0; }
chapter6_6.10
#include "stdafx.h" #include <iostream> using namespace std; void change(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } int _tmain(int argc, _TCHAR* argv[]) { int a = 1; int b = 2; change(&a, &b); cout << a << endl << b << endl; return 0; }
chapter6_6.12
#include "stdafx.h" #include <iostream> using namespace std; void change(int &x, int &y) { int temp; temp = x; x = y; y = temp; } int _tmain(int argc, _TCHAR* argv[]) { int a = 1; int b = 2; change(a, b); cout << a << endl << b << endl; return 0; }
chapter6_6.17
#include "stdafx.h" #include <string> #include <iostream> using namespace std; bool isSupper(const string &s) { int flag = 0; for (auto c : s) { if (c > 'A'&&c < 'Z') { flag = 1; return true; } else { continue; } } if (flag) return true; else return false; } string toupper1(string &s) { for (auto &c:s) { c=toupper(c); } return s; } string toupper2(string &s) { int i = 0; for (; i < s.size()-1;i++) if (s[i]>='a'&&s[i]<='z') { s[i] = s[i] - 32; } return s; } int _tmain(int argc, _TCHAR* argv[]) { string str = "my name is Xj"; cout << isSupper(str) << endl; cout<<toupper2(str)<<endl; return 0; }
chapter6_6.21
#include "stdafx.h" #include <iostream> using namespace std; int compare(int i, int *j) { if (i > (*j)) return i; else return *j; } int _tmain(int argc, _TCHAR* argv[]) { int x = 10; int y = 20; int *pt = &y; cout << compare(x, pt)<<endl; return 0; }
chapter6_6.25
#include "stdafx.h" #include <string> #include <iostream> using namespace std; int main(int argc, char* argv[]) { if (argc > 2) { int i = 2; string str = argv[1]; while (i <argc) { str = str+argv[i]; i++; } cout << str << endl; } else { cout << "error" << endl; } return 0; }将编译后Debug文件中的ConsoleApplication2_6.25.exe文件拷贝到C:\Users\Administrator目录下,在dos下执行程序,输入第二参数为hello word,结果显示如下:
相关文章推荐
- c++primer第五版课后练习答案(第十章)
- 从今天开始,把C++primer(第五版)的课后练习答案贴到我的博客里面
- C++Primer 中文版 第五版 第六章课后习题答案
- c++primer第五版课后练习答案(第五章)
- c++primer第五版课后练习答案(第一章)
- C++primer第5版课后练习习题答案9.47
- C++primer(第五版)9.3.4节,9.3.5节,9.3.6节练习答案
- C++Primer 中文版 第五版 第三章课后习题答案
- C++Primer第五版 第六章习题答案(21~30)
- C++primer(第五版)9.1节,9.2节,9.2.1节练习答案
- C++Primer 中文版 第五版 第四章课后习题答案
- C++Primer第五版 第六章习题答案(31~40)
- C++primer(第五版)9.2.2节,9.2.3节,9.2.4节练习答案
- C++Primer 中文版 第五版 第五章课后习题答案
- C++Primer 中文版 第五版 第十章课后习题答案
- C++Primer第五版课后答案地址
- C++Primer第五版 第六章习题答案(41~50)
- C++primer(第五版)9.4节练习答案
- C++Primer 中文版 第五版 第九章课后习题答案
- C++Primer第五版 第六章习题答案(51~56)