2021-11-01

C++ Primer Plus(第6版)中文版部分答案

5.8

​
#include<iostream>
#include<cstring>
int main(void)
{
	using namespace std;
	char word[20];
	int i = 0;
	cout << "Enter words(to stop,type the word done)\n";
	cin >> word;
	while (strcmp(word, "done"))
	{
		cin >> word;
		i++;
	}
	cout << "You entered a total of " << i << " words";
	
}

​

5. 9

#include<iostream>
#include<string>
int main(void)
{
	using namespace std;
	string word;
	int i = 0;
	cout << "Enter words(to stop,type the word done)\n";
	cin >> word;
	while (word != "done")
	{
		cin >> word;
		i++;
	}
	cout << "You entered a total of " << i << " words";
	
}

5.10

#include<iostream>
int main(void)
{
	using namespace std;
	int num;
	cout << "Please input a number: ";
	cin >> num;
	for (int i = 1; i < num + 1; i++)
	{
		for (int j = 0; j < (num - i); j++)
			cout << ".";
		for (int k = 0; k < i; k++)
			cout << "*";
		cout << endl;
	}
}

 7.6填充数组,反转数组,显示数组

#include<iostream>

int fill_array(double ar[],int n);
void show_array(const double ar[],int n);
void reverse_array(double ar[],int n);

int main()
{
    using namespace std;
    double scores[10];
    int num = fill_array(scores,10);
   // cout << num << endl << scores[2];
   show_array(scores,num);
   //reverse_array(scores,num);
   show_array(scores,num);
   reverse_array(&(scores[1]),num-2);
   show_array(scores,num);
   
}

int fill_array(double ar[],int n)
{
    using namespace std;
    int count = 0;
    for(int i = 0;i < n;i++)
    {
        cout << "Please input " << i+1 << " number:";
        double x;
        if(cin >> x)
        {
            ar[i] = x;
            count++;
        }
        else
            break;
    }
    return count;
}

void show_array(const double ar[],int n)
{
    using namespace std;
    for(int i = 0;i < n;i++)
    {
        cout << i + 1 << ": " << ar[i] << endl;
    }
}

void reverse_array(double ar[],int n)
{
    double t;
    for(int i = 0,j = n-1;i < j;i++,j--)
    {
        t = ar[i];
        ar[i] = ar[j];
        ar[j] = t;
    }
}

7.10 指向函数的指针

#include<iostream>

double add (double,double);
double calculate(double,double,double (*pf)(double,double));
double subtract (double x,double y);
double multiply (double x,double y);
double divide (double x,double y);

int main()
{
   //std::cout << calculate(5.5,4.2,add);
   double (*pf[4])(double,double) = {add,subtract,multiply,divide};
   for(int i = 0;i < 4;i++)
   {
       std::cout << calculate(7.6,3.8,*(pf[i])) << std::endl;
   }
}


double add (double x,double y)
{
    return x + y;
}

double subtract (double x,double y)
{
    return x - y;
}

double multiply (double x,double y)
{
    return x * y;
}

double divide (double x,double y)
{
    return x / y;
}

double calculate(double m,double n,double (*pf)(double,double))
{
    return pf(m,n);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值