Computer Science, Software Engineering, South China Normal University special master (2018 PubMed)

Preface:

All questions are seniors finishing recalled version, the situation is not clear description of the subject or less for potential problems

 

An output ascii code AZ

Source

#include<iostream>
using namespace std;
int main()
{
    char ch='A';
    while(ch>='A' && ch<='Z'){
        cout<<ch<<'\t'<<(int)ch<<endl;
        ch++;
    }
    return 0;
}

result:

A       65
B       66
C       67
D       68
E       69
F       70
G       71
H       72
I       73
J       74
K       75
L       76
M       77
N       78
O       79
P       80
Q       81
R       82
S       83
T       84
U       85
V       86
W       87
X       88
Y       89
Z       90

 

 

Second, calculate the root of a quadratic equation of the two

Source:

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
    double A,B,C;
    double x_1,x_2;
    double delta;
    cout<<"please input A,B,C:";
    cin>>A>>B>>C;
    delta=B*B-A*C*4;        //判别式
    while(delta<0){        //判别式是否合法
        cout<<"error!please input again:";
        cin>>A>>B>>C;
        delta=B*B-A*C*4;
    }

    cout<<A<<"x^2+"<<B<<"x+"<<C<<"=0"<<endl;
    x_1=(-B+sqrt(delta))/(2*A);
    x_2=(-B-sqrt(delta))/(2*A);
    cout<<"x_1="<<x_1<<endl;
    cout<<"x_2="<<x_2<<endl;
}

 

result:

The result of a quadratic equation showed a number of problems, can be optimized, such as the coefficient is smaller than zero, the output is not "+", and the coefficient is 1 when the coefficient is not output, etc.

please input A,B,C:4 4 5
error!please input again:1 -5 6
1x^2+-5x+6=0
x_1=3
x_2=2

 

Third, the output of the multiplication table 9 * 9

Source:

#include<iostream>
using namespace std;
int main()
{
    int i,j;
    for(i=1;i<=9;i++){
        for(j=1;j<=i;j++)
            cout<<j<<"*"<<i<<"="<<i*j<<"  ";
        cout<<endl;
    }
}

 

result:

1*1=1
1*2=2  2*2=4
1*3=3  2*3=6  3*3=9
1*4=4  2*4=8  3*4=12  4*4=16
1*5=5  2*5=10  3*5=15  4*5=20  5*5=25
1*6=6  2*6=12  3*6=18  4*6=24  5*6=30  6*6=36
1*7=7  2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49
1*8=8  2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64
1*9=9  2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81

 

 

Fourth, write a recursive function, seeking Fibonacci

 

Source:

#include<iostream>
using namespace std;

int function(int n){        //递归函数
    if( n==1 || n==2 )  
        return 1;
    else{
        return function(n-1)+function(n-2);
    } 
}

int main()
{
    cout<<"please input number:";     //输入层数
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cout<<function(i)<<"\t";
        if(i%5==0)
            cout<<endl;
    }
}

 

 

result:

please input number:10
1       1       2       3       5
8       13      21      34      55

 

 

Fifth, write a function, sort the array

Because it is recalled version, does not require the sort of small to large to small or large, but did not specify a bubble sort or selection sort, so this sort will use two ways to answer this question

Source

#include<iostream>
#include<cstring>
using namespace std;

void bubble(int Array[],int length){
    bool flag=false;
    for(int i=0;i<length-1;i++){
        for(int j=i;j<length-1;j++){
            if(Array[i]>Array[j]){
                int t=Array[i];
                Array[i]=Array[j];
                Array[j]=t;
                flag=true;
            }
        }
        if(flag==false)
            break;
    }
}

//选择排序
void SelectSort(int Array[],int length){
    for(int i=0;i<length;i++){
        int min=i;
        for(int j=i;j<length;j++){
            if(Array[j]<Array[min])
                min=j;
        }
        int t=Array[min];
        Array[min]=Array[i];
        Array[i]=t;
    }
}

