简单计算器的实现

简单计算器的实现

思路

  1. 以字符串形式读入每一个数
  2. 将每个字符转化为对应的数字,并以单链表存储
  3. 根据选择进行相应的操作

代码实现

#include"shenming.h"
#include"shixiang.h"
int main(){
	int n;
	while(1){
		map();
		printf("请输入你的选择:");
		scanf("%d",&n);
		switch(n){
			case 1 :
				_add();
				break;
			case 2 :
				_sub();
				break;
			case 3 :
				_mul();
				break;
			case 4 :
				_div();
				break;
			case 5 :
				exit(-1);
			default:
				printf("输入错误,退出!!!");
				exit(0);
		}
	}
}

自定义头文件

shenming.h
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h> 
int length1=0,length2=0;
typedef struct node{
	int data;
	struct node *next;		//声明链表 
}node;
void print(node *p);
void dele(node *s);
void map();

//chenfa
node *add2(node *first1,node *first2);
node *add1(node *nums[]);
node *divid(node *first,int a,int i);
node *mul(node *first1,node *first2);
node *change(char a[]);
void _mul();

//chufa
node *div(node *first1,int b);
node * change1(char a[]);
void _div();

//jiafa
node *add(node *first1,node *first2);
void _add();

//jianfa
int compete(node *first1,node *first2);
node *sub(node *first1,node *first2);
void _sub();


shixiang.h

//打印链表 
void print(node *p){
	while(p->next!=NULL){
		p=p->next;
		printf("%d",p->data);
	}
	printf("\n");
}
//清除 
void dele(node *s){
	node *p;
	while(s!=NULL){
		p=s;
		s=s->next;
		free(p);
	}
}

void map(){
	printf(
	"——————————————————————————1.加法——————————————————————————\n"
	"——————————————————————————2.减法——————————————————————————\n"
	"——————————————————————————3.乘法——————————————————————————\n"
	"——————————————————————————4.除法——————————————————————————\n"
	"——————————————————————————5.退出——————————————————————————\n"
	);
}

node *change(char a[]){
	node *head=(node *)malloc(sizeof(node));
	head->next=NULL;
	for(int i=0;i<strlen(a);i++){
		int temp=a[i]-'0';
		node *p=(node *)malloc(sizeof(node));		//头插法,逆序 
		p->data=temp;
		p->next=head->next;
		head->next=p;
	}
	return head;
}

//相乘部分 
node *add2(node *first1,node *first2){
	int a=0,c=0,d=0,f=0;
	node *result=(node *)malloc(sizeof(node));
	node *s=result,*end;
	result->next=NULL;
	while(first1->next!=NULL&&first2->next!=NULL){
		first1=first1->next;first2=first2->next;
		a=first1->data;c=first2->data;
		d=a+c;
		if(f!=0){
			d+=f;
			f=0;
		}
		f=d/10;
		d=d%10;
		node *p=(node *)malloc(sizeof(node));
		p->data=d;
		s->next=p;//尾插法 
		s=p;
	}

		while(first1->next!=NULL){
			first1=first1->next;
			node *p=(node *)malloc(sizeof(node));
			
			if(f!=0){
				first1->data+=f; 
				f=0;
			}
			if(first1->data>=10){
				f=first1->data/10;
				first1->data=first1->data%10;
				
			}
			p->data=first1->data;
			s->next=p;//尾插法 
			s=p;
		}
		while(first2->next!=NULL){
			first2=first2->next;
			node *p=(node *)malloc(sizeof(node));
			if(f!=0){
				first2->data+=f;
				f=0;
			}
			if(first2->data>=10){
				f=first2->data/10;
				first2->data=first2->data%10;
			}
			p->data=first2->data;
			s->next=p;//尾插法 
			s=p;
		}
	s->next=NULL;
	return result;
}

node *add1(node *nums[]){
	node *sta[100];
	int top=-1;
	for(int i=0;i<length2;i++){
		top+=1;
		sta[top]=nums[i];
	}
	while(top){
		node *p1,*p2;
		p1=sta[top--];
		p2=sta[top--];
		sta[++top]=add2(p1,p2);
	}
	return sta[top];
}

