简单加减乘除
Posted cokefentas
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单加减乘除相关的知识,希望对你有一定的参考价值。
#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: "); printf("a. add b. subtract c. multiply d. divide q. quit "); 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()!=‘ ‘) continue; return ch; } float get_float(void) { float input; char ch; while(scanf("%f",&input) != 1) { while((ch=getchar()) != ‘ ‘) putchar(ch); printf("is not a float number. "); 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 ",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 ",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 ",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 ",f_num,s_num,f_num/s_num); }
以上是关于简单加减乘除的主要内容,如果未能解决你的问题,请参考以下文章