欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于C語言編寫簡易的英文統(tǒng)計和加密系統(tǒng)

 更新時間:2023年05月05日 16:22:58   作者:微涼秋意  
這篇文章主要介紹如何基于C語言編寫一個簡易的英文統(tǒng)計和加密系統(tǒng),實際上就是對字符數(shù)組的基本操作的各種使用,感興趣的可以了解一下

前言

耗時一天一夜寫了一個簡易的《英文統(tǒng)計和加密系統(tǒng)》,實際上就是對字符數(shù)組的基本操作的各種使用,其中也牽扯到簡單的讀寫文件,把結構體存入文本文件等知識??傮w來說,只要在編寫前搭建好一個思維框架,逐個實現(xiàn)功能就輕松的多了。

部分功能、開發(fā)環(huán)境與項目結構

部分功能展示:

開發(fā)環(huán)境:

我使用的開發(fā)環(huán)境是 Visual Stduio 2022(2019版本應該更好)。

項目結構:

包含兩個頭文件和對應的源文件, main.c,以及三個文本文件:

主函數(shù)設計

// 英文統(tǒng)計和加密系統(tǒng)
// statistics 統(tǒng)計
// encryption 加密(結構體相關功能也在此處)
#include"statistics.h"
#include"encryption.h"
int main() {
	char buf[BUFSIZE];
	memset(buf, '0', sizeof(buf)); // 給字符數(shù)組初始化為 '0'
	showInfo(buf);
	printf("當前字母數(shù):%d\n",countCharacter(buf));
	printf("當前單詞數(shù):%d\n",countWord(buf));
	printf("第12個字母為:");
	show_i(buf, 12);
	printf("與字母 'b'相比,小于,等于,大于的字母個數(shù)分別為:");
	classify_c(buf, 'b');
	int x = count_appear(buf, 'y');
	printf("字母y(不區(qū)分大小寫)出現(xiàn)的次數(shù)為:%d\n", x);
	printf("所有y出現(xiàn)的下標為:");
	int* A = (int*)malloc(sizeof(int) * x +1);
	int *n = location_index(buf, A, 'y');
	for (int i = 0; i < x; i++) {
		printf("%3d", n[i]);
	}
	printf("\n");
	encryption(buf, 3);
	/*puts(buf);*/
	printf("將26個英文字母打包成一個結構體,附加對應次數(shù)\n");
	alphabet chars;
	Initialize(&chars);
	printf("計算字母出現(xiàn)次數(shù)并存入文本文件中:\n");
	count_letters(buf, &chars);
	encrypt en = maxTimes(chars);
	encrypt minEn = minTimes(chars);
	printf("出現(xiàn)最多的字母為:%c,對應次數(shù)為:%d\n", en.data, en.num);
	printf("出現(xiàn)最少的字母為:%c,對應次數(shù)為:%d\n", minEn.data, minEn.num);
	printf("字母平均出現(xiàn)次數(shù):%.2f\n", averageNum(chars));
	int k = 8;
	char* list = list_k(chars,k);
	printf("前%d名字母序列為:", k);
	for (int i = 0; i < k; i++) {
		printf("%2c", list[i]);
	}
	select_sort(chars);
	//setText(&chars); // 需要更新結構體文本文件時使用
}

主函數(shù)完全可以使用 循環(huán)+switch-case來做一個菜單,重復使用功能。

實現(xiàn)效果已經(jīng)在上文中展示,后序不再展示。

statistics 頭文件以及源文件

statistics.h

#pragma once // 避免重復包含頭文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUFSIZE 512
// 獲取原文長度
int get_length(char* buf);
// 獲取文本的原文內(nèi)容
void getText(char *buf);
// 得到原文內(nèi)容
void showInfo(char *buf);
// 統(tǒng)計原文字母個數(shù)
int countCharacter(char* buf);
// 統(tǒng)計原文單詞個數(shù)
int countWord(char* buf);
// 顯示原文第i個單詞
void show_i(char* buf,int i);
// 根據(jù)字符c的大小,分為三部分
void classify_c(char* buf, char c);
// 定位值為c的下標
int* location_index(char* buf, int* index,char c);
// 字符c的出現(xiàn)次數(shù)
int count_appear(char* buf, char c);

