Zclub討論區 Z板

 找回密码
 註冊
搜索
楼主: 萬能版zclub

救命啊~~~C++

[复制链接]
发表于 2004-6-23 21:29:35 | 显示全部楼层
回覆給:特洛瓦
那麼好唷!!~~~~~我都一直在修正程式呢>.<"
不過沒差~至少我有去了解裡面的一些東西^^"
~~~而且好像也不能一樣不然就準備一起被當
呵呵!!
回复

使用道具 举报

发表于 2004-6-24 01:58:00 | 显示全部楼层
回覆給:丫國
妳的程式目前真的僅剩下包牌部分沒寫完!
選號,自動選號,金額計算,四組號碼一張,漂亮美觀的介面跟美觀的輸出方式
不重複選號,號碼限定...............全部寫好囉!
就剩下還沒搞懂包牌的寫法而已!  ^.^
回复

使用道具 举报

发表于 2004-6-24 03:31:36 | 显示全部楼层

怕被人家說~~我給女生~~都不給男生~~所以我把我之前沒PO的PO出來~~
因為我覺得我這次寫的結構亂掉了~~所以不是很想PO~~但為了~~
怕被說重色輕友~~還是PO下去了~~

// Lotto4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

//想要就用~用完就丟@@|||
#define for if(0);else for
//樂透的最大數字
#define max_lotto_number 46
//樂透個數
#define lotto_number 6
//樂透記錄位置
#define lotto_path "C:\\lotto.txt"
//目錄選項數量
#define max_menu 7

//顯示目錄
void ShowMenu(int KB,int lotto[]);
//抓使用者選擇
int GetKey(int Menu,int lotto[]);

//使用者自行選號
void lotto_1(void);
//包牌包~包~包~
void lotto_2(void);
//開獎開。開。開。
void lotto_3(int lotto[]);
//檢查重複
bool check_lotto_number(int lotto_array[],int i);
//記錄樂透
long write_lotto(char *file_path,int lotto_array[],long file_pos);
//讀取樂透
long read_lotto(char *file_path,int lotto_array[],long file_pos);
//十進制轉二進制(不包括反轉)
int dec_bin(int dec_number,int bin_array[]);
//秀出樂透清單
void show_lotto_list();
//秀出樂透開獎號碼
void show_lotto(int lotto[]);
//泡沫排序
void my_sort(int lotto[],int max_l);
//檢查中獎
void lotto_win(int lotto[]);


int main(int argc, char* argv[])
{
 //開獎的號碼
 int lotto[max_lotto_number]={0};
 int Menu=1;
 do{
  ShowMenu(Menu,lotto);
  Menu=GetKey(Menu,lotto);
  switch(Menu)
  {
  case 1:
   {
    lotto_1();break;
   }
  case 2:
   {
    lotto_2();break;
   }
  case 3:
   {
    lotto_3(lotto);break;
   }
  case 4:
   {
    //清除樂透記錄
    FILE *file;
    file=fopen(lotto_path,"w");
    fclose(file);
    break;
   }
  case 5:
   {
    //秀出目前的樂透清單
    show_lotto_list();
    break;
   }
  case 6:
   {
    lotto_win(lotto);
    break;
   }
  case 7:
   {
    break;
   }
  }
 }while(Menu!=max_menu);
 printf("\n~~掰掰~~\n");
 getch();
 return 0;
}

//使用者自行選號
void lotto_1(void)
{
 int lotto_array[lotto_number];
 printf("自行選牌\n");
 for(int i=0;i<lotto_number;i++)
 {
  scanf("%d",lotto_array+i);
  if(check_lotto_number(lotto_array,i))
  {
   printf("號碼已選過~~\n");
   i--;
  }
 }
 //排序
 my_sort(lotto_array,lotto_number);
 write_lotto(lotto_path,lotto_array,0);
 getch();
}

//檢查重複
bool check_lotto_number(int lotto_array[],int i)
{
 //和現在以前的數字作比對
 //ture表重複,false表沒有重複
 for(int j=0;j<i;j++)
 {
  if(lotto_array[j]==lotto_array[i])
  {
   return true;
  }
 }
 return false;
}

//記錄樂透
long write_lotto(char *file_path,int lotto_array[],long file_pos)
{
 FILE *w_file;
 //開檔
 if((w_file=fopen(file_path,"a"))==NULL)
  printf("開檔失敗,沒有寫入\n\a");
 else
 {
  printf("開檔成功,開始寫入\n");
  fseek(w_file,file_pos,0);
  //開始寫入
  printf("寫入中...");
  for(int i=0;i<lotto_number;i++)
  {
   fprintf(w_file,"%4d",lotto_array[i]);
   printf("...");
  }
  fprintf(w_file,"\n");
  printf("寫入完成\n");
  //關檔
  if(fclose(w_file)==NULL)
   printf("\n關檔成功,已經成功的紀錄完成\n");
  return file_pos;
 }
}

