Finish what you start.

a page of Cai

dephi caitoolkit.pas

unit caitoolkit; interface // utilities related to char, string function is_letter(c:Char):Boolean; function is_number(c:Char):Boolean; procedure do_statistics(s:string); procedure do_statistics_v2(s:string); procedure PutChar(n:Integer;c:Char); // end: utilities related to char, string implementation function is_letter(c:Char):Boolean; begin if ((c>='A') and (c<='Z')) or ((c>='a') and (c<='z')) then Result := True else Result := False; end; function is_number(c:Char):Boolean; begin if ((c>='0') and (c<='9')) then Result := True else Result := False; end; procedure do_statistics(s:string); var cnt_letter:Integer; cnt_number:Integer; cnt_others:Integer; i,j:Integer; begin cnt_letter := 0; cnt_number := 0; cnt_others := 0; for i:=1 to Length(s) do begin if is_letter(s[i]) then Inc(cnt_letter) else if is_number(s[i]) then Inc(cnt_number) else Inc(cnt_others) end; Writeln('计数:',Length(s)); Writeln('字母:',cnt_letter); Writeln('数字:',cnt_number); Writeln('其他:',cnt_others); end; procedure do_statistics_v2(s:string); var cnt_letter:Integer; cnt_number:Integer; cnt_others:Integer; i:Integer; begin cnt_letter:=0; cnt_number:=0; cnt_others:=0; for i:=1 to Length(s) do begin case s[i] of 'a'..'z','A'..'Z':Inc(cnt_letter); '0'..'9':Inc(cnt_number); else Inc(cnt_others); end; end; Writeln('计数:',Length(s)); Writeln('字母:',cnt_letter); Writeln('数字:',cnt_number); Writeln('其他:',cnt_others); end; procedure PutChar(n:Integer;c:Char); var i:Integer; begin for i:=1 to n do write(c); end; end.

C++:函数指针

STL: map 学习笔记

读:HP大中华区总裁孙振耀退休感言

Joseph Problem

实现一个简单的stack: CharStack

游戏开发,上路了

Koch Fractal

 

Programming Abstractions in C++ 》第六章的一段程序,下面是效果:

 

#include <iostream>
#include <cmath>
#include "simpio.h"
#include "graphics.h"
#include "genlib.h"

const double PI = 3.1415926535;

void KochFractal(double size,int order);
void DrawFractalLine(double len,double theta,int order);
void DrawPolarLine(double r,double theta);

int main ()
{
	InitGraphics();
	cout<<"Program to draw Koch fractals"<<endl;
	cout<<"Enter edge length in inches: ";
	double size = GetReal();
	cout<<"Enter order of fractal: ";
	int order = GetInteger();
	KochFractal(size,order);


	/*
	MovePen(0.5,0.5);
	DrawLine(1,0);
	DrawLine(0,1);
	DrawLine(-1,0);
	DrawLine(0,-1);
	*/
	//MovePen(2,2);
	//DrawLine(0.5,0.5);
	//DrawArc(1,0,45);

	return 0;
}

void KochFractal(double size,int order)
{
	double x0 = GetWindowWidth()/2-size/2;
	double y0 = GetWindowHeight()/2-sqrt(3.0)*size/6;
	MovePen(x0,y0);
	DrawFractalLine(size,0,order);
	DrawFractalLine(size,120,order);
	DrawFractalLine(size,240,order);
}
void DrawFractalLine(double len,double theta,int order)
{
	if(order == 0)
	{
		DrawPolarLine(len,theta);
	}
	else
	{
		DrawFractalLine(len/3,theta,order-1);
		DrawFractalLine(len/3,theta-60,order-1);
		DrawFractalLine(len/3,theta+60,order-1);
		DrawFractalLine(len/3,theta,order-1);
	}
}
void DrawPolarLine(double r,double theta)
{
	double radians = theta/180*PI;
	DrawLine(r*cos(radians),r*sin(radians));
}

数组

