hahahia

5주차 객체 과제 본문

Language/C++

5주차 객체 과제

hahahia 2011. 5. 10. 21:17

5.10
#include <iostream>
using namespace std;
int main()
{
 int result=1;

 for(int i=1; i<=5; i++)
 {
  for (int j=1; j <= i; j++)
  {
    result *= j;
  }
  cout<< i << "! " <<result<<endl;
  result = 1;
 }
 return 0;
} // 팩토리얼 출력
5.15
#include <iostream>
#include <string>
using namespace std;

class GradeBook
{
private:
 string courseName;
 int aCount;
 int bCount;
 int cCount;
 int dCount;
 int fCount;
public:
 GradeBook (string);
 void setCourseName (string);
 string getCourseName ();
 void displayMessage();
 void inputGrades();
 void displayGradeReport();
};

GradeBook::GradeBook(string name)
{
 setCourseName(name);
 aCount=0;
 bCount=0;
 cCount=0;
 dCount=0;
 fCount=0;
}
void GradeBook::setCourseName(string name)
{
 
 if (name.length() <= 25)
  courseName=name;
 else
 {
  courseName = name.substr (0,25);
  cout<<"Name\""<<name<<"\" exceeds m,aximum length (25).\n"<<"Limiting courseName to first 25 characters.\n"<<endl;
 }
}

string GradeBook::getCourseName()
{
 return courseName;
}

void GradeBook::displayMessage()
{
 cout<<"Welcome to the grade book for\n"<<getCourseName()<<"!\n"<<endl;
}

void GradeBook::inputGrades()
{
 int grade;
 cout<<"Enter the letter grades."<<endl<<"Enter the EOF charcter to end input"<<endl;
 
 while ( ( grade = cin.get() ) != EOF )
 {
  switch ( grade )
  {
  case 'A':
  case 'a':
   aCount++;
   break;

  case 'B':
  case 'b':
   bCount++;
   break;

  case 'C':
  case 'c':
   cCount++;
   break;
     
  case 'D':
  case 'd':
   dCount++;
   break;

  case 'F':
  case 'f':
   fCount++;
   break;

  case '\n':
  case '\t':
  case ' ':
   break;

  default:
   cout<<"Incorrect letter grade entered."<<"Enter a new grade."<<endl;
   break;
  }
 }
}

void GradeBook::displayGradeReport()
{
 cout<<"\n\nNumber of students who received each letter grade.\n"
  <<"\nA: "<<aCount
  <<"\nB: "<<bCount
  <<"\nC: "<<cCount
  <<"\nD: "<<dCount
  <<"\nF: "<<fCount
  <<endl;
 cout<<"Grade average : "<<(double)(4*aCount+3*bCount+2*cCount+1*dCount+0*fCount)/(aCount+bCount+cCount+dCount+fCount)<<endl;
}


int main()
{
 GradeBook myGradeBook( " CS101 C++ Programming ");

 myGradeBook.displayMessage();
 myGradeBook.inputGrades();
 myGradeBook.displayGradeReport();
} // class를 이용한 성적입력후 학점평균계산 프로그램

5.19
#include <iostream>
using namespace std;
int main()
{
 double a=4;
 double t=0;
 for(double i=1; i<=1000; i++)
 {
  if((int)i%2==1)
  {
   a = a - (4 / (2*i+1));
  }
  else
  {
   a = a + (4 / (2*i+1));
  }
 }
 cout << a << endl;
 return 0; // π값 계산
}
5.23
#include <iostream>
using namespace std;
int main()
{
 int a;
 cout << "input a : " ;
 cin >> a ;
 for(int i=0; i<a; i++)
 {
  for(int j=a-i; j>0; j--)
  {
   cout << " ";
  }
  for(int k=0; k<=i*2; k++)
  {
   cout << "*";
  }
  cout << endl;
 }
 for(int i=0; i<a-1; i++)
 {
  for(int j=a-i-2; j<=a-1; j++)
  {
   cout << " " ;
  }
  for(int k=0; k<2*(a-1-i)-1; k++)
  {
   cout << "*";
  }
  cout << endl;
 }
 return 0;
} // diamond 모양 출력
5.29
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
 double amount;
 double principal = 24.0;
 double rate = .05;

 for (int i = 0; i<=5; i++)
 {
  for ( int year=1; year <= 384; year++)
  {
    amount = principal * pow ( 1.0 + rate, year);
  }
 cout << fixed << setprecision( 2 );
  cout << rate*100 << "% = "<< amount << endl;
  rate += .01;
  amount = 0;
 } // 원리합계
}

Comments