1. : .
2.1 : , , , .
2.2 , , , .
.
3.1 1
// 1
#include "stdafx.h"
#include <iostream>
using namespace std;
void main(){
int n;
cout<<"\n Input n =";
cin>>n;
double **matr;
matr = new double* [n];
if(matr == NULL) {
cout<<"\n He ";
return;
}
for(int i = 0; i < n; i++){
matr[i] = new double [n];
if(matr[i]== NULL) {
cout<<"\n He ";
return;
}
for(int j = 0; j < n;j++)
if(i!= j) matr [i][j] = 0;
else matr[i][j] = 1;
}
for(int i = 0; i < n; i++){
cout<<"\n string "<<":"<<i;
for(int j = 0;j< n; j++)
cout<<"\t"<<matr[i][j];
}
for(int i=0; i < n; i++) delete matr[i];
delete[]matr;
getchar();getchar();
}
3.2 1 ( .)
3.3 , , , . ( ) .
3.4 .
3.5 .(*)
3.6 .
3.6 .
4.1 . .
4.2 , 3.3 3.4 .
5.1 new, delete, malloc(), free(). .
5.2 .
5.3 , ( ) , .
5.4 .
5.5 , ( ) , .
5.6 , ( ) , .
5.7 , ( ) , .
|
|
5.8 , ( ) , .
5.9 , ( ) , .
5.10 . .
7
.
1. : .
1. , , , , .
1
(prior) - ; N(next) - ; begin - ; - , .
2. 1.
1. 1
// 1
#include "stdafx.h"
#include<string.h>
#include<iostream>
using namespace std;
struct card { //
char *author; // ...
char *title; //
char *city; //
char *firm; //
int year; //
int pages; //
};
// :
void printbook(card& car)
{ static int count = 0;
cout<<"\n"<< ++count <<". "<<car.author;
cout<<" "<<car.title<<".- "<<car.city;
cout<<": "<<car.firm<<", ";
cout<<"\n"<<car.year<<".- "<<car.pages<<" c.";
}
struct record { // (1)
card book;
record *prior;
record *next;
};
// :
card books[] = { // : (2)
{ "Wiener R.S.", "Turbo C",
"M", "ST",1991, 384},
{ "Stroustrup B.","Langvige C",
"kiev","DiaSoft",1993, 560},
{ "Turbo C++.", "For programm",
"M","INTKV",1991,394},
{ "Limppman S.B.","C++ for new",
"M","GELION",1993,496}
};
void main()
{ record *begin = NULL, // ࠠ (3)
|
|
*last = NULL, //
*list; //
// n- :
int n = sizeof(books)/sizeof(books[0]);
// :
for (int i=0;i<n; i++)
{// ( ): (4)
last = new(record);
// :
(*last).book.author = books[i].author;
(*last).book.title = books[i].title;
last->book.city = books[i].city;
last->book.firm = books[i].firm;
last->book.year = books[i].year;
last->book.pages = books[i].pages;
// ( ):
if (begin == NULL) // (5)
{last->prior = NULL;
begin = last;
last->next = NULL;
}
else
{ //
list = begin;
// -
// :
while (list) //(6)
{if (strcmp(last->book.author,
list->book.author) < 0)
{// list:
if (begin == list)
{// : (7)
last->prior = NULL;
begin = last;
}
else
{// : (8)
list->prior->next = last;
last->prior = list->prior;
}
list->prior = last;
last->next = list;
// :
break;
}
if (list->next == NULL)
{// : (9)
last->next = NULL;
last->prior = list;
list->next = last;
// :
break;
}
// :
list = list->next;
}//
//( )
} //
} //
// :
list = begin; // (10)
cout<<"\n";
while (list) {
printbook(list->book);
list = list->next;
}
getchar();
}
2. 1 ( .)
3. , card, ( ) card .
4. 1 , .
5. , *.
6. .
.
1. .
2. 3,4 5 .
1. , , ?
2. void* type*?
3. , ? , .
|
|
4. ? / /?
5. typedef , .
6. , , , , .
7. .
8. .
9. .
10. ? .
11. .
12. card, , . ( card qsort() , ).
8
,
: .
.
: , ; ; ; ; .
1. 1
// 1
#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <MATH.H>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
// class Matr
//-------------------------------------------------------------------
template <class T>
class Matr
{
long line;
long col;
T ** matr;
public:
virtual ~Matr(); //
Matr(long l, long c); //
Matr():line(0), col(0),matr(NULL){} //
Matr(Matr<T>& A); //
T * operator[](long i){return matr[i];}
template <class T>
friend Matr<T> operator*(Matr<T>& A, Matr<T>& B);
const Matr<T>& operator=(const Matr<T>& A);
template <class T>
friend Matr<T> operator*(double K, const Matr<T>& A);
Matr<T> operator+(const Matr<T>& A);
Matr<T> operator-(const Matr<T>& A);
void display();
};
// Matr<T>::Matr()
//-------------------------------------------------------------------
template <class T>
Matr<T>::Matr(long l1, long col1)
{
line = l1;
col = col1;
matr = new T*[line];
if(matr == NULL) cout<< "Out of memory";
for (int i = 0; i < line; i++){
matr[i] = new T[col];
if(matr[i] == NULL) cout<<"Out of memory";
}
}
// Matr<T>::Matr(Matr<T>& A)
//-------------------------------------------------------------------
template <class T>
Matr<T>::Matr(Matr<T>& A):line(A.line), col(A.col)
{
matr = new T*[line];
if(matr == NULL) cout<< "Out of memory";
|
|
for (int i = 0; i < line; i++){
matr[i] = new T[col];
if(matr[i] == NULL) cout<<"Out of memory";
}
for(long j = 0; j<line; j++){
for(int i = 0; i<col; i++){
matr[j][i] = A.matr[j][i];
}
}
}
// Matr<T>::~Matr()
//-------------------------------------------------------------------
template <class T>
Matr<T>::~Matr()
{
for (int i = 0; i < line; i++) delete[] matr[i];
delete matr;
}
// void display(const Matr<T>& A)
//-------------------------------------------------------------------
template <class T>
void Matr<T>::display(){
cout<<"\n";
for(int i = 0; i<line;i++){
cout<<"\n";
for(int j = 0; j < col; j++)
cout<<matr[i][j]<<"\t";
}
}
// Matr<T>::operator*()
//-------------------------------------------------------------------
template <class T>
Matr<T> operator*(Matr<T>& A,
Matr<T>& B)
{
if(!(A.col == B.line)) cout<<"\n A*B A.col!= B.line";
Matr<T> arMatr(A.line, B.col);
long l1 = A.line;
long col1 = A.col;
long col2 = B.col;
for(long i = 0; i < l1; i++){
for(long j = 0; j < col2; j++){
arMatr[i][j] = 0;
for(long k = 0; k < col1; k++) {
arMatr[i][j]+=A[i][k]*B[k][j];
}
}
}
return arMatr;
}
// Matr::operator=()
//-------------------------------------------------------------------
template <class T>
const Matr<T>& Matr<T>::operator=(const Matr<T>& A)
{
if(this == &A) return *this;
line = A.line;
col = A.col;
for(long i = 0; i<A.line; i++){
for(long j = 0; j<A.col; j++){
matr[i][j] = A.matr[i][j];
}
}
return *this;
}
// Matr<T>::operator*()
//-------------------------------------------------------------------
template <class T>
Matr<T> operator*(double K, const Matr<T>& A)
{
Matr<T> M(A.line, A.col);
for(long i = 0; i<A.line; i++){
for(long j = 0; j<A.col; j++){
M.matr[i][j] = K * A.matr[i][j];
}
}
return M;
}
// Matr<T>::operator+()
//-------------------------------------------------------------------
template <class T>
Matr<T> Matr<T>::operator+(const Matr<T>& A)
{
if(line!= A.line || col!= A.col) {
cout<<"\n A!= B";
Matr<T> M(0,0);
return M;
}
Matr<T> M(A.line, A.col);
for(long i = 0; i<A.line; i++){
for(long j = 0; j<A.col; j++){
M.matr[i][j] = matr[i][j] + A.matr[i][j];
}
}
return M;
}
// Matr<T>::operator-()
//-------------------------------------------------------------------
template <class T>
Matr<T> Matr<T>::operator-(const Matr<T>& A)
{
if(line!= A.line) {
cout<<"\n - no A.line = B.line";
Matr<T> M(0,0);
return M;
}
if(col!= A.col) {
cout<<"\n - no A.line = B.line";
Matr<T> M(0,0);
return M;
}
Matr<T> M(A.line, A.col);
for(long i = 0; i<A.line; i++){
for(long j = 0; j<A.col; j++){
M.matr[i][j] = matr[i][j] - A.matr[i][j];
}
}
return M;
}
// TMatr()
//-------------------------------------------------------------------
template <class T>
Matr<T> TMatr(Matr<T>& M){
Matr<T> TM(M.col, M.line);
for(int i = 0; i < M.line; i++)
for(int j = 0; j < M.col; j++)
TM[j][i] = M[i][j];
return TM;
}
void main(){
Matr<double> A(2,2), B(2,2);
A[0][0]=A[0][1]=A[1][0]=A[1][1] = 1;
B[0][0]=B[0][1]=B[1][0]=B[1][1] = 2;
A.display();
B.display();
A=(2.5*A-A+B)*B;
A.display();
getchar();
}
2. , , . ( , , cout<<"\n .";) main().
3. , .. , ==,!=, >, <, A * const, cout<<A. , > , max |aij |> max |bij|; i = 1,,N; j = 1, , M.
|
|
4. , , .
5. .
5. , , , , , .
6. .
7. , 3 4 .
.
1. ? , , ?
2. operator=? operator= , , ?
3. . .
4. . ++. .
5. vector () { +, -, ==, =,!=, }.
6. str () { +, -, ==, =,!=, <, >}.
7. {==,!=, <,> } ( ).
8. {==,!=, <,> } ( ).
9. , : , , .
10. , .
11. Matr , Matr.
12. , Matr .
13. ? ? ++ ? , .
N 9