64,647
社区成员
发帖
与我相关
我的任务
分享
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
using namespace std;
void randomletters(char word[], int i) {
word[i] = rand() % 26 + 'a';
return;
}
int numbers(char word[], char alpha) {
int cnt = 0;
int i = 0;
// word[200] 是随机值不一定等于\0
while (word[i] != '\0') {
if (word[i] == alpha) {
cnt++;
}
i++;
}
return cnt;
}
void MySort(char*word, char*sorti, char alpha, int*k) {
int i = 0;
while (word[i] != '\0') {
if (word[i] == alpha) {
// alpha = sorti[*k]; 谁赋值给谁?
sorti[*k] = alpha;
*k = *k + 1;
}
i++;
}
// sorti[*k] = '\0'; //只需要主函数最后一次赋值就可以了,不用每调用一次就赋值一次
return;
}
int main() {
srand(time(NULL));
malloc(201); // 写这个我看不懂啥意思,我就不改了
printf("Part_1(要求:随机生成 200个 小写字母)\n");
char word[201];
int i = 0;
while (i < 200) {
randomletters(word, i);
printf("%c", word[i]);
i++;
}
word[200] = '\0';
printf("\n");
printf("\nPart_2(计算最多出现的字母和次数)\n");
char alpha = 'a';
char maxletter = 'a';
int maxnumber = 0;
int cnt = 0;
i = 0;
while (i < 26) {
alpha = 'a' + i;
cnt = numbers(word, alpha);
if (cnt > maxnumber) {
maxnumber = cnt;
maxletter = alpha;
}
i++;
}
printf("letter %c is most frequently %i times.\n", maxletter, maxnumber);
printf("\nPart_3(计算每一个出现的字母和次数)\n");
i = 0; // 从i = 1 开始 a + 1 就变成b了
while (i < 26) {
alpha = 'a' + i;
cnt = numbers(word, alpha);
printf("letter %c comes %i times.\n", alpha, cnt);
// alpha = 'a' + i; 这个不要放在后面 先初始化变量再统计
i++;
}
printf("\nPart_4(把字母按顺序排列起来)\n");
//malloc(201);
char sorti[201];
int k = 0;
// i = 1 老问题
i = 0;
while (i < 26) {
alpha = 'a' + i;
MySort(word, sorti, alpha, &k);
// c有自定义的排序算法
//sort(word, word + 200);
i++;
}
//word[200] = '\0';
sorti[200] = '\0';
printf("(According to 'abc ...') The new alphabetical order is: %s\n", sorti);
printf("\nPart_5 (检查生成的字母个数是否与前面的一致(这里总数应该显示的是200))");
int total = 0;
i = 0;
//while (i <= 26) {
// total += cnt; // 这只是最后一个字符的数量, 相当于字母z的数量*26
// // 真要算的话重新加一遍就可以了
// i++;
//}
//i = 1;
while (i < 26) {
alpha = 'a' + i;
cnt = numbers(word, alpha);
total += cnt;
i++;
}
printf("\nTotal are %i letters:", total);
printf("\n\n");
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void Part1(char words[], int size) {
printf("Part_1(要求:随机生成 200个 小写字母)\n");
srand(time(NULL));
for (int i = 0; i < size; i++) {
words[i] = rand() % 20 + 'a';
printf("%c", words[i]);
}
printf("\n");
}
void init(char word[],int size,int count[]) {
for (int i = 0; i < size; i++)
count[word[i] - 'a']++;
}
void Part2(int count[]) {
printf("\nPart_2(计算最多出现的字母和次数)\n");
int max = count[0];
for (int i = 0; i < 26; i++) {
if (count[i] > max)
max = count[i];
}
printf("letter");
for (int i = 0; i < 26; i++) {
if (count[i] == max)
printf(" %c", 'a' + i);
}
printf(" is most frequently %d times.\n", max);
}
void Part3(int count[]) {
printf("\nPart_3(计算每一个出现的字母和次数)\n");
for (int i = 0; i < 26; i++)
printf("letter %c comes %i times.\n", 'a' + i, count[i]);
printf("\n");
}
void Part4(int count[]) {
printf("\nPart_4(把字母按顺序排列起来)\n");
for (int i = 0; i < 26; i++) {
for (int j = 0; j < count[i]; j++) {
printf("%c", 'a' + i);
}
}
printf("\n");
}
void Part5(int count[]) {
printf("\nPart_5 (检查生成的字母个数是否与前面的一致(这里总数应该显示的是200))");
int sum = 0;
for (int i = 0; i < 26; i++)
sum += count[i];
printf("\nTotal are %d letters:", sum);
printf("\n");
}
int main() {
char words[201];
int count[26] = { 0 };
Part1(words, 200);
init(words, 200, count);
Part2(count);
Part3(count);
Part4(count);
Part5(count);
system("pause");
return 0;
}