这段代码在dev中可以运行,但是到vs里运行不了,该怎么修改才能运行啊
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxn 100005
char s[maxn]={0}, infixList[maxn][10]={0};
char suffix[maxn][10]={0};
int p = 0;
char tmp[maxn]={0};
int p1 = 0;
//把字符串根据操作数、运算符、括号拆开,存储到infixList数组中
int toInfixExpression(char* s)
{
int tot = 0,
n = strlen(s); //字符串长度
for (int i = 0; i < n; i++)
{
if (s[i] >= '0' && s[i] <= '9' || 'A' <= s[i] && s[i] <= 'F') {
int p = 0;
infixList[tot][p++] = s[i];
while (i + 1 < n && (s[i + 1] >= '0' && s[i + 1] <= '9') || ('A' <= s[i+1] && s[i+1] <= 'F')) {
infixList[tot][p++] = s[++i];
}
tot++;//移动到下一位
}
else if (s[i] == ' ')
{
continue;
}
else {
infixList[tot++][0] = s[i];
}
}
return tot; //返回存放位数(放到第几位了)
}
//获得优先级
int getPriority(char c) {
if (c == '+' || c == '-')
return 1;
else if (c == '*' || c == '/')
return 2;
else
return 0; //处理'('的情况
}
//转换成后缀表达式
void parseSuffixExpression(char s[][10], int n) {
for (int i = 0; i < n; i++) {
if (s[i][0] >= '0' && s[i][0] <= '9' || 'A'<= s[i][0] && s[i][0] <='F') {
strcpy_s(suffix[p++], s[i]);
}
else if (s[i][0] == '(') {
tmp[p1++] = s[i][0];
}
else if (s[i][0] == ')') {
while (tmp[p1 - 1] != '(') {
suffix[p++][0] = tmp[--p1];
}
p1--;
}
else {
while (p1 != 0 && getPriority(tmp[p1 - 1]) >= getPriority(s[i][0])) {
suffix[p++][0] = tmp[--p1];
}
tmp[p1++] = s[i][0];
}
}
while (p1 != 0) {
suffix[p++][0] = tmp[--p1];
}
}
int hexToDecimal(char* p) {
int n = 0;
int len = 0, i = 0;
while (p[i] != '\0')
{
len += 1;
i++;
}
for (i = 0; i < len; i++)
{
if ('0' <= p[i] && p[i] <= '9')
{
n = n * 10 + p[i] - '0';
}
else
{
n = n * 10 + p[i] - 55;
}
}
return n;
}
//计算后缀表达式的值
int calculate() {
int tt[50] = { 0 };
char n1[50] = { '\0' };
char n2[50] = { '\0' };
char n3[50] = { '\0' };
int pos = 0;
int m = 1;
for (int i = 0; i < p; i++) {
int k = strlen(suffix[i]);
if (suffix[i][0] >= '0' && suffix[i][0] <= '9' || 'A' <= suffix[i][0] && suffix[i][0] <= 'F') {
//tt[pos++] = hexToDecimal(suffix[i]);
sscanf(suffix[i], "%X", &tt[pos++]);
}
else {
int b = tt[--pos];
sprintf(n2, "%X", b);
int a = tt[--pos];
sprintf(n1, "%X", a);
int res = 0;
if (suffix[i][0] == '+') {
res = a + b;
}
else if (suffix[i][0] == '-') {
res = a - b;
}
else if (suffix[i][0] == '*') {
res = a * b;
}
else if (suffix[i][0] == '/') {
if (b == 0) {
printf("除数不能为0!\n");
return -1;
}
res = a / b;
}
sprintf(n3, "%X", res);
printf("(%d) %s %c %s = %s\n", m++, n1, suffix[i][0], n2, n3);
tt[pos++] = res;
}
}
return tt[0];
}
int main() {
printf("请输入待计算的表达式:");
//fflush(stdin);
memset(s,'\0', maxn);
scanf_s("%[^\n]s", s);
int infixNum = 0;
int infixNum=toInfixExpression(s);
parseSuffixExpression(infixList, infixNum);
int ans=0;
int ans = calculate();
printf("运算结果为:%X\n", ans);
}
报错如下:
documents\visual studio 2010\projects\234567\234567\1.cpp(66): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : 参见“strcpy”的声明
1>c:\users\chen'tao\documents\visual studio 2010\projects\234567\234567\1.cpp(126): warning C4996: 'sscanf': This function or variable may be unsafe. Consider using sscanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(325) : 参见“sscanf”的声明
1>c:\users\documents\visual studio 2010\projects\234567\234567\1.cpp(131): warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(371) : 参见“sprintf”的声明
1>c:\users\documents\visual studio 2010\projects\234567\234567\1.cpp(134): warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(371) : 参见“sprintf”的声明
1>c:\users\documents\visual studio 2010\projects\234567\234567\1.cpp(154): warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(371) : 参见“sprintf”的声明
1>c:\users\documents\visual studio 2010\projects\234567\234567\1.cpp(171): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : 参见“scanf”的声明