ååã®ãšã³ããªãŒã®å 容ã1è¡ã§èŠçŽããã°ããMinGWã®gccãbashã®gccãšééããŠã€ã³ã¹ããŒã«ããããçµæãªãŒã©ã€ã§ã©ã£ã¡ã§ãæ§ããªãã£ãããšããããšã§ããä»åã®ãšã³ããªãŒã®å 容ããå ã«1è¡ã§èŠçŽããŠããŸããšãåå空éïŒnamespaceïŒãšããã®ãç¥ããªãã£ãã®ã§ãæã®ã³ãŒããã³ã³ãã€ã«éããªãã£ãããšããããšã«å°œããŸãã
ã¹ãã³ãµãŒãªã³ã¯
Â
Â
ãªãã§C++ãªã®ããšãããšããã10幎以äžåã«ä»äººããããã£ãããããªã³ãŒããæã£ãŠããããã ããããããã³ã³ãã€ã«ã§ãããªããã¡ãã£ãšæ¹é ããã° 4æ27æ¥ ã 28æ¥ ã®ãšã³ããªãŒã«æžããäŸé¡ã®ã·ãã¥ã¬ãŒã·ã§ã³ããã°ã©ã ã«ã§ããããããªãããšèããããã§ããã
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
// // ãµã€ã³ãã®ç®ã®ãã©ããã調ã¹ãããã°ã©ã // // file name : saikoro1.cpp // #include <iostream.h> #include <iomanip.h> #include <stdlib.h> #include <time.h> main() {     const int Faces = 6 ; // ãµã€ã³ãã®ç®ã®æ°     int freq[Faces] = { 0 } ; // åç®ã®é »åºŠ     int face ; // åºãç® Â Â Â Â int trial; // è©Šè¡åæ° Â Â Â Â int i ;     // è©Šè¡åæ°ãå ¥åãã     cout << "\nãµã€ã³ãã®åç®ã®åºã確çã調ã¹ã\n\n" ;     cout << "äœåããµã€ã³ããæ¯ããå ¥åããŠäžãã >> " ;     cin >> trial ;     // ãµã€ã³ããè©Šè¡åæ°åã ãæ¯ã     srand( time(NULL) ) ; // ä¹±æ°ã®åæå     for( i = 0 ; i < trial ; i++ ){         face = rand() % 6 ;         freq[face]++ ;     }     // çµæã®åºå     cout << "\n\nãµã€ã³ãã" ;     cout << trial << "åæ¯ããŸãã\n\n" ;     for( i = 0 ; i < Faces ; i++ ){         cout << setw( 5 ) << i + 1 << "ã®åºãåæ°:" ;         cout << setw( 8 ) << freq[i] << "å" << endl ;     }     cout << endl ;     return 0 ; } |
以äžãç°¡åãªããã°ã©ã ã®å 容ã®èª¬æã29è¡ç®ã§Cã®çµã¿èŸŒã¿é¢æ°randãšããã®ã6ã§å²ã£ãå°äœããµã€ã³ãã®ç®ãšããŠãããrandã¯ç䌌乱æ°ãçºçããé¢æ°ã§ãããrandã®çºçããç䌌乱æ°ã¯ãã·ã¹ãã ã«ãã£ãŠéãã確ã0ïœ32767以äžã®ç¯å²ãããªãã£ãããªã
ãã ãç䌌乱æ°ãšããã®ã¯æ¬ç©ã®ä¹±æ°ã§ã¯ãªããŠãäœåºŠç¹°ãè¿ããŠãé çªéãã«åãæ°åãåºãŠããã®ã ãããã§27è¡ç®ã§ãç䌌乱æ°ã®åæå€ãäžããé¢æ°srandãšãã·ã¹ãã æéãç§åäœïŒããŒãã©ãŒãããïŒã§åãåºãé¢æ° time(NULL) ã䜿ã£ãŠãä¹±æ°ã®åæåãè¡ã£ãŠãããç§åäœã§ç䌌乱æ°ã®çºçããæ°åãå€æŽããã®ã ã
次ã®ãããªäŸãã䜿ããšãããããããšæãã1ã7ã§å²ããšãåæ¡ã«çŸããæ°åã¯ã1ãã4ãã2ãã8ãã5ãã7ã以äžç¹°ãè¿ããšãªããã1ã7ã§å²ã£ã埪ç°å°æ°ã®åæ¡ããšããããšãç¥ããªãè ã«ãšã£ãŠã¯ã次ã®æ°åãäºæž¬ããã®ã¯é£ããããã®åæ¡ã«çŸããæ°åãšåŸªç°ããåšæãããããšå€§ããããã®ãç䌌乱æ°ãªã®ã ããã ãé çªã«åºåãããæ°åã¯æ¯ååãã«ãªãã®ã§ãæåã«å²ãæ°åã1ã§ã¯ãªã7ã®åæ°ä»¥å€ã®æ°åã«å€ãããšãããã°ã©ã ãå®è¡ãããã³ã«éã£ãé çªã§æ°åãåºåãããããšãã§ããã
ééã£ãããšèšã£ãŠããã³ã¡ã³ãããã³ã¡ã§çªã£èŸŒãã§ãã ããã
ãã®ã³ãŒããã³ã³ãã€ã«ããããšãããããšã©ãŒã¡ãã»ãŒãžãåºãŠã³ã³ãã€ã«éããªãã£ãã®ã ã
ãããªã®ã
iostream.hããªãïŒ ããããã¡ã€ã«ã¯å¥ã«ããŠã³ããŒãããªãããªããªãã®ãïŒ ããæã£ãŠæ€çŽ¢ããããC++ã®ãœãŒã¹ã³ãŒããããã€ãåºãŠããŠã#include <iostream.h>ã§ã¯ãªã#include <iostream> ãšæžããŠããã
ã.hããå€ãã°ããã®ããšæã£ãŠãã£ãŠã¿ãããã®æç¹ã§ã¯ãäœãæªãã®ãç解ããŠããªãã
åæ§ã«8è¡ç®ã®#include <iomanip.h> ã #include <iomanip> ã«å€ããŠã¿ãã9è¡ç®ãš10è¡ç®ã¯ãã®ãŸãŸã§ã次ã®ãããªãšã©ãŒã¡ãã»ãŒãžãåºãããã«ãªã£ãã
1ç»é¢ããåãŸããããªãã£ãã®ã§ã2ç»é¢ã«åå²ããŠããŸãã
âcinâ ã®ä»£ããã« âstd::cinâ ãâcoutâ ã®ä»£ããã« âstd::coutâ âŠã®äœ¿çšããå§ãããŸããã ãšïŒ
ããã§ãããããã³ãšæ¥ãã
ç¹°ãè¿ãããã ããç§ã®C++ã®ç¥èã¯10幎以äžåã§æ¢ãŸã£ãŠãããBorland C++ compiler ãšããããªãŒã®ã³ã³ãã€ã©ãšãäž»ã«æ¬¡ã®æžç±ã§å匷ããã®ã ãäžã®ãã«ãŒããã¯ã¹ã®æ¹ã¯æ¬é¡ã«é¢ä¿ãªããã ä»é²ã®CD-ROMã«Borland C++ compiler ãã€ããŠããŠæãããã®ã§è²Œã£ãŠã¿ãã
C++ããã°ã©ãã³ã°ãVol.1ã (Computer Science Textbook)
- äœè : ããŒã〠M.ãã€ãã«,ããŒã« J.ãã€ãã«,Harvey M. Deitel,Paul J. Deitel,å°å¶éäž
- åºç瀟/ã¡ãŒã«ãŒ: ãã¢ãœã³ã»ãšãã¥ã±ãŒã·ã§ã³
- çºå£²æ¥: 1999/05
- ã¡ãã£ã¢: åè¡æ¬
- è³Œå ¥: 1人 ã¯ãªãã¯: 13å
- ãã®ååãå«ãããã° (5件) ãèŠã
- äœè : å°æå¥äžé
- åºç瀟/ã¡ãŒã«ãŒ: è¬è«ç€Ÿ
- çºå£²æ¥: 2001/05/18
- ã¡ãã£ã¢: æ°æž
- è³Œå ¥: 4人 ã¯ãªãã¯: 23å
- ãã®ååãå«ãããã° (14件) ãèŠã
 åé¡ã¯äžã®æ¹ã ãåè第2çã®èš³æ¬ã ããåèã®æ¹ã¯å°ãªããšã第4çãŸã§ã¯æ¹èšãé²ãã§ãããææ°ã第äœçãªã®ãã¯ç¥ããªãããªã¢ã«ã®ç¥äººã«æ°ããåæžãæã£ãŠãã人ãããŠãæåã®æ¹ã®ããŒãžããã©ãã©ãšããã£ãããèš³æ¬ã«ã¯ãªãããŒãžãäœããŒãžããã£ããããã« âstd::cinâ ãâstd::coutâ ã¿ãããªããšãæžãããŠãããããªèšæ¶ãããããã«æ®ã£ãŠããã®ã ã
