itfhuv 2021-08-30 11:18 采纳率: 60%
浏览 36

c++链表:判断史莱姆正负,输出的正负性全是+号

前面的部分没错,错的是//3.或//4.
输出结果如图
img

#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 << " ";
    }
}

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2021-08-30 12:16
    关注

    修改如下,供参考:

    #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 (1){     //(x1 != 'x') {
            if ((x1 = getchar()) == 'x')break;
            y1 = getchar();
            p = new Node;
            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) {
            p = p->next;
            if (p->d_flag == 1) {
                
                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 << " ";
        }
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 8月30日

悬赏问题

  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal