Vous êtes sur la page 1sur 51

,

-6 , ,

: . //


.
.
www.emust.edu.mn

float, int, char .
. , , .
,
.
C++- .

.
.
. .
C++- .
OOP- . Basic
Pascal (record) .

.
,
, ,
.
, .

//parts.cpp part part1;
#include <iostream>
using namespace std; part1.partnum=6244;
struct part part1.modelnum=373;
{ part1.cost=217.55;
int partnum;
int modelnum; cout <<"dugaar"<< part1.partnum<<endl;
float cost; cout <<"zagvar"<< part1.modelnum<<endl;
}; cout <<"une"<< part1.cost<<endl;
int main() }
{

:

6244,
373,
217.55

3 . 1- , 2-
, .

struct part
{
int partnum;
int modelnum;
float cost;
};

. Struct
. . 3
.
( ; )
.
( .).

part part1;

part part1 .
. ? 3
.
2 , 4 , 8 .
struct
C++- . .. struct part part1
.


. .

part1.partnum=6244;

, ( . ),
3 . part1- partnum .
,
.
.
.

#include <iostream> int main()


using namespace std; {
Person p1;
struct Person
{ cout << "Enter Full name: ";
char name[50]; cin.get(p1.name, 50); cout << "Displaying Information." << endl;
int age; cout << "Enter age: "; cout << "Name: " << p1.name << endl;
float salary; cin >> p1.age; cout <<"Age: " << p1.age << endl;
}; cout << "Salary: " << p1.salary;
cout << "Enter salary: ";
cin >> p1.salary;
return 0;
}


.

.

//partinit.cpp
#include <iostream> cout <<"dugaar"<<part1.partnum<<endl;
using namespace std; cout <<"zagvar"<<part1.modelnum<<endl;
cout <<"une"<<part1.cost<<endl;
struct part
{ part2=part1;
int partnum;
int modelnum; cout <<"dugaar"<< part2.partnum<<endl;
float cost; cout <<"zagvar"<< part2.modelnum<<endl;
}; cout <<"une"<< part2.cost<<endl;
}
int main()
{
part part1={ 6244, 373, 217.55 };
part part2;

2 ,
. .

6244, 373, 217.55


6244, 373, 217.55

, .
, 2
. .
.

, feet inch . Inch-


() , feet- () . 12 8 12 inch 8 feet
. , inch
feet- .
,
inch feet .

#include <iostream>
using namespace std;

struct Distance{
int feet;
float inch;
}d1 , d2, sum;

int main()
{
cout << "Enter 1st distance," << endl;
cout << "Enter feet: ";
cin >> d1.feet;
cout << "Enter inch: ";
cin >> d1.inch;

cout << "\nEnter information for 2nd distance" << endl;
cout << "Enter feet: ";
cin >> d2.feet;
cout << "Enter inch: ";
cin >> d2.inch;

sum.feet = d1.feet+d2.feet;
sum.inch = d1.inch+d2.inch;

// changing to feet if inch is greater than 12


if(sum.inch > 12)
{
++ sum.feet;
sum.inch -= 12;
}

cout << endl << "Sum of distances = " << sum.feet << " feet " << sum.inch << "
inches";
return 0;
}

Distance feet inches 2 . Feet , inches
. d1 d3 , d2
. d1- ,
d2 , d3- . :
Enter 1st distance,
Enter feet: 1
Enter inch: 6
Enter information for 2nd distance
Enter feet: 150
Enter inch: 176

Sum of distances = 152 feet 170 inches


d3 = d1 + d2 . Int,
float header
.
.

#include <iostream>
#include <cstring>

using namespace std;

struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};

int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book

// book 1 specification
strcpy( Book1.title, "Programchlal");
strcpy( Book1.author, "A.Tuvshinbayar");
strcpy( Book1.subject, "C++ Programchlal");
Book1.book_id = 6495407;

// book 2 specification
strcpy( Book2.title, "Algorithm");
strcpy( Book2.author, "A.Tuvshinbayar");
strcpy( Book2.subject, "Flowgorithm");
Book2.book_id = 6495700;

// Print Book1 info
cout << "Book 1 title : " << Book1.title <<endl;
cout << "Book 1 author : " << Book1.author <<endl;
cout << "Book 1 subject : " << Book1.subject <<endl;
cout << "Book 1 id : " << Book1.book_id <<endl;

// Print Book2 info


