对地址操作会改变数值,切记
time :2017.5.4
事件:用C语言实现四则运算
#include<stdio.h>
#include<windows.h>
void Print_out();
char * Add(char * a, char * b);
char * Div(char * a, char * b);
char * Sub(char * a, char * b);
char * Mul(char * a, char * b);
char * change_order(char * out);
#define COUNTS 100
int main(int argc, char *argv[])
{
char input_number1[COUNTS+1]={'\0'};
char input_number2[COUNTS+1]={'\0'};
char *output_number;
char operator_char;
int choice = 0;
do{
Print_out();
printf("please input the first number:");
scanf("%s", input_number1);
getchar();
printf("please input the operator:");
scanf("%c", &operator_char);
printf("please input the second number:");
scanf("%s", input_number2);
switch(operator_char)
{
case '+': output_number = Add(input_number1, input_number2); break;
case '-': output_number = Sub(input_number1, input_number2); break;
case '*': output_number = Mul(input_number1, input_number2); break;
case '/': output_number = Div(input_number1, input_number2); break;
default : printf("Input error!"); break;
}
printf("The Result is:");
printf("%s", output_number);
printf("\n Are you contuin? 1-yes or 2-no:");
scanf("%d", &choice);
system("cls");
}while(choice == 1);
return 0;
}
void Print_out()
{
printf("--------Welcome to the calculator----------\n");
printf("********************************************\n");
return;
}
char * Add(char * a, char *b)
{
int add1[COUNTS] = {0}, add2[COUNTS] = {0};
int sum[COUNTS + 1] = {0};
int i = 0;
int j = 0;
int asd = 0;
int he = 0;
int temp = 0;
int m = 0;
int n = 0;
int jia = 0;
//处理add1
i = 0;
while(a[i]!='\0')
{
add1[i]=a[i]-'0';
i++;
}
m=i;// 赋值
j = 0;
while(i>j)//使个位在add[0]上
{
temp=add1[i-1];
add1[i-1]=add1[j];
add1[j]=temp;
i--;
j++;
}
//处理add2
j = 0;
while(b[j]!='\0')
{
add2[j] = b[j]-'0';
j++;
}
n = j;
i = 0;
while(j>i)
{
temp=add2[j-1];
add2[j-1]=add2[i];
add2[i]=temp;
j--;
i++;
}
//加法实现
i=0;
asd=0;
while(i <= m||i <= n)
{
he=add1[i]+add2[i];
if(he>9)
{
jia=he-10+asd;
sum[i]=jia;
asd=1;
}
else
{
jia=he+asd;
asd=0;
sum[i] = jia;
}
i++;
}
//处理输出,将int转为char
if(sum[i-1]!=0)
{
a[i-1] = sum[i-1] + '0';
}
while(i>=2)
{
a[i-2] = sum[i-2] + '0';
i=i-1;
}
a = change_order(a);
return a;
}
char * change_order(char * out)
{
int i = 0;
int j = 0;
int temp = 0;
while(out[j]!='\0')
{
j++;
}
while(j>i)
{
temp=out[j-1];
out[j-1]=out[i];
out[i]=temp;
j--;
i++;
}
return out;
}
char * Sub(char * a, char * b)
{
int add1[COUNTS] = {0}, add2[COUNTS] = {0};
int sum[COUNTS + 1] = {0};
int i = 0;
int j = 0;
int op = 0;
int asd = 0;
int he = 0;
int temp = 0;
int m = 0;
int n = 0;
int jia = 0;
//str->int(a)
while(a[i]!='\0')
{
add1[i]=a[i]-'0';
i++;
}
m=i;//m代表位数
j = 0;
while(i>j)
{
temp=add1[i-1];
add1[i-1]=add1[j];
add1[j]=temp;
i--;
j++;
}
j=0;
while(b[j]!='\0')
{
add2[j]=b[j]-'0';
j++;
}
n=j;
i = 0;
while(j>i)
{
temp=add2[j-1];
add2[j-1]=add2[i];
add2[i]=temp;
j--;
i++;
}
//个位的索引值为0;
i=0;
asd=0;
op=0;
while(i<m||i<n)
{
he=add1[i]-add2[i];
if(he<0)
{
jia=he+10+asd;
sum[i]=jia;
asd=-1;
}
else
{
jia=he+asd;
asd=0;
sum[i]=jia;
}
i++;
}
//i=max(m,n);
if(sum[i-1]!=0)
{
a[i-1] = sum[i-1] + '0';
}
else
{
a[i-1] = '\0';
}
while(i>=2)
{
a[i-2] = sum[i-2] + '0';
i=i-1;
}
a = change_order(a);
return a;
}
char * Mul(char * a, char * b)
{
int add1[COUNTS+1] = {0};
char c[COUNTS+1] = {'\0'};
int i = 0;
int m = 0;
int j = 0;
int x = 0;
int n = 0;
while(b[j]!='\0')
{
c[j] = b[j];
j++;
}
n = j;
i = 0;
while(a[i]!='\0')
{
add1[i]=a[i]-'0';
i++;
}
m=i;// 赋值
i=0;
while(i<m)
{
if(add1[i] == 1)
{
}
else
{
if(i==0)
{
for(j=1;j<add1[i]; j++)
{
if(j == 1)
{
a = Add(c,b);
printf("%s\n", a);
}
else
{
a = Add(a, b);
printf("%s\n", a);
}
}
}
else
{
for(j=1;j<=add1[i]; j++)
{
if(j == 1)
{
a = Add(c,b);
printf("%s\n", a);
}
else
{
a = Add(a, b);
printf("%s\n", a);
}
}
}
}
b[n] = '0';
x = 0;
while(a[x]!= '\0')
{
c[x] = a[x];
x++;
}
n++;
i++;
}
return a;
}
char * Div(char * a, char * b)
{
return a;
}
还未完成