statistics.c

#include"statistics.h"
// 獲取原文有效長度
int get_length(char* buf) {
	int len = 0;
	for (int i = 0; i < BUFSIZE && buf[i] != '0'; i++) {
		len++;
	}
	return len;
}
//用子函數(shù)實現(xiàn)從文本文件讀取原文的操作
void getText(char *buf) {
	FILE* file;
	file = fopen("myfile.txt", "r");
	if (file == NULL) {
		printf("打開文件失敗!");
		exit(-1);
	}
	char arr[BUFSIZE];
	memset(arr, '0', sizeof(arr));
	fgets(arr, sizeof(arr), file);
	fclose(file);
	strcpy(buf, arr);
}
//1.定義一字符數(shù)組word,用戶輸入原文或從一文本文件讀入(調(diào)用子函數(shù))原文(一段英文),
// 輸出顯示該原文,以及其總長度。
void showInfo(char* buf) {
	printf("1.輸入原文:\n");
	printf("2.讀取原文\n");
	int choice = 0;
	scanf("%d", &choice);
	rewind(stdin); // 清空緩沖區(qū)
	if (choice == 1) {
		char arr[BUFSIZE];
		memset(arr, '0', sizeof(arr));
		fgets(arr, sizeof(arr), stdin);
		strcpy(buf, arr);
	}
	else if (choice == 2) {
		getText(buf);
	}
	else {
		printf("請正確選擇獲取原文方式!"); exit(-1);
	}
	int sum = get_length(buf);
	if (choice==2) printf("總長度為:%d\n",  --sum); // 去掉自帶的 .
	else printf("總長度為:%d\n",  sum-2); // fgets會多讀一個字符
	printf("原文:%s\n", buf);
}
//3.用子函數(shù)實現(xiàn)統(tǒng)計原文中的字母個數(shù)。
int countCharacter(char* buf) {
	int i;
	int count = 0;
	for (i = 0; i < BUFSIZE&&buf[i]!= '0'; i++) {
		if (buf[i] >= 'a' && buf[i] <= 'z' || buf[i] >= 'A' && buf[i] <= 'Z') {
			count++;
		}
	}
	return count;
}
//4.用子函數(shù)實現(xiàn)統(tǒng)計原文中的單詞個數(shù)。
int countWord(char* buf) {
	int i;
	int count = 0;
	for (i = 1; i < BUFSIZE&&buf[i]!='0'; i++) {
		if (buf[i] == ' ' &&(buf[i - 1] >= 'a' && buf[i - 1] <= 'z' || buf[i - 1] >= 'A' && buf[i - 1] <= 'Z'))
			count++;
	}
	if (buf[i - 1] >= 'a' && buf[i - 1] <= 'z' || buf[i - 1] >= 'A' && buf[i - 1] <= 'Z')
		count++;
	return count;
}
//5.定義一子函數(shù),輸入任一下標值i,檢查i的輸入合法性,
// 合法輸出顯示word的第i個字母,否則輸出“輸入i不合法”。
void show_i(char* buf,int i) {
	int len = countCharacter(buf);
	if (i<1 || i>len) {
		printf("輸入i不合法\n");
		exit(-1);
	}
	int index = 0; // 記錄當前字母位序
	for (int j = 0; j < BUFSIZE && buf[j] != '0'; j++) {
		if (buf[j] >= 'a' && buf[j] <= 'z' || buf[j] >= 'A' && buf[j] <= 'Z') {
			++index;
			if (index == i) {
				printf("%c\n", buf[j]);
				return;
			}
		}
	}
}
//6.定義一子函數(shù),輸入一字母c, 分別輸出word中大于字母c的個數(shù)和小于字母c的個數(shù),
// 以及等于字母c的個數(shù)。
void classify_c(char* buf, char c) {
	char arr[BUFSIZE];
	memset(arr, '0', sizeof(arr));
	int index = 0; // 記錄當前字母位序
	for (int j = 0; j < BUFSIZE && buf[j] != '0'; j++) {
		if (buf[j] >= 'a' && buf[j] <= 'z' || buf[j] >= 'A' && buf[j] <= 'Z') {
			arr[index++] = buf[j];
		}
	}
	// 冒泡排序(升序)
	for (int i = 0; i < index; i++) {
		char flag = 'f';// 標志位
		for (int j = index-1; j > i; j--) {
			if (arr[j] < arr[j - 1]) {
				flag = 't';
				char ch = arr[j];
				arr[j]=arr[j-1];
				arr[j - 1] = ch;
			}
		}
		if (flag == 'f') break;
	}
	int l, h, e,i;
	for (l = 0, h = 0, e = 0, i = 0; i < index; i++) {
		if (arr[i] < c) l++;
		else if (arr[i] == c) e++;
		else break;
	}
	h = index - l - e;
	printf("little:%d equal:%d high:%d\n", l, e, h);
}
//7.定義一子函數(shù),實現(xiàn)字符查找功能:判定該字母是否存在,
// 是則返回每一個出現(xiàn)位置的下標(或者首次出現(xiàn)的下標位置),否則返回 - 1。
int* location_index(char* buf, int *index,char c) {
	int i;
	int in = 0;
	char flag = 'f';// 如果最終還是f,說明找不到該字母
	for (i = 0; i < BUFSIZE && buf[i] != '0'; i++) {
		if (buf[i] == c||c == buf[i]+32||c==buf[i]-32) index[in++] = i;
		flag = 't';
	}
	if (flag == 'f') return -1;
	return index;
}
//8.定義一子函數(shù),實現(xiàn)輸入一字母,統(tǒng)計原文中該字母的出現(xiàn)次數(shù)(字母不區(qū)分大小寫, 實現(xiàn)合并計數(shù))
// 并返回。函數(shù)中需要有對輸入的字母是否輸入異常判定和捕獲操作,并在主程序中進行輸入錯誤提示。
int count_appear(char* buf, char c) {
	if (!(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')) {
		printf("當前字符為%c,請輸入正確字母\n", c);
		exit(-1);
	}
	int i;
	int count = 0;
	for (i = 0; i < BUFSIZE && buf[i] != '0'; i++) {
		if (c == buf[i] || c == buf[i] + 32 || c == buf - 32) count++;
	}
	return count;
 }

encryption 頭文件以及源文件

encryption.h

#pragma once
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
// 字母以及出現(xiàn)次數(shù)的結構體
typedef struct {
	char data; // 顯示字母
	int num; // 出現(xiàn)次數(shù)
}encrypt;
typedef struct {
	encrypt letters[26];
}alphabet;
// 初始化字母表
void Initialize(alphabet *chars);
// 計算各個字母的出現(xiàn)次數(shù)
void count_letters(char* buf, alphabet* chars);
// 存入結構體文本文件
void setText(alphabet *chars);
// 返回出現(xiàn)次數(shù)最多的字母
encrypt maxTimes(alphabet chars);
// 返回出現(xiàn)次數(shù)最少的字母
encrypt minTimes(alphabet chars);
// 計算字母出現(xiàn)的平均次數(shù)
float averageNum(alphabet chars);
// 出現(xiàn)前k名的字母序列
char* list_k(alphabet chars, int k);
// 選擇升序或者降序排序
void select_sort(alphabet chars);
// 加密算法,根據(jù)n的位置循環(huán)后移
void encryption(char* buf, int n);
// 存入加密的文本文件
void setEncryptText(char* buff);

encryption.c

#include"encryption.h"
#include"statistics.h"
//9.將26個英文字母和其對應的出現(xiàn)次數(shù)打包定義為一個結構體類型來存儲。
// 初始化結構體數(shù)組
void Initialize(alphabet* chars) {
	int k = 0;
	char c = 'a';
	for (int i = 0; i < 26; i++) {
		chars->letters[i].num = k;
		chars->letters[i].data = c + i;
	}
}
//10.設計實現(xiàn)一新的子函數(shù),針對讀入的原文,統(tǒng)計出26個英文字母在單詞中出現(xiàn)的次數(shù),
//并統(tǒng)一存儲到9定義的結構體數(shù)組中,然后完成11到16的操作。
void count_letters(char* buf, alphabet* chars) {
	printf("1.輸入原文:\n");
	printf("2.讀取原文\n");
	int choice = 0;
	int sum = 0;
	scanf("%d", &choice);
	rewind(stdin); // 清空緩沖區(qū)
	if (choice == 1) {
		char arr[BUFSIZE];
		memset(arr, '0', sizeof(arr));
		fgets(arr, sizeof(arr), stdin);
		strcpy(buf, arr);
	}
	else if (choice == 2) {
		getText(buf);
	}
	else {
		printf("請正確選擇獲取原文方式!"); exit(-1);
	}
	char arr[BUFSIZE]; // 最終轉化為全部字母
	memset(arr, '0', sizeof(arr));
	int index = 0; // 記錄當前字母位序
	for (int j = 0; j < BUFSIZE && buf[j] != '0'; j++) {
		if (buf[j] >= 'a' && buf[j] <= 'z' || buf[j] >= 'A' && buf[j] <= 'Z') {
			arr[index++] = buf[j];
		}
	}
	for (int i = 0; i < index; i++) {
		for (int j = 0; j < 26; j++) {
			if (arr[i] == chars->letters[j].data || arr[i] + 32 == chars->letters[j].data)
				chars->letters[j].num++;
		}
	}
}
//11.將結構體數(shù)組結果存儲到一文本文件。
void setText(alphabet* chars) {
	FILE* fp;
	fopen_s(&fp, "struct.txt", "wt+");    //打開文件
	for (int i = 0; i < 26; i++)            //將N條信息存儲進去
	{
		fprintf(fp, "%d %d\n", chars->letters[i].data, chars->letters[i].num);
	}
	fclose(fp);    //關閉文件
	//encrypt buff[26];
	//FILE* fpp;
	//fopen_s(&fpp, "struct.txt", "rb");
	//fread(buff, sizeof(encrypt), 26, fpp); // 將N條消息全部從文件中讀取出來
	//fclose(fpp);
}
//12.設計一子函數(shù):返回出現(xiàn)次數(shù)最多的字母和對應次數(shù)。
encrypt maxTimes(alphabet chars) {
	int max = chars.letters[0].num;
	int i, index;
	for (i = 1, index = 0; i < 26; i++) {
		if (max < chars.letters[i].num) {
			max = chars.letters[i].num;
			index = i;
		}
	}
	return chars.letters[index];
}
//13.設計一子函數(shù):返回出現(xiàn)次數(shù)最少的字母和對應次數(shù)。
encrypt minTimes(alphabet chars) {
	int min = chars.letters[0].num;
	int i, index;
	for (i = 1, index = 0; i < 26; i++) {
		if (min > chars.letters[i].num) {
			min = chars.letters[i].num;
			index = i;
		}
	}
	return chars.letters[index];
}
//14.設計一子函數(shù):返回26個字母的平均出現(xiàn)次數(shù)。
float averageNum(alphabet chars) {
	float avg = 0.0;
	for (int i = 0; i < 26; i++) {
		avg += chars.letters[i].num;
	}
	avg /= 26;
	return avg;
}
//15. 設計一子函數(shù):輸入數(shù)字k,返回出現(xiàn)次數(shù)前k名的字母序列
char* list_k(alphabet chars, int k) {
	if (k < 1 || k>26) exit(-1);
	char* list = (char*)malloc(sizeof(char) * k);
	// 冒泡排序
	for (int i = 0; i < 26; i++) {
		char flag = 'f';
		for (int j = 25; j > i; j--) {
			if (chars.letters[j].num > chars.letters[j - 1].num) {
				flag = 't';
				encrypt temp = chars.letters[j];
				chars.letters[j] = chars.letters[j - 1];
				chars.letters[j - 1] = temp;
			}
		}
		if (flag == 'f') break;
	}
	for (int i = 0; i < k; i++) {
		list[i] = chars.letters[i].data;
	}
	return list;
}
//16.定義一排序子函數(shù),實現(xiàn)對結構體數(shù)組結果按照出現(xiàn)次數(shù)進行升序或降序排列
//(由用戶在運行時選擇排序方式), 輸出排序結果以及對應的字母。
void select_sort(alphabet chars) {
	printf("\n1.升序排列\(zhòng)n2.降序排列\(zhòng)n");
	int choice = 0;
	scanf("%d", &choice);
	if (choice == 2) {
		for (int i = 0; i < 26; i++) {
			char flag = 'f';
			for (int j = 25; j > i; j--) {
				if (chars.letters[j].num > chars.letters[j - 1].num) {
					flag = 't';
					encrypt temp = chars.letters[j];
					chars.letters[j] = chars.letters[j - 1];
					chars.letters[j - 1] = temp;
				}
			}
			if (flag == 'f') break;
		}
	}
	else if (choice == 1)
	{
		for (int i = 0; i < 26; i++) {
			char flag = 'f';
			for (int j = 25; j > i; j--) {
				if (chars.letters[j].num < chars.letters[j - 1].num) {
					flag = 't';
					encrypt temp = chars.letters[j];
					chars.letters[j] = chars.letters[j - 1];
					chars.letters[j - 1] = temp;
				}
			}
			if (flag == 'f') break;
		}
	}
	else {
		printf("請正確選擇排序規(guī)則!");
		exit(-1);
	}
	printf("排序結果為:");
	for (int i = 0; i < 26; i++) {
		printf("%2c", chars.letters[i].data);
	}
}
//17.用子函數(shù)實現(xiàn)將讀取的原文按規(guī)則加密后存入另一字符數(shù)組中。
//電文加密 : 問題描述:為使電文保密,常按一定規(guī)律將其轉換成密文后發(fā)送,
//收報人再按約定的規(guī)律將其譯回原文。設定加密規(guī)則為:
//每個字母變成其后的第n(n由用戶輸入指定)個字母,如A變成其后第n個字母….。
//說明 : 只對原文中的英文字母加密, 其他非英文字母要原樣保留。
void encryption(char* buf, int n) {
	n %= 26; // 確保英文字母加密后還是英文字母
	int i;
	int len = get_length(buf);
	char* arr = (char*)malloc(len * sizeof(char));
	memset(arr, '0', sizeof(arr));// 初始化
	for (i = 0; i < len; i++) {
		if (buf[i] >= 'a' && buf[i] <= 'z' || buf[i] >= 'A' && buf[i] <= 'Z') {
			if (buf[i] + n > 'z') {
				arr[i] = buf[i] + n - 26;
			}
			else if (buf[i] <= 'Z' && buf[i] + n > 'Z') {
				arr[i] = buf[i] + n - 'Z' + 'A'-1;
			}
			else {
				arr[i] = buf[i] + n;
			}
		}
		else {
			arr[i] = buf[i];
		}
	}
	printf("加密后:%s\n", arr);
	setEncryptText(arr);
}
//18.用子函數(shù)實現(xiàn)將密文存入密文文本文件中。
void setEncryptText(char* buff) {
	FILE* fp;
	fp = fopen("encryption.txt", "wt+");
	if (fp == NULL) {
		printf("打開文件失敗!");
		exit(-1);
	}
	fputs(buff, fp);
	fclose(fp);
}

myfile.txt文件中可自行輸入密文(26個英文字母大小寫,各種符號,數(shù)字等等);

struct.txt文件有26行,對應著原文中26個英文字母出現(xiàn)的次數(shù)以及ASCII碼值;

encryption.txt文件是myfile.txt文件中的字母通過循環(huán)后移 n 位得到,其中的難點在于大寫字母后移超過Z時的處理方法,這點大家可以在encryption.c文件中的encryption函數(shù)中獲得靈感。

到此這篇關于基于C語言編寫簡易的英文統(tǒng)計和加密系統(tǒng)的文章就介紹到這了,更多相關C語言英文統(tǒng)計和加密系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 看圖深入理解單鏈表的反轉

    看圖深入理解單鏈表的反轉

    今天遇到單向鏈表的反轉的問題,于是靜下心來好好想了一番。下面這篇文章主要給大家介紹了關于單鏈表反轉的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-02-02
  • C++11-20 常量表達式的使用

    C++11-20 常量表達式的使用

    本文主要介紹了C++11-20常量表達式,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • opencv實現(xiàn)顏色檢測

    opencv實現(xiàn)顏色檢測

    這篇文章主要為大家詳細介紹了opencv實現(xiàn)顏色檢測,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • C語言操作符基礎知識詳解

    C語言操作符基礎知識詳解

    這篇文章主要以圖文結合的方式為大家詳細介紹了C語言位運算基礎知識,感興趣的小伙伴們可以參考一下,希望能給你帶來幫助
    2021-10-10
  • C++實現(xiàn)重載矩陣的部分運算符

    C++實現(xiàn)重載矩陣的部分運算符

    這篇文章主要為大家詳細介紹了如何利用C++實現(xiàn)重載矩陣的部分運算符,文中的示例代碼講解詳細,對我們學習C++有一定幫助,需要的可以參考一下
    2022-10-10
  • 基于Matlab實現(xiàn)多目標粘液霉菌算法的示例代碼

    基于Matlab實現(xiàn)多目標粘液霉菌算法的示例代碼

    多目標粘液霉菌算法(MOSMA),這是最近開發(fā)的粘液霉菌算法(SMA)的多目標變體,用于處理工業(yè)中的多目標優(yōu)化問題。本文將用Matlab實現(xiàn)這一算法,需要的可以參考一下
    2022-05-05
  • C 二分查找 遞歸與非遞歸的實現(xiàn)代碼

    C 二分查找 遞歸與非遞歸的實現(xiàn)代碼

    C 二分查找 遞歸與非遞歸的實現(xiàn)代碼,需要的朋友可以參考一下
    2013-03-03
  • atoi和itoa函數(shù)的實現(xiàn)方法

    atoi和itoa函數(shù)的實現(xiàn)方法

    本文介紹了,atoi和itoa函數(shù)的實現(xiàn)方法,需要的朋友可以參考一下
    2013-03-03
  • C++使用函數(shù)的一些高級操作指南

    C++使用函數(shù)的一些高級操作指南

    C++中函數(shù)調(diào)用的方法與C語言并無區(qū)別,依舊是在調(diào)用方函數(shù)中執(zhí)行函數(shù)調(diào)用語句來實現(xiàn)函數(shù)調(diào)用,下面這篇文章主要給大家介紹了關于C++使用函數(shù)的一些高級操作,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2022-12-12
  • VS2022中使用Copilot的圖文教程

    VS2022中使用Copilot的圖文教程

    大家都知道Copilot可以自動幫助寫代碼,那么這個工具是如果使用的呢?很多朋友不是很清楚,今天小編給大家分享一篇教程關于VS2022中使用Copilot的圖文教程,感興趣的朋友一起看看吧
    2022-04-04

最新評論