|
Участник форума
Регистрация: 27.11.2008
Сообщений: 161
С нами:
9185589
Репутация:
128
|
|
Сообщение от velvetdust
Сделайте пожалуйста за вознаграждение 4 лабораторные работы (язык Си, не С++ !!!)
Вообще-то хоть на первом курсе нужно чему-то учиться и делать самому, хотя-бы такие вещи. Ну да ладно, может у тебя есть уважительная причина? ))) Вот набросал все четыре задачи, а отчеты к ним делай сам, во всяком случае, этот код намного лучше того, что лежит в так называемых примерах у тебя.
1)
Код:
#include <stdio.h>
#include <stdlib.h>
enum {NAME_LEN_MAX=100,GROUP_NAME_MAX=20};
typedef struct {
char name[NAME_LEN_MAX];
char group[GROUP_NAME_MAX];
int sex;
int height;
} student;
void fill_student(student * stud);
int main(int argc, char ** argv){
int stud_count = 0;
(void)printf("Enter the count of students: ");
(void)scanf("%d",&stud_count);
student * students = malloc(stud_count * sizeof(student));
for(int i=0; i < stud_count; i++)
fill_student(&students[i]);
for(int i=0; i < stud_count - 1; i++)
for(int j=i+1; j < stud_count; j++)
if(students[i].height == students[j].height)
(void)printf("They have equivalent height(%d) %s and %s\n",
students[i].height,students[i].name,students[j].name);
exit(EXIT_SUCCESS);
}
void fill_student(student * stud) {
printf("Enter the name: ");
scanf("%s",stud->name);
printf("Enter the group: ");
scanf("%s",stud->group);
printf("Enter the sex [0 or 1]: ");
scanf("%d",&stud->sex);
printf("Enter the height: ");
scanf("%d",&stud->height);
}
2)
Код:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum{STRING_LEN_MAX=250};
int main(int arg,char ** argv) {
FILE * fd = fopen("infile.txt","r");
FILE * out_file = fopen("outfile.txt","w");
char buf[STRING_LEN_MAX];
int i=1;
int all_count=0;
(void)fgets(buf,STRING_LEN_MAX,fd);
while(!feof(fd)){
int cur_string_count=0;
for(int j=0; j<strlen(buf)-1;j++)
if(buf[j]=='f'&&(buf[j+1]==' '||buf[j+1]==','||buf[j+1]=='.'))
++cur_string_count;
if(buf[strlen(buf)-2]=='f')
++cur_string_count;
if(cur_string_count>0)
(void)fprintf(out_file,"String number %d have %d words\n",i,cur_string_count);
all_count+=cur_string_count;
++i;
(void)fgets(buf,STRING_LEN_MAX,fd);
}
(void)fprintf(out_file,"Total: %d\n",all_count);
fclose(fd);
fclose(out_file);
exit(EXIT_SUCCESS);
}
3)
Код:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum{STRING_COUNT_MAX=100,STRING_LEN_MAX=250};
int main(int argc, char ** argv){
FILE * infile = fopen("infile.txt","r");
char * strings[STRING_COUNT_MAX];
char buf[STRING_LEN_MAX];
int string_num = 0;
(void)fgets(buf,STRING_LEN_MAX,infile);
while(!feof(infile)) {
strings[string_num]=calloc(STRING_LEN_MAX,sizeof(char));
for(int i=1,j=0;i<strlen(buf);i++){
if(buf[i]==' ' && buf[i-1]==' ')
continue;
else
strings[string_num][j++]=buf[i];
}
++string_num;
(void)fgets(buf,STRING_LEN_MAX,infile);
}
fclose(infile);
infile = fopen("infile.txt","w");
for(int i=0;i<string_num; i++)
(void)fprintf(infile,"%s\n",strings[i]);
fclose(infile);
exit(EXIT_SUCCESS);
}
4)
Код:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum{STRING_COUNT_MAX=100,STRING_LEN_MAX=250};
int main(int argc, char ** argv) {
int last_lines=0, string_num=0;
if(argc == 2)
last_lines = atoi(argv[1]);
FILE * infile = fopen("infile.txt","r");
char buf[STRING_LEN_MAX];
char * strings[STRING_COUNT_MAX];
(void)fgets(buf,STRING_LEN_MAX,infile);
while(!feof(infile)){
strings[string_num] = calloc(STRING_LEN_MAX,sizeof(char));
(void)strcpy(strings[string_num++],buf);
(void)fgets(buf,STRING_LEN_MAX,infile);
}
int string_index = 0;
if(last_lines != 0)
string_index = string_num - last_lines;
if(string_index < 0) string_index = 0;
for(int i = string_index; i < string_num; i++)
(void)printf("%s",strings[i]);
fclose(infile);
exit(EXIT_SUCCESS);
}
Разбирайся, шалопай  Удачной сессии ))))
|