ãããªããšã¯ãã«ãŒããã¯ã¹ã®æ¹ã«ãæžããŠãªãã£ãããC++é¢é£æžç±ã¯äžèš2å以å€ã«ãäœåãæã£ãŠããããã©ãã«ãæžããŠãªãã£ããç¹°ãè¿ãã10幎以äžåã®ããšã ã
ããããªèšæ¶ãé Œãã«æ€çŽ¢ãããšããã®ã¯ãæ¡å€ããããããåå空éïŒnamespaceïŒããšãããããããããããã®ãå°å ¥ãããæ矩ã«ã€ããŠã¯ãããšãããã£ãã調ã¹ããããšãããããšã©ãŒãåºãäºçŽèªã®é ã«ãçã£ç«¯ãã âstd::â ãããŒã¹ãããŠã¿ããçã£ç«¯ãããšèšã£ãŠã âcinâã âcoutâã âsetwâã âendlâ ã ãã§ããã£ãããããªãããªãèŠããã«å ¥åºåé¢ä¿ã ã
ãããªæãã
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
// // ãµã€ã³ãã®ç®ã®ãã©ããã調ã¹ãããã°ã©ã // // file name : saikoro3.cpp // #include <iostream> #include <iomanip> #include <stdlib.h> #include <time.h> main() {     const int Faces = 6 ; // ãµã€ã³ãã®ç®ã®æ°     int freq[Faces] = { 0 } ; // åç®ã®é »åºŠ     int face ; // åºãç® Â Â Â Â int trial; // è©Šè¡åæ° Â Â Â Â int i ;     // è©Šè¡åæ°ãå ¥åãã     std::cout << "\nãµã€ã³ãã®åç®ã®åºã確çã調ã¹ã\n\n" ;     std::cout << "äœåããµã€ã³ããæ¯ããå ¥åããŠäžãã >> " ;     std::cin >> trial ;     // ãµã€ã³ããè©Šè¡åæ°åã ãæ¯ã     srand( time(NULL) ) ; // ä¹±æ°ã®åæå     for( i = 0 ; i < trial ; i++ ){         face = rand() % 6 ;         freq[face]++ ;     }     // çµæã®åºå     std::cout << "\n\nãµã€ã³ãã" ;     std::cout << trial << "åæ¯ããŸãã\n\n" ;     for( i = 0 ; i < Faces ; i++ ){         std::cout << std::setw( 5 ) << i + 1 << "ã®åºãåæ°:" ;         std::cout << std::setw( 8 ) << freq[i] << "å" << std::endl ; }     std::cout << std::endl ;     return 0 ; } |
 ããã§ã³ã³ãã€ã«éã£ãïŒ ãµã€ã³ããæ¯ãåæ°ã12000åãšããŠå®è¡ããã¹ã¯ã·ã§ãäžã«ç€ºãã