node *divid(node *first,int a,int i){
	int y=0,x,t;
	node *head=(node *)malloc(sizeof(node));
	node *s=head;
	while(i){
		node *p=(node *)malloc(sizeof(node));//填充0 
		p->data=0;
		s->next=p;
		s=p;
		i--;
	}
	while(first->next!=NULL){
		first=first->next;
		x=first->data;
		t=x*a;
		if(y!=0){
			t+=y;
			y=0;
		}
		y=t/10;
		t=t%10;
		node *p=(node *)malloc(sizeof(node));
		p->data=t;
		s->next=p;
		s=p;
	}
	if(y!=0){
		node *p=(node *)malloc(sizeof(node));
		p->data=y;
		s->next=p;
		s=p;
	}
	s->next=NULL;
	return head;
}
node *mul(node *first1,node *first2){
	int c=0;
	node *nums[length2];
	node *result1=(node *)malloc(sizeof(node)),*result=(node *)malloc(sizeof(node));
	result->next=NULL;
	for(int i=0;i<length2;i++){
		first2=first2->next;
		int x=first2->data;
		nums[i]=divid(first1,x,i);
	}
	result1=add1(nums);
	while(result1->next!=NULL){
		result1=result1->next;
		node *p=(node *)malloc(sizeof(node));
		p->data=result1->data;
		p->next=result->next;
		result->next=p;
	}
	return result;
}

void _mul(){
	int a;
	char c;
	char str1[100],str2[100];
	node *first1,*first2;
	
	printf("请输入被乘数:");
	scanf("%s",str1);
	first1=change(str1);
	
	printf("请输入乘数:");
	scanf("%s",str2);
	first2=change(str2);
	
	length1=strlen(str1);
	length2=strlen(str2);
	node *result=mul(first1,first2);
	printf("结果是:");
	
	print(result);
	dele(first1);
	dele(first2);
	dele(result);
}

//相除部分 
node *div(node *first1,int b){
	node *result=(node *)malloc(sizeof(node)),*s=result;
	node *w=result;
	int t,sum=0,x,i;
	if(length1<=length2){
		
		for(i=length1-1;i>=0;i--){
			first1=first1->next;
			t=first1->data*pow(10,i);
			sum+=t;
		}
		node *p=(node *)malloc(sizeof(node));
		p->data=sum/b;
		s->next=p;
		s=p;
	}
	else{
		while(first1->next!=NULL){
			first1=first1->next;
			sum*=10;
			sum+=first1->data;
			x=sum/b;
			sum%=b;
			node *p=(node *)malloc(sizeof(node));
		    p->data=x;
			s->next=p;
			s=p;
		}
		int m=length1-1;
		while(m--){
			node *z=w->next;
			if(z->data==0){
			w->next=z->next;
			free(z);
			}
			else
				break;
		}
	}
	s->next=NULL;
	return result;
}

node * change1(char a[]){
	node *head=(node *)malloc(sizeof(node));
	node *s=head;
	for(int i=0;i<strlen(a);i++){
		int temp=a[i]-'0';
		node *p=(node *)malloc(sizeof(node));		
		p->data=temp;
		s->next=p;//尾插法 
		s=p;
	}
	s->next=NULL;
	return head;
}

void _div(){
	int a;char c,str1[100];
	int b,t;
	node *first1;

	printf("请输入被除数:");
	scanf("%s",str1);
	first1=change1(str1);
	length1=strlen(str1);
	
	printf("请输入除数:");
	scanf("%d",&b);
	t=b;
	while(t!=0){
		t/=10;
		length2++;
	}
	node *result=div(first1,b);
	printf("结果是:");
	print(result);
	dele(first1);
	dele(result);
}

//相加部分
node *add(node *first1,node *first2){
	int a=0,c=0,d=0,f=0;
	node *result=(node *)malloc(sizeof(node));
	node *s=result,*end;
	result->next=NULL;
	while(first1->next!=NULL&&first2->next!=NULL){
		first1=first1->next;first2=first2->next;
		a=first1->data;c=first2->data;
		d=(a+c)%10;
		if(f!=0){
			d+=f;	
			f=0;
		}
		f=(a+c)/10;
		node *p=(node *)malloc(sizeof(node));
		p->data=d;
		p->next=s->next;
		s->next=p;
	}
		while(first1->next!=NULL){
			first1=first1->next;
			node *p=(node *)malloc(sizeof(node));
			if(f!=0){
				first1->data+=f;
				f=0;
			}
			p->data=first1->data;
			p->next=s->next;
			s->next=p;
		}
		while(first2->next!=NULL){
			first2=first2->next;
			node *p=(node *)malloc(sizeof(node));
			
			if(f!=0){
				first1->data+=f;	
				f=0;
			}
			
			p->data=first2->data;
			p->next=s->next;//头插法 
			s->next=p;
		}
	return result;
}

void _add(){
	int a;char c;
	char str1[100],str2[100];
	node *first1,*first2;
	
	
	printf("请输入第一个数:");
	scanf("%s",str1);
	first1=change(str1);
	
	printf("请输入第二个数:");
	scanf("%s",str2);
	first2=change(str2);
	
	node *result=add(first1,first2);
	printf("结果是:");
	print(result);
	dele(first1);
	dele(first2);
	dele(result);
}

