21 match sticks
/*********************************************************/
/* matches.cpp - VC5 program */
/* Match picking - simple game */
/* J LUO, 29-Jun-2000 */
/*********************************************************/
#include<iostream.h>
const char NL='\n';
void main() {
cout << "I have got 21 mathces. Let's pick them up in turn. \n\n";
cout << "You may pick up 1, 2, 3, or 4 \n";
cout << "If you pick up the last one, you loss. \n\n";
int n = 21; // the number to matches
int i; // the number you take
while ( n>1) {
cout << "You take ... ";
cin >> i;
if ( (i<1) || (i>4) ) {
cout << "Don't try to cheat me, take 1, 2, 3, or 4, please! \n";
} else {
cout << "I take " << 5-i << endl;
n = n -5; // I'm smart!
}
}
cout << " Now take the last one, please. \n";
cout << " And you loss :-) \n\n";
}
Guess a number
/*********************************************************/
/* guess.cpp - VC5.0 */
/* Guess a number - simple game */
/* J LUO, 29-Jun-2000 */
/* J Luo, 22-Feb-2001 */
/* Modified, change to printf/scanf */
/* J Luo, 22-Feb-2001 */
/*********************************************************/
#include<iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int getNumber();
void main()
{
int n = getNumber();
char yesNo = 'y';
int m;
int k;
printf("I have got a number betweeen 1 and 100. \n");
printf("Would you like to guess? [Y/N]");
scanf("%c", &yesNo);
if ( yesNo == 'y' ) yesNo = 'Y';
k = 0;
while ( yesNo == 'Y' )
{
printf("It is: ");
scanf("%d", &m);
k = k + 1;
if ( m < n )
{
printf("%d is too small! \n", m);
} else if ( m>n ) {
printf("%d is too large! \n", m);
} else {
printf("Good, you did it in %d times! \n", k);
printf("Yes, the number is: %d \n", n);
break;
}
if ( k >= 8 )
{
printf("Sorry, you did it in %d times! \n", k);
printf("The number is: %d \n", n);
break;
}
}
if ( k == 0 )
{
printf(" Hope you like this game! \n");
} else if ( k <= 7 ) {
printf(" You are smart! :-) \n");
} else {
printf(" You need some practice. :-( \n");
}
}
int getNumber()
{
srand( (unsigned) time( NULL ) );
for (int i=0; i<1000; i++) rand();
int n;
n = 100L * rand()/(RAND_MAX+1) + 1;
return n;
}
/*********************************************************/
/* calander.cpp - VC6.0 */
/* Print out a calendar. */
/* J Luo, 02-Jun-2001 */
/*********************************************************/
#include<stdio.h>
int InputYear(int *, int []);
void PutTable(int, int [], char [12][42][3]);
void PrintTable(int *, char [12][42][3]);
void PrintHeader(int);
void PrintCheck(char table[12][42][3]);
void main()
{
int days, year;
int monthN[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char table[12][42][3] = {' '};
days = InputYear(&year, monthN);
PutTable(days, monthN, table);
PrintTable(&year, table);
}
int InputYear(int *y, int monthN[])
{
int days, year;
printf("Which year? ");
scanf("%d", &year);
*y = year;
if ( year%4 == 0 ) monthN[1] = 29;
if ( year%100 == 0 ) monthN[1] = 28;
if ( year%400 == 0 ) monthN[1] = 29;
year = year - 1;
days = 365*year + year/4 - year/100 + year/400 + 1;
return days;
}
void PutTable(int days, int monthN[], char table[12][42][3])
{
int firstDay, lastDay;
int i, j;
int n1, n2, m;
for (i=0; i<12; i++) {
firstDay = days%7-1;
lastDay = firstDay + monthN[i];
for (j=firstDay+1; j<lastDay+1; j++) {
m = j - firstDay;
n1 = m/10;
n2 = m - n1*10;
if ( n1 != 0 ) table[i][j][1] = n1 + 48;
table[i][j][2] = n2 + 48;
}
days = lastDay + 1;
}
// PrintCheck(table);
}
void PrintTable(int *year, char table[12][42][3])
{
int i, j, k, l, m;
int n1, n2;
printf("%33s%4d%s\n", " ***** ", *year, " ***** ");
for (m=0; m<12; m=m+3) {
PrintHeader(m);
n1=0;
for (l=0; l<6; l++) {
n2 = n1 + 7;
for (i=m; i<m+3; i++) {
printf("|");
for (j=n1; j<n2; j++) {
for (k=0; k<3; k++) {
printf("%c", table[i][j][k]);
}
}
printf(" ");
}
printf("|\n");
n1 = n2;
}
}
for (j=0; j<70; j++) printf("%c", '-');
printf("\n");
}
void PrintHeader(int i)
{
char *day = {" S M T W T F S "};
char monthC[12][4] =
{
"Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"
};
int j;
for (j=0; j<70; j++) printf("%c", '-');
printf("\n");
for (j=i; j<i+3; j++) printf("| %11s ", monthC[j]);
printf("|\n");
for (j=0; j<70; j++) printf("%c", '-');
printf("\n");
for (j=i; j<i+3; j++) printf("| %s", day);
printf("|\n");
}
void PrintCheck(char table[12][42][3])
{
int i, j, k;
for (i=0; i<12; i++) {
for (j=0; j<42; j++) {
if (j%7==0 ) printf("\n");
for (k=0; k<3; k++) {
printf("%c", table[i][j][k]);
}
}
printf("\n");
}
}
Which year? 2001
***** 2001 *****
----------------------------------------------------------------------
| Jan | Feb | Mar |
----------------------------------------------------------------------
| S M T W T F S | S M T W T F S | S M T W T F S |
| 1 2 3 4 5 6 | 1 2 3 | 1 2 3 |
| 7 8 9 10 11 12 13 | 4 5 6 7 8 9 10 | 4 5 6 7 8 9 10 |
| 14 15 16 17 18 19 20 | 11 12 13 14 15 16 17 | 11 12 13 14 15 16 17 |
| 21 22 23 24 25 26 27 | 18 19 20 21 22 23 24 | 18 19 20 21 22 23 24 |
| 28 29 30 31 | 25 26 27 28 | 25 26 27 28 29 30 31 |
| | | |
----------------------------------------------------------------------
| Apr | May | Jun |
----------------------------------------------------------------------
| S M T W T F S | S M T W T F S | S M T W T F S |
| 1 2 3 4 5 6 7 | 1 2 3 4 5 | 1 2 |
| 8 9 10 11 12 13 14 | 6 7 8 9 10 11 12 | 3 4 5 6 7 8 9 |
| 15 16 17 18 19 20 21 | 13 14 15 16 17 18 19 | 10 11 12 13 14 15 16 |
| 22 23 24 25 26 27 28 | 20 21 22 23 24 25 26 | 17 18 19 20 21 22 23 |
| 29 30 | 27 28 29 30 31 | 24 25 26 27 28 29 30 |
| | | |
----------------------------------------------------------------------
| Jul | Aug | Sep |
----------------------------------------------------------------------
| S M T W T F S | S M T W T F S | S M T W T F S |
| 1 2 3 4 5 6 7 | 1 2 3 4 | 1 |
| 8 9 10 11 12 13 14 | 5 6 7 8 9 10 11 | 2 3 4 5 6 7 8 |
| 15 16 17 18 19 20 21 | 12 13 14 15 16 17 18 | 9 10 11 12 13 14 15 |
| 22 23 24 25 26 27 28 | 19 20 21 22 23 24 25 | 16 17 18 19 20 21 22 |
| 29 30 31 | 26 27 28 29 30 31 | 23 24 25 26 27 28 29 |
| | | 30 |
----------------------------------------------------------------------
| Oct | Nov | Dec |
----------------------------------------------------------------------
| S M T W T F S | S M T W T F S | S M T W T F S |
| 1 2 3 4 5 6 | 1 2 3 | 1 |
| 7 8 9 10 11 12 13 | 4 5 6 7 8 9 10 | 2 3 4 5 6 7 8 |
| 14 15 16 17 18 19 20 | 11 12 13 14 15 16 17 | 9 10 11 12 13 14 15 |
| 21 22 23 24 25 26 27 | 18 19 20 21 22 23 24 | 16 17 18 19 20 21 22 |
| 28 29 30 31 | 25 26 27 28 29 30 | 23 24 25 26 27 28 29 |
| | | 30 31 |
----------------------------------------------------------------------
Common Greatest Factor - simple approach
// cgd.cpp - C++ program, VC5.0
// Find the common greatest divider
// P. 95, Chapter 3, No. 4-3
// Simple approach
// J Luo, 14-Sep-2000
#include<iostream.h>
void main()
{
int a, b, t;
cout << "Enter two integers: ";
cin >> a >> b;
if ( a > b ) {
t = a;
a = b;
b = t;
}
for ( int i=a; i>=2; i--) {
if ( a%i == 0 ) {
if (b%i == 0 ) {
cout << i << endl;
break;
}
}
}
}
Simple recursive function
/*********************************************************/
/* digits1.cpp - C++ program */
/* Digits counting */
/* Simple recursive function */
/* J LUO, 12/10/1999, Suzhou */
/* Modified */
/* J Luo, 03-Jun-2001 */
/*********************************************************/
#include <iostream.h>
int digit(int);
void main()
{
int n = 12345;
cout << n << endl;
digit(n);
}
int digit(int n)
{
int m;
m = n % 10;
n = n / 10;
if ( n ) digit(n);
cout << " M = " << m << "\t";
cout << " N = " << n << "\n";
return m ;
}
DotPlot
// dotplot1b.cpp - VC 5.0 program
// dot matrix plot, different length of the two sequence
// J Luo, 07-Nov-2000
#include<iostream.h>
#include<string.h>
void main()
{
const int n1= 17; // error if n1=16
const int n2= 13; // error if n2=12
char seq1[n1];
char seq2[n2];
strcpy(seq1,"ACGTACGTACGTACGT");
strcpy(seq2,"ACGTACGTACGT");
cout << ' ';
for (int i1=0; i1<n1-1; i1++) cout << seq1[i1]; // X axis for seq1
cout << endl;
for (int i2=0; i2<n2-1; i2++) // Y axis for seq2
{
cout << seq2[i2];
for (i1=0; i1<n1-1; i1++)
{
if ( seq2[i2] == seq1[i1] )
{
cout << 'x';
} else {
cout << ' ';
}
}
cout << endl;
}
}
// dotplot2.cpp - VC 5 program
// dot matrix plot
// J Luo, Shan Guan, 01-Nov-2000
// J Luo, Modified for different sequence size, 7-Nov-2000
#include<iostream.h>
#include<string.h>
void plot(char *, int, char *, int);
void main()
{
int n1= 16;
int n2= 12;
char *seq1;
char *seq2;
seq1 =
"ACGTACGTACGTACGT";
seq2 =
"ACGtACGtACGt";
plot(seq1,
n1, seq2, n2);
}
void plot(char *seq1, int n1, char *seq2, int n2)
{
cout
<< ' ';
for (int x=0;
x<n1; x++) cout << seq1[x];
cout << endl;
for (int y=0; y<n2; y++)
{
cout
<< seq2[y];
for
(x=0; x<n1; x++)
{
if
( seq2[y] == seq1[x] )
{
cout << 'x';
}
else {
cout << ' ';
}
}
cout
<< endl;
}
}
// dotplot3.cpp - VC 5.0 program
// dot matrix plot
// Dynamic array
// J Luo, Shan Guan, 01-Nov-2000
// Bug fixed by Fang Gang, 08-Nov-2000
#include<iostream.h>
#include<string.h>
void PutDot(char *, int, char *, int, char *&);
void Plot(char *, int, char *, int, char *&);
void main()
{
int n1= 16;
int n2= 12;
char *seq1;
char *seq2;
char *dot;
dot = new char[n1*n2];
seq1 = "ACGTACGTACGTACGT";
seq2 = "ACGTACGTACGT";
PutDot(seq1, n1, seq2, n2, dot);
Plot(seq1, n1, seq2, n2, dot);
}