发信人: shutiao(薯条|拯救水源), 信区: C
标 题: 请大家我看看我写的计算21点手中点数的小程序
发信站: 饮水思源 (2014年06月08日10:23:07 星期天)

正在学习C++,找了道题目计算手上卡片的点数和,我写一个C++,已经编译通过,而且验证
合格了。我想请大家看看我代码风格有没有什么问题,我用Emacs编辑器写的,也是初体验
。请问哪里有什么网站可以交流代码的,我想看看别人写这个程序的代码,或是请别人看
看我写的这个代码。

http://bbs.sjtu.edu.cn/file/C/1402194011181340.cpp

// File name: blackjack
// Created by: Haihua Xu
// Created on: 6/7/2014
/*
The program scores a blackjack hand.
*/

#include<cstdlib>
#include<iostream>
using namespace std;

int main()
{

int num_cards(0); // record the number of cards: 2 to 5
char value(2); // the value of the card stored in the data-type of char
char again('n'); //if play again
int score(0); //total score
int num_ace(0); // number of aces
bool busted(false); // if it is over 21
do
{
cout << "How many cards do you have ( 2, 3, 4 or 5): ";
cin >> num_cards;

score = 0;
busted = false;
num_ace = 0;

for (int i = 1; i<= num_cards ; i++)
{
cout << "There is " << num_cards - i + 1 << " cards left to enter." << endl;
cout << "Enter a card (2-9, t, j, q , k or a ): ";
cin >> value;
if ( value <= '9' && value >= '2' && !busted )
{
score += value - 48;
}
else if ( value == 't' || value == 'T' || value == 'j' || value == 'J' || val
ue == 'k' || value == 'K')
{
score += 10;
}
else if ( value == 'a' || value == 'A' )
{
num_ace ++;
score += 1;
}
else
{
cerr << "invalid input";
exit(10);
}

cout << "the updated score is " << score << "." << endl;

if ( score > 21 )
{
busted = true;
cout << "The score is " << score << " busted!" << endl;
}
}

// decide if ace can be re-accounted as 11 in the final score.
if ( num_ace )
{
if ( score + 10 <= 21)
{
score = score + 10;
cout << " A Ace has been accounted as 11 instead of 1. " << endl;
}
}

cout << "the final score is " << score << "." << endl;
cout << "Do you wish to play again: ";
cin >> again;
cout << endl;
}
while ( again == 'y' || again == 'Y' );
cout << endl;

return (0);
}

--
We all want a code to live by.

※ 来源:·饮水思源 bbs.sjtu.edu.cn·[FROM: 71.79.226.217]