Mọi người chỉ giúp mình chuyển code c++ sang c với ạ. em chuyển cout thành printf nhưng chạy ko đúng ạ
#include <iostream>
#include <stack>
#include <cmath>
#include <stdio.h>
#include <conio.h>
//#include <string>
#include <cstring>
using namespace std;
int KT_BT(char BT[])
{
stack < char > s;
int l,i,Xet;
l=strlen(BT);
i=0;
Xet=1;
while ((i<l)&&(Xet==1))
{
if(BT[i]=='(')
s.push(BT[i]);
else
{
if (BT[i]==')'&&(!s.empty()))
s.pop();
else
Xet=0;
}
i++;
}
if((i<l)||(!s.empty()))
Xet=0;
return Xet;
}
int UT(char x)
{
if (x == '+' || x == '-')
return 1 ;
if (x == '*' || x == '/' || x == '%')
return 2;
return 0;
}
bool LaSo(char x)
{
return (x>='0' && x<='9');
}
bool LaTT(char x)
{
return ((x=='+')||(x=='-')||(x=='*')||(x=='/'));
}
string Chuyen_HauTo(string TT)
{
stack < char > ss;
int i,l;
string HT="";
l=TT.length();
for (i=0;i<l;i++)
{
if(LaSo(TT[i]))
{
while (LaSo(TT[i]))
HT += TT[i++];
HT +=' ';
}
if(TT[i]=='(')
ss.push(TT[i]);
if(TT[i]==')')
{
while (ss.top()!='(')
{
HT += ss.top();
ss.pop();
HT +=' ';
}
ss.pop();
}
if(LaTT(TT[i]))
{
if (!ss.empty() && (UT(ss.top())>= UT(TT[i])))
{
HT += ss.top();
HT += ' ';
ss.pop();
}
ss.push(TT[i]);
}
}
while (!ss.empty())
{
HT += ss.top();
HT += ' ';
ss.pop();
}
return HT;
}
int TinhHauTo(string HT)
{
stack <int> ss;
int a = 0,x,y,z;
int l=HT.length();
int i=0;
while (i<l)
{
if(HT[i]!=' ')
{
if (LaSo(HT[i]))
{
while (LaSo(HT[i]))
a = a * 10 + HT[i++] - 48;
ss.push(a);
a=0;
i++;
}
if(LaTT(HT[i]))
{
x = ss.top();
ss.pop();
y = ss.top();
ss.pop();
if ( HT[i]=='+') z = y + x;
if ( HT[i]=='-') z = y - x;
if ( HT[i]=='*') z = y * x;
if ( HT[i]=='/') z = int(y) / x;
ss.push(z);
i++;
}
}
else
i++;
}
return ss.top();
}
int main()
{
char BT[50];
printf("Nhap chuoi dau ngoac:");
scanf("%s",&BT);
if(KT_BT(BT)==1)
printf("Dung quy tac\n");
else
printf("Sai quy tac\n");
string a,b;
int c;
cout<< "Nhap bieu thuc trung to:";
fflush(stdin);
getline(cin,a);
b=Chuyen_HauTo(a);
cout<< "Bieu thuc hau to:";
cout<<b;
cout<<"\nKet qua bieu thuc hau to:";
c=TinhHauTo(b);
cout<<c;
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?