程序没有做它应该 - Ç [英] Program not doing what it should - C

查看:102
本文介绍了程序没有做它应该 - Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的,从用户接收一系列数字节目(小于= 20),而最后一个'0'是指一系列的结束(不包括在系列存储)。 2阵列(X,Y)尺寸为20(0-19 + 1为'0')必须为零,而m表示Y轴阵列器官的数量。

I wrote a program that receive from user a series of numbers (<=20) while the last '0' means end of series (not included in series storing). 2 arrays (x,y) size 20 (0-19 + 1 for the '0') must be zeros, and m means number of organs in Y array.

用户必须输入数字上升(这是确定4EX。1,2,2,3,7,8,...,0)和0课程结束,如果没有,相应的错误信息会出现,程序将自动关闭。

The user must enter numbers ascending (it is ok 4ex. 1,2,2,3,7,8,...,0) and end with a '0' of course, if not, appropriate error message will appear, and program will shut off.

我们可以肯定用户将保持在&lt;输入= 20个号码

We can be sure the user will keep the <=20 numbers of input.

Ÿ阵列将是(如果一切正常使用X数组)的X,但没有重复排序后的数组。 M将Y轴的器官数量排除'0'当然。

Y array will be (if everything went ok with X array) a sorted array of X but without duplicates. 'm' will be number of organs in Y exclude '0' of course.

功能SIFT只能组织Y数组从主()。

Function SIFT must only organize the Y array for being printed from main().

示例:

如果用户将在x存储:1,1,2,3,5,5,5,6

If user will store in X: 1,1,2,3,5,5,5,6

在屏幕将是:M = 5 Y = 1,2,3,5,6

On screen will be: m = 5 Y = 1,2,3,5,6

我的code:

#include <stdio.h>
#include <string.h>

void SIFT(int x_arr[ ], int y_arr[]);

int main ()
{
    int x[20] = {0} , y[20] = {0};
    int m=0,temp=0,curr=0,i=0,j=0;

    printf("Please enter your numbers now:\n\n");

    /*enter numbers one by one. if x[i+1] value < x[i] value, err msg.
      when user want to end the series he must enter '0' which means end of string (it wont       included in x[]) */
    while ( ( temp = getchar() ) != '0' )
    {
        if (temp >= curr)
        {
            x[i] = temp;
            curr = temp;
            i++;
        }
        else
        {
            printf("The numbers are not at the right order !\n\nProgram will now terminate...\n\n");
        }
    }

    SIFT(x,y);

    for (i=0 ; y[i]=='0' ; i++) /*strlen(y) without ('0')'s includes*/
        m++;

    /*Prints  m , y's organs*/
    printf("\n\nm = %d",m);
    printf("Y = ");
    while (y[j]!='0')
    {
        printf ("%d ,",y[j]);
        j++;
    }

return 0;
}

void SIFT(int x_arr[ ], int y_arr[])
{
    int i=0,j=0;

    while (x_arr[i] != '0')
    {
        if (x_arr[i] == x_arr[i+1]) /*if current val. equals next val. -> jump dbl at x_arr*/
        {
            y_arr[j] = x_arr[i];
            i+=2;
            j++;
        }
        else
        {
            y_arr[j]=x_arr[i];
            i++;
            j++;
        }
    }    

}

对于一些未知的原因,对于任何一种合法输入的我得到了这些数字是不是在正确的顺序......的错误消息...

For some unknown reason, for any kind of legal input I'm getting a "The numbers are not at the right order..." error message...

我会很乐意,如果有人可以修复它​​,所以它应该正常工作,因为到现在为止我所看到的一切是好像是OK ......

I will be more than happy if someone can fix it so it should work properly, because until now I can see everything is seems to be OK.....

感谢:( ...

推荐答案

的getchar()不会做你认为它。阅读其描述( http://pubs.opengroup.org/onlinepubs/9699919799/functions /getchar.html )。

getchar() does not do what you think it does. Read its description ( http://pubs.opengroup.org/onlinepubs/9699919799/functions/getchar.html ).

您可以尝试 scanf()的,而不是(的 http://pubs.opengroup.org/onlinepubs/9699919799/functions/scanf.html ),或更好,与fgets()的sscanf()

You can try scanf() instead ( http://pubs.opengroup.org/onlinepubs/9699919799/functions/scanf.html ), or better, fgets() and sscanf().

这篇关于程序没有做它应该 - Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