如何在该代码基础上加入负数运算

CSDN问答 2021-12-30 13:50:12 阅读数:92

代码 基础 何在 基础上 加入
问题遇到的现象和发生背景

如何在源代码基础上加入负数运算

问题相关代码,请勿粘贴截图
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h>#include<iostream>#include<string.h>#include<stdlib.h>using namespace std;#define maxn 10#define N 100005int a[10000000];int pp = 0;struct Sqlist{ int data; struct Sqlist* next, * before;}; struct Sqlist* r1, * str1, * s1, * s11, * r2, * str2, * s2, * s22;void ab(){ char s[N], ss1[N], ss2[N]; int len1, len2, ans = 0, flag = 0, i; scanf("%s%s", ss1, ss2); len1 = strlen(ss1); len2 = strlen(ss2); r1 = (struct Sqlist*)malloc(sizeof(struct Sqlist)); r1->before = NULL; r2 = (struct Sqlist*)malloc(sizeof(struct Sqlist)); r2->before = NULL; str1 = r1; for (i = len1 - 1; i >= 0; --i) { s1 = (struct Sqlist*)malloc(sizeof(struct Sqlist)); s1->data = ss1[i] - '0'; s1->next = NULL; s1->before = str1; str1->next = s1; str1 = str1->next; } s1 = (struct Sqlist*)malloc(sizeof(struct Sqlist) ); s1->data = 0; s1->next = NULL; s1->before = str1; str1->next = s1; str1 = str1->next; str2 = r2; for (i = len2 - 1; i >= 0; --i) { s2 = (struct Sqlist*)malloc(sizeof(struct Sqlist)); s2->data = ss2[i] - '0'; s2->next = NULL; s2->before = str2; str2->next = s2; str2 = str2->next; } for (i = 0; i <= len1 - len2; ++i) { s2 = (struct Sqlist*)malloc(sizeof(struct Sqlist)); s2->data = 0; s2->next = NULL; s2->before = str2; str2->next = s2; str2 = str2->next; } ans = 0; str1 = r1->next; str2 = r2->next; for (i = 0; i <= len1; ++i) { ans = ans + str1->data + str2->data; str1->data = ans % 10; ans /= 10; str1 = str1->next; str2 = str2->next; } flag = 0; ans = 0; str1 = r1->next; while (1) { if (str1->next == NULL) break; str1 = str1->next; } while (str1 != NULL) { if (str1->data != 0) flag = 1; if (flag) { ans++; a[pp] = str1->data; pp++; } str1 = str1->before; if (str1->before == NULL) break; } printf("\n");}int main(){ memset(a, 0, sizeof(a)); cout << "请输入数据:" << endl; ab(); int xx = pp % 4; int pan = 0; cout << "输出结果为:" << endl; for (int i = 0; i < pp - 4; i++) { cout << a[i]; pan++; if (i == xx - 1) { cout << ","; pan = 0; } if (pan % 4 == 0 && pan != 0) cout << ","; //四位一输出 } for (int i = pp - 4; i < pp; i++) cout << a[i]; cout << endl; return 0;}
版权声明:本文为[CSDN问答]所创,转载请带上原文链接,感谢。 https://ask.csdn.net/questions/7613099