问题有一些:
- 条理不清楚,没有缩进没有对齐。
- 不会调试。
- 考虑不周到,上下界问题,没有对负数,非正确输入的判断。
注意内容:
- 标准输出的使用,小数的精确度和对齐问题,不熟练
- 对整数的数字分解算法
- 对已学内容的综合使用
下面是部分需要提一下的程序题目,供参考。(下面的一些输出内容因为blog的字体问题而不对齐,在终端上是不存在这样问题的。)

6.12.cpp

/**//****************************
homework 6.12
Author : Ray
Question: Calculate Charges
Data : 2008.10.08
*****************************/

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::fixed;

#include <iomanip>
using std::setw;
using std::setprecision;

double calculateCharges(double hours)


{
if(hours<=0)

{
return 0;
}else if(hours<=3)

{
return 2;
}else if(hours<24)

{
double money;
money=2+0.5*(hours-3);
if(money>10)

{
return 10;
}else

{
return money;
}
}
}

#define CARNUMBER 3

void main()


{
double hours[CARNUMBER];
cout<<"please input the used hours of "<<CARNUMBER<<" cars:";
int i=0;
for(i=0;i<CARNUMBER;i++)

{
cin>>hours[i];
}
double totalHours=0,totalMoney=0,money;
cout<<"Car\t"<<setw(5)<<"Hours\t"<<setw(6)<<"Charge"<<endl;
for(i=0;i<CARNUMBER;i++)

{
money=calculateCharges(hours[i]);
totalHours+=hours[i];
totalMoney+=money;
cout<<i<<"\t"
<<setw(5)<<fixed<<setprecision(2)<<hours[i]<<"\t"
<<setw(5)<<fixed<<setprecision(2)<<money<<endl;
}
cout<<"Total\t"<<setw(5)<<totalHours<<"\t"<<setw(5)<<totalMoney<<endl;
}


/**//*************************************************
Results:
please input the used hours of 3 cars:2 4.5 22.3
Car Hours Charge
0 2.00 2.00
1 4.50 2.75
2 22.30 10.00
Total 28.80 14.75

Attentions:
1.如何处理处理对齐
2.如何处理小数精确度
3.对0和负数的判断
*************************************************/


6.29.cpp

/**//****************************
homework 6.29
Author : Ray
Question: Perfect Numbers
Data : 2008.10.08
*****************************/
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

bool perfect(int number)


{
int sum=0;
for(int i=1;i<number;i++)

{
if(0==number%i)

{
sum+=i;
}
}
if(sum==number)

{
return true;
}else

{
return false;
}
}

#define MAXNUMBER 10000

void main()


{
cout<<"Perfect Numbers: ";
for(int i=2;i<=MAXNUMBER;i++)

{
if(perfect(i))

{
cout<<i<<"\t";
}
}
cout<<endl;
}


/**//*************************************************
Results:
Perfect Numbers: 6 28 496 8128

Attentions:
None

*************************************************/


6.31.cpp

/**//****************************
homework 6.31
Author : Ray
Question: Reverse Digits
Data : 2008.10.08
*****************************/
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int reverse(int number)


{
int div = number/10;//前面n-1位
int digit = number%10;//最后一位
int ret = 0;//返回值
while(0!=div)

{
ret=ret*10+digit;//向最后插入一位数字
digit=div%10;
div=div/10;
}
ret=ret*10+digit;
return ret;
}

void main()


{
int input;
cout<<"Input an integer(0<n<=65535)\t:";
cin>>input;
cout<<"Reverse number\t\t\t:"<<reverse(input)<<endl;
}


/**//*************************************************
Results:
Input an integer(0<n<=65535) :23451
Reverse number :15432

Attentions:
1.注意一下reverse算法,很多人使用10的幂函数和数组来存储,其实是没有必要的

*************************************************/


6.42.cpp

/**//****************************
homework 6.42
Author : Ray
Question: Towers of Hanoi
Data : 2008.10.08
*****************************/
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

void move(int oldPeg,int newPeg)


{
cout<<oldPeg<<"\t--->\t"<<newPeg<<endl;
}

void move(int number,int oldPeg,int newPeg,int tmpPeg)


{
if(1==number)//directly move

{
print(oldPeg,newPeg);
}else

{
move(number-1,oldPeg,tmpPeg,newPeg);
move(1,oldPeg,newPeg,tmpPeg);
move(number-1,tmpPeg,newPeg,oldPeg);
}
}

void main()


{
int num;
cout<<"Input the numbers of the disks:";
cin>>num;
move(num,1,3,2);
}


/**//*************************************************
Results:
Input the numbers of the disks:3
1 ---> 3
1 ---> 2
3 ---> 2
1 ---> 3
2 ---> 1
2 ---> 3
1 ---> 3

Attentions:
1.了解递归的用法即可

*************************************************/


4.42.new.cpp

/**//****************************
homework 6.42.new
Author : Ray
Question: Towers of Hanoi
Data : 2008.10.08
Image display !
*****************************/
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::left;
using std::right;

#include <string>
using std::string;

#define MAXDISKS 64

int num;
//用户输入盘子数目

int table[3][MAXDISKS];
//数组,三根柱子,每个单元表示有盘子号,-1表示没有


/**//*
组织成星星字符串
int n: 星星的半数
int mid: 星星的中间位置
*/
string getStar(int n,int mid)


{
string rtn("");
int i;
for(i=0;i<mid-n;i++)

{
rtn+=" ";
}
for(;i<mid+n;i++)

{
rtn+="*";
}
for(;i<mid*2;i++)

{
rtn+=" ";
}
return rtn;
}

//将数组里面的图画出来
void draw()


{
int i;
int j;
for(i=num;i>=0;i--)

{
for(j=0;j<3;j++)

{
cout<<getStar(table[j][i]+1,num+2);
}
cout<<endl;
}
cout<<"------------------------------------------------"<<endl;
}

//Reconstruction.
void move(int oldPeg,int newPeg)


{
int i=num;
int j=num;

//找到最上层
while(table[oldPeg-1][i]==-1 && i>0)

{
i--;
}
while(table[newPeg-1][j]==-1 && j>=0)

{
j--;
}
//搬过来
table[newPeg-1][j+1]=table[oldPeg-1][i];
table[oldPeg-1][i]=-1;
cout<<" "<<oldPeg<<"\t--->\t"<<newPeg<<endl;
draw();
}

void move(int number,int oldPeg,int newPeg,int tmpPeg)


{
if(1==number)//directly move

{
move(oldPeg,newPeg);
}else

{
move(number-1,oldPeg,tmpPeg,newPeg);
move(1,oldPeg,newPeg,tmpPeg);
move(number-1,tmpPeg,newPeg,oldPeg);
}
}


void main()


{
cout<<"Input the numbers of the disks(<=64):";
cin>>num;
//初始化
int i,j;
for(i=0;i<3;i++)

{
for(j=0;j<MAXDISKS;j++)

{
table[i][j]=-1;
}
}
//0号盘放在上面
i=0;
for(j=num-1;j>=0;j--)

{
table[0][j]=i;
i++;
}
draw();
move(num,1,3,2);
}


/**//*************************************************
Results:
Input the numbers of the disks(<=64):3

**
****
******
------------------------------------------------
1 ---> 3


****
****** **
------------------------------------------------
1 ---> 2



****** **** **
------------------------------------------------
3 ---> 2


**
****** ****
------------------------------------------------
1 ---> 3


**
**** ******
------------------------------------------------
2 ---> 1



** **** ******
------------------------------------------------
2 ---> 3


****
** ******
------------------------------------------------
1 ---> 3

**
****
******
------------------------------------------------

Attentions:
1.画图比较麻烦,可以了解一下
2.数据的结构有很多种方式,但是简单的数组会导致复杂的画图,所以可以权衡一下

*************************************************/


