代码中 c = p1.first * p2.second - p2.first * p1.second是用来干什么的
时间: 2024-03-31 08:37:24 浏览: 15
这行代码是用来计算两个二维向量的叉积的,即 $c = \vec{p_1} \times \vec{p_2}$,其中 $\vec{p_1}$ 和 $\vec{p_2}$ 分别表示两个二维向量,$p1.first$ 和 $p1.second$ 分别表示 $\vec{p_1}$ 的横纵坐标,$p2.first$ 和 $p2.second$ 分别表示 $\vec{p_2}$ 的横纵坐标。另外,两个向量的叉积的绝对值等于两个向量所围成的平行四边形的面积。在计算几何和图形学等领域,叉积是一个非常重要的概念。
相关问题
用c语言编写函数fun,其功能是:实现两个字符串的连接(不要使用库函数strcat),即把p2所指的字符串连接到p1所指的字符串的后面。 例如,分别输入下面两个字符串: FirstString-- SecondString 程序输出: FirstString--SecondString
好的,下面是实现函数fun的C代码:
```c
void fun(char *p1, char *p2)
{
while (*p1 != '\0') { // 找到p1的末尾
p1++;
}
while (*p2 != '\0') { // 将p2复制到p1的末尾
*p1 = *p2;
p1++;
p2++;
}
*p1 = '\0'; // 加上字符串结束标志
}
```
调用函数fun的方法如下:
```c
int main()
{
char str1[100] = "FirstString";
char str2[] = "SecondString";
fun(str1, str2);
printf("%s", str1);
return 0;
}
```
输出结果为:
```
FirstString--SecondString
```
注意,在调用函数fun之前,需要先定义好两个字符串,并且第一个字符串的数组大小要足够大,能够容纳下两个字符串连接后的结果。
把这段代码从c++转换为c语言#include <iostream> #include <algorithm> #include <cmath> using namespace std; struct Person { string name; int height; }; bool cmp(Person p1, Person p2) { if (p1.height != p2.height) { return p1.height < p2.height; } else { return p1.name > p2.name; } } int main() { Person people[15]; int count = 0; while (true) { cin >> people[count].name; if (people[count].name == "#") { break; } cin >> people[count].height; count++; } sort(people, people + count, cmp); int mid = count / 2; if (count % 2 == 1) { cout << "mid height=" << people[mid].height << ",name=" << people[mid].name << endl; } else { cout << "mid height=" << (people[mid - 1].height + people[mid].height) / 2.0 << endl; cout << "first person:" << people[mid - 1].name << "," << people[mid - 1].height << endl; cout << "second person:" << people[mid].name << "," << people[mid].height << endl; } for (int i = 0; i < count; i++) { cout << people[i].name << " " << people[i].height << endl; } return 0; }
以下是将该C++代码转换为C语言代码的过程:
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Person {
char name[20];
int height;
};
int cmp(const void* p1, const void* p2) {
struct Person* person1 = (struct Person*) p1;
struct Person* person2 = (struct Person*) p2;
if (person1->height != person2->height) {
return person1->height - person2->height;
} else {
return strcmp(person2->name, person1->name);
}
}
int main() {
struct Person people[15];
int count = 0;
while (1) {
scanf("%s", people[count].name);
if (strcmp(people[count].name, "#") == 0) {
break;
}
scanf("%d", &people[count].height);
count++;
}
qsort(people, count, sizeof(struct Person), cmp);
int mid = count / 2;
if (count % 2 == 1) {
printf("mid height=%d,name=%s\n", people[mid].height, people[mid].name);
} else {
printf("mid height=%.1f\n", (people[mid - 1].height + people[mid].height) / 2.0);
printf("first person:%s,%d\n", people[mid - 1].name, people[mid - 1].height);
printf("second person:%s,%d\n", people[mid].name, people[mid].height);
}
for (int i = 0; i < count; i++) {
printf("%s %d\n", people[i].name, people[i].height);
}
return 0;
}
```
主要的变化包括:
1. 头文件改为 `<stdio.h>`, `<string.h>`, `<stdlib.h>`。
2. C语言中没有 `string` 类型,需要使用字符数组(char array)。
3. C语言中的函数 `scanf` 和 `printf` 替代了C++中的 `cin` 和 `cout`。
4. C语言中的比较函数 `qsort` 参数类型不同,需要进行相应的修改。