ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

第7章 函数——C++的编程模块

2022-08-14 14:32:17  阅读:164  来源: 互联网

标签:编程 cout int double float C++ height char 模块


第7章 函数——C++的编程模块

7.8 编程练习题

  1. 第1题
#include <iostream>
using namespace  std;

// 编写一个程序,不断要求用户输入两个数,直到其中的一个为0.
// 对于两个数程序使用一个函数来计算它们的调和平均数,并将结果返回给main()
// 调和平均数 = 2.0 * x* y*(x+y)
inline double calAve(double x, double y){ //计算调和平均数
    return 2.0 * x * y *(x+y);
}

void test(){
    cout << "请输入x, y的值:" << endl;
    double x , y ;
    cin >> x >> y;
    while(x != 0.0 && y != 0 ){
        cout << "调和平均数为" << calAve(x,y) << endl;
        cout << "请输入x, y的值:" << endl;
        cin >> x >> y;
    }
}

int main(int argc, char const *argv[])
{
    test();
    return 0;
}
  1. 第2题
#include <iostream>
using namespace  std;
const int SIZE = 10;

// 编写一个程序,要求用户输入10个高尔夫成绩,并将其存入到数组里面,程序允许提早结束输入,
// 并在一行上显示所有的成绩,然后报告平均成绩。请使用3个数组处理函数来分别输入、显示和计算
// 平均成绩
int set_mark(int [] , int);
void display_mark(int [], int);
double average_mark(int[] , int);


int main(int argc, char const *argv[])
{
    int grade[SIZE] ;
    // 输入分数
    cout << "输入10个分数"<< endl;
    int count = set_mark(grade,SIZE);
    cout  << "显示10个分数,输入非数字停止输入" << endl;
    display_mark(grade,count);
    cout << "计算平均分" << endl;
    double res = average_mark(grade, count);
    cout << "平均分为:" << res << endl; 

    system("pause");
    return EXIT_SUCCESS;
}

int set_mark(int grade[] , int size){

    int i = 0;
    do{
        cout << "输入第"<< i +1 <<"Golf Marks"<<endl;
        cin >> grade[i++];
        cin.get();
        cout << "按Enter继续,或者按s键终止输入" << endl;
        if(cin.get() == 's'){
            for(;i<size;i++) grade[i] =0 ; 
        }
    }while(i < size);

    return i;
}

void display_mark(int grade[] , int size){
    
    cout << "输入的字符个数" << size << endl;
    for(int i =0 ; i< size;i++){
        cout << grade[i] << "\t" ;
    }
    putchar('\n');
}

double average_mark(int grade[] , int size){
    double sum = 0.0;
    for(int  i =0 ; i < size; i++){
        sum += grade[i];
    }

    return  sum / size;
}

第3题

#include <iostream>
using namespace  std;

//结构体作为函数的参数 
struct box{
    char marker[40];
    float height;
    float width;
    float length;
    float volum;
};

void display(box b){
    cout << b.marker << " " << b.height << " "<<b.width << " " << b.length<<" "<<b.volum<<endl;
}
void display(box *b){
    b->volum = b->height * b->width * b->length ;
    cout << b->marker << " " << b->height << " "<<b->width << " " << b->length<<" "<<b->volum<<endl;
}

int main(int argc, char const *argv[])
{
    box b = {"张三",178.9,34,45,67};
    display(b);
    display(&b);
    system("pause");
    return EXIT_SUCCESS;
}

标签:编程,cout,int,double,float,C++,height,char,模块
来源: https://www.cnblogs.com/lofly/p/16585358.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有