c语言程序设计——抽奖程序

c语言程序设计——抽奖程序,第1张

真不好玩,写好还真不简单~

中意的话,多加点分!测试过了~

/

预先将参加抽奖者的资料输入存储起来,主要用结构体数组实现,用结构体表示每项记录,包括身份z号码、姓名、电话、住址等信息。开始抽奖时,屏幕上快速闪动参与者的身份z号码,按任意键停止闪动,把中奖人的信息用显示在屏幕上。可反复抽出一、二、三等奖若干名。

设计实现以下功能:

(1)录入:完成抽奖者信息的输入;

(2)删除:完成抽奖者信息的删除;

(3)修改:允许对已经录入的数据重新进行编辑、修改;

(4)显示:显示所有中奖者的信息;

(5)抽奖:

a 抽出一等奖 1 名,如果抽过就不能再抽;

b 抽出二等奖 2 名,如果抽完就不能再抽;

c 抽出三等奖 5 名,如果抽完就不能再抽;

(6)退出程序。/

#include<stdioh>

#include <conioh>

#include <stdlibh>

#include <memoryh>

#include <stringh>

#include <timeh>

#define AWARDER_NUM 10

typedef struct tag_award

{

char ID[20];

char name[20];

char telNo[20];

char address[50];

}AWARDER;

AWARDER awarder = new AWARDER[AWARDER_NUM];

int first_award[1] = {-1};

int second_award[2] = {-1,-1};

int third_award[5] ={-1,-1,-1,-1,-1};

int awarder_num = 0;

void info_login()

{

if(awarder_num >= AWARDER_NUM)

{

AWARDER new_awarder = new AWARDER[awarder_num + AWARDER_NUM];

memcpy(new_awarder,awarder,awarder_numsizeof(AWARDER));

delete awarder;

awarder = new_awarder;

}

printf("身份z号:");

scanf("%s",awarder[awarder_num]ID);

printf("姓名:");

scanf("%s",awarder[awarder_num]name);

printf("电话号码:");

scanf("%s",awarder[awarder_num]telNo);

printf("家庭住址:");

scanf("%s",awarder[awarder_num]address);

awarder_num++;

}

void info_delete()

{

char name[20];

printf("输入您要删除的抽奖者的姓名:");

scanf("%s",name);

for(int i=0;i<awarder_num;i++)

{

if (strcmp(awarder[i]name,name) == 0)

{

printf("您选择删除的抽奖者的信息如下:\n");

printf("身份z号:%s\n",awarder[i]ID);

printf("姓名:%s\n",awarder[i]name);

printf("电话号码:%s\n",awarder[i]telNo);

printf("家庭住址:%s\n",awarder[i]address);

break;

}

}

if(i == awarder_num)

{

printf("没有找到您输入的抽奖者!\n");

}

else

{

char c;

printf("您确定要删除吗?(Y//N)");

fflush(stdin);

scanf("%c",&c);

if(c == 'Y' || c == 'y')

{

AWARDER new_award = new AWARDER[awarder_num-1];

if(i <= 0)

memcpy(new_award,awarder+1,sizeof(AWARDER) (awarder_num - 1));

else if( i >= awarder_num -1)

memcpy(new_award,awarder,sizeof(AWARDER) (awarder_num - 1));

else

{

memcpy(new_award,awarder,sizeof(AWARDER) i);

memcpy(new_award+i,awarder+i+1,sizeof(AWARDER) (awarder_num - i - 1));

}

awarder_num--;

delete awarder;

awarder = new_award;

}

}

}

void info_modify()

{

char name[20];

printf("输入您要修改的抽奖者的姓名:");

scanf("%s",name);

for(int i=0;i<awarder_num;i++)

{

if (strcmp(awarder[i]name,name) == 0)

{

printf("您选择的抽奖者的信息如下:\n");

printf("身份z号:%s\n",awarder[i]ID);

printf("姓名:%s\n",awarder[i]name);

printf("电话号码:%s\n",awarder[i]telNo);

printf("家庭住址:%s\n",awarder[i]address);

break;

}

}

if(i == awarder_num)

{

printf("没有找到您输入的抽奖者!\n");

}

else

{

printf("请输入该抽奖者的新信息:\n");

printf("身份z号:");

scanf("%s",awarder[i]ID);

printf("姓名:");

scanf("%s",awarder[i]name);

printf("电话号码:");

scanf("%s",awarder[i]telNo);

printf("家庭住址:");

scanf("%s",awarder[i]address);

}

}

