Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the code of calculator in C language

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to write the code of the calculator in C language, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe that after reading this C language calculator code how to write the article will have a harvest, let's take a look.

Two methods of C language Calculator

/ / General practice # include#include//#includevoid menu () {printf ("* *\ n"); printf ("* Welcome to the calculator page *\ n"); printf ("* 1.add 2.mul *\ n") Printf ("* 3.sub 4.div1 *\ n"); printf ("* 0.exit *\ n"); printf ("* *\ n");} int add (int x, int y) {return x + y;} int mul (int x, int y) {return x * y } int sub (int x, int y) {return x-y;} int div1 (int x, int y) {return x / y;} int main () {int n = 1; menu (); while (n) {printf ("Please enter options:\ n"); scanf ("% d", & n); int x = 0, y = 0 / * printf ("Please enter two integers!\ n"); scanf ("% d% d", & x, & y) * / / if you put two numeric inputs here, you cannot exit the system when an error occurs. Instead, the prompt "enter two integers" switch (n) {case 1: printf ("Please enter two integers!\ n"); scanf ("% d% d", & x, & y) Printf ("% d\ n", add (x, y)); break; case 2: printf ("Please enter two integers!\ n"); scanf ("% d% d", & x, & y); printf ("% d\ n", mul (x, y)); break Case 3: printf ("Please enter two integers!\ n"); scanf ("% d% d", & x, & y); printf ("% d\ n", sub (x, y)); break; case 4: printf ("Please enter two integers!\ n") Scanf ("% d% d", & x, & y); printf ("% d\ n", div1 (x, y)); break; case 0: printf ("exit Calculator! \ n "); break; default: printf (" input error, please enter again!\ n ");}

/ / use function pointer array to solve calculator problems # include#include//#includevoid menu () {printf ("* *\ n"); printf ("* Welcome to the calculator page *\ n"); printf ("* 1.add 2.mul *\ n") Printf ("* 3.sub 4.div1 *\ n"); printf ("* 0.exit *\ n"); printf ("* *\ n");} int add (int x, int y) {return x + y;} int mul (int x, int y) {return x * y } int sub (int x, int y) {return x-y;} int div1 (int x, int y) {return x / y;} int main () {int n = 1; menu (); while (n) {printf ("Please enter options:\ n"); scanf ("% d", & n); int (* arr [5]) () = {0m addmento subhead div1} / / transfer table / / define an array of function pointers that hold the function address if (n > = 0 & & n)

< 5) { int x = 0, y = 0; printf("请输入两个整数:>

\ n "); scanf ("% d% d ", & x, & y); printf ("% d\ n ", (* arr [n]) (xPowery)); / / output result} else {printf (" input error, please re-enter!\ n "); scanf ("% d ", & n) }} system ("pause"); return 0;}

Compared with the two methods, the first kind of code is more similar, more redundant; the second kind of code is simple and intuitive, and the program efficiency is high.

This is the end of the article on "how to write the code for C language calculator". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to write the code of calculators in C language". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report