c - getchar() 用于从输入中排除字符

标签 c while-loop max min getchar

这就是我正在尝试做的事情: 我的老师给了我一个作业,要求我接收来自用户的输入,例如“1,2,3,4,-3,17,-9,0,5,-8,10”,并且在用户按下回车键后,程序应该忽略“,”符号并打印正数和负数(每个)的最大值和最小值。 现在他说我们应该使用 getchar() 来执行此操作,但我不明白如何...我知道这个函数用于从用户读取单个字符(而不是使用 putchar() 打印它)而不是用于忽略字符,所以我想了解如何使用它在 while 循环中执行此类操作。

因此,总结一下,我的问题是关于有选择地读取输入(其他事情我将自己管理,尽管如果有人在代码中看到某些内容并且可以给出提示,我会很高兴,但我希望了解 getchar() 函数做与我读到的设计目的相反的事情),我已经编写和删除了代码数千次,但无法让它工作......这就是它现在的样子(并且它不起作用):

#include <stdio.h>
#include <stdlib.h>

int main(){

char enter = "\n";
int list_of_nums[100];
char ch1[100];
int neg_num[100];
int pos_num[100];

printf("please enter numbers separated by ',': \n");


while (getchar()!= enter) {
    scanf("%d" ,&list_of_nums;
    if (list_of_nums >= 0){
        getchar();
       pos_num.append(list_of_nums);
    }
    else{
                neg_num.append(list_of_nums);

        }
}



 int max_pos = max.pos_num[];
 int min_pos = min.pos_num[];
 int max_neg = max.neg_num[];
 int min_pos = min.neg_num[];

 printf("the maximum number of all the positives is: %d \n" ,max_pos);
 printf("the minimum number of all the positives is: %d \n" ,min_pos);
 printf("the maximum number of all the negatives is: %d \n" ,max_neg);
 printf("the minimum number of all the negatives is: %d \n" ,min_neg);


 return 0;



}

最佳答案

根据第一个输入选择下一个输入中发生的情况。这是负数的代码。只需添加两位数的情况并优化即可:

#include <stdio.h>
#include <stdlib.h>
#define MAX_NUMBERS 100

int main(void){
    int list_of_nums[MAX_NUMBERS];
    int pos_nums[MAX_NUMBERS];
    int neg_nums[MAX_NUMBERS];
    int size = 0, size_pos = 0, size_neg = 0, i, max_pos, min_pos, max_neg, min_neg, neg_flag = 0;
    char input;

    printf("Enter numbers separated by ',':\n");

    do {
        input = getchar();
        if(input == '-') {
            neg_flag = 1;   //for negative numbers
            input = getchar();
        }
        if(input >= '0' && input <= '9') {
            if(neg_flag) {
                list_of_nums[size] = -(input - 48); //FROM ASCII CHARACTER CODE
            } else {
                list_of_nums[size] = input - 48;
            }
            neg_flag = 0;
            size++;
        } else {
            neg_flag = 0;
        }
    } while(input != '\n');

    for(i = 0; i < size; i++) {
        printf("%d ", list_of_nums[i]);
        if(list_of_nums[i] >= 0) {
            pos_nums[size_pos] = list_of_nums[i];
            size_pos++;
        }
        else {
            neg_nums[size_neg] = list_of_nums[i];
            size_neg++;
        };
    }
    printf("\n");

    max_pos = pos_nums[0];
    min_pos = pos_nums[0];
    for(i = 1; i < size_pos; i++) {
        if(max_pos < pos_nums[i]) max_pos = pos_nums[i];
        if(min_pos > pos_nums[i]) min_pos = pos_nums[i];
    }

    max_neg = neg_nums[0];
    min_neg = neg_nums[0];
    for(i = 1; i < size_neg; i++) {
        if(max_neg < neg_nums[i]) max_neg = neg_nums[i];
        if(min_neg > neg_nums[i]) min_neg = neg_nums[i];
    }

    printf("The maximum number of all the positives is: %d\n", max_pos);
    printf("The minimum number of all the positives is: %d\n", min_pos);
    printf("The maximum number of all the negatives is: %d\n", max_neg);
    printf("The minimum number of all the negatives is: %d\n", min_neg);

    return 0;
}

P.S. For future, don't post "need help with homework" questions but try to go as far as possible in solving the task. That way you'll be able to present more researched question.

关于c - getchar() 用于从输入中排除字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53919282/

相关文章:

java - 使用扫描仪添加两个文件中的值不起作用

php - for & while循环条件比较

css - CSS 过渡持续时间的最大值是多少?

c - 优化后代码如何工作?

c - 全局char[]变量,如何在其他文件中声明它?

c - 在 C 中使用指针确定数据类型大小

java - 使用java编写两个方法min和max来查找链表中的最大值和最小值,但输入列表是整数数组

c - union 与 void 指针

c - scanf函数测试

VBA查找最大值,不使用MAX函数并打印相应的单元格