bool check_over(int cur_award,int awarderNo)

{

if(awarderNo == 1)

{

if(cur_award == first_award[0])

return true;

}

else if(awarderNo <= 3)

{

if(cur_award == first_award[0])

return true;

for(int i=0;i<awarderNo-1;i++)

if(cur_award == second_award[i])

return true;

}

else if(awarderNo <= 8)

{

if(cur_award == first_award[0])

return true;

for(int i=0;i<2;i++)

if(cur_award == second_award[i])

return true;

for(i=0;i<awarderNo-3;i++)

if(cur_award == third_award[i])

return true;

}

return false;

}

void award()

{

if(awarder_num < 8)

{

printf("人数太少,请添加足够的人再抽奖!\n");

return;

}

srand((long)time(0));

char hit_id[20];

printf("\n开始抽第一等奖(1名),按任意键开始\n");

getch();

while(!kbhit())

{

first_award[0] = rand() % awarder_num;

memcpy(hit_id,awarder[first_award[0]]ID,18);

printf("\r ");

printf("\r %s",hit_id);

}

printf("\n\n开始抽第二等奖(2名)");

for (int i=0;i<2;i++)

{

printf("\n第%d个二等奖,按任意键开始\n",i+1);

getch();

while(!kbhit())

{

second_award[i] = rand() % awarder_num;

while(check_over(second_award[i],1+i))

second_award[i] = rand() % awarder_num;

memcpy(hit_id,awarder[second_award[i]]ID,18);

printf("\r ");

printf("\r %s",hit_id);

}

}

printf("\n\n开始抽第三等奖(5名)");

for (i=0;i<5;i++)

{

printf("\n第%d个三等奖,按任意键开始\n",i+1);

getch();

while(!kbhit())

{

third_award[i] = rand() % awarder_num;

while(check_over(third_award[i],3+i))

third_award[i] = rand() % awarder_num;

memcpy(hit_id,awarder[third_award[i]]ID,20);

printf("\r ");

printf("\r %s",hit_id);

}

}

}

void info_show()

{

printf("一等奖获得者:\n");

printf("\t身份z号:%s\n",awarder[first_award[0]]ID);

printf("\t姓名:%s\n",awarder[first_award[0]]name);

printf("\t电话号码:%s\n",awarder[first_award[0]]telNo);

printf("\t家庭住址:%s\n",awarder[first_award[0]]address);

printf("二等奖获得者:\n");

for (int i=0;i<2;i++)

{

printf("第%d个二等奖获得者:\n");

printf("\t身份z号:%s\n",awarder[second_award[i]]ID);

printf("\t姓名:%s\n",awarder[second_award[i]]name);

printf("\t电话号码:%s\n",awarder[second_award[i]]telNo);

printf("\t家庭住址:%s\n",awarder[second_award[i]]address);

}

printf("三等奖获得者:\n");

for (int i=0;i<5;i++)

{

printf("第%d个三等奖获得者:\n");

printf("\t身份z号:%s\n",awarder[third_award[i]]ID);

printf("\t姓名:%s\n",awarder[third_award[i]]name);

printf("\t电话号码:%s\n",awarder[third_award[i]]telNo);

printf("\t家庭住址:%s\n",awarder[third_award[i]]address);

}

}

/提示信息/

bool raise()

{

printf("\n请输入你需要的 *** 作代号:\n");

printf("\t1录入:完成抽奖者信息的输入;\n");

printf("\t2删除:完成抽奖者信息的删除;\n");

printf("\t3修改:允许对已经录入的数据重新进行编辑、修改;\n");

printf("\t4抽奖:开始抽奖!\n");

printf("\t5显示:显示所有中奖者的信息;\n");

printf("\t6退出。\n");

printf("==>");

int operate;

fflush(stdin);

scanf("%d",&operate);

switch(operate)

{

case 1:

info_login();

break;

case 2:

info_delete();

break;

case 3:

info_modify();

break;

case 4:

award();

break;

case 5:

info_show();

break;

case 6:

return false;

default:

break;

}

printf("\n *** 作完成。\n");

return true;

}

