kiem tien, kiem tien online, kiem tien truc tuyen, kiem tien tren mang
Tuesday, January 27, 2015


//lập trình c tính giá trị của 1 biểu thức bất kỳ nhập từ bàn phím gồm các phép tính cộng, trừ, nhân, chia (+-/*). biểu thức nhập ở dạng chuỗi. giống máy tính bỏ túi casio
#include<iostream>
#include<string.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
float tinhgiatri(char *s);
void sohang(char *s,float *a,int &n);
void dau(char *s,char *b);
int kiemtra(char *s);
void main()
{
char s[1000],c;
       cout<<"TRẦN KHÁNH TOÀN\n-------------------------------------------------------------------";
loop:;
do
{
cout << "\n->nhap bieu thuc:\n\t";
fflush(stdin);
gets(s);
if(!kiemtra(s)) cout<<"\nnhap bieu thuc khong chua <space>,ky tu,phep tinh lien ke...\n\tNHAP LAI\n\n";
}while(!kiemtra(s));
cout<<"\t= "<<tinhgiatri(s)<<"\n\n\t---------------------\n\t| tiep tuc: <space> |\n\t| ket thuc: bat ky  |\n\t---------------------\n";
if(c=getch()==32) goto loop;
}
float tinhgiatri(char *s)
{
float kq,a[1000];
int i,j,n;
char b[1000];
sohang(s,a,n);
dau(s,b);
for(i=0;i<strlen(b);i++)
{
if(b[i]=='*')
{
(float)a[i]*=(float)a[i+1];
for(j=i+1;j<n-1;j++) a[j]=a[j+1];
n--;
for(j=i;j<strlen(b)-1;j++)b[j]=b[j+1];
b[strlen(b)-1]='\0';
i--;
}
if(b[i]=='/')
{
(float)a[i]/=(float)a[i+1];
for(j=i+1;j<n-1;j++) a[j]=a[j+1];
n--;
for(j=i;j<strlen(b)-1;j++) b[j]=b[j+1];
b[strlen(b)-1]='\0';
i--;
}
}
(float)kq=(float)a[0];
for(i=0;i<strlen(b);i++)
{
if(b[i]=='+') kq+=(float)a[i+1];
else kq-=(float)a[i+1];
}
return kq;
}
void sohang(char *s,float *a,int &n)
{
char z[1000];
int i,k,j,x;
n=0;
for(i=0;i<strlen(s);i++)
{
k=0;
while(s[i]>='0' && s[i]<='9') z[k++]=s[i++];
z[k]='\0';
x=atoi(z);
a[n++]=(float)x;
}
}
void dau(char *s,char *b)
{
int i,k=0;
for(i=0;i<strlen(s);i++)
if(s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/')
b[k++]=s[i];
b[k]='\0';
}
int kiemtra(char *s)
{
if(strlen(s)==0) return 0;
for(int i=0;i<strlen(s);i++)
if(s[i]==' ' ||
(s[i]>='a' && s[i]<='z') ||
(s[i]>='A' && s[i] <='Z') ||
((s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/')&& (s[i+1]=='+' || s[i+1]=='-' || s[i+1]=='*' || s[i+1]=='/')))
return 0;
return 1;
}

0 comments:

Post a Comment

domain, domain name, premium domain name for sales

Popular Posts