请优化以下代码 #include <iostream> int sum(int a, int b) { return a + b; } int jian(int a, int b) { return b - a; } float chu(float a, float b) { return a/b; } int chen(int a, int b) { return b * a; } int main() { int a = 1; int b = 2; int sum1; int jian1; int chen1; float chu1; sum1 = sum(a, b); jian1 = jian(a, b); chu1 = chu(a, b); chen1 = chen(a, b); std::cout << "a+b=" << sum1 << "\n"; std::cout << "a-b=" << jian1 << "\n"; std::cout << "a/b=" << chu1 << "\n"; std::cout << "a*b=" << chen1 << "\n"; }
时间: 2024-04-10 11:31:34 浏览: 17
#include <iostream>
int sum(int a, int b)
{
return a + b;
}
int jian(int a, int b)
{
return b - a;
}
float chu(float a, float b)
{
if (b != 0) {
return a / b;
} else {
std::cout << "Error: Division by zero!" << std::endl;
return 0;
}
}
int chen(int a, int b)
{
return b * a;
}
int main()
{
int a = 1;
int b = 2;
int sum1 = sum(a, b);
int jian1 = jian(a, b);
float chu1 = chu(a, b);
int chen1 = chen(a, b);
std::cout << "a + b = " << sum1 << "\n";
std::cout << "a - b = " << jian1 << "\n";
std::cout << "a / b = " << chu1 << "\n";
std::cout << "a * b = " << chen1 << "\n";
return 0;
}
相关问题
#include <iostream>#include <exception>#include <string>int main() { try { int a = 10; int b = 0; int c = a / b; } catch (...) { } 我要获取int c = a / b;处的行号
要获取代码中某一行的行号,可以使用预定义的宏 `__LINE__`。在这个例子中,你可以将行号保存在一个变量中,然后在 catch 语句块中使用它。例如:
```c++
#include <iostream>
#include <exception>
#include <string>
int main() {
int line_number = 0; // 定义变量来保存行号
try {
int a = 10;
int b = 0;
line_number = __LINE__; // 保存行号
int c = a / b;
}
catch (...) {
std::cout << "Exception occurred on line " << line_number << std::endl;
}
return 0;
}
```
这样,当程序执行时,如果出现异常,它将输出类似于“Exception occurred on line 8”的消息,其中 8 就是 `int c = a / b;` 这一行的行号。
#include <iostream> #include <algorithm> #include <string> #include <cstdio> #include <cmath> #include <set> #include <map> using namespace std; //const int N = 1e5 + 10; //typedef long long ll; int main() { int t; cin >> t; while (t--) { int n; set<string>a; string s; cin >> n >> s; int t = 0; for (int i = 0; i < s.size(); i++) { t = s.find(s[t]); a.insert(s.substr(i, 1)); } for (int i = 0; i < a.size(); i++) cout << a[i]; cout << endl; } return 0; }有什么问题
这段代码存在一些问题:
1. 在第二个 for 循环中,使用了下标访问 set 容器,这是错误的,因为 set 容器不支持下标访问。
2. 在第二个 for 循环中,使用了 a.size(),这也是错误的,因为 set 容器没有 size() 方法,应该使用 a.size()。
3. 在第一个 for 循环中,应该使用 s.substr(i, 1) 而不是 s.substr(0, t+1),因为后者会将 s 的前 t+1 个字符作为子串,而不是从 i 开始的一个字符。
4. 在第一个 for 循环中,应该将 a.insert() 放在 t = s.find(s[t]) 的后面,因为插入的是 s.substr(i, 1),插入顺序应该是从前往后。
修正后的代码如下:
```c++
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
set<string> a;
string s;
cin >> n >> s;
int t = 0;
for (int i = 0; i < s.size(); i++) {
a.insert(s.substr(i, 1));
t = s.find(s[t]);
}
for (auto it = a.begin(); it != a.end(); it++) cout << *it;
cout << endl;
}
return 0;
}
```
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)