//讀取樂透
long read_lotto(char *file_path,int lotto_array[],long file_pos)
{
 FILE *r_file;
 //開檔
 if((r_file=fopen(file_path,"r"))==NULL)
  printf("開檔失敗,沒有讀取\n\a");
 else
  printf("開檔成功,開始讀取\n");
 fseek(r_file,file_pos,0);
 for(int i=0;(!feof(r_file))&&(i<lotto_number);i++)
 {
  //讀取樂透
  fscanf(r_file,"%4d",lotto_array+i);
 }
 return ftell(r_file);
}

//包牌包~包~包~
void lotto_2(void)
{
 int bin_array[max_lotto_number]={0};
 int all_lotto,lotto_array[max_lotto_number];
 //取得使用者想要包牌的個數
 printf("請輸入欲包牌的數7~46...");
 scanf("%d",&all_lotto);
 //自動縮減大小
 if(all_lotto<(lotto_number+1))
  all_lotto=(lotto_number+1);
 if(all_lotto>max_lotto_number)
  all_lotto>max_lotto_number;
 //取得使用者想要包的號碼
 printf("請輸入想包的號碼...");
 for(int i=0;i<all_lotto;i++)
 {
  scanf("%d",&lotto_array[i]);
  //檢查是否重複
  if(check_lotto_number(lotto_array,i))
  {
   printf("號碼已選過~~\n");
   i--;
  }
 }
 //清除螢幕
 system("cls");
 //秀出最後使用者想要包的牌
 printf("以下為您包的牌\n");
 for(int i=0;i<all_lotto;i++)
  printf("%4d",lotto_array[i]);
 //排序
 my_sort(lotto_array,lotto_number);
 //計算包牌的結果
 printf("\n以下為包牌結果----------------------------\n");
 int cntr=0,lotto_tmp[lotto_number];
 //檔案位置
 long file_pos=0;
 for(int i=0;i<pow(2,all_lotto);i++)
 {
  if(dec_bin(i,bin_array)==lotto_number)
  {
   for(int j=0,k=0;j<all_lotto;j++)
   {
    if(bin_array[j]==1)
    {
     printf("%4d",lotto_array[j]);
     //存放在另外一個陣列以供寫入
     lotto_tmp[k++]=lotto_array[j];
    }
   }
   printf("\n");
   cntr++;
   //記錄包牌的結果
   file_pos=write_lotto(lotto_path,lotto_tmp,file_pos);
   //N個停下來一次
   if(cntr%10==0)
   {
    printf("按任意鍵繼續\n");
    getch();
   }
  }
 }
 printf("共%6d牌\n任意鍵結束",cntr);
 getch();
 //如果發生以下情形就是嚴重錯誤了~~
 if(cntr<=0)
 {
  printf("ERROR~~!!");
  getch();
  exit(1);
 }
}
//開獎開。開。開。
void lotto_3(int lotto[])
{
 system("cls");
 int lotto_1,tmp;
 //設定亂數的SEED,讓假亂數變真亂數。
 srand( (unsigned)time( NULL ) );
 printf("按六下模擬開獎囉~~\n");
 //初始化
 for (int i=0;i<max_lotto_number;i++)
  lotto[i]=i+1;
 //開始開獎
 for (int i=0;i<lotto_number;i++)
 {
  //開始攪囉,攪到爽
  for (int j=i;!_kbhit();j++)
  {
   j=(j%(max_lotto_number-i))+i;
   lotto_1=(rand()%(max_lotto_number-i))+i;
   tmp=lotto[lotto_1];
   lotto[lotto_1]=lotto[j];
   lotto[j]=tmp;
  }
  _getch();
  //秀出亂數
  printf("%4d",lotto[i]);
 }
 my_sort(lotto,lotto_number);
}

//秀出樂透清單
void show_lotto_list()
{
 system("cls");
 FILE *file;
 if((file=fopen(lotto_path,"r"))==NULL)
  printf("開檔失敗,沒有讀取\n\a");
 else
 {
  printf("開檔成功,開始讀取\n");
  for(int i=1,tmp;!feof(file);i++)
  {
   fscanf(file,"%4d",&tmp);
   if(!feof(file))
    printf("%4d",tmp);
   //幾個號碼為一組樂透
   if((i%lotto_number)==0)
    printf("\n");
   //幾組樂透號碼停下來看一下
   if((i%(lotto_number*10))==0)
    getch();
  }
  fclose(file);
  getch();
 }
}

//十進制轉二進制(不包括反轉)
int dec_bin(int dec_number,int bin_array[])
{
 int sum=0;
 for(int i=0;dec_number!=0;i++,dec_number=dec_number/2)
 {
  sum=sum+(dec_number%2);
  bin_array[i]=(dec_number%2);
 }
 return sum;
}

//秀出樂透開獎號碼
void show_lotto(int lotto[])
{
 if(lotto[0]==0)
  printf("尚未開獎\n");
 else
 {
  printf("開獎結果:\n");
  for(int i=0;i<lotto_number;i++)
   printf("%4d",lotto[i]);
 }
 printf("\n"); 
}

