![](/img/trans.png)
[英]How to check for a binary to decimal program if the input is indeed a binary number (only 1s and 0s)?
[英]C program: Checking digits entered are 1s and 0s only using arrays
所以我試圖制作一個更小的簡單程序來解決問題。 為此,我試圖檢查以確保用戶輸入一個僅包含1和0的數字,然后我將使用此數字執行除法。 例如,數字11010或10111.現在我的問題是,如果我聲明一個整數來存儲這個數字(如下所示),那么就沒有辦法檢查所有數字是1還是0對嗎?
int userInput;
現在,我可以使用一個數組。 (我知道如何做到這一點,但這導致了我的第二個問題。) 像這樣:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main (int argc, char *argv[]){
int myArray[20];
int i, input, length;
printf("Please enter how long your number is: \n");
scanf("%d", &length);
for (i = 0; i < length; i++){
printf("Please enter digit %d of your array\n", i+1);
scanf("%d", &input);
assert ((input == 1) || (input ==0));
}
例如,對於第一個數字,用戶輸入“1”,用戶輸入“0”的第二個數字,然后用戶輸入“0”的第三個數字。 我需要“抓住”這個數字。 我如何獲取此數字“100”並對其執行算術運算。 我希望這是有道理的,如果不是主持人,請給我一個機會清理它。
編輯:許多人提出了模數方法。 但是我仍然想知道我是否可以用數組做到這一點。 這是創建一個整數變量,並設置為等於用戶在數組中輸入的每個元素。
你真的不需要一次獲得一位數。
很容易提取int的數字。 請注意,12345之類的數字實際上是5 * 10 ^ 0 + 4 * 10 ^ 1 + 3 * 10 ^ 2 + 2 * 10 ^ 3 + 1 * 10 ^ 4。
要獲得最低位數,您可以在除以10時使用數字的余數(即使用%運算符將其修改為10)。 所以,12345%10是5.要獲得下一個數字,你可以將數字除以10(得到1234)然后再將10除以 - 給你4.只要你的數字中有數字,就繼續這樣做(即數字> 0)。
#include <stdio.h>
int is_valid(int number) {
if (number == 0) return 1; // its a 0.
while (number != 0) {
int digit = number % 10;
if (digit != 1 && digit != 0) return 0;
number = number / 10;
}
return 1; // no other digits were found.
}
int main() {
int n;
scanf("%d", &n);
if (is_valid(n)) printf("valid\n");
else printf("not valid\n");
return 0;
}
這是另一個想法:
只需將數字再次寫入字符串即可。 然后迭代檢查每個字符的字符串。 這樣效率稍差,但理解/編碼更簡單。
#include <stdio.h>
int is_valid(int n) {
char buffer[20];
char *c;
sprintf(buffer, "%d", n);
for(c = buffer; *c != '\0'; c++) {
if (*c != '0' && *c != '1') return 0;
}
return 1;
}
int main() {
int n;
scanf("%d", &n);
if (is_valid(n)) printf("valid\n");
else printf("not valid\n");
return 0;
}
編輯:因為你在編輯中提到你對替代方法不感興趣,只需要一種方法來“抓住”數字,因為它們被輸入,我將這個添加到我的答案中。
保持變量初始設置為0.現在每個數字進入,(我假設用戶在較低的數字之前輸入更高的數字),我們將變量乘以10並將新數字添加到它。 因此,如果用戶輸入1,0,0,我們的變量最初是0,其1(0 * 10 + 1),然后是10(1 * 10 + 0),最后是100(10 * 10 + 0),這就是我們需要的。
我認為這可能更簡單:
bool is_zeros_and_ones(int n) {
for (; n != 0; n /= 10) {
int mod = n % 10;
if (0 != mod && 1 != mod) {
return false;
}
return true;
}
因此,您可以輸入整數並在沒有數組的情況下測試它。
您不需要數組來存儲所有數字。
保持除以10並取模數得到每個數字並保留一個標記,如果所有數字都是1或0則標記。
當然只適用於整數的最大字大小...
就像是:
char isBinary = 1;
int copyInput = +input;
while(copyInput && (isBinary=copyInput%10<=1)) copyInput/=10;
也許這樣的事情就足夠了
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main (int argc, char *argv[]){
int myArray[21];
printf("Please enter your number (max 20 digits)\n");
scanf("%s", myArray);
for (i = 0; i < strlen(myArray); i++){
assert ((myArray[i] == 1) || (myArray[i] == 0));
}
你可以通過確保程序始終處理整數來欺騙你的方式。 您的數組基本上保存基數為10的二進制表示。 為什么不使用它轉換為int
。 然后,您可以像對ints
操作一樣將所有操作應用於該數字。
這就是我的意思
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main (int argc, char *argv[]){
int i, input, length;
int num = 0; /* holds the actual number */
printf("Please enter how long your number is <= %d: \n", 8 * sizeof(num) - 1);
scanf("%d", &length);
for (i = 0; i < length; i++){
printf("Please enter digit %d of your array\n", i+1);
scanf("%d", &input);
assert ((input == 1) || (input ==0));
/* humans enter numbers L TO R */
/* set that bit if it is one */
num |= (input << (length - i - 1)) ;
}
printf("Your number in base 10 is %d\n",num);
/* Now you do normal multiplications, additions, divisions */
/* The only thing remaining now is to convert from base 10 to base 2. */
}
Sample run
Please enter how long your number is <= 31:
5
Please enter digit 1 of your array
1
Please enter digit 2 of your array
1
Please enter digit 3 of your array
1
Please enter digit 4 of your array
1
Please enter digit 5 of your array
0
Your number in base 10 is 30
此解決方案僅適用於問題的第二部分。 您可以將輸入作為字符並使用atoi
將它們轉換為整數。
char arrayOfNumbers[MAX_LENGTH];
char tmpChar;
int number;
for (i = 0; i < length; i++){
printf("Please enter digit %d of your array\n", i+1);
scanf("%c", &tmpChar);
assert ((tmpChar == '1') || (tmpChar == '0'));
arrayOfNumbers[i] = tmpChar;
}
/*make sure it is NULL terminated*/
arrayOfNumbers[length] = '\0';
number = atoi(arrayOfNumbers);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.