第七章 复习题

复习题

1
提供函数定义
提供函数原型
调用函数
2

void igor(void);
// void igor();

float tofu(int);

double mpg(double, double);

long summation(long arr[], int n);
// long summation(long *arr, int n);

double doctor(const string str);
// double doctor(const char *str);
// double doctor(const char str[]);

struct boss{};
void ofcourse(boss);

struct map{};
string plot(map *);
// char *plot(map *);

3

void func(int[], int, int);
void func(int arr[], int n, int x)
{
    for (int i = 0; i < n; ++i)
    {
        arr[i] = x;
    }
}

4

void func(int *begin, int *end, int x)
{
    while (begin != end)
    {
        *begin = x;
        ++begin;
    }
}

5

double func(const double arr[], int n)
{
    double maxi = arr[0];
    for (int i = 1; i < n; ++i)
    {
        maxi = maxi > arr[i] ? maxi : arr[i];
    }
    return maxi;
}

6
对于基本类型的函数参数,函数使用的就是该参数的副本,即使不加const也不会改变原始数据。加const反而画蛇添足。
7
字符数组 char str[]
字符串常量(字符串字面值)"Hello World!"
指向字符串地址的指针char *p
8

int replace(char *str, char c1, char c2)
{
    int cnt = 0;
    while (*str != '\0')
    {
        if (*str == c1)
        {
            *str = c2;
            ++cnt;
        }
        ++str;
    }
    return cnt;
}

9
*"pizza" 含义: ”pizza"本质上是一个 char *,指向字符串首个字符(‘p’)的指针,首地址, 而 * 解除引用,获得该指针指向的值,即字符 'p'
"taco"[2] : 等价为 *("taco"+2),对 首字母位置偏移2次的地址('c’的地址) 解除引用,即是字符'c'
10
直接作为函数参数就是按值传递,使用&取址运算符获取地址。
按值传递 :操作简单,如果结构大,复制结构时占用内存大,降低运行速度
按地址传递:略微复杂,空间小,速度快
11

int judge(int (*pf)(const char));

12

struct applicant
{
    char name[30];
    int credit_ratings[3];
};

void func(applicant);
void func2(applicant *);

void func(applicant app)
{
    cout << app.name << endl;
    for (int i = 0; i < 3; ++i)
    {
        cout << app.credit_ratings[i] << " ";
    }
    cout << endl;
}
void func2(applicant *app)
{
    cout << app->name << endl;
    for (int i = 0; i < 3; ++i)
    {
        cout << app->credit_ratings[i] << " ";
    }
    cout << endl;
}

在这里插入图片描述
13

struct applicant
{
    char name[30];
    int credit_ratings[3];
};
// 声明
typedef void (*p_fun1)(applicant *);
typedef const char *(*p_fun2)(const applicant *, const applicant *);
// 函数原型
void f1(applicant *a);
const char *f2(const applicant *a1, const applicant *a2);
// 指针p1,p2
p_fun1 p1 = f1;
p_fun2 p2 = f2;
// 数组ap:5个类型与p1相同的指针
p_fun1 ap[5] = {p1, p1, p1, p1, p1};
// 指针pa:指向数组的,数组pb:10个类型与p2相同的指针
p_fun2 pb[10] = {p2, p2, p2, p2, p2, p2, p2, p2, p2, p2};
p_fun2 (*pa)[10] = &pb;

编程题

1

double func(double x,double y){
    return 2.0*x*y/(x+y);
}
int main()
{
    double x,y;
    while(cin>>x && x!=0 && cin>>y && y!=0){
       cout<<func(x,y)<<endl;
    }
    return 0;
}

2

int input(double arr[])
{
    int cnt = 0; //数据个数
    while (cnt < 10 && cin >> arr[cnt])
    {
        ++cnt;
    }
    return cnt;
}
void display(double arr[], int n)
{
    for (int i = 0; i < n; ++i)
    {
        cout << arr[i] << '\t';
    }
    cout << endl;
}
void average(double arr[], int n)
{
    double dsum = 0;
    for (int i = 0; i < n; ++i)
    {
        dsum += arr[i];
    }
    cout << dsum / n << endl;
}
int main()
{
    double grand[10] = {0};
    int n = 0;
    n = input(grand);
    display(grand, n);
    average(grand, n);
    return 0;
}

3

#include <iostream>
using namespace std;

struct box{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};
void func(box b){
    cout<<"maker: "<<b.maker<<endl;
    cout<<"height: "<<b.height<<endl;
    cout<<"width: "<<b.width<<endl;
    cout<<"length: "<<b.length<<endl;
    cout<<"volume: "<<b.volume<<endl;
}
void func2(box *pb){
    pb->volume = pb->height*pb->width*pb->length;
}
int main()
{    
    box b={"abc",1.0,2.0,3.0,0};
    func(b);
    cout<<"claculate volume: ";
    func2(&b);
    cout<<b.volume;
    return 0;
}

4

#include <iostream>
using namespace std;

double func(int up,int down){
    
    double ans = 1.0;
    while(up>0){
        ans = ans*down/up;
        up--,down--;
    }
    return 1.0/ans;
}
int main()
{    
    // 1/C(5,47) * 1/C(1,27)
    cout<<func(5,47)*func(1,27)<<endl;
    return 0;
}

结果:2.41451e-08
5

#include <iostream>
using namespace std;

long long recur(int n){
    if(n==0)return 1;
    return n*recur(n-1);
}
int main()
{    
    int n;
    while(cin>>n){
        cout<<n<<" ! = "<<recur(n)<<endl;
    }
    return 0;
}

6

#include <iostream>
using namespace std;

int Fill_array(double arr[], int n)
{
    int cnt = 0;
    while (cnt < n)
    {
        while (!(cin >> arr[cnt]))
        {
            return cnt;
        }
        ++cnt;
    }
    return n;
}
void Show_array(double arr[], int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << '\t';
    }
    cout << endl;
}
void Reverse_array(double arr[], int n)
{
    int i = 0, j = n - 1;
    double temp = 0;
    while (i < j)
    {
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        i++, j--;
    }
}
int main()
{
    int n;
    double arr[10] = {0};
    n = Fill_array(arr, 10); //填充数组
    cout << "pcs: " << n << endl;
    cout << "init: " << endl;
    Show_array(arr, n);    //显示数组
    Reverse_array(arr, n); //反转数组
    cout << "first reverse: " << endl;
    Show_array(arr, n);            //显示数组
    Reverse_array(arr + 1, n - 2); //除去两端的反转数组
    cout << "second reverse: " << endl;
    Show_array(arr, n); //显示数组

    return 0;
}

