我是靠谱客的博主 醉熏香菇,最近开发中收集的这篇文章主要介绍c++primer第五版课后练习答案(第六章),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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:UsersAdministrator目录下,在dos下执行程序,输入第二参数为hello word,结果显示如下:

最后

以上就是醉熏香菇为你收集整理的c++primer第五版课后练习答案(第六章)的全部内容,希望文章能够帮你解决c++primer第五版课后练习答案(第六章)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部