void printfbychoose(const char cKind, dShortPath dShortPath, int nEnd) { if (cKind == "T") { cout << "f" << endl; } }为何报错
时间: 2024-03-02 21:51:46 浏览: 19
在这段代码中,cKind 是一个字符类型的变量,但是在条件语句中使用了双引号,应该使用单引号。双引号表示字符串,而单引号表示字符。修改后的代码如下:
```
void printfbychoose(const char cKind, dShortPath dShortPath, int nEnd) {
if (cKind == 'T') {
cout << "f" << endl;
}
}
```
此外,这段代码中定义的 `dShortPath` 和 `nEnd` 变量在函数中没有被使用,如果没有特殊需要,可以将它们从函数参数中删除。
相关问题
void LookupNum(Student stu[]) { system("cls"); int n = Read(stu); int s; int i = 0; cout << endl << "======>> 查找学生成绩 <<======" << endl; cout << "请输入要查找学生的学号:"; cin >> s; while ((stu[i].num - s) != 0 && i < n)i++; if (i == n) { cout << "======>> 对不起,无法找到该学生...... <<======" << endl; } else { cout << "----------------------------" << endl; cout << "班级:" << stu[i].class_0 << endl; cout << "学号:" << stu[i].num << endl; cout << "姓名:" << stu[i].name << endl; cout << "电子技术:" << stu[i].elec << endl; cout << "C++程序设计:" << stu[i].c_program << endl; cout << "多媒体技术:" << stu[i].media << endl; cout << "大学英语:" << stu[i].english << endl; cout << "高等数学:" << stu[i].math << endl; cout << "大学体育:" << stu[i].sport << endl; cout << "马克思主义基本原理:" << stu[i].polity << endl; cout << "平均分:" << stu[i].average << endl; cout << "总分:" << stu[i].total << endl; } }修改为折半查找
折半查找(也称二分查找)需要一个有序数组,先比较数组中间位置的元素和目标元素的大小关系,如果相等则直接返回下标,否则根据大小关系缩小查找范围并继续查找。因此,需要对stu数组进行排序,可以使用STL中的sort函数进行升序排序,然后使用二分查找算法进行查找。
修改后的代码如下:
```
void LookupNum(Student stu[]) {
system("cls");
int n = Read(stu);
int s;
cout << endl << "======>> 查找学生成绩 <<======" << endl;
cout << "请输入要查找学生的学号:";
cin >> s;
// 先对stu数组按学号升序排序
sort(stu, stu + n, [](const Student& a, const Student& b) { return a.num < b.num; });
// 二分查找目标学号
int left = 0, right = n - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (stu[mid].num == s) {
cout << "----------------------------" << endl;
cout << "班级:" << stu[mid].class_0 << endl;
cout << "学号:" << stu[mid].num << endl;
cout << "姓名:" << stu[mid].name << endl;
cout << "电子技术:" << stu[mid].elec << endl;
cout << "C++程序设计:" << stu[mid].c_program << endl;
cout << "多媒体技术:" << stu[mid].media << endl;
cout << "大学英语:" << stu[mid].english << endl;
cout << "高等数学:" << stu[mid].math << endl;
cout << "大学体育:" << stu[mid].sport << endl;
cout << "马克思主义基本原理:" << stu[mid].polity << endl;
cout << "平均分:" << stu[mid].average << endl;
cout << "总分:" << stu[mid].total << endl;
return; // 直接返回
} else if (stu[mid].num < s) {
left = mid + 1;
} else {
right = mid - 1;
}
}
cout << "======>> 对不起,无法找到该学生...... <<======" << endl;
}
```
需要注意的是,折半查找要求数组有序,因此在查找之前需要对stu数组按学号升序排序。另外,如果找到目标学生,直接返回即可,不需要继续查找。
用java语言完成以下代码:const int m=3;int n=0; void incr ( char id){ int i; for (i=1;i<=m; i++){ n=n+1; cout<<id<<": "<<i<<"@n="<<n<<endl; } } void main (){ cobegin { incr ( 'A') ;incr ( 'B') ;incr ( 'c') ; } cout<<"This sum is: "<<n<<endl;
把C++代码翻译成Java代码如下:
```java
public class Main {
static final int m = 3;
static int n = 0;
public static void incr(char id) {
for (int i = 1; i <= m; i++) {
n = n + 1;
System.out.println(id + ": " + i + "@n=" + n);
}
}
public static void main(String[] args) {
new Thread(() -> incr('A')).start();
new Thread(() -> incr('B')).start();
new Thread(() -> incr('C')).start();
System.out.println("This sum is: " + n);
}
}
```
其中`cobegin`和`cout`是C++的关键字,Java中没有对应的语法,需要用其他方式替换。在这里,我们使用Java 8的Lambda表达式和`System.out.println`来替换。同时,我们在`main`方法里启动了三个线程。