void main()

{

while(raise());

}

单用C标准里的东西,可能无法做;得借助于 *** 作系统或其他第三方库提供的多线程。单用C++2003及其以前的标准也无法实现,原因同C,如果依据C++2011标准(2011年刚出台的C++标准)到是可以——C++2011标准加入了多线程,要求C++编译器支持多线程,但是现在的编译器估计都还没有支持。

现在的可以了,你试下

import javaawtBorderLayout;

import javaawtColor;

import javaawtFont;

import javaawtGridLayout;

import javaawteventActionEvent;

import javaawteventActionListener;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJLabel;

import javaxswingJPanel;

import javaxswingJTextField;

import javaxswingSwingConstants;

public class RandomTest extends JFrame {

private static final long serialVersionUID = 6764423443555437772L;

boolean isStop = false;

private JLabel jl = new JLabel();

private JTextField jt = new JTextField();

private JButton start = new JButton("start");

private JButton stop = new JButton("stop");

private JPanel jp = new JPanel();

public RandomTest() {

thisjlsetHorizontalAlignment(SwingConstantsCENTER);

thisjlsetText("60"); // 设置随机数范围1到60

thisjlsetForeground(Colorred); // 设置字体颜色

thisjlsetFont(new Font("Dialog", 1, 24)); // 设置字体样式、大小

GridLayout layout = new GridLayout(1, 2);

thisadd(jl, BorderLayoutNORTH);

thisadd(jt, BorderLayoutCENTER);

thisjpadd(start, layout);

thisjpadd(stop, layout);

thisadd(jp, BorderLayoutSOUTH);

thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE); // 关闭窗口

startaddActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

isStop = false;

JTextThread jtt = new JTextThread(jt);

jttstart();

}

});

stopaddActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

isStop = true;

}

});

}

public static void main(String[] args) {

RandomTest rt = new RandomTest();

rtsetSize(400, 300);

rtsetLocationRelativeTo(null); // 设置窗口初始位置,屏幕中央

rtsetVisible(true);

}

class JTextThread extends Thread {

private JTextField jt;

public JTextThread(JTextField jt) {

thisjt = jt;

}

@Override

public void run() {

while (!isStop) {

try {

Threadsleep(100);

int random = (int) (Mathrandom() 60 + 1);

jtsetText(StringvalueOf(random));

jtsetForeground(Colorblue); // 设置字体颜色

jtsetFont(new Font("Dialog", 1, 48)); // 设置字体样式、大小

jtsetHorizontalAlignment(SwingConstantsCENTER);

} catch (InterruptedException e) {

eprintStackTrace();

}

}

}

}

}

主要利用VB中的Rnd函数,来实现随机查找和打乱排序的功能,从而实现随机抽奖的目的。Rnd函数的语法结构是Rnd[(number)],可选的number参数是

single或任何有效的数值表达式。Rnd函数返回小于1但大于或等于0的值。number

的值决定了

Rnd

生成随机数的方式。为了生成某个范围内的随机整数,可使用以下公式:

Int((upperbound

lowerbound

1)

×

Rnd

lowerbound)

这里,upperbound

是随机数范围的上限,而

lowerbound

则是随机数范围的下限。

另外,程序中还使用了INI文件,Windows

INI文件,可解释为Windows初始化文件。它是一种专门用来保存应用程序初始化信息和运行环境信息的文本文件。ini文件是一种文本文件,它可以通过Notepad等文本编辑器进行编辑。ini文件具有特定的格式。一个INI文件是由若干个段(section)组成的,每个段中包含若干关键字(key)及相应的值(value)。创建应用程序自己的INI文件,通过INI文件保存应用程序的一些运行环境信息,然后在程序中读取INI文件中的设置信息并据以处理。一旦程序的运行环境需要变更,则可以通过直接修改INI文件,或在程序中提供专门的界面间接地修改INI文件来保证程序的可用性。

源程序及注释如下:

'窗体源程序

Option

Explicit

Dim

m_strNameArray()

As

MyName

Dim

m_bIsStart

As

Boolean

Dim

m_nNameIndex

As

Integer

Dim

MAX_INDEX

