Шпоры по программированию и основам алгоритмизации1
.docРешение уравнений методом половинного деления.
tg (x/2)-ctg(x/2)+x=0
e=0.0001 [1,2]
# include <conio.h><iostream.h><math.h>
void main()
{double a=1,b=2,e=0.0001;
double x,fx,fa;
fa=tan(x/2)-1/tan(x/2)+x;
x=(a+b)/2;
fx=tan(x/2)-1/tan(x/2)+x;
if(x==0) cout<<x;
else {while ((b-a)>e)
{if((fa*fx)<0) b=x;
else {a=x; fa=fx;}
x=(a+b)/2; fx=tan(x/2)-1/tan(x/2)+x;}}
if ((b-a)<=e) cout<<”корень Ур.”<<x;
getch();}}
Функции. Указатели.
Перемножить матрици
#include<iostream.h> <stdio.h><conio.h>
#define N 2
void main()
{int kolvo,l,i,j,k,u;
clrscr();
int A1[N][N],B1[N][N],C[N][N];
int A[N][N]={1,2,1,1};
int B[N][N]={1,2,1,1};
cout<<"Vvedite kolvo: ";
cin>>kolvo;
for (k=0;k<N;k++)
for (l=0;l<N;l++)
{ C[k][l]=0;}
cout<<"Vvedite matricu 1"<<": \n";
for (i=0;i<N;i++)
for (j=0;j<N;j++)
cin>>C[i][j];
for (u=0;u<kolvo-1;u++)
{cout<<"Vvedite matricu "<<u+2<<": \n";
for (k=0;k<N;k++)
for (l=0;l<N;l++)
cin>>A[k][l];
proiz(C,N,A,N); }
for (k=0;k<N;k++)
{for (l=0;l<N;l++)
cout<<" "<<C[k][l];
cout<<"\n"; }
getch();}
Структуры
Структура: substance, поля: название вещества,
плотность, проводимость (проводник,
полупроводник, изолятор). Ключевые поля: название
вещества. Поиск самого лёгкого вещества заданной
проводимости.
Функция ввода одной строки таблицы:
void input (struct substance*ps)
{cout<<"enter name:";
cin>>ps->name;
cout<<"plotnost = ";
cin>>ps->plotnost;
cout<<"Vvedite provodim:\n"<<"provodnik - 1\n"
<<"poluprovodn - 2\n"<<"izolat - 3\n";
cin>>ps->provodimost;}
Листинг основной функции программы:
#include <string.h>
void main()
{struct substance sub[N];
int n,i,j,Z,imin=0;
char pr[7];
clrscr();
cout<<"Chislo vesestv v tablice:";
cin>>n;
for (i=0;i<n;i++)
input (&sub[i]);
cout<<"Provodimost:";
cin>>pr;
Z=1000;
for (i=0;i<n;i++)
{if ((strcmp(sub[i].provodimost,pr)==0)&&(sub[i].plotnost<Z))
{ Z=sub[i].plotnost;
imin=i; }
if (Z==1000) cout<<"Vesestv s takoy provodimostyu net";
else cout<<"Samoe legkoe vesestvo - "<<sub[imin].name;}
cout<<"Samoe legkoe vesestvo - "<<sub[imin].name;
getch();}
Строки
Вывести на экран самое длинное слово
заданного предложения и указать его длину.
Листинг программы:
#include<iostream.h><string.h><conio.h><stdio.h>
void main()
{const int N=100;
const int L=100;
char words [N][L], word [N];
char*pStr;
char*pStr2;
char*del="!,.-:;()";
char*ravno;
int i,m,m1,k,z;
clrscr();
m=0;m1=0;k=0;
cout<<"Vvedite predlozhenie"<<"\n";
for(i=0;i<N;i++)
{ cin>>words[i];
pStr=strchr(words[i],'.');
if (*pStr!='.') pStr=strchr(words[i],'!');
k=k+1;
if (pStr) goto exit; }
exit: for(i=0;i<k;i++)
{ m=strlen(words[i]);
if (m>m1) m1=m, z=i;}
ravno=strtok(words[z],del);
m=strlen(ravno);
if ((pStr)==(pStr2=strchr(words[z],*pStr))) m1=m1-1;
cout<<"Samoe dlinnoe slovo - "<<ravno
<<" "<<m1<<" bukv";
getch();}
Файлы
#include <iostream.h> <fstream.h>
void main (void)
{ ifstream input_file("BOOKINFO.DAT"); char line[64]; while (! input_file.eof())
{ input_file.getline(line, sizeof(line)); cout << line << endl; }}
void main(void) {ifstream input_file("BOOKINFO.DAT"); char word[64] ; while (! input_file.eof()) {input_file >> word; cout << word << endl;}}
void main(void) {ifstream input_file("BOOKINFO.DAT"); char letter; while (! input_file.eof()) {letter = input_file.get(); cout << letter;}}
Списки
Создать односвязный не кольцевой список,
элементами которого являются структуры
Структура: substance, поля: название вещества, плотность,
проводимость (проводник, полупроводник, изолятор).
Ключевые поля: название вещества.
#include<string.h><iostream.h><stdio.h><conio.h><stdlib.h>
struct Substance
{char metal[10];int pl;
char prov[10];};
struct Node
{Substance tr;
Node* next;};
typedef Node * Pointer;
int get(int,int);
void read(Substance&t0);
Pointer remove(Pointer beg0);
void SortBD(Pointer&beg0);
Pointer add(Pointer beg0,Substance t0);
void Find(Pointer beg0);
void Read(Substance &t0)
{clrscr();
cout<<"Input metal:\n";
scanf("%10s",t0.metal);
cout<<"Input plotnost:\n";
t0.pl=get(1,15000);
cout<<"Input provodimost:\n";
scanf("%10s",t0.prov);}
void Find(Pointer beg0)
{Pointer p,pr;
int flag=0;
Substance t0;
cout<<"Input provodimost:";
cin>>t0.prov; p=beg0;
while(p!=NULL)
{if (!strcmp(p->tr.prov,t0.prov))
{flag=1;
cout<<p->tr.metal<<" "<<p->tr.pl<<" "<<p->tr.prov<<endl;}
p=p->next;}
if(!flag)cout<<"Item doesnt exist";
getch();}
void PrintDbase(Pointer beg0)
{Pointer p;
clrscr(); p=beg0;
printf("%-10s %-10d %-10s\n","Metal","Plotnost","Provodimost");
while(p!=NULL)
{printf("%-10s %-10d %-10s\n",p->tr.metal,p->tr.pl,p->tr.prov);
p=p->next;}}
void SortBD(Pointer&beg0)
{int fl;
Pointer p1,p2,p3;
if((beg0==NULL)||(beg0->next==NULL))return;
do
{fl=0;
p2=beg0;
p3=p2->next;
if(p2->tr.pl>p3->tr.pl)
{p2->next=p3->next;
p3->next=p2;
beg0=p3;
fl=1;}
p1=beg0;
while(p1->next->next!=NULL)
{p2=p1->next;
p3=p2->next;
if(p2->tr.pl>p3->tr.pl)
{p2->next=p3->next;
p3->next=p2;
p1->next=p3;
fl=1;}
p1=p1->next;}}
while(fl==1);
}Pointer add(Pointer beg0,Substance t0)
{Pointer p,q;
p=new Node;
p->tr=t0;
p->next=NULL;
q=beg0;
if(beg0!=NULL)
{while (q->next!=NULL) q=q->next;
q->next=p;}
else beg0=p;
return beg0;}
void main()
{Pointer beg;
int key,i;
clrscr();
beg=NULL;
while(1)
{Substance t;
clrscr(); cout<<"\n";
cout<<"1.Add new item\n";
cout<<"2.View base\n";
cout<<"3.Search by provodimost\n";
cout<<"4.Sorting\n";
cout<<"5.Ext\n\n\n\n";
cout<<"Your choice...:";
key = get(1,5);
switch(key)
{case 1:Read(t); beg=add(beg,t);break;
case 2:PrintDbase(beg);getch();break;
case 3:Find(beg);break;
case 4:SortBD(beg);clrscr();PrintDbase(beg);getch();break;
case 5:return;}}}
int get(int min,int max)
{int i=0;
fscanf(stdin,"%d",&i);
fflush(stdin);
while((i>max)||(i<min))
{printf("Your have incorectly entered.Please enter again(%d-%d):",min,max);
fscanf(stdin,"%d",&i);
fflush(stdin);}
return i;}