我寫的,從用戶接收一系列數字(< = 20),而最後一個「0」是指一系列的結束(不包括在序列存儲)的程序。 2個數組(x,y)大小爲20('0'的0-19 + 1)必須爲零,m表示Y數組中的器官數量。程序沒有做它應該 - Visual C
用戶必須輸入數字升序(它是確定4EX。1,2,2,3,7,8,...,0),並用「0」課程結束,如果不是,適當的錯誤消息將出現,程序將關閉。
我們可以肯定,用戶將保持輸入的< = 20個號碼。 Y數組將會是(如果X數組一切正常)X有序數組,但沒有重複數組。 'm'將是Y的器官數量,當然不包括'0'。
函數SIFT只能組織從main()打印的Y數組。
實施例:
如果用戶將在X存儲:1,1,2,3,5,5,5,6
在屏幕將是:M = 5 Y = 1, 2,3,5,6
我的代碼:
#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++;
}
}
}
對於一些未知的原因,對於任何一種合法輸入的我得到一個「的數字不正確的順序...」錯誤信息......
我會很樂意,如果有人可以修復它,所以它應能正常工作,因爲到現在爲止我所看到的一切是似乎是確定... ..
感謝:(...
你試過打印temp'和'curr'的'值是多少? – 2011-04-14 14:09:12
將來,請嘗試創建一個更具描述性的標題。有關示例,請參閱其他問題的標題。 – 2011-04-14 14:28:31