啊哈磊_编程从这里起步
标题:
大神!怎么让计算器可以计算英文?
[打印本页]
作者:
hundunzhishi
时间:
2012-12-6 19:04
标题:
大神!怎么让计算器可以计算英文?
帮帮忙,我想让计算器可以计算英文数字的,类似于:2a+2a=4a之类,帮帮忙,求助啊!跪下了……
作者:
未来科技公司
时间:
2012-12-8 21:23
printf("%da+%da=%da");其他程序和平常一样
作者:
osblow
时间:
2013-4-22 18:10
今天刚看见这条,动手写一下
#include <stdio.h>
#include <stdlib.h>
/*本方法不够完善,
但可以应付基本的带未知数的运算。
但用乘法的时候,默认是把之前得出的所有带未知数的结果
与之相乘,相当于加减法优先于乘法*/
typedef struct n
{
char sign[10];
float num;
struct n *next;
}number;
int operation;
int count=0;
char temp[20];
number *result;
int mystrcmp(char*,char*);
void mystrcopy(char*,char*,int);
number* init()
{
number *head;
head=malloc(sizeof(number));
head->num=0;
head->sign[0] = 0;
head->next=NULL;
return head;
}
number *invert(char *c)
{
number *one=init();
char *t=c;
int i=0;
while(*t!='\0')
{
if(!(*t<='9'&&*t>='0'))
{
one->sign[i]=*t;
i++;
}
t++;
}
one->sign[i]='\0';
one->num=atof(c);
return one;
}
number *add(number *n)
{
while(n->next!=NULL)n=n->next;
number *one;
one=(number*)malloc(sizeof(number));
one->sign[0]='\0';
one->next=NULL;
n->next=one;
return one;
}
int calculate()
{
number *tempres=result;
number *one=invert(temp);printf("%f%s\n",one->num,one->sign);getch();
if(count==0)
{
tempres->num=one->num;
mystrcopy(tempres->sign,one->sign,0);
count++;
return 1;
}
while(tempres!=NULL)
{
switch(operation)
{
case 1:
if(!mystrcmp(tempres->sign,one->sign))
tempres->num-=one->num;
else
{
tempres=add(tempres);
mystrcopy(tempres->sign,one->sign,0);
tempres->num=0-one->num;
}
count++;
break;
case 2:
if(!mystrcmp(tempres->sign,one->sign))
tempres->num+=one->num;
else
{
tempres=add(tempres);
mystrcopy(tempres->sign,one->sign,0);
tempres->num=one->num;
}
count++;
break;
case 3:
mystrcopy(tempres->sign,one->sign,1);
tempres->num*=one->num;
count++;
break;
case 4:
if(one->num==0)return 0;
else
{
tempres=add(tempres);
tempres->num=1/one->num;
char *t="-1";
mystrcopy(tempres->sign,t,1);
}
count++;
free(one);
return 1;
case 5:
return 0;
default:
return 1;
}
tempres=tempres->next;
}
free(one);
return 1;
}
int main()
{
int i=1;
result=init();
while(1)
{
puts("减(1)\n加(2)\n乘(3)\n除(4)\n等于(5)\n");
puts("每次输入式子之前,选择一种运算:");
scanf("%d",&operation);
printf("输入第%d个式子:\n(要看结果请输入fuck)\n",i);
scanf("%s",temp);
if(!calculate())break;
i++;
}
while(result!=NULL)
{
printf("+ (%.2f%s) ",result->num,result->sign);
result=result->next;
}
getch();
return 0;
}
int mystrcmp(char *a,char *b)
{
int i=0;
char j;
while(1)
{
j=a[i]^b[i];
if(j!=0)
return 1;
if(a[i]==0&&b[i]==0)return 0;
i++;
}
}
void mystrcopy(char *a,char *b,int s)
{
if(s==0)
{
do
{
*a=*b;
a++;
b++;
}while(*b!='\0');
*a='\0';
}else
{
while(*a!='\0')a++;
*a='*';
a++;
do
{
*a=*b;
a++;
b++;
}while(*b!='\0');
*a='\0';
}
}
作者:
osblow
时间:
2013-4-22 19:03
似乎除法那有问题改了一下
#include <stdio.h>
#include <stdlib.h>
/*本方法不够完善,
但可以应付基本的带未知数的运算。
但用乘除法的时候,默认是把之前得出的所有带未知数的结果
与之相乘除,相当于加减法优先于乘法*/
typedef struct n
{
char sign[10];
float num;
struct n *next;
}number;
int operation;
int count=0;
char temp[20];
number *result;
int mystrcmp(char*,char*);
void mystrcopy(char*,char*,int);
number* init()
{
number *head;
head=malloc(sizeof(number));
head->num=0;
head->sign[0] = 0;
head->next=NULL;
return head;
}
number *invert(char *c)
{
number *one=init();
char *t=c;
int i=0;
while(*t!='\0')
{
if(!(*t<='9'&&*t>='0'))
{
one->sign[i]=*t;
i++;
}
t++;
}
one->sign[i]='\0';
one->num=atof(c);
return one;
}
number *add(number *n)
{
while(n->next!=NULL)n=n->next;
number *one;
one=(number*)malloc(sizeof(number));
one->sign[0]='\0';
one->next=NULL;
n->next=one;
return one;
}
int calculate()
{
number *tempres=result;
number *one=invert(temp);
if(count==0)
{
tempres->num=one->num;
mystrcopy(tempres->sign,one->sign,0);
count++;
return 1;
}
if(operation==4)
if(one->num!=0)
{
char *t="-1";
mystrcopy(one->sign,t,1);
one->num=1/one->num;
}
else
return 1;
while(tempres!=NULL)
{
switch(operation)
{
case 1:
if(!mystrcmp(tempres->sign,one->sign))
tempres->num-=one->num;
else
{
tempres=add(tempres);
mystrcopy(tempres->sign,one->sign,0);
tempres->num=0-one->num;
}
count++;
break;
case 2:
if(!mystrcmp(tempres->sign,one->sign))
tempres->num+=one->num;
else
{
tempres=add(tempres);
mystrcopy(tempres->sign,one->sign,0);
tempres->num=one->num;
}
count++;
break;
case 3:case 4:
mystrcopy(tempres->sign,one->sign,1);
tempres->num*=one->num;
count++;
break;
/*case 4:
if(one->num==0)return 0;
else
{
char *t="-1";
mystrcopy(one->sign,t,1);
mystrcopy(tempres->sign,one->sign,1);
tempres->num/=one->num;
}
count++;*/
case 5:
return 0;
default:
return 1;
}
tempres=tempres->next;
}
free(one);
return 1;
}
int main()
{
int i=1;
result=init();
while(1)
{
puts("减(1)\n加(2)\n乘(3)\n除(4)\n等于(5)\n");
puts("每次输入式子之前,选择一种运算:");
scanf("%d",&operation);
printf("输入第%d个式子:\n(要看结果请输入fuck)\n",i);
scanf("%s",temp);
if(!calculate())break;
i++;
}
while(result!=NULL)
{
printf("+ (%.2f%s) ",result->num,result->sign);
result=result->next;
}
getch();
return 0;
}
int mystrcmp(char *a,char *b)
{
int i=0;
char j;
while(1)
{
j=a[i]^b[i];
if(j!=0)
return 1;
if(a[i]==0&&b[i]==0)return 0;
i++;
}
}
void mystrcopy(char *a,char *b,int s)
{
if(s==0)
{
do
{
*a=*b;
a++;
b++;
}while(*b!='\0');
*a='\0';
}else
{
while(*a!='\0')a++;
*a='*';
a++;
do
{
*a=*b;
a++;
b++;
}while(*b!='\0');
*a='\0';
}
}
欢迎光临 啊哈磊_编程从这里起步 (https://bbs.codeaha.com/)
Powered by Discuz! X3.2