7
懒得抄书上的代码,有机会再说
8
懒得抄书上的代码,有机会再说
9

#include <iostream>
using namespace std;
const int SLEN = 30;
struct student
{
    char fullname[SLEN];
    char hobby[SLEN];
    int ooplevel;
};
int getinfo(student pa[], int n)
{
    int cnt = 0;
    while (cnt < n)
    {
        // name
        cout << "student #" << cnt + 1 << "name: ";
        cin.get(pa[cnt].fullname, SLEN);
        if (pa[cnt].fullname[0] == '\0')
            break;
        while (cin.get() != '\n')
            continue; //解决字符超限问题
        // hobby
        cout << "student #" << cnt + 1 << "hobby: ";
        cin.get(pa[cnt].hobby, SLEN);
        while (cin.get() != '\n')
            continue; //解决字符超限问题
        // opplevel
        cout << "student #" << cnt + 1 << "opplevel: ";
        cin >> pa[cnt].ooplevel;
        cin.get(); //吸收上一个学生最后数据的回车,以判断下一个学生的name是否为空
        cnt++;
    }
    return cnt;
}
void display1(student st)
{
    cout << st.fullname << endl;
    cout << st.hobby << endl;
    cout << st.ooplevel << endl;
}
void display2(student *st)
{
    cout << st->fullname << endl;
    cout << st->hobby << endl;
    cout << st->ooplevel << endl;
}
void display3(const student pa[], int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << pa[i].fullname << endl;
        cout << pa[i].hobby << endl;
        cout << pa[i].ooplevel << endl;
    }
}
int main()
{
    cout << "Enter class size: ";
    int class_size;
    cin >> class_size;
    while (cin.get() != '\n')
    {
        continue;
    }
    student *ptr_stu = new student[class_size];
    int entered = getinfo(ptr_stu, class_size);
    cout << "pcs: " << entered << endl;
    for (int i = 0; i < entered; i++)`在这里插入代码片`
    {
        display1(ptr_stu[i]);
        display2(&ptr_stu[i]);
    }
    display3(ptr_stu, entered);
    delete[] ptr_stu;
    cout << "Done\n";

    return 0;
}

10
手动调用:

#include <iostream>
using namespace std;
double add(double x, double y)
{
    return x + y;
}
double mul(double x, double y)
{
    return x * y;
}
double sub(double x, double y)
{
    return x - y;
}
double calculate(double x, double y, double (*p)(double, double))
{
    return p(x, y); // return (*p)(x,y);
}

int main()
{
    int x, y;
    while (cin >> x >> y)
    {
        cout << "add: " << calculate(x, y, add) << endl;
        cout << "mul: " << calculate(x, y, mul) << endl;
        cout << "sub: " << calculate(x, y, sub) << endl;
    }
    return 0;
}

指针数组调用:

#include <iostream>
using namespace std;
double add(double x, double y)
{
    return x + y;
}
double mul(double x, double y)
{
    return x * y;
}
double sub(double x, double y)
{
    return x - y;
}
double calculate(double x, double y, double (*p)(double, double))
{
    return p(x, y); // return (*p)(x,y);
}

int main()
{
    int x, y;
    double (*p1)(double x, double y) = add;
    double (*p2)(double, double) = mul;
    double (*p3)(double, double) = sub;
    double (*pf[3])(double, double) = {p1, p2, p3};
    char name[3][6] = {"add: ", "mul: ", "sub: "};
    while (cin >> x >> y)
    {
        for (int i = 0; i < 3; i++)
        {
            cout << name[i] << calculate(x, y, pf[i]) << endl;
            // cout<<name[i]<<calculate(x,y,*pf[i])<<endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值