int a = i >= 0 ? num1[i] - '0' : 0这句代码是什么意思
时间: 2024-03-08 14:46:43 浏览: 18
这行代码是一个三目运算符,也叫条件运算符。它的基本语法是:
```c
表达式1 ? 表达式2 : 表达式3
```
当表达式1为真时,整个表达式的值为表达式2的值;当表达式1为假时,整个表达式的值为表达式3的值。
具体到这个代码,它的意思是:如果i>=0,则a的值为num1[i] - '0';否则a的值为0。其中,num1是一个字符数组,num1[i]表示字符数组num1中下标为i的字符;'0'表示字符0的ASCII码值,'0' - '0' 的结果为0,'1' - '0' 的结果为1,以此类推。所以,这行代码的作用是将字符数组num1中下标为i的字符转换为对应的数字,并将结果赋值给a。如果i小于0,则a的值为0。
相关问题
#include <iostream> #include <cstring> using namespace std; #define MAX_LENGTH 100 // 高精度加法 string add(string num1, string num2) { string res = ""; // 存放结果 int carry = 0; // 存放进位 int len1 = num1.size(); int len2 = num2.size(); // 从低位到高位逐位相加 for (int i = len1 - 1, j = len2 - 1; i >= 0 || j >= 0; i--, j--) { int a = i >= 0 ? num1[i] - '0' : 0; int b = j >= 0 ? num2[j] - '0' : 0; int sum = a + b + carry; carry = sum / 10; res = to_string(sum % 10) + res; } while (res.size()>1 && res[0]=='0') { res.erase(0,1); } if (carry > 0) { res = to_string(carry) + res; } return res; } // 高精度减法 string sub(string num1, string num2) { string res = ""; // 存放结果 int borrow = 0; // 存放借位 int len1 = num1.size(); int len2 = num2.size(); // 从低位到高位逐位相减 for (int i = len1 - 1, j = len2 - 1; i >= 0 || j >= 0; i--, j--) { int a = i >= 0 ? num1[i] - '0' : 0; int b = j >= 0 ? num2[j] - '0' : 0; int diff = a - b - borrow; if (diff < 0) { diff += 10; borrow = 1; } else borrow = 0; res = to_string(diff) + res; } // 去掉前导0 while (res.size()>1 && res[0]=='0') { res.erase(0,1); } // 如果结果为0,则去掉负号 if (res == "0") { borrow = 0; } // 加上负号 if (borrow > 0) { res = "-" + res; } return res; } int main() { string num1, num2, op; cin >> num1 >> op >> num2; if (op == "+") { cout << add(num1, num2) << endl; } else if (op == "-") { cout << sub(num1, num2) << endl; } return 0; }这段程序没有考虑被减数小于减数的情况,请改正他使其能够正确返回被减数小于减数时的结果
可以在执行高精度减法时,判断被减数是否小于减数,如果是则交换两个数的位置,并在最终结果前加上负号即可。修改后的代码如下:
```
#include <iostream>
#include <cstring>
using namespace std;
#define MAX_LENGTH 100
// 高精度加法
string add(string num1, string num2) {
string res = "";
int carry = 0;
int len1 = num1.size();
int len2 = num2.size();
for (int i = len1 - 1, j = len2 - 1; i >= 0 || j >= 0; i--, j--) {
int a = i >= 0 ? num1[i] - '0' : 0;
int b = j >= 0 ? num2[j] - '0' : 0;
int sum = a + b + carry;
carry = sum / 10;
res = to_string(sum % 10) + res;
}
while (res.size()>1 && res[0]=='0') {
res.erase(0,1);
}
if (carry > 0) {
res = to_string(carry) + res;
}
return res;
}
// 高精度减法
string sub(string num1, string num2) {
string res = "";
int borrow = 0;
int len1 = num1.size();
int len2 = num2.size();
bool negative = false; // 是否为负数
if (len1 < len2) { // 如果被减数长度小于减数长度,则交换两个数的位置
swap(num1, num2);
len1 = num1.size();
len2 = num2.size();
negative = true; // 标记为负数
} else if (len1 == len2) { // 如果两个数长度相等,则从高位到低位比较大小
for (int i = 0; i < len1; i++) {
if (num1[i] < num2[i]) {
swap(num1, num2);
len1 = num1.size();
len2 = num2.size();
negative = true; // 标记为负数
break;
} else if (num1[i] > num2[i]) {
break;
}
}
}
for (int i = len1 - 1, j = len2 - 1; i >= 0 || j >= 0; i--, j--) {
int a = i >= 0 ? num1[i] - '0' : 0;
int b = j >= 0 ? num2[j] - '0' : 0;
int diff = a - b - borrow;
if (diff < 0) {
diff += 10;
borrow = 1;
} else {
borrow = 0;
}
res = to_string(diff) + res;
}
while (res.size()>1 && res[0]=='0') {
res.erase(0,1);
}
if (negative) { // 如果是负数,则在最终结果前加上负号
res = "-" + res;
}
return res;
}
int main() {
string num1, num2, op;
cin >> num1 >> op >> num2;
if (op == "+") {
cout << add(num1, num2) << endl;
} else if (op == "-") {
cout << sub(num1, num2) << endl;
}
return 0;
}
```
bool isPerfectSquare(int num) { int i = 1; while (num > 0) { num -= i; i += 2; } return num == 0; }请写出该代码的注释
/* 这是一个判断一个数是否是完全平方数的函数
* 输入参数:一个整数 num
* 输出结果:true(是完全平方数)或 false(不是完全平方数)
* 算法原理:完全平方数可以表示为一系列连续奇数的和,例如:
* 1 = 1
* 4 = 1 + 3
* 9 = 1 + 3 + 5
* 16 = 1 + 3 + 5 + 7
* ...
* 因此可以采用循环的方式依次减去连续的奇数,如果最终减到了0,就是完全平方数,否则不是
*/