首页 > 编程语言 >第7章 函数——C++的编程模块

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

时间:2022-08-14 14:22:18浏览次数:46  
标签:编程 cout int double float C++ height 模块

第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,模块
From: https://www.cnblogs.com/lofly/p/16585358.html

相关文章

  • [c++] windows下检查当前程序是不是以管理员权限运行
    很多程序运行的时候,一些操作需要管理员权限才能运行,正好微软提供了一个接口IsUserAnAdmin,可以让我们很方便的检测出当前程序是不是以admin运行的。#include<ShlObj.h>......
  • 2022.8.14 模块化、Webpack、Vue-element-admin
    06、模块化相当于形成包6.1、简介模块化产生的背景随着网站逐渐变成”互联网应用程序”,嵌入网页的Javascript代码越来越庞大,越来越复杂。Javascript模块化编程,已经成......
  • 实现0开销的 c++ 接口例子
    //network.hclassNetwork{public:boolsend(constchar*host,uint16_tport,conststd::string&message);staticNetw......
  • 基于C++的OpenGL 09 之材质
    1.引言本文基于C++语言,描述OpenGL的材质前置知识可参考:基于C++的OpenGL08之基础光照-当时明月在曾照彩云归-博客园(cnblogs.com)笔者这里不过多描述每个名词......
  • C++之类模板的分文件编写问题以及解决
    C++之类模板的分文件编写问题以及解决建议模板不要分文件编写Person.h文件#pragmaonce#include<iostream>usingnamespacestd;#include<string>template<c......
  • C++之友元函数
    C++之友元函数某些地方可以访问,某些地方不可以访问。#include<iostream>usingnamespacestd;#include<string>classBuilding{ //让全局的好朋友函数变成类......
  • C++之常函数和常对象
    C++之常函数和常对象为了让this指向的成员属性值不可更改,在函数的实现前边加上const.voidshowInfo()const//常函数不能修改指针指向的值{ this->m_A=1000;......
  • Shell编程
    变量:类型只有数字、字符串、数组,不用分号#!/bin/shstr_name="jack"#变量赋值,等号俩边不能有空格,比如str_name="jack"、str_name="jack"echo$str_nameecho${str_n......
  • 时间模块time-常用方法
    时间戳#获取时间戳time.time()#时间元组转为时间戳time.mktime(time.localtime())时间元组#获取时间元组time.localtime()#time.struct_time( #年月日 #......
  • C#并发编程-4 同步
    如果程序用到了并发技术,那就要特别留意这种情况:一段代码需要修改数据,同时其他代码需要访问同一个数据。这种情况就需要考虑同步地访问数据。如果下面三个条件都满足,就必......