6.47.cpp

/**//****************************
homework 6.47
Author : Ray
Question: a computer-assisted instruction program
Data : 2008.10.08
*****************************/

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::fixed;

#include <string>
using std::string;

#include <iomanip>
using std::setprecision;

#include <stdlib.h>
#include <time.h>

#define QNUM 10//题目数量

int level=0,type=0,count=0;;

void displayLevel()


{
cout<<"**************************************\n";
cout<<" WELCOME TO \n";
cout<<" Computer Assisted Instruction \n";
cout<<"**************************************\n";
cout<<endl;
cout<<".SELECT A LEVEL\n";
cout<<" 1 - level 1, one-digit integers\n";
cout<<" 2 - level 2, two-digit integers\n";

cin>>level;
while(level<1 || level>2)

{
cout<<"Wrong level! Try again
"<<endl;
cin>>level;
}
}

void displayType()


{
cout<<"\n.SELECT A TYPE\n";
cout<<" 1 - Addition\n";
cout<<" 2 - Subtraction\n";
cout<<" 3 - Multiplication\n";
cout<<" 4 - Division\n";
cout<<" 5 - All types\n";
cin>>type;
while(type<1||type>5)

{
cout<<"Wrong level! Try again
"<<endl;
cin>>type;
}
}


string rspA[]=
{"Very good!"
,"Excellent!"
,"Nice work!"
,"Keep up the good work!"};


string rspB[]=
{"No. Please try again."
,"Wrong. Try once more."
,"Don't give up!"
,"No. Keep trying."};

enum Response
{GOOD,BAD};

void responses(Response r)


{
int i = rand()%4;
switch(r)

{
case GOOD:
cout<<rspA[i]<<endl;
count++;
break;
case BAD:
cout<<rspB[i]<<endl;
break;
default:
break;
};
}

void displayResults()


{
double ratio = count/(double)QNUM;
cout<<"\n---------------------------------------\n";
cout<<".RESULTS \n";
cout<<"Questions\t: "<<QNUM<<endl;
cout<<"Correct \t: "<<count<<endl;
cout<<"Ratio \t: "<<setprecision(4)<<ratio*100<<"%\n";
cout<<"---------------------------------------\n\n";
if(ratio<0.75)

{
cout<<"Please ask your instructor for extra help!\n";
}else

{
cout<<"You pass the Exam !\n";
}
cout<<"\n**************************************\n";
cout<<" GOOD BYE \n";
cout<<"**************************************\n";
}

int createNumber()


{
switch(level)

{
case 1:
return 1+rand()%9;
default:
return 1+rand()%99;
}
}

void add()


{
int r;
int n=createNumber();
int m=createNumber();
cout<<m<<"+"<<n<<"=";
cin>>r;
if(r==m+n)

{
responses(GOOD);
}else

{
responses(BAD);
}
}

void sub()


{
int r;
int n=createNumber();
int m=createNumber();
if(m<n)

{
r=n;
n=m;
m=r;
}
cout<<m<<"-"<<n<<"=";
cin>>r;
if(r==m-n)

{
responses(GOOD);
}else

{
responses(BAD);
}
}

void mul()


{
int r;
int n=createNumber();
int m=createNumber();
cout<<m<<"x"<<n<<"=";
cin>>r;
if(r==m*n)

{
responses(GOOD);
}else

{
responses(BAD);
}
}

void div()


{
int r;
int n=createNumber();
int m=createNumber();
r=n*m;
cout<<r<<"/"<<m<<"=";
cin>>m;
if(m==n)

{
responses(GOOD);
}else

{
responses(BAD);
}
}

void question(int i)


