C_数组and字符串操作记录

介绍:

本文主要使用了:
一、“int[]” and “char[]”数组
二、rand()随机函数
三、“字符串输入函数:gets()” and “字符串输出函数:puts()” and “字符串对比函数:strcmp()”函数
在代码中有详细使用方法在此就不一一赘述

源码

#include<stdio.h>
#include<stdlib.h>
//新增头文件:
#include<time.h>//rand()函数的头文件
#include<string.h>//gets()、puts()函数的头文件

/***************************************************
作业1:
第一部分:使用rand随机生成的10个数字,输入到数组中,
第二部分:输入一个数字判断在数组中是否有这个数字
***************************************************/
void Test_1()
{
	//第一部分
	srand((unsigned)time(NULL));//初始化time
	int arr[10];
	printf("随机生成的10个数字:");
	for (int i = 0; i < 10; ++i)
	{
		arr[i] = rand() % 100;
		printf("%d  ", arr[i]);
	}
	//第二部分
	int number;//定义一个变量
	printf("\n input Number:");
	scanf("%d", &number);
	for (int i = 0; i <= 10; ++i)
	{
		if (number == arr[i])
		{
			printf("Number Enter correctly\n"); break;
		}
		else if (i == 10)
		{
			printf("Number Input error\n");
		}
	}
}
/*****************************************
作业2;
判断输入的字符串是否相同,相同则输出字符串
******************************************/
void Test_2()
{
	char brr1[61],brr2[61];
	int ret;
//输入字符串部分
	printf("PS:30个汉字(60个英文)以内的字符串\n");
	printf("请输入第一段:\n");
	gets(brr1);//输入字符串(中间可以用空格)
	printf("请输入第二段:\n");
	gets(brr2);//输入字符串(中间可以用空格)
//判断字符串是否相同
	if (ret = strcmp(brr1, brr2) == 0){//strcmp(参数1,参数2)用来对比字符串是否相同,相同则返回为0
		printf("字符串相同\n");
		puts(brr1);//输出字符串
		printf("\n");
	}  
	else{
		printf("字符串不相同\n");
	}
}
int main()
{
	Test_1();
	Test_2();

	system("pause");
	return 0;
}

运算结果

判断数组中是否有此数字

在这里插入图片描述

判断两个字符串数组是否相同

在这里插入图片描述

以下是一个基于控制台的小游戏,它是猜数字游戏,玩家需要猜出随机生成的数字。代码涵盖了一维数组字符串、指针、文件等知识点。 ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #define GUESS_NUM 5 // 猜测次数 #define MAX_NUM 100 // 最大数字 #define FILENAME "score.txt" // 存储分数的文件名 struct Score { // 分数记录结构体 char name[20]; int score; }; void play_game(); // 游戏主体 int get_guess(); // 获取玩家猜测的数字 void print_result(int secret_num, int guess); // 输出猜测结果 void print_scores(); // 输出分数榜 void save_score(int score); // 保存分数 void sort_scores(struct Score scores[], int count); // 排序分数榜 int main() { srand(time(NULL)); // 设置随机数种子 play_game(); // 开始游戏 return 0; } void play_game() { printf("Welcome to Guess the Number!\n\n"); printf("I'm thinking of a number between 1 and %d.\n", MAX_NUM); printf("You have %d tries to guess what it is.\n\n", GUESS_NUM); int secret_num = rand() % MAX_NUM + 1; // 生成 1~MAX_NUM 的随机数 int guess_count = 0; // 猜测次数 int guess; while (guess_count < GUESS_NUM) { guess = get_guess(); print_result(secret_num, guess); guess_count++; if (guess == secret_num) { printf("\nCongratulations! You guessed the number in %d tries.\n", guess_count); save_score(GUESS_NUM - guess_count); return; } } printf("\nSorry, you didn't guess the number. It was %d.\n", secret_num); } int get_guess() { int guess; printf("Guess #%d: ", GUESS_NUM - (GUESS_NUM - 1)); scanf("%d", &guess); while (getchar() != '\n') {} // 清除缓存区 return guess; } void print_result(int secret_num, int guess) { if (guess < secret_num) { printf("Too low!\n"); } else if (guess > secret_num) { printf("Too high!\n"); } else { printf("You got it!\n"); } } void print_scores() { printf("Scoreboard\n"); printf("----------\n"); FILE *file = fopen(FILENAME, "r"); if (!file) { // 文件不存在 printf("No scores yet.\n"); return; } struct Score scores[100]; int count = 0; while (!feof(file) && count < 100) { // 读取记录 fscanf(file, "%s %d", scores[count].name, &scores[count].score); count++; } fclose(file); sort_scores(scores, count); // 排序 for (int i = 0; i < count; i++) { printf("%d. %s: %d\n", i + 1, scores[i].name, scores[i].score); } } void save_score(int score) { printf("\nYour score is %d.\n", score); printf("Enter your name: "); char name[20]; fgets(name, 20, stdin); // 获取姓名 name[strlen(name) - 1] = '\0'; // 删除换行符 FILE *file = fopen(FILENAME, "a"); if (file) { fprintf(file, "%s %d\n", name, score); // 写入记录 fclose(file); } print_scores(); // 输出分数榜 } void sort_scores(struct Score scores[], int count) { for (int i = 0; i < count - 1; i++) { for (int j = i + 1; j < count; j++) { if (scores[i].score < scores[j].score) { // 按分数排序 struct Score temp = scores[i]; scores[i] = scores[j]; scores[j] = temp; } } } } ``` 代码中使用了文件操作将玩家的得分记录保存下来,并可以在程序中查看分数榜。游戏的主要逻辑在 `play_game()` 中实现,包括生成随机数、获取玩家输入、判断猜测结果等。排序算法使用的是简单的冒泡排序。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值