cout << "Book 2 title : " << Book2.title <<endl;
cout << "Book 2 author : " << Book2.author <<endl;
cout << "Book 2 subject : " << Book2.subject <<endl;
cout << "Book 2 id : " << Book2.book_id <<endl;

return 0;
}

Book 1 title : Programchlal


Book 1 author : A.Tuvshinbayar
Book 1 subject : C++ Programchlal
Book 1 id : 6495407
Book 2 title : Algorithm
Book 2 author : A.Tuvshinbayar
Book 2 subject : Flowgorithm
Book 2 id : 6495700

.

#include <iostream>
#include <cstring>

using namespace std;


void printBook( struct Books book );

struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};

int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book

// book 1 specification
strcpy( Book1.title, "Algorithm Programchlal");
strcpy( Book1.author, "R.Turbat");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;

// book 2 specification
strcpy( Book2.title, "Algorithm Programchlal");
strcpy( Book2.author, "A.Tuvshinbayar");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;

// Print Book1 info
printBook( Book1 );

// Print Book2 info


printBook( Book2 );

return 0;
}
void printBook( struct Books book ) {
cout << "Book title : " << book.title <<endl;
cout << "Book author : " << book.author <<endl;
cout << "Book subject : " << book.subject <<endl;
cout << "Book id : " << book.book_id <<endl;
}

.
.

Room dining={{13, 6.5}, {10, 0.0}};

Room distance .
. distance
{13, 6.5} , distance {10, 0.0}
.

.
. Cardsharp 3
, .
. .

struct card
{
int number;
int suit;
}

2- 14 . 11- 14
, , , . 0- 3
. , , , .

const int clubs=0; //
const int diamonds=1; //
const int hearts=2; //
const int spades=3; //

const int jack=11; //


const int queen=12; //
const int king=13; //
const int ace=14; //

struct card
{
int number;
int suit;
}

.

7
2
3
1- 3-
2- 3-
1- 2-
? (1,2,3) 3
. .

.
.
.
. ,
. 1- 3-, 3- 2-, 2- 1- .
,
, .

.

.
.

. Pascal
.


. .

#include <iostream>
using namespace std;

enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

int main()
{
week today;
today = Wednesday;
cout << "Day " << today+1;
return 0;
}

day 4

.
.
0, 1 . Sun
0, Sat 6 . today+1 .
.

#include <iostream>
using namespace std;

enum season
{ spring = 0,
summer = 4,
autumn = 8,
winter = 12
};
int main()
{
season odoo;
odoo= summer;
cout << "zun " <<odoo<<"sar urgeljilne";
return 0;
}

Zun 4 sar urgeljilne

.

#include <iostream>
using namespace std;

enum seasons { spring = 34, summer = 4, autumn = 9, winter = 32};

int main() {

seasons s;

s = summer;
cout << "Summer = " << s << endl;

return 0;
}

Summer=4

C++-
. .
chcount .
space ( )
. .

// Computes time difference of two time period
// Time periods are entered by the user

#include <iostream>
using namespace std;

struct TIME
{
int seconds;
int minutes;
int hours;
};

void computeTimeDifference(struct TIME, struct TIME, struct TIME *);


int main()
{
struct TIME t1, t2, difference;

cout << "Enter start time." << endl;


cout << "Enter hours, minutes and seconds respectively: ";
cin >> t1.hours >> t1.minutes >> t1.seconds;

cout << "Enter stop time." << endl;


cout << "Enter hours, minutes and seconds respectively: ";
cin >> t2.hours >> t2.minutes >> t2.seconds;

computeTimeDifference(t1, t2, &difference);


cout << endl << "TIME DIFFERENCE: " << t1.hours << ":" << t1.minutes << ":"
<< t1.seconds;
cout << " - " << t2.hours << ":" << t2.minutes << ":" << t2.seconds;
cout << " = " << difference.hours << ":" << difference.minutes << ":" <<
difference.seconds;
return 0;
}
void computeTimeDifference(struct TIME t1, struct TIME t2, struct TIME
*difference){

if(t2.seconds > t1.seconds)


{
--t1.minutes;
t1.seconds += 60;
}

difference->seconds = t1.seconds - t2.seconds;
if(t2.minutes > t1.minutes)
{
--t1.hours;
t1.minutes += 60;
}
difference->minutes = t1.minutes-t2.minutes;
difference->hours = t1.hours-t2.hours;
}


Enter start time.
Enter hours, minutes and seconds respectively: 00
01
10
Enter stop time.
Enter hours, minutes and seconds respectively: 23
55
23

TIME DIFFERENCE: 0:1:10 - 23:55:23 = -24:5:47



