刀992 2022-06-09 23:04 采纳率: 100%
浏览 162
已结题

在dev中能运行,在VS中运行不了

这段代码在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”的声明

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

2条回答 默认 最新

  • Hann Yang 全栈领域优质创作者 2022-06-09 23:58
    关注

    报错内容贴出来看看呢。 

    devc++中能运行,说明语法是没有错的;猜想原因是两个软件的string.h库不一致引起的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月18日
  • 已采纳回答 6月10日
  • 修改了问题 6月10日
  • 创建了问题 6月9日

悬赏问题

  • ¥20 matlab中的双层数值积分
  • ¥50 服务器打印水晶报表问题
  • ¥15 Delphi image图片缩放问题
  • ¥30 gradle环境下javafx项目如何使用druid连接池
  • ¥15 服务器打印水晶报表问题
  • ¥15 初学者用plt报错,求解答
  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题