मैं दो बार नहीं, एक बार q वर्ण दर्ज करके प्रोग्राम से कैसे बाहर निकल सकता हूं?
मुझे एक बार फिर "q" दर्ज करने की आवश्यकता क्यों है?
मुझे लगता है कि अभिव्यक्ति || scanf("%c", &operation)
ने काम नहीं किया।
/*
* Name: Calculator.
* Description: Calculator for simple math operations.
*
* Compiler: Apple LLVM version 10.0.0 (clang-1000.11.45.5).
* Coding style: Google.
*/
#include <locale.h>
#include <math.h>
#include <stdio.h>
#define TRUE 1
#define EXIT 'q'
double add(double x, double y);
double subtract(double x, double y);
double multiply(double x, double y);
double divide(double x, double y);
double degree(double x, int y);
double sqrt(double x);
double mod(double x, double y);
double div(double x, double y);
int main(void) {
char *locale = setlocale(LC_ALL, "");
printf("Examples:\n\n");
printf("1 + 2\n");
printf("1 - 2\n");
printf("1 * 2\n");
printf("1 / 2\n");
printf("1 ^ 2\n");
printf("(sqrt): s 2\n");
printf("(mod): 1 m 2\n");
printf("(div): 1 d 2\n\n");
printf("Input for exit: \"q\"\n\n");
while (TRUE) {
double x, y;
char operation;
scanf("%lf %c %lf", &x, &operation, &y) ||
scanf("%c %lf", &operation, &x) || scanf("%c", &operation);
switch (operation) {
case ' ':
break;
case '\n':
break;
case '+':
printf("Result = %.2lf\n", add(x, y));
break;
case '-':
printf("Result = %.2lf\n", subtract(x, y));
break;
case '*':
printf("Result = %.2lf\n", multiply(x, y));
break;
case '/':
if (y != 0) {
printf("Result = %.2lf\n", divide(x, y));
} else {
printf("\nError!.\n");
}
break;
case '^':
printf("Result = %.2lf\n", degree(x, y));
break;
case 's':
printf("Result = %.2lf\n", sqrt(x));
break;
case 'm':
printf("Result = %.2lf\n", divide(x, y));
break;
case 'd':
printf("Result = %.2lf\n", divide(x, y));
break;
case EXIT:
printf("Input symbol \"%c\"\nExit...\n", EXIT);
return 0;
}
}
return 0;
}
double add(double x, double y) { return (x + y); }
double subtract(double x, double y) { return (x - y); }
double multiply(double x, double y) { return (x * y); }
double divide(double x, double y) { return (x / y); }
double degree(double x, int y) {
int response = 1;
while (y) {
if (y & 1) response *= x;
x *= x;
y >>= 1;
}
return response;
}
2 जवाब
आपके scanf
कॉल [शायद] एक दूसरे के साथ हस्तक्षेप करेंगे।
यदि पहला विफल हो जाता है, तो यह [शायद] दूसरों को बाधित करेगा (अर्थात पहले वाले ने पहले ही stdin
से डेटा खींच लिया है, इसलिए अन्य को कुछ भी दिखाई नहीं देगा)।
परिणामी बफ़र पर fgets
और sscanf
का उपयोग करने का एक निश्चित तरीका है:
do {
char buf[1000];
fgets(buf,sizeof(buf),stdin);
if (sscanf(buf,"%lf %c %lf", &x, &operation, &y) == 3)
break;
if (sscanf(buf,"%c %lf", &operation, &x) == 2)
break;
if (sscanf(buf,"%c", &operation) == 1)
break;
// error ...
} while (0);
मुझे लगता है कि नीचे दिए गए कोड स्निपेट को चलाने से आपके प्रश्न का उत्तर मिल जाएगा।
#include<stdio.h>
void main(){
double x,y;
char operation='n';
int i;
printf("Enter the variables\n");
i=scanf("%lf %c %lf",&y, &operation, &x);
printf("This is operation, %c and this is long float %lf, this is y %lf and i %d\n",operation,x,y, i);
i=scanf("%c %lf", &operation, &x);
printf("This is operation, %c and this is long float %lf, this is y %lf and i %d\n",operation,x,y, i);
i=scanf("%c", &operation);
printf("This is operation, %c and this is long float %lf, this is y %lf and i %d\n",operation,x,y, i);
}
यह वह आउटपुट है जो मुझे मिला है
Enter the variables
x
This is operation, n and this is long float 0.000000, this is y 0.000000 and i 0
x
This is operation, x and this is long float 0.000000, this is y 0.000000 and i 1
This is operation, x and this is long float 0.000000, this is y 0.000000 and i 1
क्या हो रहा है कि पहला स्कैनफ स्टेटमेंट आपके इनपुट 'क्यू' को पढ़ता है और बफर को छोड़ देता है क्योंकि यह ठीक से स्वरूपित नहीं होता है और 0 को वापस कर देता है। जो दूसरा स्कैनफ चलाने का कारण बनता है।
दूसरा स्कैनफ़ आपके 'क्यू' को पढ़ता है, इसे ऑपरेशन ऑब्जेक्ट में जोड़ता है, और '1' देता है जिससे तीसरा स्कैन नहीं चलता है।
मैं इस मुद्दे को हल करने का सबसे अच्छा तरीका सुझाऊंगा, यह इनपुट का एक मानक रूप रखना है। हमेशा पात्रों को पहले लें, और युगल बाद में।
यह भी देखें कि स्कैनफ क्या लौटाता है। यह ठीक से पार्स किए गए पहचानकर्ताओं की संख्या लौटाता है।