请教热心大神,程序还是有点问题的,请帮忙看一下哪边有问题,谢谢~

csdnPhoenixtree 2019-06-07 09:16:28
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void randomletters(char word[],int i){

word[i]=rand()%26+'a';

return;
}

int numbers(char word[],char alpha){
int cnt=0;
int i=0;

while(word[i]!='\0'){
if(word[i]==alpha){
cnt++;
}

i++;
}

return cnt;
}

void sort(char*word,char*sorti,char alpha,int*k){
int i=0;
while(word[i]!='\0'){

if(word[i]==alpha){
alpha=sorti[*k];
*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++;
}
printf("\n");



printf("\nPart_2(计算最多出现的字母和次数)\n");

word[201];

char alpha='a';
char maxletter='a';
int maxnumber=0;

int cnt=0;

i=1;
while(i<=26){
cnt=numbers(word,alpha);

if(cnt>maxnumber){
maxnumber=cnt;
maxletter=alpha;
}
alpha='a'+i;

i++;
}
printf("letter %c is most frequently %i times.\n",maxletter,maxnumber);


printf("\nPart_3(计算每一个出现的字母和次数)\n");
i=1;
while(i<=26){
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;
while(i<=26){

alpha='a'+i;
sort(word,sorti,alpha,&k);

i++;
}

sorti[200]='\0';

printf("(According to 'abc ...') The new alphabetical order is: %s\n",sorti);


printf("\nPart_5 (检查生成的字母个数是否与前面的一致(这里总数应该显示的是200))");

int total=0;
i=1;
while(i<=26){
total+=cnt;

i++;
}

printf("\nTotal are %i letters:",total);
printf("\n\n");

system("pause");
return 0;
}



之前有的修改方案会运行报错。
然后又尝试做一下,还是有问题的,各位热心大神能否帮忙稍微详细的看一看怎么修改比较好呢?
谢谢!
...全文
119 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdnPhoenixtree 2019-06-10
  • 打赏
  • 举报
回复
引用 7 楼 s_father 的回复:
#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;
}
谢谢您的注解和回复,很详细,非常感谢哦~
csdnPhoenixtree 2019-06-10
  • 打赏
  • 举报
回复
引用 5 楼 gouyanfen 的回复:
运行效果,上次给你改的你没有对比
可能运行环境有关吧,其实一直有报错的,不过仍然非常感谢您的注解和指导帮助的~
csdnPhoenixtree 2019-06-10
  • 打赏
  • 举报
回复
引用 2 楼 Italink 的回复:
可以参考

#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;
}
好的,学习了,谢谢哦~
s_father 2019-06-09
  • 打赏
  • 举报
回复
#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;
}
gouyanfen 2019-06-08
  • 打赏
  • 举报
回复
图片发错了
gouyanfen 2019-06-08
  • 打赏
  • 举报
回复
运行效果,上次给你改的你没有对比
gouyanfen 2019-06-08
  • 打赏
  • 举报
回复
#include<stdio.h> #include<stdlib.h> #include<time.h> void randomletters(char word[],int i){ word[i]=rand()%26+'a'; return; } int numbers(char word[],char alpha){ int cnt=0; int i=0; while(word[i]!='\0'){ if(word[i]==alpha){ cnt++; } i++; } return cnt; } void sort(char*word,char*sorti,char alpha,int&k){ int i=0; while(word[i]!='\0'){ if(alpha==word[i]){ 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++; } printf("\n"); printf("\nPart_2(计算最多出现的字母和次数)\n"); word[201]; char alpha='a'; char maxletter='a'; int maxnumber=0; int cnt=0; i=0; while(i<26){ cnt=numbers(word,alpha); if(cnt>maxnumber){ maxnumber=cnt; maxletter=alpha; } alpha='a'+i; i++; } printf("letter %c is most frequently %i times.\n",maxletter,maxnumber); printf("\nPart_3(计算每一个出现的字母和次数)\n"); i=0; while(i<26){ alpha='a'+i; cnt=numbers(word,alpha); printf("letter %c comes %i times.\n",alpha,cnt); i++; } printf("\nPart_4(把字母按顺序排列起来)\n"); //malloc(201); char sorti[201]; int k=0; i=0; while(i<26){ alpha='a'+i; sort(word,sorti,alpha,k); i++; } 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){ alpha='a'+i; cnt=numbers(word,alpha); total+=cnt; i++; } printf("\nTotal are %i letters:",total); printf("\n\n"); system("pause"); return 0; } 上次帮你改过了,怎么还有同样的问题
Italink 2019-06-07
  • 打赏
  • 举报
回复
Part1随机数%26,写错了
Italink 2019-06-07
  • 打赏
  • 举报
回复
可以参考

#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;
}
csdnPhoenixtree 2019-06-07
  • 打赏
  • 举报
回复

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