#include <stdio.h>
int main()
{
char ch,c, s[2];\
int m, n,l,i,a;
float x, y,z;
printf("Enter the operation of your choice:");
printf("\na. add s.subtract\n");
printf("m. multiply d.divide\n");
printf("q.quit\n");
scanf_s("%s", &s,2);
ch = s[0];
rewind(stdin);
while (ch != 'q')
{
switch (ch)
{
case 'a':
{ printf("Enter first number:\n");
while (scanf_s("%f", &x) != 1)
{
char c = 0;
while (c=getchar() != '\n')
putchar(c);
printf(" is not a number ,the correct number such as -1 ,3 or 25,please enter again");
}
for (i = 10,m=0,l=1;l != 0;i *= 10)
{
a = (int)(x * i);
l = a % 10;
if (l != 0)
m++;
}
printf("Enter second number:\n");
while (scanf_s("%f", &y) != 1)
{
c = 0;
while (c=getchar() != '\n')
putchar(c);
printf(" is not a number ,the correct number such as -1 ,3 or 25,please enter again");
}
for (i = 10, n = 0,l=1;l != 0;i *= 10)
{
a = (int)(x * i);
l = a % 10;
if (l != 0)
n++;
}
z = x + y;
if (m == 0 && n == 0)
printf("%.*f+%.*f=%.*f\n", m, x,m, y,m, z);
else
{
if (m > n)
printf("%.*f+%.*f=%.*f\n", m, x,m, y,m, z);
else
printf("%.*f+%.*f=%.*f\n", n, x,n, y,n, z);
}
break;
}
case 's':
{ printf("Enter first number:\n");
while (scanf_s("%f", &x) != 1)
{
char c = 0;
while (c=getchar() != '\n')
putchar(c);
printf(" is not a number ,the correct number such as -1 ,3 or 25,please enter again");
}
for (i = 10, m = 0,l=1;l != 0;i *= 10)
{
a = (int)(x *i);
l = a % 10;
if (l != 0)
m++;
}
printf("Enter second number:\n");
while (scanf_s("%f", &y) != 1)
{
char c = 0;
while (c=getchar() != '\n')
putchar(c);
printf(" is not a number ,the correct number such as -1 ,3 or 25,please enter again");
}
for (i = 10, n = 0,l=1;l != 0;i *= 10)
{
a = (int)(x * i);
l = a % 10;
if (l != 0)
n++;
}
z = x - y;
if (m == 0 && n == 0)
printf("%.*f-%.*f=%.*f\n", 0, x,0, y, 0,z);
else
{
if (m > n)
printf("%.*f-%.*f=%.*f\n", m, x, m, y, m, z);
else
printf("%.*f-%.*f=%.*f\n", n, x, n, y, n, z);
}
break;
}
case 'm':
{ printf("Enter first number:\n");
while (scanf_s("%f", &x) != 1)
{
char c = 0;
while (c=getchar() != '\n')
putchar(c);
printf(" is not a number ,the correct number such as -1 ,3 or 25,please enter again");
}
for (i = 10, m = 0,l=1;l != 0;i *= 10)
{
a = (int)(x * i);
l = a % 10;
if (l != 0)
m++;
}
printf("Enter second number:\n");
while (scanf_s("%f", &y) != 1)
{
char c = 0;
while (c=getchar() != '\n')
putchar(c);
printf(" is not a number ,the correct number such as -1 ,3 or 25,please enter again");
}
for (i = 10, n = 0,l=1;l != 0;i *= 10)
{
a = (int)(x * i);
l = a % 10;
if (l != 0)
n++;
}
z = x * y;
if (m == 0 && n == 0)
printf("%.*f*%.*f=%.*f\n", 0, x,0, y,0, z);
else
{
if (m > n)
printf("%.*f*%.*f=%.*f\n", m, x, m, y, m, z);
else
printf("%.*f*%.*f=%.*f\n", n, x, n, y, n, z);
}
break;
}
case 'd':
{ printf("Enter first number:\n");
while (scanf_s("%f", &x) != 1)
{
char c = 0;
while (c = getchar() != '\n')
putchar(c);
printf(" is not a number ,the correct number such as -1 ,3 or 25,please enter again\n");
}
for (i = 10, m = 0, l = 1;l != 0;i *= 10)
{
a = (int)(x * i);
l = a % 10;
if (l != 0)
m++;
}
printf("Enter second number:\n");
while (scanf_s("%f", &y) != 1 && !((y > -0.0000001) && (y < 0.0000001)))
{
char c = 0;
while (c = getchar() != '\n')
putchar(c);
printf(" is not a number ,the correct number such as -1 ,3 or 25,please enter again\n");
if ((y > -0.0000001) && (y < 0.0000001))
{
printf("the divisor can not be 0. please enter again\n");
continue;
}
}
for (i = 10, n = 0, l = 1;l != 0;i *= 10)
{
a = (int)(x * i);
l = a % 10;
if (l != 0)
n++;
}
z = x / y;
if (m == 0 && n== 0)
printf("%.*f/%.*f=%.*f\n", 0, x,0, y,0, z);
else
{
if (m > n)
printf("%.*f/%.*f=%.*f\n", m, x, m, y, m, z);
else
printf("%.*f/%.*f=%.*f\n", n, x, n, y, n, z);
}
break;
}
case 'q': break;
default :
{
printf("Enter the operation of your choice:");
printf("\na. add s.subtract\n");
printf("m. multiply d.divide\n");
printf("q.quit\n");
scanf_s("%s", &s, 2);
ch = s[0];
continue;
}
}
printf("Enter the operation of your choice:");
printf("\na. add s.subtract\n");
printf("m. multiply d.divide\n");
printf("q.quit\n");
scanf_s("%s", &s, 2);
ch = s[0];
}
return 0;
}

