标题:这个程序的伪代码算法怎么写啊???急啊
取消只看楼主
亲爱的不2
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-4-19
结帖率:0
已结贴  问题点数:20 回复次数:0 
这个程序的伪代码算法怎么写啊???急啊
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<string>
using namespace std;
const int N=1000;

void shoot ( bool& targetAlive, double accuracy );
int startDuel(char n);

int main()
{  
    char yes_no;
    ofstream outfile;
    do
    {
        outfile.open("outfile.txt",ios::app);
        if(outfile.fail())
        {
            cout<<"File opening failed!\n";
            exit(1);                                                /*打开文件不成功则退出*/
        }
        int A=0,B=0,C=0,k,winner;
        char n;
        double player1,player2,player3;
        cout<<"Please choose which means to swap!\n"
            <<"* * * * * * * * * * * * * * * * * * * * * * * * \n"
            <<"* * * *          请输入正整数n          * * * * \n"
            <<"* * * *  n=1表示Aaron第一次没有故意失手 * * * * \n"
            <<"* * * *  n=2表示Aaron第一次故意失手     * * * * \n"
            <<"* * * * * * * * * * * * * * * * * * * * * * * * \n";
        cin>>n;
        while(n!='1'&&n!='2')
        {
            cout<<"输入错误,请重新输入:\n";
            cin>>n;
        }
        outfile<<"第"<<n<<"种游戏方法\n";                                            /*把n的值记录入文件*/
        for(k=0;k<=N;k++)
        {
            string win;
            winner=startDuel(n);
            if(winner==1) A++;                                        /*用A,B,C分别记录三个人赢的次数*/
            if(winner==2) B++;
            if(winner==3) C++;
            if(winner==1)
                win="Aaron";
            if(winner==2)
                win="Bob";
            if(winner==3)
                win="Charlie";
            outfile<<"第"<<k<<"游戏的赢家是:"<<win;               /*把每次的游戏结果记录入outfile指定的文件当中*/
            outfile<<"\n";
        }
        player1=(double)A/N;
        player2=(double)B/N;
        player3=(double)C/N;
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(3);
        cout<<" Aaron获胜概率是\n";
        cout<<player1<<endl;                                                        /*打印player1赢的概率*/
        cout<<"Bob获胜概率是\n";                                                   
        cout<<player2<<endl;                                                        /*打印player2赢的概率*/
        cout<<"Charlie获胜概率是\n";
        cout<<player3<<endl;                                                        /*打印player3赢的概率*/
        outfile<<"\n";                                                                                                  
        outfile<<"Aaron获胜概率是:"<<player1;
        outfile<<"\n";
        outfile<<"Bob获胜概率是:"<<player2;
        outfile<<"\n";
        outfile<<"Charlie获胜概率是:"<<player3;
        outfile<<"\n";
        outfile<<"Aaron胜利的次数:"<<A<<endl;
        outfile<<"Bob胜利的次数:"<<B<<endl;
        outfile<<"Charlie胜利的次数:"<<C<<endl;
        cout<<"你还想要继续决斗吗?(Y  or  N):\n";    /*提示用户尝试另一种游戏方法*/
        cin>>yes_no;
        while(yes_no!='y' && yes_no!='Y' && yes_no!='n' && yes_no!='N')
        {
            cout<<"字符输入有误,请重新输入!\n";
            cin>>yes_no;
        }
        outfile.close();
    }
    while(yes_no=='Y'||yes_no=='y');
    return 0;
}

void shoot ( bool& targetAlive, double accuracy )/*此函数模拟一次射击过程*/
{
    double shooting;
    shooting=rand()%101/100.0;                  /*产生0到1的随机数*/
    if(shooting<accuracy)
        targetAlive=false;                      /*随机数小于概率,该人死亡*/
    else
        targetAlive=true;
}

int startDuel(char n)                            /*此函数模拟一次完整的游戏过程*/
{
    int winner,player=3;
    bool Aaron=true,Bob=true,Charlie=true;         /*赋初值为true,表示每个人初始阶段都是活着*/
    if(n=='1')                                     /*A第一次不故意失手*/
    {
        shoot(Charlie,0.33);                    /*A先打C*/
        if(Charlie==false)                        /*若C被A打死,则进入A与B互打的循环,直到A与B之间只剩一人*/     
        {   
            player--;
            while(player>1)
            {
                if(Bob&&Aaron)
                    shoot(Aaron,0.5);
                      if(Aaron==false)   
                          player--;
                      else
                         {
                             if(Bob&&Aaron)
                                shoot(Bob,0.33);
                                 if(Bob==false)
                                  player--;
                        }
            }
         }
        else                                   /*若C活着,则进行下列过程*/
         {
           shoot(Charlie,0.5);                        /*B打C*/
           if(Charlie)                           /*若C活着,则进行下列过程*/
            {  
              shoot(Bob,1.0);
              player--;                           /*C打B,B肯定死,人数减一*/
                 shoot(Charlie,0.33);                    /*轮到A打C*/
                 if(Charlie==false)   
                player--;                       /*若C死,人数减一*/
                else
                 {
                 shoot(Aaron,1.0);               /*若C活着,则C打A,A肯定死,人数减一*/
                  player--;
              }
          }
         else                                   /*若C被B打死,则进入A与B互打的循环,直到A与B之间只剩一人*/
           {
               while(player>1)
                {
                    if(Aaron&&Bob)
                        shoot(Bob,0.33);
                        if(Bob==false)  
                            player--;
                        else
                         {
                             if(Aaron&&Bob)
                                shoot(Aaron,0.5);
                                if(Aaron==false)
                                 player--;
                         }
                }
            }
        }
         if(Aaron)   
            winner=1;                           
        if(Bob)     
            winner=2;
        if(Charlie)
            winner=3;
        return winner;
    }
    if(n=='2')                                      /*A第一次故意失手*/
    {
           shoot(Charlie,0.5);                            /*B打C*/
         if(Charlie)                                /*若C活着,则进行下列过程*/
            {  
              shoot(Bob,1.0);
              player--;                                /*C打B,B肯定死,人数减一*/
              shoot(Charlie,0.33);                    /*轮到A打C*/
              if(Charlie==false)   
                player--;                            /*若C死,人数减一*/
              else
                {
                  shoot(Aaron,1.0);                    /*若C活着,则C打A,A肯定死,人数减一*/
                  player--;
                }
         }
         else                                        /*若C被B打死,则进入A与B互打的循环,直到A与B之间只剩一人*/
           {
               while(player>1)
                {
                    if(Aaron&&Bob)
                        shoot(Bob,0.33);
                        if(Bob==false)  
                            player--;
                        else
                         {
                             if(Aaron&&Bob)
                                shoot(Aaron,0.5);
                                if(Aaron==false)
                                 player--;
                         }
                }
            }

        if(Aaron)   
            winner=1;                           
        if(Bob)     
            winner=2;
        if(Charlie)
            winner=3;
        return winner;
    }
 }
搜索更多相关主题的帖子: namespace include double failed 
2013-04-19 16:53



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-404860-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.654534 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved