练习后缀表达式求值下面的代码为什么运行不了啊
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<stack>
using namespace std;
int OutPut(char exp[])
{
stack<char> S;
int i = 0;
while (1)
{
switch (exp[i])
{
case '/0':
return S.top();
case '*':
case '/':
case '+':
case '-':
int op1 = S.top(); S.pop();
int op2 = S.top(); S.pop();
S.push(op1 + op2);
i++;
break;
default:
S.push(exp[i]);
i++;
break;
}
}
}
int main()
{
char input[] = " ";
printf("请输入后缀式:\n\n");
scanf("%s", input);
printf("原式 = %d\n", OutPut(input));
return 0;
}
修复过后运行效果图, 如有帮助给个采纳谢谢
#include <stdio.h>
#include <stack>
int OutPut(char exp[])
{
std::stack<int> S;
int i = 0;
int op1, op2; // 将变量声明移至 switch 外部
while (1)
{
switch (exp[i])
{
case '\0':
return S.top();
case '*':
case '/':
case '+':
case '-':
op1 = S.top(); S.pop(); // 赋值给 op1
op2 = S.top(); S.pop(); // 赋值给 op2
if (exp[i] == '*') {
S.push(op1 * op2);
} else if (exp[i] == '/') {
S.push(op2 / op1);
} else if (exp[i] == '+') {
S.push(op1 + op2);
} else if (exp[i] == '-') {
S.push(op2 - op1);
}
i++;
break;
default:
if (exp[i] >= '0' && exp[i] <= '9') {
S.push(exp[i] - '0');
i++;
} else {
printf("输入无效\n");
return 0; // 或者进行其他错误处理
}
break;
}
}
}
int main()
{
char input[100];
printf("请输入后缀式:\n\n");
scanf("%s", input);
printf("原式 = %d\n", OutPut(input));
return 0;
}
代码(C语言)双向链式线性表 Visual Studio 2019
/**
* 数据结构 C语言 双向链式线性表
* @FileName Dual_LinkedList.c
* @author W.Lionel.Esaka
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
enum status {
Error = 0,
Corrrect = 1,
Overflow = -1
};
enum status Status;
typedef char ElemType;
typedef struct DualList {
ElemType data;
struct DualList* Previous;
struct DualList* Next;
}DualNode, * DualLinkList;
/*创建指定长度链表 尾插法*/
//如果链表为空,则创建一个链表,指针域指向自己
void ListInit(DualNode** p,int Number)
{
DualNode* temp;
DualNode* target;
int i;
printf("输入节点的值,输入0结束\n");
for (i = 0; i < Number; i++)
{
if (*p == NULL) //如果输入的链表是空。则创建一个新的节点,使其next指针指向自己 (*head)->next=*head;
{
*p = (DualNode*)malloc(sizeof(DualNode));
if (!*p) exit(0);
(*p)->data = (char)(i + 65);
(*p)->Next = *p;
(*p)->Previous = *p;
}
else
{
for (target = *p; target->Next != *p; target = target->Next);//寻找尾节点
temp = (DualNode*)malloc(sizeof(DualNode));
if (!temp)exit(0);
temp->data = (char)(i + 65);
temp->Next = *p; //新节点指向头节点
(*p)->Previous = temp;
target->Next = temp; //尾节点指向新节点
temp->Previous = target;
}
}
}
/*插入元素*/
void ListInsert(DualNode** list, int Position)
{
DualNode* p, * target, * temp;
int i;
ElemType item;
printf("请输入所要插入的数据:\n");
scanf("%d", &item);
fflush(stdin);
if (Position == 1)
{
temp = (DualNode*)malloc(sizeof(DualNode));
if (!(temp))
return Error;
temp->data = item;
for (target = (*list); target->Next != (*list); target = target->Next);
temp->Next = (*list);
target->Next = temp;
(*list)->Previous = temp;
(*list) = temp;
}
else
{
target = (*list);
for (i = 1; i < (Position - 1); i++)
{
target = target->Next;
}
temp = (DualNode*)malloc(sizeof(DualNode));
if (!(temp))
return Error;
temp->data = item;
p = target->Next;
target->Next = temp;
temp->Next = p;
p->Previous = temp;
temp->Previous = target;
}
}
/*删除元素*/
void ListDelete(DualNode** list, int Position)
{
DualNode* temp, * target;
int i = 1;
if (Position == 1)
{
for (target = (*list); target->Next != (*list); target = target->Next);
temp = *list;
(*list) = (*list)->Next;
target->Next = (*list);
(*list)->Previous = target;
free(temp);
}
else
{ //删除其他节点
for (i = 1, target = *list; target->Next != *list && i != Position - 1; target = target->Next, i++);
if (target->Next == *list)
{
for (target = *list; target->Next->Next != *list; target = target->Next);
temp = target->Next;
target->Next = *list;
(*list)->Previous = target;
free(temp);
}
else
{
temp = target->Next;
target->Next = temp->Next;
temp->Next->Previous = target;
free(temp);
}
}
}
/*查找元素*/
int ListSearch(DualNode* list, int Elem)
{
DualNode* target;
int i = 1;
for (target = list; target->data != Elem && target->Next != list; i++)
{
target = target->Next;
}
if (target->Next == list && target->data != list->data)
{
return Error;
}
return i;
}
/*遍历链表 正序*/
void ListDisplayForward(DualNode* list)
{
DualNode* target;
target = list;
do {
printf("%c\t", target->data);
target = target->Next;
} while (target != list);
printf("\n\n");
}
/*遍历链表 逆序*/
void ListDisplayBackward(DualNode* list)
{
DualNode* target;
target = list->Previous;
do {
printf("%c\t", target->data);
target = target->Previous;
} while (target != list->Previous);
printf("\n\n");
}
/*字母表给定顺序输出*/
void ListDisplayAlphabet(DualNode* list, int Position)
{
DualNode* target;
target = list;
int i = 0;
if (Position > 0)
{
for(i = 1; i <= Position;i++)
target = target->Next;
}
if (Position < 0)
{
i = abs(Position);
for (i = 1; i <=abs(Position); i++)
{
target = target->Previous;
}
}
i = 1;
do {
printf("%c\t", target->data);
target = target->Next;
i++;
} while (i <= 26);
printf("\n\n");
}
/*入口*/
void main()
{
DualNode* list = NULL;
ListInit(&list, 26);
ListDisplayForward(list);
ListDisplayBackward(list);
/* ListInsert(&list, 5);
ListInsert(&list, 6);
ListDisplayForward(list);
ListDisplayBackward(list);
ListDelete(&list, 5);
ListDelete(&list, 6);
ListDisplayForward(list);
ListDisplayBackward(list);*/
ListDisplayAlphabet(list, 52);
ListDisplayAlphabet(list, 3);
ListDisplayAlphabet(list, -3);
}