标签:utc with span please lease input string letter while
#include <stdio.h> #include <string.h> char get_choice(void); char get_first(void); float get_float(void); void add(void); void subtract(void); void multiply(void); void divide(void); int main(void) { int choice; while((choice=get_choice())!=‘q‘) { switch (choice) { case ‘a‘: add(); break; case ‘b‘: subtract(); break; case ‘c‘: multiply(); break; case ‘d‘: divide(); break; case ‘q‘: break; } } return 0; } char get_choice(void) { int ch; printf("input the letter of your choice:\n"); printf("a. add\nb. subtract\nc. multiply\nd. divide\nq. quit\n"); ch = get_first(); while((ch<‘a‘ || ch>‘d‘) && ch!=‘q‘) { printf("please respond with a,b,c,d,q:"); ch = get_first(); } return ch; } char get_first(void) { int ch; ch = getchar(); while(getchar()!=‘\n‘) continue; return ch; } float get_float(void) { float input; char ch; while(scanf("%f",&input) != 1) { while((ch=getchar()) != ‘\n‘) putchar(ch); printf("is not a float number.\n"); printf("please input a float number,such as 1.1, 2.5:"); } return input; } void add(void) { float f_num,s_num; printf("input first number:"); f_num = get_float(); printf("input second number:"); s_num = get_float(); printf("%f + %f = %f\n",f_num,s_num,f_num+s_num); } void subtract(void) { float f_num,s_num; printf("input first number:"); f_num = get_float(); printf("input second number:"); s_num = get_float(); printf("%f - %f = %f\n",f_num,s_num,f_num-s_num); } void multiply(void) { float f_num,s_num; printf("input first number:"); f_num = get_float(); printf("input second number:"); s_num = get_float(); printf("%f * %f = %f\n",f_num,s_num,f_num*s_num); } void divide(void) { float f_num,s_num; printf("input first number:"); f_num = get_float(); printf("input second number:"); s_num = get_float(); if(s_num == 0) { printf("input a number other than 0:"); s_num = get_float(); } printf("%f / %f = %f\n",f_num,s_num,f_num/s_num); }
标签:utc with span please lease input string letter while
原文地址:https://www.cnblogs.com/cokefentas/p/12283349.html