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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值