본문 바로가기

[C++] 과제 4 객체로 작성하기.

반응형

3.  입력시 분과 초로 변환하는 프로그램을 작성하세요.

#include <iostream>

   

using namespace std;

   

class Sec

{

public:

  int a,b;  //맴버변수 

    

  void Print();  //맴버함수 선언

  void Print2();

  void Inpput();

};

   

void Sec::Inpput()  //맴버함수 정의

{

  cin>>a;

}

   

   

void Sec::Print()  //맴버함수 정의

{

  cout << b << " min" <<" "<< a-(b*60<<" sec";

}

   

void Sec::Print2()  //맴버함수 정의

{

    cout << a << " sec";

}

   

   

   

int main()

{

  Sec pt1,pt2;    //객체 생성.

    

  pt1.a;      //객체

  pt2.b;

   

  cout << "Input seconds :";

   

  pt1.Inpput();  //입력!!

  pt2.b = (pt1.a/60);  

   

  if( pt1.a <= 60)

   

  {

   

    pt1.Print2();

      

   

  }

   

  else if( pt1.a > 60)

   

  {

   

    //pt1.Print();

    cout << pt2.b << " min" <<" "<< pt1.a-(pt2.b*60<<

sec";

   

  }

   

}

   

   

4./*입력된 정수의 2 보수를 구하여 10진수, 16진수 형태로 출력하세요.

   

출력) Input Number : 1

   

2's complement(10진수) : -1

   

2's complement(16진수) : ffffffff*/

   

#include <iostream>

using namespace std;

   

class Trans

{

public:

  int a,b;  //멤버변수 선언.

};

   

   

int main()

{

  Trans pt1, pt2;

   

  pt1.a;

  pt2.b;

   

  cout << " Input number : ";

  cin >> pt1.a;

  pt2.b = (~pt1.a)+1;

   

  cout << "2's complement(10진수):" << pt2.b << "\n";

  cout << "2's complement(16진수):" <<hex<<pt2.b;

   

}

   

    

   

반응형
-->