hihi_pipi33
21-10-2008, 21:20
Đây là code của CT:tính giá trị biểu thức hậu tố!Biểu thức đc nhập vào từ bàn phím!Tôi viết hoài mà máy vẫn báo lỗi ,mong các bạn sửa giùm lỗi!
// lỗi "Pointers and fixed size buffers may only be used in an unsafe context", ở hàm push và get.
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using t=System.Console;
namespace BTCTDLva_giai_thuat
{
class stack1
{
private int count;
private int[] data;
public void init()
{
// khoi tao stack
count = -1;
data = new double[n];
}
public bool isempty()
{
// kiem tra ngan xep rong
return (count == -1);
}
public int get(float* th)
//MAY BAO LOI O PHAN (FLOAT *TH)
{
// lay phan tu tren cung ngan xep ra kiem tra
if (count < 0)
return 0;
else
{
*th = data[count];
return 1;
}
}
public int push(float th)
// MAY BAO LOI O PHAN (FLOAT TH)
{
// day mot phan tu vao ngan nho
if (count > 99)
return 0;
data[++count] = th;
return;
}
public int pop(float *th)
{
// lay mot phan tu ra khoi ngan nho
if (count < 0)
return 0;
else
{
*th = data[count--];
return 1;
}
}
public void sdisplay()
{
// hien thi cac gia tri trong ngan xep
int j = 0;
for (j = 0; j <= count; j++)
t.WriteLine("%d", data[j]);
}
// khai bao xong ngan xep
// tiep theo la khai bao cac ham xu ly
// Chuong trinh chinh
static void main()
{
// khai bao bien
char[] xauvao;
char ch;
int i = 0, len, done = 1;
float th1, th2, ketqua;
stack a = newstack();
a.init(); // khoi tao ngan xep
// nhap bieu thuc trung to
t.Write("ddanh gia bieu thuc dang hau to");
t.Write(" luu y: 0<=toan hang<=9");
t.Write("toan tu :" + "," - "," * "," / "");
t.Write("chao tat ca cac ban");
do
{
// vong lap
t.Write("ban hay nhap vao mot bieu thuc");
gets(xauvao);
len = strlen(xauvao);
if (len != 0)
{
t.Write("cac buoc thuc hien");
// tinh gia tri bieu thuc hau to
for (i = 0; i <= len - 1; i++)
{
ch = xauvao[i];
if ((ch >= '0') && (ch <= '9'))
{
a.push(ch - '0');
t.Write("\n Stack: ");
a.sdisplay();
get();
}
else
if (ch != ' ')
{
if (a.isempty()) t.Write("\n vu phuong thao");
else
a.pop(&th2);
if (a.isempty()) t.Write("\n nguyen thi nga");
else
a.pop(&th1);
switch (ch)
{
case '+': a.push(th1 + th2);
t.Write("\n Stack: ");
a.sdisplay();
getch();
break;
case '-': a.push(th1 - th2);
t.Write("\n Stack: ");
a.sdisplay();
getch();
break;
case '*': a.push(th1 * th2);
t.Write("\n Stack: ");
a.sdisplay();
getch();
break;
case '/': a.push(th1 / th2);
t.Write("\n Stack: ");
a.sdisplay();
getch();
break;
} //switch
} //if
} // for
a.pop(&ketqua);
t.Write("\n Ket qua bieu thuc la: %5.2f", ketqua);
} // if
else
done = 0;
} while (done);
}
}
}
cảm ơn rất nh`! ming các bạn giúp! tôi đang cần lắm!
// lỗi "Pointers and fixed size buffers may only be used in an unsafe context", ở hàm push và get.
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using t=System.Console;
namespace BTCTDLva_giai_thuat
{
class stack1
{
private int count;
private int[] data;
public void init()
{
// khoi tao stack
count = -1;
data = new double[n];
}
public bool isempty()
{
// kiem tra ngan xep rong
return (count == -1);
}
public int get(float* th)
//MAY BAO LOI O PHAN (FLOAT *TH)
{
// lay phan tu tren cung ngan xep ra kiem tra
if (count < 0)
return 0;
else
{
*th = data[count];
return 1;
}
}
public int push(float th)
// MAY BAO LOI O PHAN (FLOAT TH)
{
// day mot phan tu vao ngan nho
if (count > 99)
return 0;
data[++count] = th;
return;
}
public int pop(float *th)
{
// lay mot phan tu ra khoi ngan nho
if (count < 0)
return 0;
else
{
*th = data[count--];
return 1;
}
}
public void sdisplay()
{
// hien thi cac gia tri trong ngan xep
int j = 0;
for (j = 0; j <= count; j++)
t.WriteLine("%d", data[j]);
}
// khai bao xong ngan xep
// tiep theo la khai bao cac ham xu ly
// Chuong trinh chinh
static void main()
{
// khai bao bien
char[] xauvao;
char ch;
int i = 0, len, done = 1;
float th1, th2, ketqua;
stack a = newstack();
a.init(); // khoi tao ngan xep
// nhap bieu thuc trung to
t.Write("ddanh gia bieu thuc dang hau to");
t.Write(" luu y: 0<=toan hang<=9");
t.Write("toan tu :" + "," - "," * "," / "");
t.Write("chao tat ca cac ban");
do
{
// vong lap
t.Write("ban hay nhap vao mot bieu thuc");
gets(xauvao);
len = strlen(xauvao);
if (len != 0)
{
t.Write("cac buoc thuc hien");
// tinh gia tri bieu thuc hau to
for (i = 0; i <= len - 1; i++)
{
ch = xauvao[i];
if ((ch >= '0') && (ch <= '9'))
{
a.push(ch - '0');
t.Write("\n Stack: ");
a.sdisplay();
get();
}
else
if (ch != ' ')
{
if (a.isempty()) t.Write("\n vu phuong thao");
else
a.pop(&th2);
if (a.isempty()) t.Write("\n nguyen thi nga");
else
a.pop(&th1);
switch (ch)
{
case '+': a.push(th1 + th2);
t.Write("\n Stack: ");
a.sdisplay();
getch();
break;
case '-': a.push(th1 - th2);
t.Write("\n Stack: ");
a.sdisplay();
getch();
break;
case '*': a.push(th1 * th2);
t.Write("\n Stack: ");
a.sdisplay();
getch();
break;
case '/': a.push(th1 / th2);
t.Write("\n Stack: ");
a.sdisplay();
getch();
break;
} //switch
} //if
} // for
a.pop(&ketqua);
t.Write("\n Ket qua bieu thuc la: %5.2f", ketqua);
} // if
else
done = 0;
} while (done);
}
}
}
cảm ơn rất nh`! ming các bạn giúp! tôi đang cần lắm!