{
cout<<"\n("<<i+1<<")\t";
int t=type;
if(type==5)

{
t=1+rand()%5;
}
switch(t)

{
case 1:add();break;
case 2:sub();break;
case 3:mul();break;
default:div();
}
}

void main()


{
displayLevel();
displayType();
srand(time(0));
cout<<"\n.OK. Let’s begin
\n";
cout<<"---------------------------------------\n";
int i;
for(i=0;i<QNUM;i++)

{
question(i);
}

displayResults();
}


/**//*************************************************
Results:
**************************************
WELCOME TO
Computer Assisted Instruction
**************************************

.SELECT A LEVEL
1 - level 1, one-digit integers
2 - level 2, two-digit integers
1

.SELECT A TYPE
1 - Addition
2 - Subtraction
3 - Multiplication
4 - Division
5 - All types
5

.OK. Lets Begin 
---------------------------------------

(1) 16/8=2
Very good!

(2) 1/1=1
Nice work!

(3) 6+6=12
Very good!

(4) 35/5=7
Excellent!

(5) 14/7=2
Nice work!

(6) 12/2=6
Nice work!

(7) 5-2=3
Excellent!

(8) 8-2=6
Nice work!

(9) 7+9=16
Excellent!

(10) 4-4=0
Very good!

---------------------------------------
.RESULTS
Questions : 10
Correct : 10
Ratio : 100%
---------------------------------------

You pass the Exam !

**************************************
GOOD BYE
**************************************

Attentions:
1.减法注意两个数的大小,除法应该要能整除
2.内容较多,注意函数的应用,条理清楚
*************************************************/


宾果游戏

/**//****************************
homework
Author : Ray
Question: Bingo
Data : 2008.10.08
*****************************/

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <stdlib.h>
#include <time.h>

void main()


{
int number[4];
srand(time(0));

//create 4 numbers
int i=1;
number[0]=1+rand()%9;
while(i<4)

{
number[i]=0+rand()%10;
bool isSame = false;
for(int j=0;j<i;j++)

{
if(number[i]==number[j])

{
isSame=true;
break;
}
}
if(!isSame)

{
i++;
}
}
cout<<number[0]<<number[1]<<number[2]<<number[3]<<endl;

cout<<"**********************"<<endl;
cout<<" Bingo Game"<<endl;
cout<<"**********************"<<endl;

//guess the number
int input,guess[4],t,j;
for(t=0;t<16;t++)

{
cout<<"Input a number(4 digits):";
cin>>input;
while(input<1000 || input>9999)

{
cout<<"Wrong number. Try again."<<endl;
cout<<"Input a number(4 digits):";
cin>>input;
}
//分解
j=2;
int div = input/10;
guess[3] = input%10;
while(div!=0)

{
guess[j]=div%10;
div/=10;
j--;
}
//比较
int a=0,b=0;
for(i=0;i<4;i++)

{
if(guess[i]==number[i])

{
a++;
}else

{
for(j=0;j<4;j++)

{
if(guess[i]==number[j])

{
b++;
}
}
}
}
if(a==4)

{
cout<<"You Win !"<<endl;
return;
}else if(t<16)

{
cout<<a<<"A"<<b<<"B"<<endl;
cout<<16-t-1 <<" times left. Try again
\n";
}
}
cout<<"You Lost !"<<endl;
}


/**//*************************************************
Results:
7865
**********************
Bingo Game
**********************
Input a number(4 digits):1234
0A0B
15 times left. Try again 
Input a number(4 digits):5678
0A4B
14 times left. Try again 
Input a number(4 digits):6587
0A4B
13 times left. Try again 
Input a number(4 digits):7856
2A2B
12 times left. Try again 
Input a number(4 digits):8756
0A4B
11 times left. Try again 
Input a number(4 digits):7865
You Win !
Press any key to continue

Attentions:
1.主要是随机数和循环结构的应用
2.没有使用函数,因为需要使用指针
3.数字的分解与判断
*************************************************/