âstd::â ã¯çç¥ããæ¹æ³ããã£ãã¯ããšæ€çŽ¢ãããšããããã£ããèŠã€ãã£ãã宣èšéšã« âusing namespace std;â ã®äžè¡ãè¿œå ããã°ããã®ã ïŒ ãããªæãã
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
// // ãµã€ã³ãã®ç®ã®ãã©ããã調ã¹ãããã°ã©ã // // file name : saikoro4.cpp // #include <iostream> #include <iomanip> #include <stdlib.h> #include <time.h> using namespace std; main() {     const int Faces = 6 ; // ãµã€ã³ãã®ç®ã®æ°      int freq[Faces] = { 0 } ; // åç®ã®é »åºŠ     int face ; // åºãç® Â Â Â Â int trial; // è©Šè¡åæ° Â Â Â Â int i ;     // è©Šè¡åæ°ãå ¥åãã     cout << "\nãµã€ã³ãã®åç®ã®åºã確çã調ã¹ã\n\n" ;     cout << "äœåããµã€ã³ããæ¯ããå ¥åããŠäžãã >> " ;     cin >> trial ;     // ãµã€ã³ããè©Šè¡åæ°åã ãæ¯ã     srand( time(NULL) ) ; // ä¹±æ°ã®åæå     for( i = 0 ; i < trial ; i++ ){         face = rand() % 6 ;         freq[face]++ ;     }     // çµæã®åºå     cout << "\n\nãµã€ã³ãã" ;     cout << trial << "åæ¯ããŸãã\n\n" ;     for( i = 0 ; i < Faces ; i++ ){         cout << setw( 5 ) << i + 1 << "ã®åºãåæ°:" ;         cout << setw( 8 ) << freq[i] << "å" << endl ;     }     cout << endl ;     return 0 ; } |
æåã«ç€ºããã³ãŒãïŒsaikoro1.cppïŒãšã®éãã¯ã7è¡ç®ãš8è¡ç®ã® â.hâ ããªãããšãšã11è¡ç®ãè¿œå ãããŠããããšã ãã®ã¯ãã
念ã®ãããããã³ã³ãã€ã«ããŠå®è¡ãåäœã確èªããã
ããããããã£ããåäœã確èªããã³ãŒããããã°ã«è²Œãä»ããŠããããç»é¢ããã³ããŒããŠæ°ããªãã¡ã€ã«ã«ä¿åããŠå床ã³ã³ãã€ã«ïŒå®è¡ã§ãããšãããŸã§ç¢ºèªããŠããŸãïŒåœç¶ãªããè¡çªå·ã¯ã³ããŒããªãã§ãã ãããè¡šãäœæããŠããŸãã®ã§ãã³ãŒãã ãã³ããŒã§ããã¯ãã§ãïŒã
ãåå空éããC++ã®ããŒãžã§ã³ã«ã€ããŠã¯ãããããå°ããã€å匷ãããã
ãããããçªã£èŸŒã¿ã©ããã®ãããšã³ããªãŒãæžããšãã³ã¡ã çäžÂ çæ³ã«ãªããšããæ³åããä»åºŠãçºåããŸãããã³ã¡ã³ããã ãã£ãæ¹ã
ã«æè¬ããŸãããã£ãºãã«ããããªããšã¯ã§ããªãã®ã§ãåæšéžæããŠå°ããã€æ°ããããšãè©ŠããŠã¿ãŸãã
è¿œèšïŒ
ãããããèå¿ã®åé¡ã®ã·ãã¥ã¬ãŒã·ã§ã³ã¯ã©ããªã£ãããšãããšããŸãã¯4æ28æ¥ã®åé¡ãå®ã¯ãã£ã¡ã®æ¹ãç°¡åã ã£ãã
æç3å²ã¡ããã©ã®æè ã5æåžã§3æ¬ã®ããããæã€ç¢ºçãæ±ããã
ã³ãŒããè¡æ°ã¯å¿ èŠãªãã®ã§è¡šç€ºããæéãæãã¿ãŸããã
//
// file name : sananda.cpp
//#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
using namespace std;main()
{
  const int Dasu = 5 ; // æåž = 5  long int siai ; // è©Šåæ°
  long int sankai = 0; // 3åããããæã£ãè©Šåæ°
  int kekka ; // çµæïŒ1ïŒãããã0ïŒã¢ãŠãïŒ
  double wariai ; // 3åããããæã£ãå²å
  int i ,j ;  // è©Šè¡åæ°ãå ¥åãã
  cout << "\næç3å²ã®æè ã5æåžã§3å®æãã確ç\n\n" ;
  cout << "çµ±èšãåãåæ°ïŒè©Šåæ°ïŒãå ¥åããŠãã ãã >> " ;
  cin >> siai ;  // è©Šåæ°ã ãç¹°ãè¿ã
  srand( time(NULL) ) ; // ä¹±æ°ã®åæå
  for( i = 0 ; i < siai ; i++ ){
    kekka = 0 ;
    for( j = 0 ; j < Dasu ; j++ ){
    // 0ãã9ãŸã§ã®ä¹±æ°ã3ã§å²ã£ãäœãã1ã®ãšãããããšããïŒ3/10ïŒ
      if( (rand() % 10 )%3 == 1 ) kekka++ ;
    }
    // cout << "\nkekka = " << kekka << endl; // ãããã°çšåºå
    if ( kekka == 3 ) sankai++ ;
  }
  wariai = static_cast< double >( sankai ) / siai;  // çµæã®åºå
  cout << "\n\nè©Šåæ°ã¯" << endl ;
  cout << siai << "åã§ã\n";
  cout << "\n3åããããæã£ãè©Šåã®æ°ã¯" << endl ;
  cout << sankai << "åã§ã"<< endl ;
  cout << "\n3åããããæã£ãè©Šåã®å²åã¯" << endl ;
  cout << wariai << "ã§ã"<< endl ;  cout << endl ;
  return 0 ;
}
å®è¡çµæã®ã¹ã¯ã·ã§ã
4æ27æ¥ã®åé¡ã
10æ¬ã®ããã®äžã«2æ¬ã®åœããããããããAãBãCãé ã«1æ¬ãã€åŒããAãBãCãããããåœãã確çãæ±ãããÂ
 ã³ãŒããããã£ãããã³ãŒãããªã©ãšèšããªããçŽ äººããã£ãŠãŸã