//减法部分
//比较 
int compete(node *first1,node *first2){
	node *s1=(node *)malloc(sizeof(node));
	node *s2=(node *)malloc(sizeof(node));
	
	s1->next=NULL;s2->next=NULL;
	
	while(first1->next!=NULL){
		first1=first1->next;first2=first2->next;
		
		node *p=(node *)malloc(sizeof(node));
		p->data=first1->data;
		p->next=s1->next;
		s1->next=p;
		
		node *q=(node *)malloc(sizeof(node));
		q->data=first2->data;
		q->next=s2->next;
		s2->next=q;
	}
	while(s1->next!=NULL){
		s1=s1->next;s2=s2->next;
		if((s1->data)>(s2->data)){
			return 1;
		}
	}
	return 0;
}

//相减 
node *sub(node *first1,node *first2){
	int a=0,c=0,d=0,f=0,b;
	node *result=(node *)malloc(sizeof(node));
	node *q=result;
	result->next=NULL;
	node *s=result;
	if(length1>length2){
		while(first2->next!=NULL){
		first1=first1->next;first2=first2->next;
		a=first1->data;
		b=first2->data;
		if(a<b){
			a+=10;
			first1->next->data-=1;		//相减 
		}
		d=a-b;
		node *p=(node *)malloc(sizeof(node));
		p->data=d;
		p->next=s->next;
		s->next=p;
		}
		while(first1->next!=NULL){
			first1=first1->next;
			node *p=(node *)malloc(sizeof(node));//处理残余数 
			p->data=first1->data;
			p->next=s->next;
			s->next=p;
		}
		while(length1--){
			node *z=q->next;
			if(z->data==0){		//清除前导 0 
			q->next=z->next;
			free(z);
			}
		}	
	}
	else if(length1==length2){
		int flag=compete(first1,first2);
		if(flag){
			while(first1->next!=NULL){
				first1=first1->next;first2=first2->next;
				a=first1->data;
				b=first2->data;
				if(a<b){
					a+=10;
					first1->next->data-=1;		//相减 
				}
				d=a-b;
				node *p=(node *)malloc(sizeof(node));
				p->data=d;
				p->next=s->next;
				s->next=p;
			}
		while((q->next->data==0)&&length1){
			node *z=q->next;
			if(z->data==0){
			q->next=z->next;		//清除前导 0  
			free(z);
			}
			length1--;
		}	
		}
		
		
	else{
	while(first1->next!=NULL){
				first1=first1->next;first2=first2->next;
				a=first2->data;
				b=first1->data;
				if(a<b){
					a+=10;
					first2->next->data-=1;		//相减 
				}
				d=a-b;
				node *p=(node *)malloc(sizeof(node));
				p->data=d;
				p->next=s->next;
				s->next=p;
			}
		while((q->next->data==0)&&length1){
			node *z=q->next;
			if(z->data==0){
			q->next=z->next;			//清除前导 0  
			free(z);
			}
			length1--;
		}
		if(result->next->data!=0) result->next->data=-result->next->data;	//判断,是否将头部置为负		
	}	
	}
	
	
	else{
		while(first1->next!=NULL){
		first1=first1->next;first2=first2->next;
		a=first2->data;
		b=first1->data;
		if(a<b){
			a+=10;
			first2->next->data-=1;	//相减 
		}
		d=a-b;
		node *p=(node *)malloc(sizeof(node));
		p->data=d;
		p->next=s->next;
		s->next=p;
		}
		while(first2->next!=NULL){
			first2=first2->next;
			node *p=(node *)malloc(sizeof(node));	//处理残余数 
			p->data=first2->data;
			p->next=s->next;
			s->next=p;
		}
		while(length2--){
			node *z=q->next;
			if(z->data==0){			//清除前导 0 
			q->next=z->next;
			free(z);
			}
		}
		result->next->data=-result->next->data;		//头部置为负	
	}
	return result;
}


void _sub(){
	char c;
	char str1[100],str2[100];
	node *first1,*first2;
	printf("请输入被减数:");
	scanf("%s",str1);
	first1=change(str1);
	printf("请输入减数:");
	scanf("%s",str2);
	first2=change(str2);
	length1=strlen(str1);
	length2=strlen(str2);
	node *result=sub(first1,first2);
	printf("结果是:");
	print(result);
	dele(first1);
	dele(first2);
	dele(result);
} 

总结

  1. 熟悉了多文件编程
  2. 对链表,栈等数据结构有了更深的了解
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值