33,311
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
float add(float a,float b){
return a + b;
}
float subtract(float a, float b){
return a - b;
}
float multiply(float a, float b){
return a * b;
}
float divide(float a, float b){
return a / b;
}
void printMenu(){
printf("Enter the operation of your choice: \n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
}
int main(){
char option = 'a';
int is_correct;
char ch;
float f_num,s_num;
while(option != 'q'){
printMenu();
scanf("%1c",&option);
while(getchar() != '\n' ){
continue;
}
if(option == 'q'){
break;
}
else if(option == 'a' || option == 's' || option == 'm' || option == 'd' ){
if(option == 'd'){
printf("Enter first number: ");
while(scanf("%f",&f_num) != 1){
printf("It is not a number\n");
printf("Please enter a number, such as 2.5, -1/78E8, or 3: ");
}
printf("Enter second number: ");
while(scanf("%f",&s_num) != 1){
printf("It is not a number\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
while(s_num == 0){
printf("Enter a number other than 0 ");
scanf("%f",&s_num);
}
printf("%f / %f = %f\n",f_num,s_num,divide(f_num,s_num));
}
else{
printf("Enter first number: ");
while(scanf("%f",&f_num) != 1){
printf("It is not a number\n");
printf("Please enter a number, such as 2.5, -1/78E8, or 3: ");
}
printf("Enter second number: ");
while(scanf("%f",&s_num) != 1){
printf("It is not a number\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
switch(option){
case 'a':
printf("%f + %f = %f \n",f_num,s_num,add(f_num,s_num) );
break;
case 's':
printf("%f - %f = %f \n",f_num,s_num,subtract(f_num,s_num) );
break;
case 'm':
printf("%f * %f = %f \n",f_num,s_num,multiply(f_num,s_num) );
break;
}
}
}
else{
is_correct = 0;
printf("Your choice is %d\n",option);
printf("Please input a correct choice\n");
}
}
printf("Bye");
return 0;
}
#include <stdio.h>
char get_choice(void);
int check_first_input (void);
int check_second_input (void);
float add(void);
float subtract(void);
float multiply(void);
float divide(void);
int main(void)
{
char choice;
while ((choice = get_choice()) != 'q')
{
switch(choice)
{
case 'a': add(); break;
case 's': subtract(); break;
case 'm': multiply(); break;
case 'd': divide(); break;
default :printf("please enter the right word.\n");continue;
}
}
printf("done!");
return 0;
}
char get_choice(void)
{
char choice;
printf("Enter the operation of your choice: \n");
printf("a. add s. subtract \n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
choice = getchar();
while(getchar() !='\n')
continue;
return choice;
}
float add(void)
{
float first_number,second_number;
float result;
first_number = check_first_input();
second_number = check_second_input();
result = first_number + second_number;
printf("%.1f + %.1f = %.1f\n",first_number,second_number,result);
return result;
}
float subtract(void)
{
float first_number,second_number;
float result;
first_number = check_first_input();
second_number = check_second_input();
result = first_number - second_number;
printf("%.1f - %.1f = %.1f\n",first_number,second_number,result);
return result;
}
float multiply(void)
{
float first_number,second_number;
float result;
first_number = check_first_input();
second_number = check_second_input();
result = first_number * second_number;
printf("%.1f * %.1f = %.1f\n",first_number,second_number,result);
return result;
}
float divide(void)
{
float first_number,second_number;
float result;
first_number = check_first_input();
second_number = check_second_input();
while (second_number == 0)
{
printf("enter a number other than 0: ");
second_number = check_second_input();
}
result = first_number / second_number;
printf("%.1f / %.1f = %.1f\n",first_number,second_number,result);
return result;
}
int check_first_input (void)
{
int input;
char ch;
printf("please enter the first number: ");
while ((scanf("%d",&input)) == 0)
{
while ((ch = getchar()) != '\n') //这里的getchar()获得是上次input的值,并不是再从键盘获取新的值
putchar(ch);
printf(" is not an number.\n");
printf("please enter a number,such as 2.5,-1.78E8,or 3 : ");
}
while(getchar() !='\n') //我之前想法是这边的不等于改成等于,但是一旦这样修改那么Enter将会进入while
continue; // 那么这个enter键将被使用而不会将输入送入缓存区。
return input;
}
int check_second_input (void)
{
int input;
char ch;
printf("please enter the second number: ");
while ((scanf("%d",&input)) == 0)
{
while ((ch = getchar()) != '\n')
putchar(ch);
printf(" is not an number.");
printf("please enter a number,such as 2.5,-1.78E8,or 3 :");
}
while(getchar() !='\n')
continue;
return input;
}
#include <stdio.h>
float add(float a,float b){
return a + b;
}
float subtract(float a, float b){
return a - b;
}
float multiply(float a, float b){
return a * b;
}
float divide(float a, float b){
return a / b;
}
void printMenu(){
printf("Enter the operation of your choice: \n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
}
int main(){
char option = 'a';
int is_correct;
char ch;
float f_num,s_num;
while(option != 'q'){
printMenu();
scanf("%1c",&option);
while(getchar() != '\n' ){
continue;
}
if(option == 'q'){
break;
}
else if(option == 'a' || option == 's' || option == 'm' || option == 'd' ){
if(option == 'd'){
printf("Enter first number: ");
while(scanf("%f",&f_num) != 1){
printf("It is not a number\n");
printf("Please enter a number, such as 2.5, -1/78E8, or 3: ");
}
printf("Enter second number: ");
while(scanf("%f",&s_num) != 1){
printf("It is not a number\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
while(s_num == 0){
printf("Enter a number other than 0 ");
scanf("%f",&s_num);
}
printf("%f / %f = %f\n",f_num,s_num,divide(f_num,s_num));
}
else{
printf("Enter first number: ");
while(scanf("%f",&f_num) != 1){
printf("It is not a number\n");
printf("Please enter a number, such as 2.5, -1/78E8, or 3: ");
}
printf("Enter second number: ");
while(scanf("%f",&s_num) != 1){
printf("It is not a number\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
switch(option){
case 'a':
printf("%f + %f = %f \n",f_num,s_num,add(f_num,s_num) );
break;
case 's':
printf("%f - %f = %f \n",f_num,s_num,subtract(f_num,s_num) );
break;
case 'm':
printf("%f * %f = %f \n",f_num,s_num,multiply(f_num,s_num) );
break;
}
}
}
else{
is_correct = 0;
printf("Your choice is %d\n",option);
printf("Please input a correct choice\n");
}
}
printf("Bye");
return 0;
}
#include <stdio.h>
float add(float a,float b){
return a + b;
}
float subtract(float a, float b){
return a - b;
}
float multiply(float a, float b){
return a * b;
}
float divide(float a, float b){
return a / b;
}
void printMenu(){
printf("Enter the operation of your choice: \n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
}
int main()
{
char option = 'a';
int is_correct;
char ch;
float f_num,s_num;
while(option != 'q'){
printMenu();
scanf("%c", &option);
while (getchar() != '\n' ) {
continue;
}
if(option == 'q'){
break;
}
else if(option == 'a' || option == 's' || option == 'm' || option == 'd' ){
if(option == 'd'){
printf("Enter first number: ");
while(scanf("%f",&f_num) != 1){
printf("It is not a number\n");
printf("Please enter a number, such as 2.5, -1/78E8, or 3: ");
}
printf("Enter second number: ");
while(scanf("%f",&s_num) != 1){
printf("It is not a number\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
while(s_num == 0){
printf("Enter a number other than 0 ");
scanf("%f", &s_num);
}
printf("%f / %f = %f\n", f_num, s_num, divide(f_num,s_num));
} else {
printf("Enter first number: ");
while(scanf("%f", &f_num) != 1) {
printf("It is not a number\n");
printf("Please enter a number, such as 2.5, -1/78E8, or 3: ");
}
printf("Enter second number: ");
while(scanf("%f", &s_num) != 1){
printf("It is not a number\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
switch(option){
case 'a':
printf("%f + %f = %f \n",f_num,s_num,add(f_num,s_num) );
break;
case 's':
printf("%f - %f = %f \n",f_num,s_num,subtract(f_num,s_num) );
break;
case 'm':
printf("%f * %f = %f \n",f_num,s_num,multiply(f_num,s_num) );
break;
}
}
getchar(); /*接收第二个输入后残留在内存里的换行符'\n' */
} else{
is_correct = 0;
printf("Your choice is %d\n",option);
printf("Please input a correct choice\n");
}
}
printf("Bye\n");
return 0;
}
因为输入s_num后,会有换行符'\n'残留在输入缓存里了,所以再次输入的内容会被getchar();接收。因此,需要再次输入;
结局办法如上代码,加一句getchar()即可,这句就是为了接收输入s_num后的换行符。