//
// AãBãCã®äžäººã10æ¬äž2æ¬ã®åœãããããåŒããŠåœãã確ç
//
// file name : kujibiki.cpp
//#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
using namespace std;main()
{
  const int Kuji = 10 , Atari = 2 ; // ãã = 10æ¬ã åœã ïŒ 2æ¬Â  long int sikosu ; // è©Šåæ°
  long int Aatari, Batari, Catari ; // AãBãCãåœãã£ãåæ°
  int Akuji, Bkuji, Ckuji ; // AãBãCãåŒãããã
  double Awari, Bwari, Cwari ; // AãBãCãåœãã£ãå²å
  int i ;  // è©Šè¡åæ°ãå ¥åãã
  cout << "\nAãBãCã®äžäººã10æ¬äž2æ¬ã®åœãããããåŒããŠåœãã確ç\n\n" ;
  cout << "çµ±èšãåãåæ°ïŒãããåŒãåæ°ïŒãå ¥åããŠãã ãã >> " ;
  cin >> sikosu ;  // è©Šè¡æ°ã ãç¹°ãè¿ã
  srand( time(NULL) ) ; // ä¹±æ°ã®åæå
  Aatari = Batari = Catari = 0 ;  for( i = 0 ; i < sikosu ; i++ ){
    // A ããããåŒããåœãã§ããã°åœãã£ãæ°ãäžã€å¢ãã
    Akuji = rand() % Kuji;
    if( Akuji < Atari )Aatari++; // Aã®åŒããå€ã2æªæºã§ããã°åœã    Bkuji = rand() % 10;// B ããããåŒã
    // BãAãšåããããåŒãããšã¯ãªãã®ã§ãåãã§ããã°ããçŽã
    while( Bkuji == Akuji )Bkuji = rand() % Kuji;
    // B ãåœãã§ããã°åœãã£ãæ°ãäžã€å¢ãã
    if( Bkuji < Atari )Batari++; // Bã®åŒããå€ã2æªæºã§ããã°åœã    Ckuji = rand() % 10;// C ããããåŒã
    // CãAãŸãã¯BãšåããããåŒãããšã¯ãªãã®ã§ãåãã§ããã°ããçŽã
    while( Ckuji == Akuji || Ckuji == Bkuji)Ckuji = rand() % Kuji;
    // C ãåœãã§ããã°åœãã£ãæ°ãäžã€å¢ãã
    if( Ckuji < Atari )Catari++; // Cã®åŒããå€ã2æªæºã§ããã°åœã    //cout << "\nAkuji = " << Akuji << endl; // ãããã°çšåºå
    //cout << "\nBkuji = " << Bkuji << endl; // ãããã°çšåºå
    //cout << "\nCkuji = " << Ckuji << endl; // ãããã°çšåºå
  }
  Awari = static_cast< double >( Aatari ) / sikosu;
  Bwari = static_cast< double >( Batari ) / sikosu;
  Cwari = static_cast< double >( Catari ) / sikosu;  // çµæã®åºå
  cout << "\n\nè©Šè¡æ°ã¯" << endl ;
  cout << sikosu << "åã§ã\n";
  cout << "\nAãåœãã£ãåæ°ã¯ " << Aatari << "åã§ã"<< endl ;
  cout << "\nBãåœãã£ãåæ°ã¯ " << Batari << "åã§ã"<< endl ;
  cout << "\nCãåœãã£ãåæ°ã¯ " << Catari << "åã§ã"<< endl ;
  cout << "\nAãåœãã£ãå²åã¯" << Awari << "ã§ã"<< endl ;
  cout << "\nBãåœãã£ãå²åã¯" << Bwari << "ã§ã"<< endl ;
  cout << "\nCãåœãã£ãå²åã¯" << Cwari << "ã§ã"<< endl ;  cout << endl ;
  return 0 ;
}
ã¹ã¯ã·ã§ãÂ
ããããã¹ã¯ãªãŒã³ããã³ããŒããã³ãŒããåã³ã³ãã€ã«ããŠåäœç¢ºèªããŠããŸãã
äŸã«ãã£ãŠãééããšãäœããæ°ã¥ãã®ç¹ãããã°ããã²æããŠãã ããm(_ _)m
ã¹ãã³ãµãŒãªã³ã¯
Â
Â