zoukankan      html  css  js  c++  java
  • C++primer plus第六版课后编程题答案 6.2

    //编写一个程序,最多将10个donation值读入到一个double数组中。
    //程序遇到非数字输入时将结束输入,并报告这些数字的平均值以
    //及数组中有多少个数字大于平均值。
    
    
    #include <iostream>
    #include <cctype>
    #include <array>
    using namespace std;
    const int MAX=10;
    void main62()
    {
    	//double donation[MAX];
    	array<double,MAX>donation={0};
    	char ch;
    	//for(int i=0;i<MAX;i++)
    	//{
    		cout<<"Please enter the number:"<<endl;
    		double sum=0;
    		double avg=0;
    		double count=0;
    		//cin.get(ch);
    		cin.sync();
    		int i=0;
    		//double temp;
    		double temp;
    		
    		cout<<"Please enter the "<<i+1<<" number:";
    		while(i<MAX&&cin>>temp) //程序当输入非数字时,总会异常跳出,什么原因呢?
    		{
    			donation[i]=temp;
    			sum+=donation[i];
    			i++;
    			cout<<"Please enter the "<<i+1<<" number:";
    
    		}
    		cin.sync();
    		avg=sum*1.0/i;
    		for(int j=0;j<i;j++)
    		{
    			if(donation[j]>avg)
    				count++;
    		}
    		cout<<"The sum is "<<sum<<endl;
    		cout<<"The avg is "<<avg<<endl;
    		cout<<"Have  "<<count<<"  number is bigger than avg";
    		//cin.get(); //不要用cin.get(),因为这只是读取一个字符,如果你后面输入了多个非数字字母,
    		//cin.get();//窗口很快就会消失不见
    		getchar();
    		getchar(); //用getchar()或者是system("pause")
    		//system("pause");
    		
    	
    	}
    
    
    
    参考:http://zhidao.baidu.com/question/1987844098735710907.html?quesup2&oldq=1

    
    
    
    
    
    
  • 相关阅读:
    TCP/IP详解V2(六)之TCP协议
    TCP/IP详解V2(四)之TCP协议
    TCP/IP详解V2(三)之TCP协议
    epoll源码剖析
    UDT源码剖析(二)之启动与结束
    TCP/IP详解V2(二)之UDP协议
    TCP/IP详解V2(一)之协议控制块
    UDT源码剖析(一)之总览
    SPI通信协议(SPI总线)学习
    第12章Cortex-M4-SPI-Bus
  • 原文地址:https://www.cnblogs.com/qq84435/p/3664840.html
Copyright © 2011-2022 走看看