【发布时间】:2016-11-16 11:23:56
【问题描述】:
我有问题。我需要 tool.c 文件中的函数来仅循环所需的次数。就像它在循环整个输入后停止一样。我的老师说我应该通过第二个参数,但是下课了,我不知道它应该是什么样子。
main.c
#include <stdio.h>
#include <stdlib.h>
#include "tools.h"
int main(int argc, char *argv[]) {
int count[256] = { 0 };
int c;
while ( (c=getchar())!=EOF ){
count[c]++;
}
switch (argc > 1 && argv[1][1]) {
case 'm': case 'M':
mostOften(count);
break;
case 'l': case 'L':
leastOften(count);
break;
default:
mostOften(count);
break;
}
return 0;
}
tools.c
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "tools.h"
void mostOften(int *s) {
int j, a = 0;
int max=0, cha;
for(j=32; j<126; j++){
if(s[j]>max) {
max=s[j];
cha=j;
}
a++;
}
printf("char %c: %d times\n", cha, max);
}
void leastOften(int *s) {
int j, a = 0;
int min=INT_MAX, cha;
for(j=32; j<126; j++){
if(s[j] && s[j]<=min) {
min=s[j];
cha=j;
}
a++;
}
printf("char %c: %d times\n", cha, min);
}
例如如果我输入
段落
我希望它只循环9次,基本上我需要设置一些if语句来停止循环
【问题讨论】:
-
请将
case 'm': case 'M':分开放置。 -
switch (argc > 1 && argv[1][1]) {应该是if(argc > 1) switch (argv[1][1]) { -
请编辑您的问题并向我们展示输入和预期输出的示例。
-
@MichaelWalz 也许是这样的。
-
@zimek-atomek 你期望什么输出?