您的位置:首页 > 编程语言 > C语言/C++

c++primer第五版课后练习答案(第六章)

2014-12-18 20:09 369 查看
chapter6_6.3

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,结果显示如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: