Demo entry 6661080

t

   

Submitted by mk on Nov 20, 2017 at 01:59
Language: C++. Code size: 2.0 kB.

#include "stdafx.h"
#include <string.h>
#include <iostream>
#define FALSE 0
#define TRUE !FALSE

using namespace std;

class MovAvg {
	public :
		int n ;
		int cnt ;
		float data[100] ;

	public :
		void init(){
			int i ;
			this->cnt = 0 ;
			for(i = 0 ; i < 100 ; i++){
				this->data[i] = 0 ;
			}
		} ;

		void printData(){
			int i = this->cnt ;
			int j = 0 ;

			cout << "\n ==> 평균값 [" ;
			while(j <= i){
				cout.precision(2) ;
				cout << this->data[j] ;
				if ( j < i ){
					cout << "," ;
				}
				j++ ;
			}
			cout << "]" << endl;
		} ;

		void doAvg(){
			int i, j ;
			i = this->cnt ;
			float tempSum = 0 ;
			for( ; i >= 0 ; i--){

				//n으로 나눌 자격 통과
				if((i - this->n) >= 0 ){
					for(j = 0 ;j < this->n ; j++ ){
						tempSum += this->data[i-j] ;
					}
					this->data[i] = tempSum/(float)this->n ;
				}else{
					for(j = 0 ; j <= i ; j++ ){
						tempSum += this->data[j] ;
					}
					this->data[i] = tempSum/(float)j ;
				}
				tempSum = 0 ;
			}
		} ;
} ;

int main()
{
	int i,j, sw ;
	float tmp, inputData ;

	MovAvg *mAvg ;
	mAvg = new MovAvg() ;

	j = 0 ;
	tmp = 0 ;
	do{
		mAvg->init() ;
		while(1){
			cout << "이동평균 윈도 크기를 설정하십시오." ;
			cin >> j ;
			if(j > 0){
				mAvg->n = j ;
				break ;
			}else if( j <= 0 && mAvg->n > 0){
				break ;
			}
		}
		i = 0 ;
		while(i < 100){
			cout << "자료를 입력해주세요(최대 99개까지 입력 가능)" << endl ;
			cout << "숫자 외 입력 시 입력 종료" << endl ;
			cin >> inputData ;
			if(cin.fail()){
				std::cout << "문자가 입력됐습니다." << std::endl ;
				std::cout << "데이터 입력종료" << std::endl ;
				cin.clear() ;
				cin.ignore(256, '\n') ;
				break ;
			}

			mAvg->data[i] = (float) inputData ;
			i++ ;
		}
		i-- ;
		mAvg->cnt = i ;
		//
		mAvg->doAvg() ;
		//
		mAvg->printData() ;

		cout << "다시 이동거리 평균을 구하시겠습니까?" << endl;
		cout << "1.다시하기  2.프로그램 종료" << endl ;
		cin >> sw ;
		sw = sw - 2 ;
	}while(sw) ;
    return 0;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).