我正在尝试制作一个二进制计算器,在输入q
时终止该程序。结果,我创建了一个名为quit
的变量,作为输入的标志q
。但是,当我为变量scanf
设置q
时,如果未按下q
,它似乎会将输入分开。例如,如果我输入110 + 110
,我会得到:
110 + 110 = 1000
好像在做10 + 110
。
输入必须全部在一行中。输入的格式始终为:
操作数操作符操作数
这就是为什么我有:
scanf(" %s %c %s",Binary1, &op, Binary2);
我的代码。但是,如果用户输入只是q
或Q
,则会终止该程序。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double convertDouble(char Bin []);
double convertBinary(double theAns);
double calculate(double num1,double num2,char op);
main(void) {
char op;
float num1,num2;
char quit;
double n1,n2,ans;
double binAns;
char Binary1[20];
char Binary2[20];
char Input[100];
char myChar;
int i = 0;
// quit = 'k';
while (quit != 'q'){
printf("Enter an expression using binary numbers or Q to quit: ");
// scanf("%s", &quit);
if(quit == 'q' ){
exit(EXIT_SUCCESS);
}else{
}
scanf(" %s %c %s",Binary1, &op, Binary2);
/* while(Binary1[i] > 49 || Binary2[i] > 49){
if(Binary1[i] >= 'q' || Binary1[i]<= 'Q' ){
exit(0);
quit = 'q';
}else{
printf("please enter binary: ");
scanf("%s %c %s\n",&Binary1, &op, &Binary2);
}
i++;
} */
// quit[0] = Binary1[0];
printf("quit = %c\n", quit);
printf("Binary1 =%s\n", Binary1);
printf("op =%c\n", op);
printf("Binary2 =%s\n", Binary2);
n1 = convertDouble(Binary1);
n2 = convertDouble(Binary2);
ans = calculate(n1,n2,op);
binAns = convertBinary(ans);
// printf("value of n1 = %f\n", n1);
// printf("value of n2 = %f\n", n2);
// printf("value of binAns = %f\n", binAns);
// printf("ans = %f", ans);
printf(" = %f\n",binAns);
} // end of while
printf("quit = %c\n", quit);
printf("Binary1 =%s\n", Binary1);
printf("op =%c\n", op);
printf("Binary2 =%s\n", Binary2);
printf("quit");
exit(EXIT_SUCCESS);
}
有关我的程序的更多信息:我将输入二进制文件作为字符串的原因是因为它需要将它们转换为double
。它在名为convertDouble
的不同函数中执行此操作,此函数未粘贴在此处。它计算函数计算中的答案(也未粘贴)。答案在convertBinary
函数中转换为二进制,然后显示。
如何运行的示例
Enter an expression using binary numbers or Q to quit: 1101.001 * 101.1101
= 1001100.0100101
Enter an expression using binary numbers or Q to quit: 0.0000000001 * 0.0000000001
= 0.00000000000000000001
Enter an expression using binary numbers or Q to quit: 0.111 * 1000.0
= 111.0
Enter an expression using binary numbers or Q to quit: 11.0 * 11.0
= 1001.0
Enter an expression using binary numbers or Q to quit: 11.11 + 11.11
= 111.1
Enter an expression using binary numbers or Q to quit: 101.1 / 10.0
= 10.11
Enter an expression using binary numbers or Q to quit: 1001.11 - 11.01
= 110.1
Enter an expression using binary numbers or Q to quit: q
答案 0 :(得分:3)
保持代码尽可能简单,保留功能所需的最小变量和函数数。
仔细考虑该计划的每个要求,将它们视为个别小问题......
喜欢这个......
#include <stdio.h>
#include <stdlib.h>
#define BUFLEN 32
double convertToDouble(const char *chars) {
return strtod(chars, NULL);
}
double calculateAnswer(double left, char operator, double right) {
switch (operator) {
case '<': return left < right; break;
case '>': return left > right; break;
case '=': return left == right; break;
case '*': return left * right; break;
case '/': return left / right; break;
}
/* do something */
return -1;
}
int main(int argc, char *argv[]) {
char buffer[BUFLEN*2];
printf("Enter an expression using binary numbers or Q to quit: ");
while (fgets(buffer, BUFLEN, stdin)) {
char left[BUFLEN] = {0},
right[BUFLEN] = {0},
operator = 0;
if(sscanf(buffer, "%s %c %s\n", left, &operator, right)) {
if (!operator) {
if (left[0] == 'q' || left[0] == 'Q') {
printf("Bye!\n");
break;
}
}
printf("%s %c %s = %f\n",
left, operator, right,
calculateAnswer(
convertToDouble(left),
operator,
convertToDouble(right)));
} else {
/* do something */
}
printf("Enter an expression using binary numbers or Q to quit: ");
}
return 0;
}
请注意,此代码并不正确,它是您描述的要求的近似值,而不需要付出太多努力。
更重要的是,它很容易理解。
答案 1 :(得分:0)
您可以阅读文本行(函数getline
或gets_s
)并解析它(函数sscanf
)。尝试这样的事情:
char buffer[128];
int nbParsedItems;
printf("Enter an expression using binary numbers or Q to quit: ");
gets_s(buffer, 127);
if (buffer[0] == 'q' || buffer[0] == 'Q')
return;
nbParsedItems = sscanf(buffer, " %s %c %s", Binary1, &op, Binary2);
if (nbParsedItems == 3) /* in other cases you should ask user to enter request again */
{
/* calculate */
}
答案 2 :(得分:0)
Ctrl + C是终止控制台程序的标准方法。但是如果你想使用&#39; q&#39;作为终止信号,您可以执行以下操作:
char line[128];
float a, b;
char op;
while(true) {
printf("Enter an expression using binary numbers or q to quit: ");
if( gets(line) ) {
if( strcmp(line, "q") == 0 || strcmp(line, "Q") == 0 ) {
printf("See you later");
break;
}
if( sscanf(line, "%f %c %f", &a, &op, &b) != 3 ) {
printf("Error: wrong expression. Please try again");
continue;
}
...
}
}