怎么把那個方塊打成字符,怎么可以在不知道輸入什么字母或者符號情況的情況下原原本本的列印出來??
我見他們scanf可以,難道是scanf_s沒有把值傳給c?
uj5u.com熱心網友回復:
修改如下,供參考:#include <stdio.h>
int main()
{
char ch,c, s[2];
int m, n,l,i,a;
float x, y,z;
printf("Enter the operation of your choice:");
printf("\na. add s.subtract\n");
printf("m. multiply d.divide\n");
printf("q.quit\n");
scanf_s("%s", s,2);
ch = s[0];
rewind(stdin);
while (ch != 'q')
{
switch (ch)
{
case 'a':
{ printf("Enter first number:\n");
while (scanf_s("%f", &x) != 1)
{
//char c ;
while ((c = getchar())!='\n')
putchar(c);
printf(" is not a number ,the correct number such as -1 ,3 or 25,please enter again\n");
}
for (i = 10,m=0,l=1;l != 0;i *= 10)
{
a = (int)(x * i);
l = a % 10;
if (l != 0)
m++;
}
printf("Enter second number:\n");
while (scanf_s("%f", &y) != 1)
{
//c = 0;
while ((c=getchar()) != '\n')
putchar(c);
printf(" is not a number ,the correct number such as -1 ,3 or 25,please enter again\n");
}
for (i = 10, n = 0,l=1;l != 0;i *= 10)
{
a = (int)(x * i);
l = a % 10;
if (l != 0)
n++;
}
z = x + y;
if (m == 0 && n == 0)
printf("%.*f+%.*f=%.*f\n", m, x,m, y,m, z);
else
{
if (m > n)
printf("%.*f+%.*f=%.*f\n", m, x,m, y,m, z);
else
printf("%.*f+%.*f=%.*f\n", n, x,n, y,n, z);
}
break;
}
case 's':
{ printf("Enter first number:\n");
while (scanf_s("%f", &x) != 1)
{
//char c = 0;
while ((c=getchar()) != '\n')
putchar(c);
printf(" is not a number ,the correct number such as -1 ,3 or 25,please enter again\n");
}
for (i = 10, m = 0,l=1;l != 0;i *= 10)
{
a = (int)(x *i);
l = a % 10;
if (l != 0)
m++;
}
printf("Enter second number:\n");
while (scanf_s("%f", &y) != 1)
{
//char c = 0;
while ((c=getchar()) != '\n')
putchar(c);
printf(" is not a number ,the correct number such as -1 ,3 or 25,please enter again\n");
}
for (i = 10, n = 0,l=1;l != 0;i *= 10)
{
a = (int)(x * i);
l = a % 10;
if (l != 0)
n++;
}
z = x - y;
if (m == 0 && n == 0)
printf("%.*f-%.*f=%.*f\n", 0, x,0, y, 0,z);
else
{
if (m > n)
printf("%.*f-%.*f=%.*f\n", m, x, m, y, m, z);
else
printf("%.*f-%.*f=%.*f\n", n, x, n, y, n, z);
}
break;
}
case 'm':
{ printf("Enter first number:\n");
while (scanf_s("%f", &x) != 1)
{
//char c = 0;
while ((c=getchar()) != '\n')
putchar(c);
printf(" is not a number ,the correct number such as -1 ,3 or 25,please enter again\n");
}
for (i = 10, m = 0,l=1;l != 0;i *= 10)
{
a = (int)(x * i);
l = a % 10;
if (l != 0)
m++;
}
printf("Enter second number:\n");
while (scanf_s("%f", &y) != 1)
{
// char c = 0;
while ((c=getchar()) != '\n')
putchar(c);
printf(" is not a number ,the correct number such as -1 ,3 or 25,please enter again\n");
}
for (i = 10, n = 0,l=1;l != 0;i *= 10)
{
a = (int)(x * i);
l = a % 10;
if (l != 0)
n++;
}
z = x * y;
if (m == 0 && n == 0)
printf("%.*f*%.*f=%.*f\n", 0, x,0, y,0, z);
else
{
if (m > n)
printf("%.*f*%.*f=%.*f\n", m, x, m, y, m, z);
else
printf("%.*f*%.*f=%.*f\n", n, x, n, y, n, z);
}
break;
}
case 'd':
{ printf("Enter first number:\n");
while (scanf_s("%f", &x) != 1) != 1)
{
//char c = 0;
while ((c = getchar()) != '\n')
putchar(c);
printf(" is not a number ,the correct number such as -1 ,3 or 25,please enter again\n");
}
for (i = 10, m = 0, l = 1;l != 0;i *= 10)
{
a = (int)(x * i);
l = a % 10;
if (l != 0)
m++;
}
printf("Enter second number:\n");
while (scanf_s("%f", &y) != 1 && !((y > -0.0000001) && (y < 0.0000001)))
{
//char c = 0;
while ((c = getchar()) != '\n')
putchar(c);
printf(" is not a number ,the correct number such as -1 ,3 or 25,please enter again\n");
if ((y > -0.0000001) && (y < 0.0000001))
{
printf("the divisor can not be 0. please enter again\n");
continue;
}
}
for (i = 10, n = 0, l = 1;l != 0;i *= 10)
{
a = (int)(x * i);
l = a % 10;
if (l != 0)
n++;
}
z = x / y;
if (m == 0 && n== 0)
printf("%.*f/%.*f=%.*f\n", 0, x,0, y,0, z);
else
{
if (m > n)
printf("%.*f/%.*f=%.*f\n", m, x, m, y, m, z);
else
printf("%.*f/%.*f=%.*f\n", n, x, n, y, n, z);
}
break;
}
case 'q': break;
default :
{
printf("Enter the operation of your choice:");
printf("\na. add s.subtract\n");
printf("m. multiply d.divide\n");
printf("q.quit\n");
scanf_s("%s", s, 2);
ch = s[0];
continue;
}
}
printf("Enter the operation of your choice:");
printf("\na. add s.subtract\n");
printf("m. multiply d.divide\n");
printf("q.quit\n");
scanf_s("%s", s, 2);
ch = s[0];
}
return 0;
}
uj5u.com熱心網友回復:

第162行多打了,修改如下:
while (scanf("%f", &x) != 1 )
uj5u.com熱心網友回復:
老哥,謝謝了又是你
uj5u.com熱心網友回復:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/248679.html
標籤:C語言