As

Integer

Dim

m_nSelectNum

As

Integer

'被选定数

Dim

nScrollStep

As

Integer

Dim

nScrollWidth

As

Integer

Dim

bScrollState

As

Boolean

Dim

nEnableSecond

As

Integer

Dim

m_strTitle

As

String

Dim

m_strAppTitle

As

String

Dim

m_strScrollTitleLeft

As

String

Dim

m_strScrollTitleRight

As

String

Private

Sub

Command_Start_Stop_Click()

If

m_bIsStart

=

True

Then

'按停止钮

m_bIsStart

=

False

Command_Start_StopCaption

=

“开始"

Label_FlashNameVisible

=

True

Timer_FlashNameEnabled

=

True

Timer_ScrollNameEnabled

=

False

Label_FlashName

=

m_strNameArray(m_nNameIndex)strName

“中奖了!"

m_strNameArray(m_nNameIndex)bIsSelect

=

True

m_nSelectNum

=

m_nSelectNum

1

Dim

Temp

As

MyName

Temp

=m_strNameArray(MAX_INDEX)

m_str

Name

Array(MAX-INDEX)

=

m_strNameArray(m_nNameIndex)

m_strNameArray(m_nNameIndex)

=Temp

MAX_INDEX

=

MAX_INDEX

1

If

MAX_INDEX

=

0

Then

MsgBox

“非常感谢您使用本软件"

End

If

Else

'按开始钮

m_bIsStart

=

True

Command_Start_StopCaption

=

“停止"

Command_Start_StopEnabled

=

False

Timer_ScrollNameEnabled

=

True

Timer_FlashNameEnabled

=

False

Label_FlashNameCaption

=

“"

End

If

End

Sub

Private

Sub

Form_Load()

Form_BounsScaleMode

=

3

m_nNameIndex

=

0

m_bIsStart

=

False

Timer_ScrollNameEnabled

=

True

Timer_ScrollTitleEnabled

=

True

Label_FlashNameVisible

=

False

Label_ScrollNameCaption

=

“"

nEnableSecond

=

0

'定义起始秒数

ReDimNameArray

'获得文本中的名字和打乱名字顺序

nScrollStep

=

5

'设定滚动字的步长

nScrollWidth

=

Label_CongruationLeft

'设定title的移动宽度

bScrollState

=

False

'设定缺省的开始滚动方向为向左

m_nSelectNum

=

0

'初始化被选定数为0

Init

'初始化本程序的界面

End

Sub

Private

Sub

Timer_FlashName_Timer()

'闪动中奖者姓名

If

Label_FlashNameVisible

=

True

Then

Label_FlashNameVisible

=

False

Else

Label_FlashNameVisible

=

True

End

If

End

Sub

Private

Sub

Timer_ScrollName_Timer()

'滚动出现名字

If

m_bIsStart

=

True

Then

If

m_nNameIndex

>=

MAX_INDEX

Then

m_nNameIndex

=

0

End

If

m_nNameIndex

=m_nNameIndex

1

If

m_strNameArray(m_nNameIndex)bIsSelect

=

True

Then

If

m_nNameIndex

<

MAX-INDEX

Then

m_nNameIndex

=

m_nNameIndex

1

Else

m_nNameIndex

=

0

End

If

End

If

Label_ScrollNameCaption

=

m_str

NameArray(m_nNameIndex)strName

'End

If

End

If

End

Sub

Private

Sub

Timer_ScrollTitle_Timer()

'滚动“恭喜发财"字样

If

bScrollState

=

False

Then

'向左滚

nScrollStep

=

10

Label_CongruationCaption

=

m_strScrollTitleLeft

If

nScrollWidth

>

0

Then

nScrollWidth

=

nScrollWidth

nScrollStep

Else

bScrollState

=

True

End

If

Else

'向右滚

nScrollStep

=

-10

Label_CongruationCaption

=

m_strScrollTitleRight

If

nScrollWidth

<

Form_BounsScaleWidth

Label_CongruationWidth

Then

nScrollWidth

=

nScrollWidth

nScrollStep

Else

bScrollState

=

False

End

If

End

If

Label_CongruationLeft

=

nScrollWidth

'以下为8秒钟内使“停止"按钮有效

If