数组的大小可以这样求

 

#include <iostream>
#include "genlib.h"
#include "simpio.h"

int main ()
{
	int A[]={1,2,3,4,5,};
	int n = sizeof A / sizeof A[0];
	cout<<"n = "<<n<<endl;

	return 0;
}

 

例程

/*
 * Project: Gymjudge
 * Created by CS106 C++ Assignment Wizard 0.1
 *
 * Name: [TODO: enter name here]
 * Section: [TODO: enter section leader here]
 * [TODO: Describe assignment]
 */
#include <iostream>
#include "genlib.h"
#include "simpio.h"

const int MAX_JUDGES = 100;
const double MAX_SCORE = 10.0;
const double MIN_SCORE = 0.0;

void ReadAllScores(double scores[],int nJudges);
double GetScore(int nJudges);
double Mean(double array[],int n);




int main ()
{
	double scores[MAX_JUDGES];
	cout<<"Enter number of judges: ";
	int nJudges = GetInteger();
	if(nJudges>MAX_JUDGES)Error("Too many judges.");
	ReadAllScores(scores,nJudges);
	cout<<"The average score is "<<Mean(scores,nJudges)<<endl;

	return 0;
}

void ReadAllScores(double scores[],int nJudges)
{
	for(int i = 0;i<nJudges;i++)
	{
		scores[i] = GetScore(i+1);
	}
}
double GetScore(int judge)
{
	while(true)
	{
		cout<<"Score for judge #"<<judge<<": ";
		double score = GetInteger();
		if(score>=MIN_SCORE && score<=MAX_SCORE)return score;
		cout<<"That score is out of range. Try again."<<endl;
	}
}

double Mean(double array[],int n)
{
	double sum = 0.0;
	for(int i = 0;i<n;i++)
	{
		sum += array[i];
	}
	return sum/n;
} 

 

enum usage

枚举容许你定义自己的类型,他不容易出错,而且在代码中,枚举变量能自我说明。

模板:

        enum nameT { element-list };

例子:

        enum colorT { Blue, Green, Red, Yellow };

        enum directionT { North, East, South, West }; 

 

 

 枚举变量在机器内部是是当作integer来保存的,默认情况下,从0开始分配。所以:North = 0, East = 1, South = 2, and West = 3。整数可以由程序员自己分配:

        enum coinT {

                Penny = 1,

                Nickel = 5,

                Dime = 10,

                Quarter = 25,

                HalfDollar = 50 

        };

 例子程序:


#include <iostream>
#include "genlib.h"

enum directionT{
	North,
	East,
	South,
	West
};


string DirectionName(directionT dir);
/*transfer a directionT to a corresponding string
*Usage:string strdir=DirectionName(North);
*Result: strdir="North";	
*/
directionT StringTodirectionT(string strdir);
/*transfer a string to a corresponding directionT
*Usage:directionT dir=StringTodirectionT("West");
*Result: dir=West;
*/
int main ()
{
	string strdir;
	directionT dir;
	while(true)
	{
		cout<<"Next direction?";
		cin>>strdir;
		if("quit"==strdir)
			break;
		dir=StringTodirectionT(strdir);
		cout<<DirectionName(dir)<<endl;
	}

	return 0;
}


/*transfer a directionT to a corresponding string
*Usage:string strdir=DirectionName(North);
*Result: strdir="North";	
*/
string DirectionName(directionT dir)
{
	switch(dir)
	{
	case North:
		return "North";
	case East:
		return "East";
	case South:
		return "South";
	case West:
		return "West";
	default:
		return "This is not a legal variable of directionT!";
	}
}

/*transfer a string to a corresponding directionT
*Usage:directionT dir=StringTodirectionT("West");
*Result: dir=West;
*/
directionT StringTodirectionT(string strdir)
{
	if("North"==strdir){return North;}
	if("East"==strdir){return East;}
	if("South"==strdir){return South;}
	if("West"==strdir){return West;}
}