,
.
.
isWord
. isWord
. if
else , else
.

//cardenum.cpp
#include <iostream.h>

const int jack=11; //


const int queen=12; //
const int king=13; //
const int ace=14; //

enum Suit {clubs,diamonds,hearts,spades};

struct card

0 .
, 0- .

Enum Suit {clubs=1,diamonds,hearts,spades};

diamonds = 2, hearts = 3, spades = 4 .


. ,
. :

day1.suit=hearts;
cout << day1.suit;

hearts , 3 .

enum months {Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};

enum lightswitch {off,on};

enum chess {pawn,knight,bishop,rook,queen,king};


6
2 .
C++- ,
.
.
,
. .
, .
.

1. ,
2. , 3- , - , ,

3. U.CS101
4. he2must.blogspot.com
5. https://www.programiz.com/cpp-programming/enumeration
6. https://www.programiz.com/cpp-programming/structure

Vous aimerez peut-être aussi

  • Programming Lec 6
    Programming Lec 6
    Document51 pages
    Programming Lec 6
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 15
    Lecture 15
    Document40 pages
    Lecture 15
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lec1-1 Corporate Activity Shine
    Lec1-1 Corporate Activity Shine
    Document61 pages
    Lec1-1 Corporate Activity Shine
    Amarsaikhan Tuvshinbayar
    0% (1)
  • Lecture 8 2018
    Lecture 8 2018
    Document54 pages
    Lecture 8 2018
    Amarsaikhan Tuvshinbayar
    100% (3)
  • Lecture - 10
    Lecture - 10
    Document35 pages
    Lecture - 10
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 12
    Lecture 12
    Document34 pages
    Lecture 12
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 13
    Lecture 13
    Document46 pages
    Lecture 13
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lecture 11
    Lecture 11
    Document24 pages
    Lecture 11
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lecture 3 2018
    Lecture 3 2018
    Document42 pages
    Lecture 3 2018
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 5 2018
    Lecture 5 2018
    Document54 pages
    Lecture 5 2018
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lecture 7 20108
    Lecture 7 20108
    Document47 pages
    Lecture 7 20108
    Amarsaikhan Tuvshinbayar
    75% (4)
  • Lecture 4 2018
    Lecture 4 2018
    Document47 pages
    Lecture 4 2018
    Amarsaikhan Tuvshinbayar
    100% (4)
  • Lecture 6 2018
    Lecture 6 2018
    Document50 pages
    Lecture 6 2018
    Amarsaikhan Tuvshinbayar
    100% (3)
  • Lecture 9
    Lecture 9
    Document46 pages
    Lecture 9
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 2 2018
    Lecture 2 2018
    Document33 pages
    Lecture 2 2018
    Amarsaikhan Tuvshinbayar
    100% (1)
  • Lecture 1 2018
    Lecture 1 2018
    Document39 pages
    Lecture 1 2018
    Amarsaikhan Tuvshinbayar
    100% (6)
  • Programming Lec 7
    Programming Lec 7
    Document57 pages
    Programming Lec 7
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Programming Lec 4
    Programming Lec 4
    Document39 pages
    Programming Lec 4
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • IT101 Lecture 4
    IT101 Lecture 4
    Document72 pages
    IT101 Lecture 4
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 3
    Lecture 3
    Document72 pages
    Lecture 3
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Koosen 2018 Algorithm 2
    Koosen 2018 Algorithm 2
    Document48 pages
    Koosen 2018 Algorithm 2
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 2
    Lecture 2
    Document80 pages
    Lecture 2
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 1
    Lecture 1
    Document54 pages
    Lecture 1
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Baikal Hotolbor
    Baikal Hotolbor
    Document2 pages
    Baikal Hotolbor
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 3
    Lecture 3
    Document72 pages
    Lecture 3
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Programming Lec 8
    Programming Lec 8
    Document32 pages
    Programming Lec 8
    Amarsaikhan Tuvshinbayar
    100% (1)
  • PL - 3
    PL - 3
    Document47 pages
    PL - 3
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Koosen 2018 Algorithm Programming 3
    Koosen 2018 Algorithm Programming 3
    Document46 pages
    Koosen 2018 Algorithm Programming 3
    Amarsaikhan Tuvshinbayar
    100% (1)
  • Programming Lec 9
    Programming Lec 9
    Document39 pages
    Programming Lec 9
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Programming Lec 6
    Programming Lec 6
    Document51 pages
    Programming Lec 6
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation