一、 a到z的字母用ASCII码表示,显示出来
#include <iostream>
using namespace std;
int main(){
for (char c = 'a'; c <= 'z'; c++) {
cout << c << ":" << (int)c<<endl;
}
}
二、 求一元二次方程的根
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
double s = b * b - 4 * a * c;
if (s > 0) {
double x1 = (-b + sqrt(s)) / (2 * a);
double x2 = (-b - sqrt(s)) / (2 * a);
cout << "x1:" << x1 << endl;
cout << "x2:" << x2 << endl;
}
else if (s == 0) {
double x = (-b) / (2 * a);
cout << "x:" << x << endl;
}
else {
double x1 = (-b) / (2 * a);
double x2 = (sqrt(-s)) / (2 * a);
cout << "x1:" << x1 << "+"<<x2 << "i" << endl;
cout << "x2:" << x1 << "-" << x2 << "i"<<endl;
}
}
三、 写一个函数,从小到大排序一个数组
#include <iostream>
using namespace std;
//冒泡排序
void bubbleSort(int num[],int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (num[j] > num[j + 1]) {
int temp = num[j + 1];
num[j + 1] = num[j];
num[j] = temp;
}
}
}
}
int main() {
int num[10] = { 98,64,35,29,8,3,54,22,48,96 };
bubbleSort(num,10);
for (int i = 0; i < 10; i++) {
cout << num[i] << " ";
}
}
四、 写一个异常处理 以c=a/b为例
#include <iostream>
using namespace std;
double division(double a, double b) {
if (b == 0) {
throw "error"; //抛出异常
}
else {
return a / b;
}
}
int main() {
double a, b;
cin >> a >> b;
try
{
double answer = division(a,b);
cout << "a/b=" << answer << endl;
}
catch (const char * msg) //这里必须添加const
{
cout << msg;
}
return 0;
}
五、 打印9*9乘法表
#include <iostream>
#include <iomanip>
using namespace std;
void print() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
cout << setw(5) << i << "*" << j << "=" << i * j;
}
cout << endl;
}
}
int main() {
print();
return 0;
}
六、 写一个递归函数,打印出斐波那契数列
#include <iostream>
#include <iomanip>
using namespace std;
//F(0) = 0,F(1) = 1, F(n) = F(n - 1) + F(n - 2)
int cal(int n) {
if (n == 0) {
return 0;
}
else if (n == 1) {
return 1;
}
else {
return cal(n - 1) + cal(n - 2);
}
}
int main() {
int count = 0;
for (int i = 1; i <= 10; i++) {
int answer = cal(i);
cout << setw(5) << answer;
count++;
if (count % 5 == 0) {
cout << endl;
}
}
}
七、 新建一个关于学生的类或结构体,包含学号,姓名,语文,数学,英语成绩;并输入一个整数n,然后分别输入各学生数据
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string sno;
string sname;
double chineseScore;
double mathScore;
double englishScore;
public:
Student(); //这里必须有无参构造函数,否则无法动态创建数组对象
Student(string sno, string sname, double c, double m, double e);
void display();
void setSno() { //必须有set方法
cin >> sno;
}
void setSname() {
cin >> sname;
}
void setChineseScore() {
cin >> chineseScore;
}
void setMathScore() {
cin >> mathScore;
}
void setEnglishScore() {
cin >> englishScore;
}
};
Student::Student() {}
Student::Student(string sno, string sname, double c, double m, double e) {
this->sno = sno;
this->sname = sname;
chineseScore = c;
mathScore = m;
englishScore = e;
}
void Student::display() {
cout << "sno:" << sno << endl;
cout << "sname:" << sname << endl;
cout << "chineseScore" << chineseScore << endl;
cout << "mathScore" << mathScore << endl;
cout << "englishScore" << englishScore << endl;
}
int main() {
int n;
cout << "请输入n:";
cin >> n;
Student * stu = new Student[n]; //只有动态分配才能使用非常量n创建数组
for (int i = 0; i < n; i++) {
cout << "请输入第" << i + 1 << "个学生的信息:";
stu[i].setSno();
stu[i].setSname();
stu[i].setChineseScore();
stu[i].setMathScore();
stu[i].setEnglishScore();
}
for (int i = 0; i < n; i++) {
stu[i].display();
cout << endl;
}
}
八、 运用第7题的类,要求写三个函数。(均用二进制输入输出)
(1)把多个学生信息存到一个文件中
(2)从一个文件中提取所有学生信息,并打印
(3)从一个文件中复制所有学生信息,复制到另外一个文件。
#include <iostream>
#include <fstream>
using namespace std;
class Student {
private:
string sno;
string sname;
double chineseScore;
double mathScore;
double englishScore;
public:
Student(); //这里必须有无参构造函数,否则无法动态创建数组对象
Student(string sno, string sname, double c, double m, double e);
void display();
void setSno() { //必须有set方法
cin >> sno;
}
void setSname() {
cin >> sname;
}
void setChineseScore() {
cin >> chineseScore;
}
void setMathScore() {
cin >> mathScore;
}
void setEnglishScore() {
cin >> englishScore;
}
};
Student::Student() {}
Student::Student(string sno, string sname, double c, double m, double e) {
this->sno = sno;
this->sname = sname;
chineseScore = c;
mathScore = m;
englishScore = e;
}
void Student::display() {
cout << "sno:" << sno << endl;
cout << "sname:" << sname << endl;
cout << "chineseScore" << chineseScore << endl;
cout << "mathScore" << mathScore << endl;
cout << "englishScore" << englishScore << endl;
}
//存入学生信息
void task1(Student *stu) {
ofstream output("f1.dat", ios::binary);
if (!output) {
cout << "文件打开失败";
exit(0);
}
for (int i = 0; i < 3; i++) {
output.write((char*)&stu[i], sizeof(stu[i]));
}
cout << "存入文件成功" << endl;
output.close();
}
//从文件输出打印学生信息
void task2() {
ifstream input("f1.dat", ios::binary);
if (!input) {
cout << "文件打开失败";
exit(0);
}
Student stu[3];
for (int i = 0; i < 3; i++) {
input.read((char*)&stu[i], sizeof(stu[i]));
stu[i].display();
cout << endl;
}
input.close();
}
//从一个文件复制到另一个文件
void task3() {
ifstream input("f1.dat", ios::binary);
if (!input) {
cout << "文件打开失败" << endl;
exit(0);
}
Student stu[3];
for (int i = 0; i < 3; i++) {
input.read((char*)&stu[i], sizeof(stu[i]));
}
input.close();
ofstream output("f2.dat", ios::binary);
if (!output) {
cout << "文件打开失败" << endl;
exit(0);
}
for (int i = 0; i < 3; i++) {
output.write((char*)&stu[i], sizeof(stu[i]));
}
output.close();
cout << "从一个文件写入另一个文件成功";
}
int main() {
Student stu[3] = {
Student("123","张三",15,64,77),
Student("456","王五",18,32,64),
Student("789","赵柳",32,64,55)
};
task1(stu);
task2();
task3();
return 0;
}