用c语言写一个简易抽奖程式,要求涉及到档案的 *** 作
#include <bits/stdc++.h>using namespace std int n,a[233] int main(){ freopen("a.in","r",stdin) freopen("a.out","w",stdout) scanf("%d",&n) for (int i=0i<ni++) scanf("%d",&a[i]) printf("%d\n",a[rand()%n]) }
求用c语搏举言自制一个简易抽奖程式
用data.txt档案储存以下内容:
13725528132 李桂荣
13725528131 李二来
13725528133 张荣刚
13725528130 荣南
13725528137 王三
13725528138 吴立
13725528139 郭德纲
13725528140 周星驰
13725528141 张曼玉
13725528142 张艺谋
13725528152 秦香莲
13725528162 潘金莲
13725528172 李大嘴
13725528182 展堂
原始码如下
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_NUM 9999
定义储存人名和电话的资料结构
struct Person
{
char name[20]
char telno[15]
char award
}
int num = 0统计人数
FILE *fp档案指标
Person persons[MAX_NUM]定义阵列
int awarder_1[1] = {-1}一等奖
int awarder_2[2] = {-1, -1}二等奖
int awarder_3[5] = {-1, -1, -1, -1, -1}三等奖
读取档案
void readdata()
{
int i = 0阵列下标
Person person
档案开启
fp = fopen("data.txt", "r")
if (fp == NULL)
{
printf("开启档案data.txt失败!\n")
return
}
当档案不为空
while (!feof(fp))
{
num ++
fscanf(fp, "%s", person.telno)
fscanf(fp, "%s", person.name)
person.award = 'F'
persons[i++] = person
}
}
初始化标识
void init()
{
for(int i = 0i <numi++)
{
persons[i].award = 'F'
}
}
显示单个中奖资讯
void info( int i)
{
printf("手机旁银谈号码: %s 姓名: %s\n", persons[i].telno, persons[i].name)
}
void main()
{
char again = 'Y'
读取档案
readdata()
printf("简单抽奖程式\n")
srand((long)time(0))
while(again == 'Y' || again == 'y')
{
初始化标识
init()
printf("\n开始抽第一等奖(1名),按任意键开始...\n")
getchar()
awarder_1[0] = abs(rand() % num)
while (persons[awarder_1[0]].award == 'T')
{
awarder_1[0] = rand() % num
}
persons[awarder_1[0]].award = 'T'
info(awarder_1[0])
printf("\n开始抽第二等奖(2名)\n")
for (int i = 0i <2i++)
{
printf("\n第%d个二等奖,按任意键开始...\n", i+1)
getchar()
awarder_2[i] = rand() % num
while (persons[awarder_2[i]].award == 'T')
{
awarder_2[i] = rand() % num
}
persons[awarder_2[i]].award = 'T'
info(awarder_2[i])
}
printf("\n\n开始抽第三等奖(5名)\n")
for (i = 0i <5i++)
{
printf("\n第%d个三等奖,按任意键开始...\n", i + 1)
getchar()
awarder_3[i] = rand() % num
while (persons[awarder_3[i]].award == 'T')
{
awarder_3[i] = rand() % num
}
persons[awarder_3[i]].award = 'T'
info(awarder_3[i])
}
printf("\n是否重新开始抽奖?(Y or N)...\n")
again = getchar()
}
getchar()
return
}
C语言怎么写一个简易秒表
用到time.h档案
里面有个clock()函式,返回一个clock_t型别的数字,表示从程式执行开始,CPU的"滴答"数
而在time.h里有个常量CLOCKS_PER_SEC表示每秒钟有多少个"滴答".
这样,(((float)clock())/CLOCKS_PER_SEC)*1000这样的表示式就能得到从程式执行开始到现在的经过的时间.
程式的大致思路是这样的,程式按下1的时候记下当时的程式执行时间.
从这时起,每时刻捡取程式执行时间,然后减去先前的值,就可以得到已经计时的时间了.
只要让使用者按下0结束计时就好了
在conio.h档案里,有个函式kbhit()是个非阻塞函式,用来检查键盘缓冲里有没有按键按下,若有,则返回1,若没有,则返回0,以此来作为判断,若返回1,则捡取按键,测试它是不是0或者1,若返回0,则表示使用者没有动作,继续原来的工作,即继续计时或等待命令.
时间紧,求帮写一个简单C语言程式
#include <stdio.h>
void show(int i)
{
switch(i) {
case 0:
printf("零")
break
case 1:
printf("壹")
break
case 2:
printf("贰")
break
case 3:
printf("叁")
break
case 4:
printf("肆")
break
case 5:
printf("伍")
break
case 6:
printf("陆")
break
case 7:
printf("柒")
break
case 8:
printf("捌")
break
case 9:
printf("玖")
break
}
}
void showwei(int i)
{
if (i != 5 &&i != 9) { 不是万位和亿位
i = i % 4
}
switch(i){
case 2:
printf("拾")
break
case 3:
printf("佰")
break
case 4:
printf("千")
break
case 5:
printf("万")
break
case 9:
printf("亿")
break
}
}
void test1(int n)
{
int i = 0
int j
int b[20]
while(1) {
b[i] = n % 10
i++
n = n / 10
if (n == 0) {
break
}
}
for(j = i - 1j >= 0j--) {
show(b[j]) 输出一二三四
}
printf("\n")
for(j = i - 1j >= 0j--) {
show(b[j])
showwei(j +1) 输出十百千万
}
printf("\n")
}
void main()
{
long int i
printf("请输入整数:")
scanf("%ld",&i)
test1(i) 输出繁体
}
用c语言写一个简易数独的思路。要程式码
#include<stdio.h>int num[9][9], xy[9][9]int check(int x, int y) {int i, m, n for(i = 0i <9i++)if ((xy[x][y] == xy[i][y]&&i != x)||(xy[x][y] == xy[x][i]&&i != y))return 0 for(i = 0, m = x / 3 * 3, n = y / 3 * 3i <9i++)if (xy[x][y] == xy[m + i / 3][n + i % 3]&&m + i / 3 != x&&n + i % 3 != y)return 0 return 1}void search(int x, int y) {if (x == 9)for(x = 0x <9x++) {for(y = 0y <9y++)printf("%d ", xy[x][y]) printf("\n") }else if (num[x][y])search(x + (y + 1) / 9, (y + 1) % 9) elsefor(xy[x][y] = 1xy[x][y] <= 9xy[x][y]++)if (check(x, y))search(x + (y + 1) / 9, (y + 1) % 9) return}int main() {int i, j for(i = 0i <9i++)for(j = 0j <9j++) {scanf("%d", &num[i][j]) xy[i][j] = num[i][j] }search(0, 0) return 0} 输入为9行9列整数,已知的整数填写对应的数字,尚待计算的未知数字填写0。
该程式码的思路很简单,就是从第一行第一列开始依次填入数字,检查是否是在同一行、同一列、同一宫有没有填入重复数字,如果没有就继续填入下一个数字,如果有就返回。
虽然效率稍低,但原理简单、表述直白、易于理解,更有效率的程式码是使用十字连结串列完成,如有兴趣可继续深入
编写C语言程式,实现对磁碟档案的 *** 作
#include "stdio.h"
#include "string.h"
main()
{
FILE *fp
fp=fopen("data.txt","w")
char a[100]="hello world!"
fwrite(a,strlen(a)+1 ,sizeof(char),fp )
fclose(fp)
fp=fopen("data.txt","r")
char b[100]
fread(b,strlen(a)+1 ,sizeof(char),fp )
fclose(fp)
printf("%s\n",b)
}
c语言,写一个抽奖程式,可以选择重复抽奖也可以选择退出,能调概率
srand(time(0));
int chance;
输入概率(10 - 100 %)
rand() % chance == 0 为中奖;
其他不中;
帮忙写一个c语言程式,作用是写一个分割档案的程式,原档案太大,无法装的下,需要分割
#include <stdio.h>#include <string.h>#define TARGET_SIZE (100*1024*1024) 分割为100M一个档案int main(){char name[100] FILE *in, *out char target[100] int t=1,c, n=0 printf("input source file name and path:") gets(name) in = fopen(name, "rb") if(in == NULL){printf("can not read file\n") return -1 }while(!feof(in)){sprintf(target, "%s.part%d",name, t) out = fopen(target, "wb") if(out == NULL){printf("open %d file to write failed\n",t) return -1 }n = 0 while((c=fgetc(in)) != EOF &&n<TARGET_SIZE){fputc(c, out) n++ }fclose(out) t ++ }fclose(in) printf("done! the file is cut to %d file(s)\n", t-1) return 0}
用data.txt文件保存以下内容:13725528132 李桂荣
13725528131 李二来
13725528133 张荣刚
13725528130 荣南
13725528137 王三
13725528138 吴立
13725528139 郭德纲
13725528140 周星驰
13725528141 张曼玉
13725528142 张艺谋
13725528152 秦香莲
13725528162 潘金莲
13725528172 李大嘴
13725528182 展堂
//源代码如下
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_NUM 9999
//定义保存人名和电话的数据结构
struct Person
{
char name[20]
char telno[15]
char award
}
int num = 0//统计人数
FILE *fp//文件指针
Person persons[MAX_NUM]//定义数组
int awarder_1[1] = {-1}//一等奖
int awarder_2[2] = {-1, -1}/弊让毁/二等奖
int awarder_3[5] = {-1, -1, -1, -1, -1}//三等奖
//读取文件
void readdata()
{
int i = 0//数组下标
Person person
//文件打开
fp = fopen("data.txt", "r")
if (fp == NULL)
{
printf("打开文件data.txt失败!\n")
return
}
//当文件不为空
while (!feof(fp))
{
num ++
fscanf(fp, "%s", person.telno)
fscanf(fp, "%s", person.name)
person.award = 'F'
persons[i++] = person
}
}
//初始化标识
void init()
{
for(int i = 0i <numi++)
{
persons[i].award = 'F'
}
}
//显示单个中奖信息
void info( int i)
{
printf("手机号码: %s 姓名: %s\n", persons[i].telno, persons[i].name)
}
void main()
{
char again = 'Y'
//读取文件
readdata()
printf("简单抽奖程序\n")
srand((long)time(0))
while(again == 'Y' || again == 'y')
{
//初始化标识
init()
printf("\n开始抽第一等奖(1名),按任意键开始...\n")
getchar()
awarder_1[0] = abs(rand() % num)
while (persons[awarder_1[0]].award == 'T')
{
awarder_1[0] = rand() % num
}
persons[awarder_1[0]].award = 'T'
info(awarder_1[0])
printf("\n开始抽第二等奖(2名)\n")
for (int i = 0i <2i++)
{
printf("\n第%d个二等奖,按任意键开始...\n", i+1)
getchar()
awarder_2[i] = rand() % num
while (persons[awarder_2[i]].award == 'T'滑缓)
{
awarder_2[i] = rand() % num
}
persons[awarder_2[i]].award = 'T'
info(awarder_2[i])
}
printf("\n\n开始抽第三租备等奖(5名)\n")
for (i = 0i <5i++)
{
printf("\n第%d个三等奖,按任意键开始...\n", i + 1)
getchar()
awarder_3[i] = rand() % num
while (persons[awarder_3[i]].award == 'T')
{
awarder_3[i] = rand() % num
}
persons[awarder_3[i]].award = 'T'
info(awarder_3[i])
}
printf("\n是否重新开始抽奖?(Y or N)...\n")
again = getchar()
}
getchar()
return
}
#include<stdio.h>#include <conio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <time.h>
#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_num*sizeof(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=0i<awarder_numi++)
{
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=0i<awarder_numi++)
{
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=0i<awarderNo-1i++)
if(cur_award == second_award[i])
return true
}
else if(awarderNo <= 8)
{
if(cur_award == first_award[0])
return true
for(int i=0i<2i++)
if(cur_award == second_award[i])
return true
for(i=0i<awarderNo-3i++)
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=0i<2i++)
{
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=0i<5i++)
{
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=0i<2i++)
{
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=0i<5i++)
{
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())
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)