int main(){

    //冒泡排序测试
    int Array_1[5]={36,72,43,21,57};
    cout<<"Array_1:"<<endl;
    cout<<"Before Sort:"<<endl;
    for(int i=0;i<5;i++)
        cout<<Array_1[i]<<" ";
    cout<<endl;
    cout<<"After Sort:"<<endl;
    bubble(Array_1,5);
    for(int i=0;i<5;i++)
        cout<<Array_1[i]<<" ";
    cout<<endl;

    //选择排序测试
    int Array_2[5]={75,82,99,56,102};
    cout<<"Array_2:"<<endl;
    cout<<"Before Sort:"<<endl;
    for(int i=0;i<5;i++)
        cout<<Array_2[i]<<" ";
    cout<<endl;
    cout<<"After Sort:"<<endl;
    SelectSort(Array_2,5);
    for(int i=0;i<5;i++)
        cout<<Array_2[i]<<" ";
    cout<<endl;
}

 

result:

Array_1:
Before Sort:
36 72 43 21 57
After Sort:
21 36 43 72 57
Array_2:
Before Sort:
75 82 99 56 102
After Sort:

 

Six, c = a / b catch exceptions

Source:

#include<iostream>
using namespace std;

double division(double a,double b){
    if(b==0)
        throw "error";
    return a/b;
}

int main(){
    double a,b;
    while(1){
        cout<<"a/b, please input a b:";
        cin>>a>>b;
        try{
            double result=division(a,b);
            cout<<a<<"/"<<b<<"="<<result<<endl;
            break;
        }catch(const char *msg){
            cout<<"error!!"<<endl;
        }
    }
}

 

result:

a/b, please input a b:3 0
error!!
a/b, please input a b:3 1.5
3/1.5=2

 

 

Seven, write a class or structure, including school name Yushu Wai results, enter n, n number of new, then respectively input

Source:

#include<iostream>
#include<string>
using namespace std;

struct Student{
    string name;
    string id;
    double Chinses;
    double Math;
    double English;
};

int main(){
    int n;
    cout<<"please input n:";
    cin>>n;

    Student *stu = new Student[n];

    for(int i=0;i<n;i++){
        cout<<endl;
        cout<<"please input name:";
        cin>>stu[i].name;
        cout<<"please input ID:";
        cin>>stu[i].id;
        cout<<"please input Chinese Score:";
        cin>>stu[i].Chinses;
        cout<<"please input Math score:";
        cin>>stu[i].Math;
        cout<<"please input English Score:";
        cin>>stu[i].English;
    }

    cout<<endl<<"display:"<<endl;
    for(int i=0;i<n;i++){
        cout<<"*******************************"<<endl;
        cout<<"name:"<<stu[i].name<<endl;
        cout<<"ID:"<<stu[i].id<<endl;
        cout<<"Chinese Score:"<<stu[i].Chinses<<endl;
        cout<<"Math score:"<<stu[i].Math<<endl;
        cout<<"English Score:"<<stu[i].English<<endl;
        cout<<"*******************************"<<endl;
    }
}

 

result:

please input n:5

please input name:jack
please input ID:01
please input Chinese Score:99
please input Math score:98
please input English Score:97

please input name:mike 
please input ID:02
please input Chinese Score:80
please input Math score:90
please input English Score:100

please input name:oscar
please input ID:03
please input Chinese Score:77
please input Math score:99
please input English Score:87

please input name:rookie
please input ID:04
please input Chinese Score:88
please input Math score:66
please input English Score:77

please input name:jackeylove
please input ID:05
please input Chinese Score:59
please input Math score:59
please input English Score:59

display:
*******************************
name:jack
ID:01
Chinese Score:99
Math score:98
English Score:97
*******************************
*******************************
name:mike
ID:02
Chinese Score:80
Math score:90
English Score:100
*******************************
*******************************
name:oscar
ID:03
Chinese Score:77
Math score:99
English Score:87
*******************************
*******************************
name:rookie
ID:04
Chinese Score:88
Math score:66
English Score:77
*******************************
*******************************
name:jackeylove
ID:05
Chinese Score:59
Math score:59
English Score:59
*******************************

 

 

Eight, with class 7, write three functions, a class is added to the binary file, a read, there is a backup, the backup to student.bak student.dat

No time to write up tomorrow ~

 

Published 24 original articles · won praise 1 · views 972

Guess you like

Origin blog.csdn.net/SampsonTse/article/details/104463971