C++复习例题:
sdust zjs老师
#include<iostream>
#include<vector>
#include<numeric>
using namespace std;
class Matrix
{
public:
vector<vector<double>> Array;
Matrix();
Matrix(vector<vector<double>> A);
void resize(int row,int col);
int rerow();
int recol();
Matrix& operator+(Matrix& A);
Matrix& operator-(Matrix& A);
Matrix& operator*(Matrix& A);
};
Matrix temp;
Matrix::Matrix()
{
this->resize(1,1);
}
Matrix::Matrix(vector<vector<double>> A)
{
int row=A.size();
int col=A[0].size();
this->resize(row,col);
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
this->Array[i][j]=A[i][j];
}
}
}
void Matrix::resize(int row, int col)
{
this->Array.resize(row);
for(int i= 0;i<this->Array.size();i++)
{
this->Array[i].resize(col);
}
}
int Matrix::rerow() {return this->Array.size();}
int Matrix::recol() {return this->Array[0].size();}
Matrix& Matrix::operator+(class Matrix & A)
{
int row = this->rerow();
int col = this->recol();
temp.resize(row,col);
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
{
temp.Array[i][j]=this->Array[i][j]+A.Array[i][j];
}
return temp;
}
Matrix & Matrix::operator-(Matrix & A)
{
int row = this->rerow();
int col = this->recol();
temp.resize(row,col);
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
temp.Array[i][j]=this->Array[i][j]-A.Array[i][j];
}
}
return temp;
}
Matrix & Matrix::operator*(Matrix & A)
{
int row1 = this->rerow();
int col1 = this->recol();
int row2 = A.rerow();
int col2 = A.recol();
temp.resize(row1,col2);
if(col1!=row2)
cerr<<"Matrix was wrong"<<endl;
else
{
for(int i=0;i<row1;i++)
{
for(int j=0;j<col2;j++)
{
vector<double>tem;
for(int k=0;k<col1;k++)
{
tem.push_back(this->Array[i][k]*A.Array[k][j]);
}
temp.Array[i][j]=accumulate(tem.begin(),tem.end(),0);
}
}
}
return temp;
}
int main()
{
vector<double> c;
c.push_back(1);
c.push_back(2);
c.push_back(3);
vector<vector<double >> T;
T.push_back(c);
T.push_back(c);
T.push_back(c);
Matrix a(T);
a*a;
return 0;
}
#include<strstream>
#include<iostream>
using namespace std;
int main()
{
char c[50]={"12 34 65 -23 -32 33 61 99 321 32"};
int a[10],t;
istrstream strin(c,sizeof(c));
for(int i=0;i<10;i++)
{
strin>>a[i];
}
for(int i=0;i<10;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
for(int i=0;i<9;i++)
for(int j=0;j<9-i;j++)
{
if(a[j]<a[j+1])
{
t=a[j];a[j]=a[j+1];a[j+1]=t;
}
}
ostrstream strout(c,sizeof(c));
for(int i=0;i<10;i++)
strout<<a[i]<<" ";
strout<<ends;
cout<<c;
system("pause");
return 0;
}
// function judge the solve;
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float a, b, c, disc;
cin >> a >> b >> c;
disc = (b * b - 4 * a * c);
if (a == 0 || disc < 0)
{
cerr << "wrong input" << endl;
exit(0);
}
double x1, x2;
x1 = (-b + pow(disc, 0.5)) / 2 / a;
x2 = (-b - pow(disc, 0.5)) / 2 / a;
cout << x1 << " " << x2 << endl;
system("pause");
return 0;
}
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a;
cin >> a;
cout << dec << a << endl;
cout << hex << a << endl;
cout << oct << a << endl;
cout << setbase(8) << endl;
char* pt = "China";
cout << setw(10) << pt << endl;
cout << setw(10) << setfill('*') << pt << endl;
cout << setiosflags(ios::scientific) << setprecision(8) << pt << endl;
cout << setiosflags(ios::fixed) << pt << endl;
system("pause");
return 0;
}
#include<iostream>
using namespace std;
int main()
{
char* pt = "BASIC";
for (int i = 0; i <= 4; i++)
{
//cout.put(pt[i]);
cout.put(*(pt + i));
}
system("pause");
return 0;
}
#include<iostream>
using namespace std;
int main()
{
float grade;
while (cin >> grade)
{
if (grade = 85)
cout << grade << "good" << endl;
}
system("pasue");
return 0;
}
#include<iostream>
using namespace std;
int main()
{
char c;
while ((c=cin.get())!=EOF)
{
cout << c ;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
char c[20];
cin.getline(c, 10, '/');
cout << c << endl;
//cin.ignore();
cin.get(c, 10);
cout << c << endl;
system("pause");
return 0;
}
#include<iostream>
using namespace std;
int main()
{
char c;
while (cin.get(c))
{
if (c != ' ')
cout.put(c);
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
char c[20];
char x;
cin.getline(c, 15, '/');
cout << c << endl;
x = cin.peek();
cout << x << endl;
cin.putback(c[0]);
cin.getline(c, 15, '/');
cout << c << endl;
system("pause");
return 0;
}
int a[10] = { 1,2,4,5,3,6,7,9,8,0 };
ofstream outfile("t1.txt", ios::out);
if (!outfile)
{
cerr << "Open file error" << endl;
abort();
}
for (int i = 0; i < 10; i++)
{
outfile << a[i] << " ";
cout << a[i] << endl;
}
outfile.close();
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
int a[10];
ifstream infile("t1.txt", ios::in);
if (!infile)
{
cerr << "error" << endl;
exit(0);
}
for (int i = 0; i < 10; i++)
{
infile >> a[i];
cout << a[i] << " ";
}
int max = a[0];
int order = 0;
for (int i = 0; i < 10; i++)
{
if (a[i] > max)
{
max = a[i];
order = i;
}
}
cout << max << " " << order << endl;
system("pause");
return 0;
}
}
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
char c[80];
ofstream outfile("t2.txt", ios::out);
if (!outfile)
{
cerr << "error" << endl;
exit(0);
}
cin.getline(c, 80);
for (int i = 0; c[i] != 0; i++)
{
if (c[i] >= 'a' && c[i] <= 'z' || c[i] >= 'A' && c[i] <= 'Z')
{
outfile << c[i];
cout << c[i];
}
}
cout << endl;
outfile.close();
ifstream infile("t2.txt", ios::in);
if (!infile)
{
cerr << "error" << endl;
exit(0);
}
ofstream outf("t3.txt", ios::out);
char ch;
while (infile.get(ch))
{
if (ch >= 'a' && ch <= 'z')
ch = ch - 32;
cout << ch;
outf.put(ch);
}
infile.close();
outf.close();
system("pause");
return 0;
}
void Read_Display(char* filename)
{
ifstream infile(filename, ios::in);
if (!infile)
{
cerr << "Openfile error" << endl;
abort();
}
char c;
while (infile.get(c))
{
cout.put(c);
}
cout << endl;
infile.close();
}
#include<iostream>
#include<fstream>
using namespace std;
struct student
{
int num;
char name[20];
};
int main()
{
student stud[5];
student stud1[5];
ifstream infile("stud1.dat", ios::in | ios::binary);
for (int i = 0; i <= 4; i++)
{
infile.read((char*)&stud[i/2], sizeof(stud[0]));
}
infile.close();
//infile.read((char*)&stud[0], sizeof(stud));
stud[2].num = 8899123;
strcpy(stud[2].name, "你哈");
ofstream outfile("stud1.dat", ios::out | ios::binary);
outfile.seekp(2 * sizeof(stud), ios::beg);
outfile.write((char*)&stud[2], sizeof(stud[0]));
outfile.close();
ifstream infile1("stud1.dat", ios::in | ios::binary);
infile1.read((char*)&stud1[0], sizeof(stud1));
infile1.close();
for (int i = 0; i < 5; i++)
{
cout << stud1[i].num << " " << stud1[i].name << endl;
}
system("pause");
return 0;
}
//二分法
#include<iostream>
#include<cmath>
using namespace std;
double F(double x)
{
return x + exp(x) - 2;
}
double turnal(double X1, double X2)
{
const double delta = 0.0001;
double x;
x = (X1 + X2) / 2;
if (F(x) > 0)
X2 = x;
else if (F(x) < 0)
X1 = x;
else
X1 = X2 = x;
double X0 = (X1 + X2) / 2;
if (abs(F(X0)) <= delta || abs(X1 - X2) <= delta)
return F(X0);
return turnal(X1, X2);
}
int main()
{
cout<<turnal(0, 1);
system("pause");
}
#include<iostream>
#include<cmath>
using namespace std;
double function(double x)
{
return (x*x*x - 11 * x*x + 32 * x - 28);
}
double dY(double x)
{
return (3 * x*x - 22 * x + 32);
}
double getx(double x)
{
return -function(x) / dY(x) + x;
}
double Nuwten(double x, double delta)
{
if (abs(function(x)) <= delta)
{
return x;
}
x = getx(x);
return Nuwten(x, delta);
}
int main()
{
cout << Nuwten(0, 0.000001);
system("pause");
return 0;
}