PS:老师叫弄得一个模拟发布五福代码,要求
1.敬业福发布的概率是其他的0.1倍
2.敬业福的数量不超过10张,且每次都保存
//就是程序退出以后还是保存数据
代码:
#include "stdio.h" #include "time.h" #include "stdlib.h" void meun(int num); int main(){ int a,num,i,b; FILE *fp; if((fp=fopen("C:\\Users\\Sure\\Desktop\\text.txt","r+"))==NULL){ printf("fail to open file\n"); exit(0); } fscanf(fp,"%d",&num); meun(num); while(~scanf("%d",&a)){ srand(time(0)); if(a==2) return 0; else{ if(num<10){ b=rand()%41+1; if(b<=10) printf("你获得爱国福一张!\n"); else if(b<=20 && 10<b) printf("你获得和谐福一张!\n"); else if(b<=30 && 20<b) printf("你获得友善福一张!\n"); else if(b<=40 && 30<b) printf("你获得富强福一张!\n"); else{ printf("你获得敬业福一张!\n"); num++; rewind(fp); fprintf(fp,"%d",num); } } else{ b=rand()%40+1; if(b<=10) printf("你获得爱国福一张!\n"); else if(b<=20 && 10<b) printf("你获得和谐福一张!\n"); else if(b<=30 && 20<b) printf("你获得友善福一张!\n"); else if(b<=40 && 30<b) printf("你获得富强福一张!\n"); } } } fclose(fp); return 0; } void meun(int num){ printf("*********************\n"); printf("*******集五福*******\n"); printf(" 1.生成五福\n"); printf(" 2.退出系统\n"); printf("*********************\n"); printf(" 已发出的敬业福数量:%d\n",num); printf("请输入你的选择:"); }