What can I do after learning the three structures of C language? (write a calculator and keep updating)

Name: Gong Zheng

Place: Home

Experiment time: March 25, 2020

Experimental items:

Purpose and requirements of the experiment

1,......
2,......

Experimental content

Experiment exercise:

1. Simple description of the problem: simple description of the project
2 experiment code: it needs to use MARKDOWN to insert code.
3 problem analysis: (focus on the analysis of the problems in the process of the experiment and the solutions.)

Experiment exercise:

1. Brief description of the problem:
2 experiment code:
3 problem analysis: (focus on the analysis of the problems in the process of the experiment and the solutions.)

Experiment exercise:

1. Brief description of the problem:
2 experiment code:
3 problem analysis: (focus on the analysis of the problems in the process of the experiment and the solutions.)

Experiment exercise:

1. Brief description of the problem:
2 experiment code:
3 problem analysis: (focus on the analysis of the problems in the process of the experiment and the solutions.)

Experiment exercise:

1. Brief description of the problem:
2 experiment code:
3 problem analysis: (focus on the analysis of the problems in the process of the experiment and the solutions.)

Project training: the realization of calculator

Early version zero

2020.3.20 9: 50

Version characteristics

Calculation of addition, subtraction, multiplication and division of simple recycling

Design thinking

Use switch case to judge whether the input character is + ` - ` * ` / and calculate accordingly

Flow chart

Problems encountered in the design process and improvement methods

The variable is defined by double, corresponding to scanf (""% lf ""), not% f;

Code:

#include<stdio.h>
int main()
{
    double tot,i,j;
    char ch;
    printf("Please enter the first number:") ;
    scanf("%lf",&i);
    printf("Please enter operator;");
    scanf("%c",&ch);
    printf("Please enter the second number:") ;
    scanf("%lf",&j);
    if(ch=='+') {
        tot=i+j;
    }else if(ch=='-'){
        tot=i-j;
    }else if(ch=='*'){
        tot=i*j;
    }else if(ch=='/'){
        tot=i/j;
    }
    printf("%f",tot);
    return 0;
}

First edition

2020.3.20 10:10

Version characteristics

Simplify the code and merge the statements; loop input is possible

Design thinking

zero version can't loop input. The first generation added loop structure for input

Flow chart

Problems encountered in the design process and improvement methods

1. Format of circular input: input the first number first, and then the number after circular calculation, so it can be superposed;
2. Choose which writing method to use in the loop: you can merge some codes with the for loop;

Code:

#include<stdio.h>
int main()
{
    int sum,j;
    
    printf("Please enter the first number:") ;
    scanf("%d",&sum);
    char ch;
    int t;
    for(t=2;getchar()!='=';t++){
        printf("Please enter the operator and the%d Number:",t);
        ch=getchar();
        scanf("%d",&j);
        switch(ch){
            case'+':sum+=j;break;
            case'-':sum-=j;break;
            case'*':sum*=j;break;
            case'/':sum/=j;break;
        }
        printf("%d\n",sum);
    }
    
    printf("%d",sum);
    return 0;
}

The second edition

2020.3.20 14:36

Version characteristics

Added menu, but only two options are available
Optimized code interface to refresh the screen

Design thinking

The first version of the computer function is too monotonous, and the menu function is added to realize multiple groups of data calculation (non superposition operation); the program can be exited

Flow chart

Problems encountered in the design process and improvement methods:

1. How to judge the key entered by the user: use getch() in "include < conio. H >;
2. How to run the program until the user exits: use do while (1); infinite loop
3. How to optimize the interface: use the system("cls") in "include < windows. H >", clear the screen content every time the program is completed, and make the layout simple

Code

#include<stdio.h>
#Include < conio. H > / / key for getch() to read user input 
#Include < windows. H > / / for screen cleaning 
int main()

{
	//Multiple operations and groups of data
	do{
	//menu
	printf("---------------------menu--------------------\n") ;
	
	printf("↑: Add, subtract, multiply and divide                                 \n") ;
	
	printf("Esc: Sign out                                    \n") ;
	
	printf("Home: Set up                                   \n") ;
	
	printf("---------------------------------------------\n") ;
	//Judge user input options 
	char userput;
	userput=getch();//First call, then if is the second call, actual value 
	//Addition, subtraction, multiplication and division 
	
	if(userput==72) 
	{	
		printf("---------------------------------------------\n"); 
		printf("|               Addition, subtraction, multiplication and division mode                |\n"); 
		printf("---------------------------------------------\n"); 
		int sum,j;
		printf("Please enter the first number:") ;
		scanf("%d",&sum);
		char ch;
		int t;
		//Multiple input and calculation of single group of data 
		for(t=2;getchar()!='=';t++){//The purpose of t is to enter the number for the following prompt
		//Get the operator entered by the user and the number 
		printf("Please enter operator and%d Number(Such as:+8,Enter if the operation ends==):",t);
		
		ch=getchar();
		        scanf("%d",&j);
		//Judge operator, make corresponding operation 
		switch(ch){
			case'+':sum+=j;break;
			case'-':sum-=j;break;
			case'*':sum*=j;break;
			case'/':sum/=j;break;
		}
		printf("Results of this calculation=%d\n",sum);
		}
		//printf("final result =% d",sum);
		}
		//Exit procedure 
		else if(userput==27)
		{
			printf("---------------------------------------------\n"); 
			printf("|                  Exit procedure                 |\n"); 
			printf("---------------------------------------------\n"); 
			break;
		}
		//In order to make the layout beautiful, the header file "include < windows. H > is used to clear the screen after each calculation 
		system("cls") ;
	}while(1);
	return 0;
}


The Third Edition

2020.3.20 15:05

Version characteristics

Refine Settings tab: keep decimal; refresh screen

Design thinking

Perfect the Settings tab to perform the following operations: keep several decimal places; close screen refresh

Flow chart

Problems encountered in the design process and improvement methods

1. How to close screen refresh: two areas, the first to determine whether to close, the second to close
2. How to keep several decimal places: the * in%. * lf represents the value of a variable, that is, the variable with several decimal places

Code

#include<stdio.h>
#Include < conio. H > / / key for getch() to read user input 
#Include < windows. H > / / for screen cleaning 
int main()
{
	int set;//It is used to set the tab. The reason is that each cycle can be used without changing the value of set, so as not to affect the next cycle 
	int point=2;//It is used to set the tab. n decimal places are reserved for the output number, and two decimal places are reserved by default 
	//Multiple operations and groups of data
	do{
		//menu
		printf("---------------------menu--------------------\n") ;
		printf("↑: Add, subtract, multiply and divide                                 \n") ;
		printf("Esc: Sign out                                    \n") ;
		printf("Home: Set up                                   \n") ;
		printf("---------------------------------------------\n") ;
		
		//Judge user input options 
		char userput;
		userput=getch();//First call, then if is the second call, actual value 
		//Addition, subtraction, multiplication and division 
		if(userput==72)//72 indicates user input up arrow 
		{	
			printf("---------------------------------------------\n"); 
			printf("|               Addition, subtraction, multiplication and division mode                |\n"); 
			printf("---------------------------------------------\n"); 
	
			double sum,j;
			printf("Please enter the first number:") ;
			scanf("%lf",&sum);
			char ch;
			int t;
	
			//Multiple input and calculation of single group of data 
			for(t=2;getchar()!='=';t++){//The purpose of t is to enter the number for the following prompt 
	
			//Get the operator entered by the user and the number 
			printf("Please enter operator and%d Number(Such as:+8,with==Sign out):",t);
			ch=getchar();
	                scanf("%lf",&j);
	        
			
			//Judge operator, make corresponding operation 
			switch(ch){
				case'+':sum+=j;break;
				case'-':sum-=j;break;
				case'*':sum*=j;break;
				case'/':sum/=j;break;
			}
			printf("Results of this calculation=%.*lf\n",point,sum);
			}
			
		}
		
		//Exit procedure 
		else if(userput==27)
		{
			printf("---------------------------------------------\n"); 
			printf("|                  Exit procedure                 |\n"); 
			printf("---------------------------------------------\n"); 
			break;
		}
			
		//Settings tab
		
			//Judgement area 
		else if(userput==71)
		{
			printf("---------------------------------------------\n"); 
			printf("|                    Set up                   |\n"); 
			printf("|1.Refresh screen                                 |\n"); 
			printf("|2.Reserved decimal                                 |\n"); 
			printf("---------------------------------------------\n"); 
			int setin;
			char setchoose;
			printf("Please enter a number to enter the option:") ;
			scanf("%d",&setin);
			//1. Refresh the screen 
			if(setin==1)
			{
				printf("Close refresh screen[y/n]:");
				scanf("%c",&setchoose);
				if(setchoose=='y'){
					set=1;
				} else{
					set=0;
				}
			}
			//2. Keep decimal
			if(setin==2) 
			{
				printf("How many decimal places do you want to keep? :");
				scanf("%d",&point);//You don't need to go through the execution area 
			}
			
		}
		
			//Execution area
		if(set==1)//The choice is to make yes
		{
		
		} 
		else//no was chosen 
		{
			//In order to make the layout beautiful, the header file "include < windows. H > is used to clear the screen after each calculation 
			system("cls") ;
		}
	}while(1);
	return 0;
}

The Fourth Edition

2020.3.20 20:04

Version characteristics

Optimized addition, subtraction, multiplication and division (decimal operation, thanks to% lf);
The decimal conversion mode is added;

Design thinking

Adding decimal conversion to other base modes on the basis of the original calculator

Flow chart

The flow chart is too complicated. The author has already taken the dog

Problems encountered in the design process and improvement methods

1. How to convert decimal system to other base system
① Convert to octal, hexadecimal: very simple as long as% d% o% x output on the line (respectively corresponding to decimal, octal, hexadecimal)
② Convert to binary: This is a bit more complex. It needs to divide by two to get the remainder and add it in reverse order. Therefore, it needs to use a cycle. Each time, it needs to divide by two to get the remainder, and the final result is reverse order. It is equivalent to judging a number is a few digits and disassembling the number in reverse order

Code

#include<stdio.h>
#Include < conio. H > / / key for getch() to read user input 
#Include < windows. H > / / for screen cleaning 
int main()
{
	int set;//It is used to set the tab. The reason is that each cycle can be used without changing the value of set, so as not to affect the next cycle 
	int point=2;//It is used to set the tab. n decimal places are reserved for the output number, and two decimal places are reserved by default 
	double i,j,k,sum,num,left;//Participate in calculation: addition, subtraction, multiplication and division, base conversion 
	int ip,jp,kp,sump,nump,leftp;//Participate in calculation: addition, subtraction, multiplication and division, base conversion 
	//Multiple operations and groups of data
	do{
		//menu
		printf("---------------------menu--------------------\n") ;
		printf("↑: Add, subtract, multiply and divide                                 \n") ;
		printf("↓: Binary conversion                                 \n") ;
		printf("Esc: Sign out                                    \n") ;
		printf("Home: Set up                                   \n") ;
		printf("---------------------------------------------\n") ;
		
		//Judge user input options 
		char userput;
		userput=getch();//First call, then if is the second call, actual value 
		
		//Addition, subtraction, multiplication and division 
		if(userput==72)//72 indicates user input up arrow 
		{	
			printf("---------------------------------------------\n"); 
			printf("|               Addition, subtraction, multiplication and division mode                |\n"); 
			printf("---------------------------------------------\n"); 
	
			double sum;
			printf("Please enter the first number:") ;
			scanf("%lf",&sum);
			char ch;
			int t;
	
			//Multiple input and calculation of single group of data 
			for(t=2;getchar()!='=';t++){//The purpose of t is to enter the number for the following prompt 
	
			//Get the operator entered by the user and the number 
			printf("Please enter operator and%d Number(Such as:+8,with==Sign out):",t);
			ch=getchar();
	        scanf("%lf",&j);
	        
			
			//Judge operator, make corresponding operation 
			switch(ch){
				case'+':sum+=j;break;
				case'-':sum-=j;break;
				case'*':sum*=j;break;
				case'/':sum/=j;break;
			}
			printf("%lf%c%lf=%.*lf\n",sum,ch,j,point,sum);
			}
			
		}
		
		//Binary conversion
		else if(userput==80) 
		{
			printf("---------------------------------------------\n"); 
			printf("|               Base conversion mode                |\n"); 
			printf("---------------------------------------------\n"); 
			do{
				printf("Please enter the decimal integer you want to convert:");
				scanf("%d",&ip);
				printf("You want to convert___Radix:");
				scanf("%d",&jp);
				
				//Processing data 
				kp=ip;//The value of temporary IP is decomposed as follows 
				sump=0;//Initial sum = 0 
				for(nump=1;ip>0;nump*=10) {
					leftp=ip%jp;//Remainder 
					ip/=jp;//Rounding 
					sump+=nump*leftp;//Reverse order addition 
				}
				ip=kp;//IP exchange back to original value 
				printf("%d Of%d The radix number is%d\n",ip,jp,sump);
				
				
				printf("Press any key to continue( Esc Exit)\n");//Borrow the function of exit program 
				userput=getch();
			}while(userput!=27);
			 
		}
		
		//Exit procedure 
		else if(userput==27)
		{
			printf("---------------------------------------------\n"); 
			printf("|                  Exit procedure                 |\n"); 
			printf("---------------------------------------------\n"); 
			break;
		}
			
		//Settings tab
		
			//Judgement area 
		else if(userput==71)
		{
			printf("---------------------------------------------\n"); 
			printf("|                    Set up                   |\n"); 
			printf("|1.Refresh screen                                 |\n"); 
			printf("|2.Reserved decimal                                 |\n"); 
			printf("---------------------------------------------\n"); 
			int setin;
			char setchoose;
			printf("Please enter a number to enter the option:") ;
			scanf("%d",&setin);
			//1. Refresh the screen 
			if(setin==1)
			{
				printf("Close refresh screen[y/n]:");
				scanf(" %c",&setchoose);//Considering the reading of scanf, the end of the last scanf is \ n. when doing this scanf, count \ n in% c, and make sure to add a space 
				if(setchoose=='y'){
					set=1;
				} else{
					set=0;
				}
			}
			//2. Keep decimal
			else if(setin==2) 
			{
				printf("How many decimal places do you want to keep? :");
				scanf("%d",&point);//You don't need to go through the execution area 
			}
			
		}
		
			//Execution area
		if(set==1)//The choice is to make yes
		{
			printf("Modified success\n");
		} 
		else//no was chosen 
		{
			//In order to make the layout beautiful, the header file "include < windows. H > is used to clear the screen after each calculation 
			system("cls") ;
		}


	}while(1);
	
	
	 
	return 0;
}

The Fifth Edition

I can't remember the time<

Version characteristics

New tab: decimal conversion (currently only decimal conversion is supported)
Optimize input function: it can judge whether the user input is legal
Fix some bug s: mentioned in the comments

Design thinking

Judge whether user input is legal and optimize program operation

Flow chart

Because the flow chart is so complicated, I'm afraid that the author's drawing is bald!!!

Problems encountered in the design process and improvement methods

1. How to judge whether the user is legal?
① Here we need to mention the buffer. If the user input format exceeds the length, for example, 1+1 is required, and the user input 1+1+2, then the operation is normal, but there is still +2 left in the buffer that has not been taken away by scanf. In the next scanf, this +2 will be taken away without reading the user input. At this time, it is likely to cause program running errors;
There is a code while(getchar()!='\n'); the cache can be emptied. The principle is that getchar will take one character from the buffer each time. As long as it is not assigned to other variables, getchar() can always take the buffer characters and run in the while loop
② If a number is required and the user enters a character such as +, the result will also be affected. At this time, it is necessary to determine whether the user input meets the requirements
The first method uses scanf ("% d", & x) to determine whether the input is an integer. The principle is that scanf will return a value. When a variable is entered correctly, it will return 1. Two variables are 2
The second is to use isdigit to judge whether the input is a number. There are other codes for judging whether the input is a character
The third is the most conventional direct judgment x=1. Each method has its own advantages and disadvantages, depending on which judgment your code is suitable for

Code

#include<stdio.h>
#Include < conio. H > / / key for getch() to read user input 
#Include < windows. H > / / for screen cleaning 
int main()
{
	int set;//It is used to set the tab. The reason is that each cycle can be used without changing the value of set, so as not to affect the next cycle 
	int point=2;//It is used to set the tab. n decimal places are reserved for the output number, and two decimal places are reserved by default 
	double i,j,k,sum,num,left;//Participate in calculation: addition, subtraction, multiplication and division, base conversion 
	int ip,jp,kp,sump,nump,leftp;//Participate in calculation: addition, subtraction, multiplication and division, base conversion 
	
	//Multiple operations and groups of data
	do{
		//menu
		printf("---------------------menu--------------------\n") ;
		printf("↑: Add, subtract, multiply and divide                                 \n") ;
		printf("↓: Binary conversion                                 \n") ;
		printf("Home: Set up                                   \n") ;
		printf("Esc: Exit procedure                                \n") ;
		printf("---------------------------------------------\n") ;
		
		//Judge user input options 
		printf("Press the key to access the options:\n");
		char userput,userput0;
		userput0=getch();//First call 
		userput=getch();//The second call, this time the actual value 
		
		//Addition, subtraction, multiplication and division 
		if(userput==72)//72 indicates user input up arrow 
		{	
			printf("---------------------------------------------\n"); 
			printf("|               Addition, subtraction, multiplication and division mode                |\n"); 
			printf("---------------------------------------------\n"); 
	
			double sum;
			printf("Please enter the first number:");
			while(scanf("%lf",&sum)!=1) {//Scanf can be executed in while, and scanf has its own return value. If you enter a variable correctly, it can return 1 
				printf("illegal input,Please re-enter\n");
				while(getchar()!='\n');//When the user enters a wrong format, getchar() is used to take the first character in the buffer, so that the write can clear the buffer, so as to avoid the error of ch=getchar() 
			}
			char ch;
			int t;
			num=sum;//The function of num: when the value of sum is temporarily stored for the final output of the service, the operation expression is correct 
			
			
			//Multiple input and calculation of single group of data 
			for(t=2;getchar()!='=';t++){//The purpose of t is to enter the number for the following prompt 
		        while(1) {
		        	printf("Please enter operator and%d Number(Such as:+8,with==Sign out):",t);
		        	ch=getchar();
					scanf("%lf",&j); 
					if(ch='='){
						break;
					}
		        	else if(scanf("%lf",&j)!=1||(ch!='+'&&ch!='-'&&ch!='*'&&ch!='/'&&ch!='='))
					{
		        		printf("illegal input,Please re-enter\n");
		        		while(getchar()!='\n');//Clear cache 
					}
				}
				
				//Judge operator, make corresponding operation 
				switch(ch){
					case'+':sum+=j;break;
					case'-':sum-=j;break;
					case'*':sum*=j;break;
					case'/':sum/=j;break;
				}
				printf("%lf%c%lf=%.*lf\n",num,ch,j,point,sum);
				num=sum;//The correct value is displayed when the calculation result is output__*__=__ 
			}
			
		}
		
		//Binary conversion
		else if(userput==80) 
		{
			do{ 
			printf("---------------------------------------------\n"); 
			printf("|               Base conversion mode                |\n"); 
			printf("|1.Decimal to other base                       |\n"); 
			printf("|2.Other base to decimal                       |\n");  
			printf("|3.Binary to other base                       |\n"); 
			printf("---------------------------------------------\n"); 
			printf("Please select the serial number to enter the option:") ;
			while(scanf("%d",&userput)!=1&&userput<1&&userput>4) {
				printf("illegal input,Please re-enter\n");
				while(getchar()!='\n');//Clear cache 
			}
		
			if(userput==1)
			{
			do{
				printf("Please enter the decimal integer you want to convert:");
				while(scanf("%d",&ip)!=1) {
					printf("illegal input,Please re-enter\n");
					while(getchar()!='\n');//Clear cache 
				}
				printf("You want to convert to___Radix:");
				while(scanf("%d",&jp)!=1) {
					printf("illegal input,Please re-enter\n");
					while(getchar()!='\n');//Clear cache 
				}
				
				//Processing data 
				kp=ip;//The value of temporary IP is decomposed as follows 
				sump=0;//Initial sum = 0 
				for(nump=1;ip>0;nump*=10) {
					leftp=ip%jp;//Remainder 
					ip/=jp;//Rounding 
					sump+=nump*leftp;//Reverse order addition 
				}
				ip=kp;//IP exchange back to original value 
				printf("%d Of%d The radix number is%d\n",ip,jp,sump);
				
				printf("Press any key to continue( Esc Go back to the previous menu)\n");//Borrow the function of exit program 
				userput=getch();
			}while(userput!=27);
			}
			else if(userput==2) 
			{
				printf("Conversion type is octal/Hexadecimal[8/16]"); 
			}
			
			
			
			printf("You're in base conversion mode") ;
			printf("Press any key to continue( Esc Back to main menu)\n");//Borrow the function of exit program 
			userput=getch();
			}while(userput!=27) ;
			 
		}
		
		//Exit procedure 
		else if(userput==27)
		{
			printf("---------------------------------------------\n"); 
			printf("|                  Exit procedure                 |\n"); 
			printf("---------------------------------------------\n"); 
			break;
		}
			
		//Settings tab
		
			//Judgement area 
		else if(userput==71)
		{
			printf("---------------------------------------------\n"); 
			printf("|                    Set up                   |\n"); 
			printf("|1.Refresh screen                                 |\n"); 
			printf("|2.Reserved decimal                                 |\n"); 
			printf("---------------------------------------------\n"); 
			char setchoose;
			printf("Please enter a number to enter the option:") ;
			while(scanf("%c",&userput)!=1) {
				printf("illegal input,Please re-enter\n");
				while(getchar()!='\n');//Clear cache 
			}
			//1. Refresh the screen 
			if(userput=='1')
			{
				printf("Close refresh screen[y/n]:");
				scanf(" %c",&setchoose);//Considering the reading of scanf, the end of the last scanf is \ n. when doing this scanf, count \ n in% c, and make sure to add a space 
				if(setchoose=='y'){
					set=1;
					printf("Modified success\n");
				} else{
					set=0;
				}
			}
			//2. Keep decimal
			else if(userput=='2') 
			{
				printf("How many decimal places do you want to keep? :");
				scanf("%d",&point);//You don't need to go through the execution area because there are several decimal places reserved by default. You need to go through this area to modify them. If you put them in the execution area, they will be modified every time 
			}
			
		}
		
		//Press no key
		else{
			printf("Please press the correct key, enter to continue"); 
			while(getchar()!='\n');
		} 
		
		
		
		
		
			//Set tab execution area
		if(set==1)//The choice is to make yes
		{
			printf("***************That's history****************\n"); 
		} 
		else//No or no 
		{
			//In order to make the layout beautiful, the header file "include < windows. H > is used to clear the screen after each calculation 
			system("cls") ;
		}
		
		
	


	}while(1);
	
	
	 
	return 0;
}


The Sixth Edition

2020.3.21 12:30

Version characteristics

Fixed some bug s: for example, in addition, subtraction, multiplication and division mode, judging whether the user's input is legal will result in judgment errors
Improve the base conversion, optimize the base conversion interface and operation on the basis of the previous generation, and improve the user experience

Notice

Professional calculation may appear in later versions: including calculation of mathematical functions and description of functions

Design thinking

Flow chart

Problems encountered in the design process and improvement methods

Code

#include<stdio.h>
#Include < conio. H > / / key for getch() to read user input 
#Include < windows. H > / / for screen cleaning 
#include<math.h> 
int main()
{
	int set;//It is used to set the tab. The reason is that each cycle can be used without changing the value of set, so as not to affect the next cycle 
	int point=2;//It is used to set the tab. n decimal places are reserved for the output number, and two decimal places are reserved by default 
	double i,j,k,sum,num,left;//Participate in calculation: addition, subtraction, multiplication and division, base conversion 
	int ip,jp,kp,sump,nump,leftp;//Participate in calculation: addition, subtraction, multiplication and division, base conversion 

	//Multiple execution procedures 
	do{
		//menu
		printf("---------------------menu--------------------\n") ;
		printf("↑: Add, subtract, multiply and divide                                 \n") ;
		printf("↓: Binary conversion                                 \n") ;
		printf("←: Professional Computing                                 \n") ;
		printf("Home: Set up                                   \n") ;
		printf("Esc: Exit procedure                                \n") ;
		printf("---------------------------------------------\n") ;
		
		//Judge user input options 
		printf("Press the key to access the options:\n");
		char userput,userput0;
		
		userput0=getch();//First call 
		userput=getch();//The second call, this time the actual value 
		
		//Addition, subtraction, multiplication and division 
		if(userput==72)//72 means up arrow of user input 
		{	
			printf("---------------------------------------------\n"); 
			printf("|               Addition, subtraction, multiplication and division mode                |\n"); 
			printf("---------------------------------------------\n"); 
	
			double sum;
			printf("Please enter the first number:");
			while(scanf("%lf",&sum)!=1) {//Scanf can be executed in while, and scanf has its own return value. If you enter a variable correctly, it can return 1 
				printf("illegal input,Please re-enter\n");
				while(getchar()!='\n');//When the user enters a wrong format, getchar() is used to take the first character in the buffer, so that the write can clear the buffer, so as to avoid the error of ch=getchar() 
			}
			char ch;
			int t;
			num=sum;//The function of num: when the value of sum is temporarily stored for the final output of the service, the operation expression is correct 
			
			
			//Multiple input and calculation of single group of data 
			for(t=2;getchar()!='=';t++){//The purpose of t is to enter the number for the following prompt 
		        while(1) {
		        	printf("Please enter operator and%d Number(Such as:+8,with==Sign out):",t);
		        	ch=getchar();
					//scanf("%lf",&j);
					if(ch=='=')
					{
						printf("About to quit.............\n");
						system("pause");
						break;
					}
					else if(scanf("%lf",&j)!=1||(ch!='+'&&ch!='-'&&ch!='*'&&ch!='/'&&ch!='='))
					{
		        		printf("illegal input,Please re-enter\n");
		        		while(getchar()!='\n');//Clear cache 
					}else break; 
				}
				
				//Judge operator, make corresponding operation 
				switch(ch){
					case'+':sum+=j;break;
					case'-':sum-=j;break;
					case'*':sum*=j;break;
					case'/':sum/=j;break;
				}
				printf("%lf%c%lf=%.*lf\n",num,ch,j,point,sum);
				num=sum;//The correct value is displayed when the calculation result is output__*__=__ 
			}
			
		}
		
		//Binary conversion
		else if(userput==80) 
		{
			do{ 
			printf("---------------------------------------------\n"); 
			printf("|               Base conversion mode                |\n"); 
			printf("|1.binary mode                               |\n"); 
			printf("|2.octal mode                                |\n"); 
			printf("|3.Decimal Mode                                |\n"); 
			printf("|4.Hex mode                             |\n"); 
			printf("---------------------------------------------\n"); 
		
			//Improvement
			printf("Please enter the serial number to enter the mode:");
			while(scanf("%d",&userput)!=1) {
				printf("illegal input,Please re-enter\n");
				while(getchar()!='\n');//Clear cache 
			}
			//Decimal to other base 
			if(userput==3)
			{
			do{
				printf("---------------------------------------------\n"); 
				printf("|                Decimal Mode                  |\n"); 
				printf("---------------------------------------------\n"); 
				printf("Please enter a decimal integer:");
				while(scanf("%d",&ip)!=1) {
					printf("illegal input,Please re-enter\n");
					while(getchar()!='\n');//Clear cache 
				}
				//Handle conversion of other base numbers to binary numbers
				kp=ip;//The value of temporary IP is decomposed as follows 
				sump=0;//Initial sum = 0 
				for(nump=1;ip>0;nump*=10) {
					leftp=ip%2;//Remainder 
					ip/=2;//Rounding 
					sump+=nump*leftp;//Reverse order addition 
				}
				ip=kp;//IP changes back to the original user input value 
				printf("Decimal number%d The binary number of is%d\n",ip,sump);
				printf("Decimal number%d The octal number of is%o\n",ip,ip);
				printf("Decimal number%d The hexadecimal number of is%x\n",ip,ip);
				
				printf("Press any key to continue conversion( Esc Go back to the previous menu)\n");//Borrow the function of exit program 
				userput0=getch();
			}while(userput0!=27);
			}
			//Octal to other 
			else if(userput==2) 
			{
				printf("---------------------------------------------\n"); 
				printf("|                octal mode                  |\n"); 
				printf("---------------------------------------------\n"); 
				printf("Please enter octal number:"); 
				scanf("%d",&ip);
				//Handle conversion of other base numbers to binary numbers
				kp=ip;//The value of temporary IP should be decomposed as follows 
				sump=0;//Initial sum = 0 
				for(nump=1;ip>0;nump*=10) {
					leftp=ip%2;//Remainder 
					ip/=2;//Rounding 
					sump+=nump*leftp;//Reverse order addition 
				}
				ip=kp;//IP changes back to the original user input value 
				printf("Octal number%o The binary number of is%d\n",ip,sump);
				printf("Octal number%o The decimal number of is%d\n",ip,ip);
				printf("Octal number%o The hexadecimal number of is%x\n",ip,ip);
				
			}
			else if(userput==4) 
			{
				printf("---------------------------------------------\n"); 
				printf("|               Hex mode                |\n"); 
				printf("---------------------------------------------\n"); 
				printf("Please enter hexadecimal number:"); 
				scanf("%d",&ip);
				//Handle conversion of other base numbers to binary numbers
				kp=ip;//The value of temporary IP is decomposed as follows 
				sump=0;//Initial sum = 0 
				for(nump=1;ip>0;nump*=10) {
					leftp=ip%2;//Remainder 
					ip/=2;//Rounding 
					sump+=nump*leftp;//Reverse order addition 
				}
				ip=kp;//IP changes back to the original user input value 
				printf("Hexadecimal number%x The binary number of is%d\n",ip,sump);
				printf("Hexadecimal number%x The decimal number of is%d\n",ip,ip);
				printf("Hexadecimal number%x The hexadecimal number of is%x\n",ip,ip);
			}
			else if(userput==1)
			{
				printf("---------------------------------------------\n"); 
				printf("|                binary mode                 |\n"); 
				printf("---------------------------------------------\n"); 
				do{
					printf("Please enter binary number:") ;
					scanf("%d",&ip) ;
					//Binary to decimal
					kp=ip;
					sump=0;
					for(nump=0;ip>0;nump++){
						leftp=ip%10;// Get the last one 
						ip/=10;//Remove the last digit
						if(leftp!=1&&leftp!=0) {
							printf("Illegal binary, please operate again!\n");
							break;
						}else if(leftp==1){
							sump+=pow(2,nump);
						}else if(leftp==0){
						}
					}
				}while(ip>0);
				printf("Binary number%d The octal number of is%o\n",kp,sump);
				printf("Binary number%d The decimal number of is%d\n",sump,sump);
				printf("Binary number%d The hexadecimal number of is%x\n",sump,sump);
				
			} 
				 
			
			
			
			
				printf("You're in base conversion mode") ;
				printf("Press any key to continue( Esc Back to main menu)\n");//Borrow the function of exit program 
				userput0=getch();
			}while(userput0!=27) ;
		}
	
		
		//Exit procedure 
		else if(userput==27)
		{
			printf("---------------------------------------------\n"); 
			printf("|                  Exit procedure                 |\n"); 
			printf("---------------------------------------------\n"); 
			break;
		}
			
		//Settings tab
		
			//Judgement area 
		else if(userput==71)
		{
			printf("---------------------------------------------\n"); 
			printf("|                    Set up                   |\n"); 
			printf("|1.Refresh screen                                 |\n"); 
			printf("|2.Reserved decimal                                 |\n"); 
			printf("---------------------------------------------\n"); 
			char setchoose;
			printf("Please enter a number to enter the option:") ;
			while(scanf("%c",&userput)!=1) {
				printf("illegal input,Please re-enter\n");
				while(getchar()!='\n');//Clear cache 
			}
			//1. Refresh the screen 
			if(userput=='1')
			{
				printf("Close refresh screen[y/n]:");
				scanf(" %c",&setchoose);//Considering the reading of scanf, the end of the last scanf is \ n. when doing this scanf, count \ n in% c, and make sure to add a space 
				if(setchoose=='y'){
					set=1;
					printf("Modified success\n");
				} else{
					set=0;
				}
			}
			//2. Keep decimal
			else if(userput=='2') 
			{
				printf("How many decimal places do you want to keep? :");
				scanf("%d",&point);//You don't need to go through the execution area because there are several decimal places reserved by default. You need to go through this area to modify them. If you put them in the execution area, they will be modified every time 
			}
			
		}
		
		//Press no key
		else{
			printf("Please press the correct key, enter to continue"); 
			while(getchar()!='\n');
		} 
		
		
		
		
		
			//Set tab execution area
		if(set==1)//The choice is to make yes
		{
			printf("***************That's history****************\n"); 
		} 
		else//No or no 
		{
			//To make the layout beautiful, the header file #include<windows.h> is used to clear the screen after each calculation 
			system("cls") ;
		}
		
		
	


	}while(1);
	
	
	 
	return 0;
}


Tags: C Windows calculator

Posted on Tue, 24 Mar 2020 09:18:30 -0400 by shivers