Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

prg-sem-sp / 3 / sobran

.doc
Скачиваний:
12
Добавлен:
28.03.2015
Размер:
43.01 Кб
Скачать

// Сколько раз первое слово

#include <stdio.h>

#include <string.h>

#include <alloc.h>

void main(){

char *s=(char *)malloc(80),*q=(char *)malloc(80);int col=0;

printf("Input string");

scanf("%s",q);

gets(s);

while((s=strstr(s,q))!=NULL)

{

if((s[strlen(q)]==' '||s[strlen(q)]=='\0')&&*(s-1)==' ') col++;

s+=strlen(q);

}

printf("%d",col);

}

// Сколько раз введенное слово

#include <stdio.h>

#include <string.h>

#include <alloc.h>

void main(){

char *s=(char *)malloc(80),*q=(char *)malloc(80);int col=0;

printf("Input string:");

gets(s);

printf("Input word:");

gets(q);

while((s=strstr(s,q))!=NULL)

{

if(s[strlen(q)]==' '||s[strlen(q)]=='\0') col++;

s+=strlen(q);

}

printf("%d",col);

}

// Из bin файла считать матрицу и вывести ее главную диагональ

#include <stdio.h>

#include <alloc.h>

#include <math.h>

void main(){

FILE *fp=fopen("my.bin","rb");

int *ptr, size=0,temp;

while(!feof(fp)){

ptr=(int *)realloc(ptr,sizeof(int)*(size+1));

fread(&temp,1,sizeof(int),fp);

if(!feof(fp)) ptr[size++]=temp;

}

if(float(sqrt(size))!=(int)sqrt(size)) printf("non square matrics");

for(int i=0;i<size;i+=(int)sqrt(size)+1) printf("%d ",ptr[i]);

}

// Из bin файла считать матрицу,найти max элемент и сумму индексов

// четных элементов

#include <stdio.h>

#include <alloc.h>

#include <math.h>

#include <stdlib.h>

void main(){

printf("Vvadite razmer:");

int r,c,temp,max,sum=0,fl=0;

scanf("%d %d",&c,&r);

if(!(r%2)||!(c%2)) {printf("net");exit(0);}

FILE *fp=fopen("r.bin","rb");

for(int i=0;i<r;i++)

for(int j=0;j<c;j++)

{

fread(&temp,1,sizeof(int),fp);

if(!fl) {max=temp;fl=1;}

if(!feof(fp)){

if(max<temp) max=temp;

if(!(temp%2)) sum+=(i+j+2);

}

}

printf("max:%d sum:%d",max,sum);

}

// Считать структуру и вывести на экркн

#include <stdio.h>

struct mem{

char name[100];

int age;

float sb;

};

void main(){

mem ptr;

FILE * fp=fopen("my.bin","rb");

fread(&ptr,1,sizeof(mem),fp);

name[99]='\0';

printf("%s %d %f",ptr.name,ptr.age,ptr.sb);

fclose(fp);

}

// Перевести из одной в другую ,считать из бинарника

#include <stdio.h>

#include <stdlib.h>

struct {

long z;

int r;

}x;

void main(){

FILE *fp=fopen("my.bin","rb");

fread(&x,1,sizeof(x),fp);

char str[80];

ltoa(x.z,str,x.r);

printf("%ld -> %s",x.z,str);

}

// Динамические массивы

#include <stdio.h>

#include <string.h>

#include <alloc.h>

void main(){

char *s=(char *)malloc(80),*q=(char *)malloc(80);int col=0;

printf("Input string");

scanf("%s",q);

gets(s);

while((s=strstr(s,q))!=NULL)

{

if((s[strlen(q)]==' '||s[strlen(q)]=='\0')&&*(s-1)==' ') col++;

s+=strlen(q);

}

printf("%d",col);

}

// Перевести дробное 0<float<1 в (7) систему

#include <stdio.h>

void main(){

float q;

printf("Input float:");

scanf("%f",&q);int i=0;

printf("0.");

while(i++<8){

printf("%d",(int)(q*7));

q=q*7-(int)(q*7);

}

}

// Найти след матрицы

#include <stdio.h>

void main(){

int size;

printf("Input razmer:");

scanf("%d",&size);int pr=1,q;

for(int i=0;i<size;i++)

for(int j=0;j<size;j++){

printf("a[%d][%d]=",i,j);

scanf("%d",&q);

if(i==j) pr*=q;

}

printf("%d",pr);

}

// Из текстового файла считать и вывести три поля

#include <stdio.h>

void main(){

FILE * fp=fopen("my.txt","rt");

char name[128];int age;float sb;

fscanf(fp,"%s",name);

fscanf(fp,"%d",&age);

fscanf(fp,"%f",&sb);

printf("NAME: %s \n AGE:%d \n SB: %f",name,age,sb);

}

// Из bin-файла считать кол-во элементов в строке и вывести матрицу

#include <stdio.h>

#include <alloc.h>

void main(){

FILE * fp=fopen("my.bin","rb");

int col;

fread(&col,1,sizeof(int),fp);

int * ptr=(int *)malloc(col*sizeof(int));

while(!feof(fp)){

fread(ptr,col,sizeof(int),fp);

printf("\n");

if(!feof(fp)) for(int i=0;i<col;i++) printf("%d ",ptr[i]);

}

}

//Найти max из float-ов,считанных из bin-файла,вывести еге и его порядковый

//номер

#include <stdio.h>

void main(){

FILE * fp=fopen("float.bin","rb");

float z,max;int i=0,j=0;int flag=0;

while(!feof(fp)){

fread(&z,sizeof(float),1,fp);

if(!flag) {max=z;flag=1;} else

if(!feof(fp)) if(z>max) {max=z;j=i;}

i++;

}

printf("%f %d",max,j);

}

// Найти след матрицы,считанный из бинарного файла

#include <stdio.h>

#include <stdlib.h>

void main(){

FILE * fp=fopen("my.txt","rt");

int r,c,sled=1,temp;

fscanf(fp,"%d %d ",&r,&c);

if(r!=c) {printf("NET SLEDA!!!");exit(0);}

for(int i=0;i<r;i++)

for(int j=0;j<c;j++){

fscanf(fp,"%d",&temp);

if(i==j) sled*=temp;

}

printf("Sled: %d",sled);

}

// long (z) -> int (m) перевести

#include <stdio.h>

#include <stdlib.h>

void main(){

FILE * fp=fopen("my.bin","rb");

long z;int m;

fread(&z,sizeof(z),1,fp);

fread(&m,sizeof(m),1,fp);

char str[80];

ltoa(z,str,m);

printf("%s",str);

}

// Перевести из (10) -> (2)

#include<conio.h>

#include<stdio.h>

#include<string.h>

void main(){

int q;

printf("Input chislo:");

scanf("%d",&q);int x[15];

int i=0;

do{

x[i++]=q%2;

q=q/2;

}while(q>0);

while(i--) printf("%d",x[i]);

}

// Транспонирование матрицы

#include <stdio.h>

#include <alloc.h>

void main(){

int r,c;

printf("Input r,c:");

scanf("%d %d",&r,&c);

int * matr=(int *)malloc(r*c*sizeof(int));

int * matr_t=(int *)malloc(r*c*sizeof(int));

for(int i=0;i<r;i++)

for(int j=0;j<c;j++){

printf("a[%d][%d]=",i,j);

scanf("%d",&matr[i*c+j]);

}

for(i=0;i<c;i++){printf("\n");

for(int j=0;j<r;j++)

printf("%d ",matr_t[i*c+j]=matr[j*c+i]);

}

}

3

Соседние файлы в папке 3