电子商务网站推广计划,wordpress海报式分享,广告创意设计是什么,wordpress不能进后台C语言作为一门广泛使用的编程语言#xff0c;已经成为了许多IT领域从业者的必备技能之一。为了帮助大家更好地掌握C语言#xff0c;本文将为大家提供一些专门的C真题。
1. 指针
题目一#xff1a;请编写一个函数#xff0c;函数名为swap#xff0c;交换两个整数型变量的… C语言作为一门广泛使用的编程语言已经成为了许多IT领域从业者的必备技能之一。为了帮助大家更好地掌握C语言本文将为大家提供一些专门的C真题。
1. 指针
题目一请编写一个函数函数名为swap交换两个整数型变量的值。
void swap(int *a, int *b) {int temp *a;*a *b;*b temp;
}
题目二请编写一个函数实现对一个整型数组进行冒泡排序。
void bubble_sort(int arr[], int len) {for (int i 0; i len - 1; i) {for (int j 0; j len - i - 1; j) {if (arr[j] arr[j 1]) {swap(arr[j], arr[j 1]);}}}
}
题目三请编写一个函数实现将一个字符数组中的每个字符倒序输出。
void reverse_string(char *str) {int len strlen(str);for (int i len - 1; i 0; i--) {cout str[i];}
}
2. 继承与多态
题目一请编写一个基类Shape和三个派生类Circle、Rectangle和Triangle实现求各自面积的功能。
class Shape {
public:virtual double get_area() {return 0;}
};class Circle : public Shape {
private:double r;public:Circle(double r) {this-r r;}double get_area() {return 3.14 * r * r;}
};class Rectangle : public Shape {
private:double l, w;public:Rectangle(double l, double w) {this-l l;this-w w;}double get_area() {return l * w;}
};class Triangle : public Shape {
private:double a, b, c;public:Triangle(double a, double b, double c) {this-a a;this-b b;this-c c;}double get_area() {double p (a b c) / 2;return sqrt(p * (p - a) * (p - b) * (p - c));}
};
题目二请编写一个基类Animal和三个派生类Cat、Dog和Bird实现各自发出声音的功能。
class Animal {
public:virtual void make_sound() {cout Animal makes sound! endl;}
};class Cat : public Animal {
public:void make_sound() {cout Meow~ endl;}
};class Dog : public Animal {
public:void make_sound() {cout Woof! endl;}
};class Bird : public Animal {
public:void make_sound() {cout Chirp~ endl;}
};
3. STL库
题目一请使用STL库中的vector定义一个整型数组并利用vector进行遍历、排序、查找等操作。
vectorint vec {8, 3, 6, 2, 7, 4, 1, 5};// 遍历
for (auto item : vec) {cout item ;
}
cout endl;// 排序
sort(vec.begin(), vec.end());
for (auto item : vec) {cout item ;
}
cout endl;// 查找
auto it find(vec.begin(), vec.end(), 7);
if (it ! vec.end()) {cout Found: *it endl;
}
else {cout Not found! endl;
}
题目二请使用STL库中的map定义一个字典实现添加、查找、删除单词的功能。
mapstring, string dict;// 添加单词
dict.insert(pairstring, string(apple, 苹果));
dict.insert(make_pair(banana, 香蕉));
dict[cherry] 樱桃;// 查找单词
auto it dict.find(apple);
if (it ! dict.end()) {cout it-second endl;
}
else {cout Not found! endl;
}// 删除单词
dict.erase(banana);
4. 文件操作
题目一请编写一个程序将一个文本文件中的每一行反转。
ifstream fin(input.txt);
ofstream fout(output.txt);string line;
while (getline(fin, line)) {reverse(line.begin(), line.end());fout line endl;
}fin.close();
fout.close();
题目二请编写一个程序实现将一个二进制文件中的数据进行加密和解密。
ifstream fin(input.bin, ios::binary);
ofstream fout(output.bin, ios::binary);char key 123; // 密钥可以自己定义char ch;
while (fin.get(ch)) {ch ^ key; // 异或运算实现加密和解密fout.put(ch);
}fin.close();
fout.close();