前面的部分没错,错的是//3.或//4.
输出结果如图
#include<iostream>
using namespace std;
struct Node {
char x = ' ', y = ' ';//x存储0 y储存-或*,分别代表史莱姆和通电状况
int d_flag = 0;//相异的flag,0是前后相同,1,反之,前后相异
char sex = ' ';
struct Node* next=NULL;
};
struct Node* head = NULL , * p = NULL, * r = NULL;
int main() {
//1.输入史莱姆及其关系
cout << "输入史莱姆0和通电情况-,输入x则停止输入"<<endl;
char x1='y', y1='y';
//cin >> x1 >> y1;
head = new Node;
r = head;
while (x1 != 'x') {
p = new Node;
x1 = getchar();
y1 = getchar();
p->x = x1;
p->y = y1;
p->next = NULL;
if(r!=NULL)
r->next = p;
r = p;
}
p = head;
//2.通过是否通电,判断前后是否相异
while (p->next != NULL) {
p = p->next;
if (p->y == '-')p->d_flag = 1;
}
//3.通过相异性判断正负
p = head;
p->next->sex = '+';
while (p->next!= NULL) {
if (p->d_flag == 1) {
p = p->next;
p->sex = '-';
}
else
p = p->next;
p->sex = '+';
}
//4.输出两种结果
p = head;
while (p->next != NULL) {
//if (p->x = 'x')break;
p = p->next;
cout << p->sex << " ";
}
}