void ShowMenu(int KB,int lotto[])
{
 system("cls");
 //秀出樂透開獎號碼
 show_lotto(lotto);
 printf("--------------------------\n");
 printf("請按右邊數字鍵的2(向下移動)\n或按8(向上移動)並按enter確定\n");
 printf("--------------------------\n");
 if(KB==1)
  printf("<*> 1.自行選號\n");
 else
  printf("    1.自行選號\n");
 if(KB==2)
  printf("<*> 2.包牌選號\n");
 else
  printf("    2.包牌選號\n");
 if(KB==3)
  printf("<*> 3.樂透開獎\n");
 else
  printf("    3.樂透開獎\n");
 if(KB==4)
  printf("<*> 4.清除我的樂透清單\n");
 else
  printf("    4.清除我的樂透清單\n");
 if(KB==5)
  printf("<*> 5.秀出我的樂透清單\n");
 else
  printf("    5.秀出我的樂透清單\n");
 if(KB==6)
  printf("<*> 6.對獎\n");
 else
  printf("    6.對獎\n");
 if(KB==7)
  printf("<*> 7.離開\n");
 else
  printf("    7.離開\n");
}

int GetKey(int Menu,int lotto[])
{
 int KB;
 do
 {
  KB=getch();
  switch(KB)
  {
  case    50:
   if(Menu<max_menu)
    Menu++;
   break;
  case    56:
   if(Menu>1)
    Menu--;
   break;
  }
  ShowMenu(Menu,lotto);
 }while(KB!=13);
 return Menu;
}

//泡沫排序
void my_sort(int lotto[],int max_l)
{
 for(int i=0;i<max_l;i++)
  for(int j=0;j<max_l;j++)
  {
   if(lotto[i]<lotto[j])
   {
    int tmp=lotto[i];
    lotto[i]=lotto[j];
    lotto[j]=tmp;
   }
  }
}

//檢查中獎
void lotto_win(int lotto[])
{
 if(lotto[0]==0)
  return;
 int lotto_array[lotto_number];
 //複製樂透
 int lotto_w[lotto_number];
 for(int i=0;i<lotto_number;i++)
  lotto_w[i]=lotto[i];
 //排序,方便檢查
 my_sort(lotto_w,lotto_number);
 //讀取樂透清單
 FILE *file;
 int tmp;
 if((file=fopen(lotto_path,"r"))==NULL)
  printf("開檔失敗,沒有讀取\n\a");
 else
 {
  printf("開檔成功,開始讀取\n");
  for(int i=1,cntr=1;!feof(file);i++)
  {
   fscanf(file,"%4d",&tmp);
   if(!feof(file))
   {
    printf("%4d",tmp);
    lotto_array[i-1]=tmp;
   }
   //幾個號碼為一組樂透
   if((i=i%lotto_number)==0)
   {
    //排序,方便檢查
    my_sort(lotto_array,lotto_number);
    //開始檢查
    int win=0;
    for(int j=0;j<lotto_number;j++)
    {
     for(int k=0;k<lotto_number;k++)
      if(lotto_w[j]==lotto_array[k])
       win++;
    }
    //設定中獎項目
    printf("......");
    switch(win)
    {
    case 3:
     {
      printf("*參獎*\n");
     }break;
    case 4:
     {
      printf(":貳獎:\n");
     }break;
    case 5:
     {
      printf("[壹獎]\n");
     }break;
    case 6:
     {
      printf("<頭獎>\n");
     }break;
    default:
     {
      printf(" 槓龜 \n");
     }break;
    }
    if(((cntr++)%10)==0)
     getch();
   }
  }
  fclose(file);
  getch();
 }
}

回复

使用道具 举报

发表于 2004-6-24 09:01:25 | 显示全部楼层
回覆給:特洛瓦
你寫程式的功力滿強的~~~~~~~~~!!
有幾段程式碼幾乎都看不懂!!
呵呵~~~~~~~~~~~~~~~  ^.^"
回复

使用道具 举报

发表于 2004-6-24 11:45:36 | 显示全部楼层
回覆給:真夜

你看不太懂~~應該是因為~~我寫太濫了@@~~
因為結構亂了~~沒規劃直接寫~~這是我常犯的毛病~~
所以~~恩~~是我的錯~~
回复

使用道具 举报

发表于 2004-6-24 12:12:51 | 显示全部楼层
回覆給:特洛瓦
我想應該不是~~~~~~~是我功力太爛了!
看了一下很難者初看的頭緒!!
想像中...功力超強者可以一目千行..................(說笑的^.^)
不過有很多用法倒是滿強的~~~~學起來~~學起來........哈哈!
我比較會用中規中舉的方法~~~~!  ^^|||||汗..
回复

使用道具 举报

Yahoo
您需要登录后才可以回帖 登录 | 註冊

本版积分规则

手機版|Archiver|Z板 Sitetag 訪客統計

GMT+8, 2024-10-12 04:34 , Processed in 0.116910 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表