小明由于消费记录不好被妈妈没收了零花钱,愤然离家出走,娶了老婆,结果又被老婆查账,被没收了工资,穷困潦倒,只好到这里来盗墓了,然后不幸被你的坑爹程序陷害,被活埋了。
——from太戈编程上的一道题的题面
几个坑爹程序如下
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<string.h>
#define N1 2
#define N2 10
struct user{
int num;
char name[10];
char password[8];
float time;
int rank;
};
struct user user1,user2[100];
int count;
int flag1 = 0;
void menu();
void regist();
void login();
void guess();
void query();
void sort();
void save();
void show();
int main(){
int n;
while(1){
menu();
printf("\n 请输入要选择的项目序号:");
scanf("%d",&n);
switch(n){
case 1:regist();break;
case 2:login();break;
case 3:guess();break;
case 4:query();break;
case 5:show();break;
case 6:exit(0);
default:printf(" 输入错误,请输入列表中的序号!\n");
}
}
}
void menu(){
system("cls");
printf("\n");
printf(" 猜数游戏挑战赛\n");
printf(" 1.用户注册\n");
printf(" 2.用户登录\n");
printf(" 3.开始猜数\n");
printf(" 4.个人历史成绩查询\n");
printf(" 5.当前排行榜显示\n");
printf(" 6.退出");
}
void regist(){
FILE *fp;
int i,j;
count = 0;
if((fp = fopen("users.txt","a+")) == NULL){
printf("文件打开失败!\n");
return;
}
for(i = 0;feof(fp) == 0;i++){
fscanf(fp,"%d %s %s %f %d\n",&user2[i].num,&user2[i].name,
&user2[i].password,&user2[i].time,&user2[i].rank);
if(user2[i].num == 0)count = 0;
else count++;
}
printf(" 当前已有用户数为:%d\n",count);
printf(" 请输入新用户名称和密码,用空格隔开,按回车确认:\n");
scanf("%s%s",user1.name,user1.password);
for(j = 0;j < count;j++)
if(strcmp(user1.name,user2[j].name) == 0){
printf("用户已存在.\n");
getch();
return;
}
user1.num = count + 1;
user1.time = -1;
user1.rank = -1;
fprintf(fp,"%d %s %s %.2f %d\n",user1.num,user1.name,
user1.password,user1.time,user1.rank);
printf(" 新用户注册成功!\n");
getch();
fclose(fp);
}
void login(){
FILE *fp;
int i,j;
count = 0;
if((fp = fopen("users.txt","r")) == NULL){
printf("文件打开失败!\n");
return;
}
for(i = 0;feof(fp) == 0;i++){
fscanf(fp,"%d %s %s %f %d\n",&user2[i].num,&user2[i].name,
&user2[i].password,&user2[i].time,&user2[i].rank);
}
count = i;
printf("\n 请输入姓名和密码,用空格隔开,按回车键确认:\n");
scanf("%s%s",user1.name,user1.password);
for(j = 0;j < count;j++)
if(strcmp(user1.name,user2[j].name) == 0&&
strcmp(user1.password,user2[j].password) == 0){
user1 = user2[j];
flag1 = 1;
printf("登录成功!\n");
break;
}
if(flag1 == 0)
printf(" 用户名或密码不正确,请重新登录或重新注册!\n");
fclose(fp);
getch();
}
void guess(){
clock_t start,finish;
float duration;
int i,j;
int guess;
int flag2 = 1;
int magic[N1];
int counter[N1] = {0};
int correct[N1] = {0};
if(flag1 == 0){
printf(" 请先注册!\n");
getch();
return;
}
srand(time(NULL));
for(i = 0;i < N1;i++)
magic[i] = rand() % 100 + 1;
start = clock();
for(i = 0;i < N1;i++){
printf(" 请猜第%d个数:\n",i + 1);
while(counter[i] < N2){
printf(" 用户输入一个数: ");
scanf("%d",&guess);
counter[i]++;
if(guess > magic[i])
printf(" 错! 猜大了!\n");
else if(guess < magic[i])
printf(" 错! 猜小了!\n");
else{
printf(" 正确!\n");
correct[i] = 1;
break;
}
}
printf(" 结束\n");
}
finish = clock();
duration = (float)(finish - start) / CLOCKS_PER_SEC;
for(i = 0;i < N1;i++)
printf("magic = %2d,counter = %d,correct = %d\n",
magic[i],counter[i],correct[i]);
printf("Total time is %f\n",duration);
getch();
for(i = 0;i < N1;i++)
if(correct[i] != 1){
flag2 = 0;
break;
}
if(flag2 == 1){
if(user1.time > 0){
if(duration < user1.time)
user1.time = duration;
else
printf(" 本次计时没有小于历史记录中的时间,不进行更新");
}else
user1.time = duration;
for(j = 0;j < count;j++)
if(strcmp(user1.name,user2[j].name) == 0)
user2[j] = user1;
save();
}else
printf(" 由于所有数没有全猜对,因此本次游戏不作记录。");
getch();
}
void save(){
FILE *fp;
int i;
sort();
if((fp = fopen("users.txt","w")) == NULL){
printf("文件打开失败!保存没成功\n");
return;
}
for(i = 0;i < count;i++)
fprintf(fp,"%d %s %s %.2f %d\n",user2[i].num,user2[i].name,
user2[i].password,user2[i].time,user2[i].rank);
fclose(fp);
}
void sort(){
int i,j;
struct user temp;
for(i = 0;i < count - 1;i++)
for(j = 0;j < count - 1 - i;j++)
if(user2[j].time > user2[j + 1].time){
temp = user2[j];
user2[j] = user2[j + 1];
user2[j + 1] = temp;
}
for(i = 0;i < count;i++)
user2[i].rank = i + 1;
}
void query(){
if(flag1 == 0){
printf(" 请先登录!\n");
getch();
return;
}
printf(" 用户姓名=%s 猜数用时 = %.2f 排名 = %d",
user1.name,user1.time,user1.rank);
getch();
}
void show(){
FILE *fp;
int i;
if((fp = fopen("users.txt","r")) == NULL){
printf("文件打开失败!\n");
return;
}
printf("\n 姓名 用时 排名\n");
for(i = 0;feof(fp) == 0;i++){
fscanf(fp,"%d %s %s %f %d\n",&user2[i].num,user2[i].name,
user2[i].password,&user2[i].time,&user2[i].rank);
printf(" %s %.2f %d\n",user2[i].name,
user2[i].time,user2[i].rank);
}
fclose(fp);
getch();
}
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
#define High 30
#define Width 75
int cells[High][Width];
void gotoxy(int x,int y){
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
void startup(){
int i,j;
for(i = 0;i < High;i++){
for(j = 0;j < Width;j++){
cells[i][j] = rand() % 2;
}
}
}
void show(){
gotoxy(0,0);
int i,j;
for(i = 1;i <= High - 1;i++){
for(j = 1;j <= Width - 1;j++){
if(cells[i][j] == 1)
printf("0");
else
printf(" ");
}
printf("\n");
}
Sleep(25);
}
void updateWithoutInput(){
int NewCells[High][Width];
int NeighbourNumber;
int i,j;
for(i = 1;i <= High - 1;i++){
for(j = 1;j <= Width - 1;j++){
NeighbourNumber = cells[i - 1][j - 1] + cells[i - 1][j]
+ cells[i - 1][j + 1] + cells[i][j - 1] + cells[i][j + 1]
+ cells[i + 1][j - 1] + cells[i + 1][j] + cells[i + 1][j + 1];
if(NeighbourNumber == 3)
NewCells[i][j] = 1;
else if(NeighbourNumber == 2)
NewCells[i][j] = cells[i][j];
else
NewCells[i][j] = 0;
}
}
for(i = 1;i <= High - 1;i++){
for(j = 1;j <= Width - 1;j++)
cells[i][j] = NewCells[i][j];
}
}
void updateWithInput(){
}
int main(){
srand(time(NULL));
startup();
while(1){
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define High 15
#define Width 20
int ball_x,ball_y;
int ball_vx,ball_vy;
int position_x,position_y;
int ridus;
int left,right;
int canvas[High][Width] = {0};
void gotoxy(int x,int y){
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
void startup(){
ridus = 1;
position_x = High - 1;
position_y = Width / 2;
left = position_y - ridus;
right = position_y + ridus;
ball_x = position_x - 1;
ball_y = position_y;
ball_vx = -1;
ball_vy = 1;
canvas[ball_x][ball_x] = 1;
int k,i;
for(k = left;k <= right;k++)
canvas[position_x][k] = 2;
for(k = 0;k < Width;k++)
for(i = 0;i < High / 4;i++)
canvas[i][k] = 3;
}
void show(){
gotoxy(0,0);
int i,j;
for(i = 0;i < High;i++){
for(j = 0;j < Width;j++){
if(canvas[i][j] == 0)
printf(" ");
else if(canvas[i][j] == 1)
printf("O");
else if(canvas[i][j] == 2)
printf("-");
else if(canvas[i][j] == 3)
printf("#");
}
printf("|\n");
}
for(j = 0;j < Width;j++)
printf("-");
printf("\n");
}
void updateWithoutInput(){
if(ball_x == High - 2){
if((ball_y >= left)&&(ball_y <= right)){
}
else{
printf("сно╥й╖╟эё║\n");
system("pause");
exit(0);
}
}
static int speed = 0;
if(speed < 7)speed++;
if(speed == 7){
speed = 0;
canvas[ball_x][ball_y] = 0;
ball_x = ball_x + ball_vx;
ball_y = ball_y + ball_vy;
canvas[ball_x][ball_y] = 1;
if((ball_x == 0)||(ball_x == High - 2))
ball_vx = -ball_vx;
if((ball_y == 0)||(ball_y == Width - 1))
ball_vy = -ball_vy;
if(canvas[ball_x - 1][ball_y] == 3){
ball_vx = -ball_vx;
canvas[ball_x - 1][ball_y] = 0;
// printf("\a");
}
}
}
void updateWithInput(){
char input;
if(kbhit()){
input = getch();
if(input == 'a'&&left > 0){
canvas[position_x][right] = 0;
position_y--;
left = position_y - ridus;
right = position_y + ridus;
canvas[position_x][left] = 2;
}
if(input == 'd'&&right < Width - 1){
canvas[position_x][left] = 0;
position_y++;
left = position_y - ridus;
right = position_y + ridus;
canvas[position_x][right] = 2;
}
}
}
int main(){
startup();
while(1){
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
活埋1 活埋2 活埋3 活埋4 https://cdn.luogu.com.cn/upload/image_hosting/2rux8ebc.png无