nEnableSecond

<=

49

Then

If

m_bIsStart

=

True

Then

nEnableSecond

=nEnableSecond

1

End

If

Else

If

m_bIsStart

=

True

Then

Command_Start_StopEnabled

=

True

nEnableSecond

=

0

End

If

End

If

End

Sub

'动态定义数组

Private

Sub

ReDimNameArray()

Dim

nMaxIndex

As

Integer

Dim

strMaxIndex

As

String

Dim

nIndex

As

Integer

Dim

bIsBegin

As

Boolean

bIsBegin

=

False

nIndex

=

0

Open

AppPath

“\nametxt"

For

Input

As

#1

'读文件

Do

Until

EOF(1)

If

bIsBegin

=

False

Then

Line

Input

#1,

strMaxIndex

nMaxIndex

=

Val(strMaxIndex)

MAX_INDEX

=

nMaxIndex

1

ReDim

m_strNameArray(0

To

nMaxIndex

1)

bIsBegin

=

True

Else

Line

Input

#1,

m_strNameArray(nIndex)strName

m_strNameArray(nIndex)bIsSelect

=

False

nIndex

=

nIndex

1

End

If

Loop

'以下为打乱人员顺序10次

Dim

i

As

Integer

Dim

j

As

Integer

Dim

Temp

As

String

Dim

nRandomNum

As

Integer

For

j

=

0

To

10

For

i

=

0

To

nMaxIndex

1

nRandomNum

=

((nMaxIndex

1)

×

Rnd)

'利用Rnd函数

Temp

=

m_strNameArray(i)strName

m_strNameArray(i)strName

=

m_strNameArray(nRandomNum)strName

m_strNameArray(nRandomNum)strName

=

Temp

Next

i

Next

j

End

Sub

Private

Sub

Init()

'读取INI文件

Dim

X

As

Long

Dim

lpFileName

Dim

Temp

As

String

×

50

lpFileName

=

AppPath

“\Sortitionini"

X

=

GetPrivateProfileString(“SYSTEM",“AppTitle",“抽奖程序",

Temp,

Len(Temp),

lpFileName)

m_strAppTitle

=

Trim(Temp)

Temp

=“"

X

=

GetPrivateProfileString(“SYSTEM",

"Title",

"欢迎使用抽奖程序",

Temp,

Len(Temp),

lpFileName)

m_strTitle

=

Trim(Temp)

Temp

=

“"

X

=

GetPrivateProfileString(“SYSTEM",“ScrollTitleRight",

“恭喜发财!!!",

Temp,

Len(Temp),

lpFileName)

m_strScrollTitleRight

=

Trim(Temp)

X

=

GetPrivateProfileString(“SYSTEM",“ScrollTitleLeft",

“龙年大发!!!",

Temp,

Len(Temp),

lpFileName)

m_strScrollTitleLeft

=

Trim(Temp)

Form_BounsCaption

=

m_strAppTitle

Label_CompanyTitleCaption

=

m_strTitle

End

Sub

模块源程序:

'用于读取ini文件的API函数

Declare

Function

GetPrivateProfileString

Lib

“kernel32"

Alias

“GetPrivateProfileStringA"

(ByVal

lpApplicationname

As

String,

ByVal

lpKeyName

As

String,

ByVal

lpDefault

As

String,

ByVal

lpReturnedString

As

String,

ByVal

nSize

As

Long,

ByVal

lpFileName

As

String)

As

Long

Public

Type

MyName

strName

As

String

bIsSelect

As

Boolean

End

Type

由于程序利用的windows

ini文件保存一些标题信息,因而可以方便的修改使用环境,及标题内容。

见ini文件内容:

[SYSTEM]

;应用程序的form名称

AppTitle=“风云电脑抽奖Test"

;窗口的内的标题(限9个字)

Title=“大抽奖"

;右滚动的文字(仅能为如下格式:XXXX!!!)

ScrollTitleRight=“恭喜发财!!!"

;左滚动的文字(仅能为如下格式:XXXX!!!)

ScrollTitleLeft=“祝您好运!!!"

以上就是关于c语言程序设计——抽奖程序全部的内容,包括:c语言程序设计——抽奖程序、C语言抽奖程序、java随机抽号程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10113089.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存