игра брюс 2048
Главная / Программирование / Программирование на языке C в Microsoft Visual Studio 2010 / Тест 13

Программирование на языке C в Microsoft Visual Studio 2010 - тест 13

Упражнение 1:
Номер 1
Что такое структура?

Ответ:

 (1) конечная именованная последовательность однотипных величин 

 (2) именованный объект, хранящий данные на каком-либо носителе 

 (3) совокупность нескольких переменных, часто различных типов 


Номер 2
В чем отличие структуры от массива?

Ответ:

 (1) структура может содержать элементы разных типов 

 (2) структура не имеет имени 

 (3) структура не может содержать несколько элементов одинакового типа 


Номер 3
Описан шаблон структуры:

struct addr
{
  char name[30];
  char street[40];
  char city[20];
  char state[3];
  unsigned long int zip;
};

Какие утверждения в данном случае являются верными?		

Ответ:

 (1) объявлена переменная addr 

 (2) объявлен тип addr 

 (3) после объявления структуры не должно быть ; 


Упражнение 2:
Номер 1
Что в приведенном описании структуры является ее тегом?

struct addr {
  char name[30];
  char street[40];
  char city[20];
  char state[3];
  unsigned long int zip;
} addr_info, binfo;
		
		

Ответ:

 (1) addr 

 (2) name 

 (3) street 

 (4) city 

 (5) state 

 (6) zip 

 (7) addr_info 

 (8) binfo 


Номер 2
Какие переменные, имеющие тип заданной структуры, объявлены в приведенном описании?

struct addr {
  char name[30];
  char street[40];
  char city[20];
  char state[3];
  unsigned long int zip;
} addr_info, binfo;
		
		

Ответ:

 (1) addr 

 (2) name 

 (3) street 

 (4) city 

 (5) state 

 (6) zip 

 (7) addr_info 

 (8) binfo 


Номер 3
Какие поля имеет приведенная структура?

struct addr {
  char name[30];
  char street[40];
  char city[20];
  char state[3];
  unsigned long int zip;
} addr_info, binfo;
		
		

Ответ:

 (1) addr 

 (2) name 

 (3) street 

 (4) city 

 (5) state 

 (6) zip 

 (7) addr_info 

 (8) binfo 


Упражнение 3:
Номер 1
Объявление какой структуры содержит ошибку?

Ответ:

 (1) struct Worker { char fio[30]; int date, code; double salary; };  

 (2) struct List; struct Link { List *p; Link *prev; };  

 (3) struct Test { int a, b; Test t; };  

 (4) struct A {int a; double x; }; struct B {A a; double x; };  


Номер 2
Какое объявление структур(-ы) не содержит ошибку?

Ответ:

 (1) struct Worker { char fio[30]; int date, code; double salary; };  

 (2) struct List; struct Link { List *p; Link *prev; };  

 (3) struct Test { int a, b; Test t; };  

 (4) struct A {int a; double x; }; struct B {A a; double x; };  


Номер 3
Определите, содержит ли приведенное объявление структур ошибки? Если содержит, то укажите, какие именно?

struct A {int a; double x; };
struct B {A a; B b; double x; }; 
			
		

Ответ:

 (1) элемент структуры B не может иметь тип другой структуры A 

 (2) поля разных структур в одной программе не могут иметь одинаковые имена 

 (3) элементы b структуры B не может иметь тип этой же структуры 

 (4) объявления не содержат ошибок 


Упражнение 4:
Номер 1
Объявлена структура:

struct addr
{
  char name[30];
  char street[40];
  char city[20];
  char state[3];
  unsigned long int zip;
};

Каким образом данная структура хранится в памяти?		
		

Ответ:

 (1) files 

 (2) files 

 (3) files 


Номер 2
Объявлена структура:

struct Worker
{
  char fio[30];
  int date, code;
};

Каким образом данная структура хранится в памяти?		
		

Ответ:

 (1) files 

 (2) files 

 (3) files 


Номер 3
Объявлена структура:

struct Account
{
  int account;
  char name[30];
  int balance;
};

Каким образом данная структура хранится в памяти?		
		

Ответ:

 (1) files 

 (2) files 

 (3) files 


Упражнение 5:
Номер 1
Какая программа содержит ошибку?

Ответ:

 (1) struct { char title[51]; char author[51]; int year; int page; float price; } lb; int main (void) { strcpy_s(lb.title, 50, "Экстремальное вождение. Гоночные секреты"); strcpy_s(lb.author, 50, "М.Г. Горбачев"); lb.year = 2007; lb.page = 303; lb.price = 200.00F; printf("\nTitle:\t\t\t %s\n", lb.title); printf("Author:\t\t\t %s\n", lb.author); printf("Year:\t\t\t %d\n", lb.year ); printf("Number of pages:\t %d p.\n", lb.page ); printf("Price:\t\t\t %1.2f y.e.\n", lb.price); return 0; }  

 (2) int main (void) { struct Book { char title[51]; char author[51]; int year; int page; float price; } lb[2]; int i; strcpy_s(lb[0].title, 50, "Экстремальное вождение. Гоночные секреты"); strcpy_s(lb[0].author, 50, "М.Г. Горбачев"); lb[0].year = 2007; lb[0].page = 303; lb[0].price = 200.00F; strcpy_s(lb[1].title, 50, "Маракотова бездна"); strcpy_s(lb[1].author, 50, "А.К. Дойл"); lb[1].year = 1993; lb[1].page = 285; lb[1].price = 150.40F; for (i=0; i<2; i++) { printf("\nTitle:\t\t\t %s\n", lb[i].title); printf("Author:\t\t\t %s\n", lb[i].author); printf("Year:\t\t\t %d\n", lb[i].year ); printf("Number of pages:\t %d p.\n", lb[i].page ); printf("Price:\t\t\t %1.2f y.e.\n\n", lb[i].price); } return 0; }  

 (3) struct Book { char title[51]; char author[51]; int year; int page; float price; }; int main (void) { Book *lb; strcpy_s(lb.title, 50, "Экстремальное вождение. Гоночные секреты"); strcpy_s(lb.author, 50, "М.Г. Горбачев"); lb.year = 2007; lb.page = 303; lb.price = 200.00F; printf("\nTitle:\t\t\t %s\n", lb.title); printf("Author:\t\t\t %s\n", lb.author); printf("Year:\t\t\t %d\n", lb.year ); printf("Number of pages:\t %d p.\n", lb.page ); printf("Price:\t\t\t %1.2f y.e.\n", lb.price); return 0; }  


Номер 2
Какая программа не содержит ошибок?

Ответ:

 (1) struct { char title[51]; char author[51]; int year; int page; float price; } lb; int main (void) { strcpy_s(lb.title, 50, "Экстремальное вождение. Гоночные секреты"); strcpy_s(lb.author, 50, "М.Г. Горбачев"); lb.year = 2007; lb.page = 303; lb.price = 200.00F; printf("\nTitle:\t\t\t %s\n", lb.title); printf("Author:\t\t\t %s\n", lb.author); printf("Year:\t\t\t %d\n", lb.year ); printf("Number of pages:\t %d p.\n", lb.page ); printf("Price:\t\t\t %1.2f y.e.\n", lb.price); return 0; }  

 (2) int main (void) { struct Book { char title[51]; char author[51]; int year; int page; float price; } lb[2]; int i; strcpy_s(lb[0].title, 50, "Экстремальное вождение. Гоночные секреты"); strcpy_s(lb[0].author, 50, "М.Г. Горбачев"); lb[0].year = 2007; lb[0].page = 303; lb[0].price = 200.00F; strcpy_s(lb[1].title, 50, "Маракотова бездна"); strcpy_s(lb[1].author, 50, "А.К. Дойл"); lb[1].year = 1993; lb[1].page = 285; lb[1].price = 150.40F; for (i=0; i<2; i++) { printf("\nTitle:\t\t\t %s\n", lb[i].title); printf("Author:\t\t\t %s\n", lb[i].author); printf("Year:\t\t\t %d\n", lb[i].year ); printf("Number of pages:\t %d p.\n", lb[i].page ); printf("Price:\t\t\t %1.2f y.e.\n\n", lb[i].price); } return 0; }  

 (3) struct Book { char title[51]; char author[51]; int year; int page; float price; }; int main (void) { Book *lb; strcpy_s(lb.title, 50, "Экстремальное вождение. Гоночные секреты"); strcpy_s(lb.author, 50, "М.Г. Горбачев"); lb.year = 2007; lb.page = 303; lb.price = 200.00F; printf("\nTitle:\t\t\t %s\n", lb.title); printf("Author:\t\t\t %s\n", lb.author); printf("Year:\t\t\t %d\n", lb.year ); printf("Number of pages:\t %d p.\n", lb.page ); printf("Price:\t\t\t %1.2f y.e.\n", lb.price); return 0; }  


Номер 3
Какая программа не содержит ошибок?

Ответ:

 (1) struct { char title[51]; char author[51]; int year; int page; float price; } *lb; int main (void) { strcpy_s(lb->title, 50, "Экстремальное вождение. Гоночные секреты"); strcpy_s(lb->author, 50, "М.Г. Горбачев"); lb->year = 2007; lb->page = 303; lb->price = 200.00F; printf("\nTitle:\t\t\t %s\n", lb->title); printf("Author:\t\t\t %s\n", lb->author); printf("Year:\t\t\t %d\n", lb->year ); printf("Number of pages:\t %d p.\n", lb->page ); printf("Price:\t\t\t %1.2f y.e.\n", lb->price); return 0; }  

 (2) struct Book { char title[51]; char author[51]; int year; int page; float price; }; int main (void) { Book *lb; strcpy_s(lb.title, 50, "Экстремальное вождение. Гоночные секреты"); strcpy_s(lb.author, 50, "М.Г. Горбачев"); lb.year = 2007; lb.page = 303; lb.price = 200.00F; printf("\nTitle:\t\t\t %s\n", lb.title); printf("Author:\t\t\t %s\n", lb.author); printf("Year:\t\t\t %d\n", lb.year ); printf("Number of pages:\t %d p.\n", lb.page ); printf("Price:\t\t\t %1.2f y.e.\n", lb.price); return 0; }  

 (3) int main (void) { struct Book { char title[51]; char author[51]; int year; int page; float price; } lb[2]; int i; strcpy_s(lb[0].title, 50, "Экстремальное вождение. Гоночные секреты"); strcpy_s(lb[0].author, 50, "М.Г. Горбачев"); lb[0].year = 2007; lb[0].page = 303; lb[0].price = 200.00F; strcpy_s(lb[1].title, 50, "Маракотова бездна"); strcpy_s(lb[1].author, 50, "А.К. Дойл"); lb[1].year = 1993; lb[1].page = 285; lb[1].price = 150.40F; for (i=0; i<2; i++) { printf("\nTitle:\t\t\t %s\n", lb[i].title); printf("Author:\t\t\t %s\n", lb[i].author); printf("Year:\t\t\t %d\n", lb[i].year ); printf("Number of pages:\t %d p.\n", lb[i].page ); printf("Price:\t\t\t %1.2f y.e.\n\n", lb[i].price); } return 0; }  


Упражнение 6:
Номер 1
Каким образом должна быть объявлена структура Account и переменные acc1 и acc2 для корректного выполнения приведенной программы?

void PrintStruct(char*, int, int);

int main (void)
{
	<Объявление структуры Account и переменных acc1, acc2>

	acc2 = &acc1;
	
	acc1.account = 346578;
	acc1.name = "Ivanov";
	acc1.balance = 30000;
	PrintStruct(acc1.name, acc1.account, acc1.balance); 

	acc2->account = 90005;
	acc2->name = "Pertov";
	acc2->balance = 46000;
	PrintStruct(acc2->name, acc2->account, acc2->balance); 

	return 0; 
}

void PrintStruct(char *str, int a, int b)
{
	printf("Name: %s\n", str);
	printf("Account: %d\n", a);
	printf("Balance: %d\n\n", b);
}
	
		

Ответ:

 (1) struct Account { int account; char name[30]; int balance; } acc1, *acc2;  

 (2) struct Account { int account; char *name; int balance; } *acc1, *acc2;  

 (3) struct Account { int account; char *name; int balance; } acc1, *acc2;  


Номер 2
Какие варианты объявления структуры Account и переменных acc1 и acc2 являются неверными для приведенной программы?

void PrintStruct(char*, int, int);

int main (void)
{
	<Объявление структуры Account и переменных acc1, acc2>

	acc2 = &acc1;
	
	acc1.account = 346578;
	acc1.name = "Ivanov";
	acc1.balance = 30000;
	PrintStruct(acc1.name, acc1.account, acc1.balance); 

	acc2->account = 90005;
	acc2->name = "Pertov";
	acc2->balance = 46000;
	PrintStruct(acc2->name, acc2->account, acc2->balance); 

	return 0; 
}

void PrintStruct(char *str, int a, int b)
{
	printf("Name: %s\n", str);
	printf("Account: %d\n", a);
	printf("Balance: %d\n\n", b);
}
	
		

Ответ:

 (1) struct Account { int account; char name[30]; int balance; } acc1, *acc2;  

 (2) struct Account { int account; char *name; int balance; } acc1, *acc2;  

 (3) struct Account { int account; char *name; int balance; } *acc1, *acc2;  


Номер 3
Какие варианты объявления структуры Account и переменных acc1 и acc2 являются верными для приведенной программы?

void PrintStruct(char*, int, int);

int main (void)
{
	<Объявление структуры Account и переменных acc1, acc2>

	acc2 = &acc1;
	
	acc1.account = 346578;
	strcpy(acc1.name, "Ivanov");
	acc1.balance = 30000;
	PrintStruct(acc1.name, acc1.account, acc1.balance); 

	acc2->account = 90005;
	strcpy(acc2->name, "Pertov");
	acc2->balance = 46000;
	PrintStruct(acc2->name, acc2->account, acc2->balance); 

	return 0; 
}

void PrintStruct(char *str, int a, int b)
{
	printf("Name: %s\n", str);
	printf("Account: %d\n", a);
	printf("Balance: %d\n\n", b);
}
	
		

Ответ:

 (1) struct Account { int account; char name[30]; int balance; } acc1, *acc2;  

 (2) struct Account { int account; char *name; int balance; } acc1, *acc2;  

 (3) struct Account { int account; char *name; int balance; } *acc1, *acc2;  


Упражнение 7:
Номер 1
Чему будет равно значение переменной s в результате выполнения приведенной программы?

int main (void)
{
	struct A { 
		int number;
		int count;
	} a1[2], a2[2];
	int s;
	
	a1[0].number = 1;
	a1[0].count = 12;
	a1[1].number = 1;
	a1[1].count = 12;

	a2[0] = a1[0];
	s = a1[0].count + a2[0].count;

	return 0; 
}
		
		

Ответ:

 (1) 13 

 (2) 24 

 (3) при вычислении значения переменной s возникнет ошибка, так как массив a2 не инициализирован 


Номер 2
Какое сообщение будет выведено на экран в результате выполненния приведенной программы?

int main (void)
{
	struct student { 
		char *name;
		char *birthday;
		int group;
	} s1, s2;

	int s;
	
	s1.name = "Ivanov";
	s1.birthday = "10.03.1985";
	s1.group = 1014;

	s2.name = "Petrov";
	s2.birthday = "08.04.1985";
	s2.group = 1309;

	s2 = s1;

	if (s2.group == s1.group)
		printf("Students %s and %s are studying in the same group\n", s1.name, s2.name);
	else
		printf("Students %s and %s are not studying in the same group\n", s1.name, s2.name);

	return 0; 
}
		
		

Ответ:

 (1) Students Ivanov and Petrov are studying in the same group 

 (2) Students Ivanov and Petrov are not studying in the same group 

 (3) при выполнении программы возникнет ошибка 


Номер 3
Какое сообщение будет выведено на экран в результате выполненния приведенной программы?

int main (void)
{
	struct student { 
		char *name;
		char *birthday;
		int group;
	} s1, s2;

	int s;
	
	s1.name = "Ivanov";
	s1.birthday = "10.03.1985";
	s1.group = 1014;

	s2.name = "Petrov";
	s2.birthday = "08.04.1985";
	s2.group = 1309;

	s2.group = s1.group;

	if (s2.group == s1.group)
		printf("Students %s and %s are studying in the same group\n", s1.name, s2.name);
	else
		printf("Students %s and %s are not studying in the same group\n", s1.name, s2.name);

	return 0; 
}
		
		

Ответ:

 (1) при выполнении программы возникнет ошибка 

 (2) Students Ivanov and Petrov are studying in the same group 

 (3) Students Ivanov and Petrov are not studying in the same group 


Упражнение 8:
Номер 1
Каким образом должна быть объявлена структура group и переменная g для корреткной работы программы?

int main (void)
{
	struct stud {    
		char *name;  
		char *surname; 
		int age;         
		double av_mark; 
	}; 

	<Объявление структуры group и переменной gr>            

	gr.number = 3;
	gr.quantity = 21;
	gr.student.name = "Ivan";
	gr.student.surname = "Ivanov";
	gr.student.age = 20;
	gr.student.av_mark = 4.25;

	printf("Group Number: %d\nThe number of students in the group: %d\n", gr.number, gr.quantity);
	printf("%s %s - %1.2f", gr.student.surname, gr.student.name, gr.student.av_mark);

    return 0;
 }
		
		

Ответ:

 (1) struct group { int number; int quantity; stud student; } gr;  

 (2) struct group { int number; int quantity; stud *student; } gr;  

 (3) struct group { int number; int quantity; struct stud student; } gr;  


Номер 2
В каком случае структура group и переменная g объявлены неверно для приведенной программы?

int main (void)
{
	struct stud {    
		char *name;  
		char *surname; 
		int age;         
		double av_mark; 
	}; 

	<Объявление структуры group и переменной gr>            

	gr.number = 3;
	gr.quantity = 21;
	gr.student.name = "Ivan";
	gr.student.surname = "Ivanov";
	gr.student.age = 20;
	gr.student.av_mark = 4.25;

	printf("Group Number: %d\nThe number of students in the group: %d\n", gr.number, gr.quantity);
	printf("%s %s - %1.2f", gr.student.surname, gr.student.name, gr.student.av_mark);

    return 0;
 }
		
		

Ответ:

 (1) struct group { int number; int quantity; char *name; char *surname; int age; double av_mark; } gr;  

 (2) struct group { int number; int quantity; struct stud student; } gr;  

 (3) struct group { int number; int quantity; stud student; } gr;  


Номер 3
Какая программа содержит ошибку?

Ответ:

 (1) int main (void) { struct stud { char *name; char *surname; int age; double av_mark; }; struct group { int number; int quantity; struct stud student; } gr; gr.number = 3; gr.quantity = 21; gr.student.name = "Ivan"; gr.student.surname = "Ivanov"; gr.student.age = 20; gr.student.av_mark = 4.25; printf("Group Number: %d\nThe number of students in the group: %d\n", gr.number, gr.quantity); printf("%s %s - %1.2f", gr.student.surname, gr.student.name, gr.student.av_mark); return 0; }  

 (2) int main (void) { struct stud { char *name; char *surname; int age; double av_mark; }; struct group { int number; int quantity; struct stud *student; } gr; gr.number = 3; gr.quantity = 21; gr.student.name = "Ivan"; gr.student.surname = "Ivanov"; gr.student.age = 20; gr.student.av_mark = 4.25; printf("Group Number: %d\nThe number of students in the group: %d\n", gr.number, gr.quantity); printf("%s %s - %1.2f", gr.student.surname, gr.student.name, gr.student.av_mark); return 0; }  

 (3) int main (void) { struct stud { char *name; char *surname; int age; double av_mark; } st; struct group { int number; int quantity; struct stud *student; } gr; gr.student = &st; gr.number = 3; gr.quantity = 21; gr.student->name = "Ivan"; gr.student->surname = "Ivanov"; gr.student->age = 20; gr.student->av_mark = 4.25; printf("Group Number: %d\nThe number of students in the group: %d\n", gr.number, gr.quantity); printf("%s %s - %1.2f", gr.student->surname, gr.student->name, gr.student->av_mark); return 0; }  


Упражнение 9:
Номер 1
В какой программе используются вложенные структуры?

Ответ:

 (1) int main (void) { struct student { char *name; char *surname; int group; int ses[5]; double av; struct student *p; } st; int i, sum = 0; st.name = "Ivan"; st.surname = "Ivanov"; st.group = 1024; st.ses[0] = 5; st.ses[1] = 4; st.ses[2] = 5; st.ses[3] = 5; st.ses[4] = 5; for (i=0; i<5; i++) sum +=st.ses[i]; st.av = (double)sum/5; printf("Name: %s %s\n", st.surname, st.name); printf("Group: %d\n", st.group); printf("Average mark: %.2f\n\n", st.av); return 0; }  

 (2) int main (void) { struct student { char *name; char *surname; int group; int ses[5]; double av; } st; int i, sum = 0; st.name = "Ivan"; st.surname = "Ivanov"; st.group = 1024; st.ses[0] = 5; st.ses[1] = 4; st.ses[2] = 5; st.ses[3] = 5; st.ses[4] = 5; for (i=0; i<5; i++) sum +=st.ses[i]; st.av = (double)sum/5; printf("Name: %s %s\n", st.surname, st.name); printf("Group: %d\n", st.group); printf("Average mark: %.2f\n\n", st.av); return 0; }  

 (3) int main (void) { struct stud { char *name; char *surname; int age; double av_mark; } st; struct group { int number; int quantity; struct stud *student; } gr; gr.student = &st; gr.number = 3; gr.quantity = 21; gr.student->name = "Ivan"; gr.student->surname = "Ivanov"; gr.student->age = 20; gr.student->av_mark = 4.25; printf("Group Number: %d\nThe number of students in the group: %d\n", gr.number, gr.quantity); printf("%s %s - %1.2f", gr.student->surname, gr.student->name, gr.student->av_mark); return 0; }  


Номер 2
В какой программе не используются вложенные структуры?

Ответ:

 (1) int main (void) { struct student { char *name; char *surname; int group; int ses[5]; double av; struct student *p; } st; int i, sum = 0; st.name = "Ivan"; st.surname = "Ivanov"; st.group = 1024; st.ses[0] = 5; st.ses[1] = 4; st.ses[2] = 5; st.ses[3] = 5; st.ses[4] = 5; for (i=0; i<5; i++) sum +=st.ses[i]; st.av = (double)sum/5; printf("Name: %s %s\n", st.surname, st.name); printf("Group: %d\n", st.group); printf("Average mark: %.2f\n\n", st.av); return 0; }  

 (2) int main (void) { struct student { char *name; char *surname; int group; int ses[5]; double av; } st; int i, sum = 0; st.name = "Ivan"; st.surname = "Ivanov"; st.group = 1024; st.ses[0] = 5; st.ses[1] = 4; st.ses[2] = 5; st.ses[3] = 5; st.ses[4] = 5; for (i=0; i<5; i++) sum +=st.ses[i]; st.av = (double)sum/5; printf("Name: %s %s\n", st.surname, st.name); printf("Group: %d\n", st.group); printf("Average mark: %.2f\n\n", st.av); return 0; }  

 (3) int main (void) { struct stud { char *name; char *surname; int age; double av_mark; } st; struct group { int number; int quantity; struct stud *student; } gr; gr.student = &st; gr.number = 3; gr.quantity = 21; gr.student->name = "Ivan"; gr.student->surname = "Ivanov"; gr.student->age = 20; gr.student->av_mark = 4.25; printf("Group Number: %d\nThe number of students in the group: %d\n", gr.number, gr.quantity); printf("%s %s - %1.2f", gr.student->surname, gr.student->name, gr.student->av_mark); return 0; }  


Номер 3
Какой файл будет создан в результате выполнения приведенной программы?

int main (void)
{
	struct student {    
		char *name;  
		char *surname; 
		int group;         
		int ses[5];
		double av;
		struct student *p;
	} st; 
	FILE *fp;
	int i, sum = 0;

	st.name = "Ivan";
	st.surname = "Ivanov";
	st.group = 1024;
	st.ses[0] = 5;
	st.ses[1] = 4;
	st.ses[2] = 5;
	st.ses[3] = 5;
	st.ses[4] = 5;

	for (i=0; i<5; i++) sum +=st.ses[i];
	st.av = sum/5;
	
	if ((fp = fopen("g:\\marks.txt", "w+t")) == 0)
		printf("Open file error");
	else {
		fprintf(fp, "Name: %s %s\n", st.surname, st.name);
		fprintf(fp, "Group: %d\n", st.group);
		fprintf(fp, "Average mark: %.2f\n\n", st.av);
		fclose(fp);
	};

    return 0;
 }
		
		

Ответ:

 (1) files 

 (2) files 

 (3) files 


Упражнение 10:
Номер 1
Необходимо написать программу, которая позволит ввести с клавиатуры данные о поездах. Также в программе должен быть предусмотрен поиск по номеру поезда. Какая программа выполняет поставленную задачу?

Ответ:

 (1) int main (void) { struct train { char *nazn; int numb; char *time; } tr[3]; int i = 0, sum = 0, n, ch=0; char *str; while(ch!=121) { printf("\nEnter information about train (Q - quit): \n"); printf("Enter destination of the train: "); gets(tr[i].nazn); printf("Enter number of the train: "); gets(str); tr[i].numb = atoi(str); printf("Enter time of scheduled departure: "); gets(tr[i].time); ++i; printf("Quit? (y/n): "); ch = _getche(); }; printf("\n\nEnter train number for search: "); scanf("%d",&n); for (i=0; i<3; i++) { if (tr[i].numb == n) { printf("\n\tTrain: %s", tr[i].nazn); printf("\n\tTime: %s", tr[i].time); } } if (i>=3) printf("There is no train with this number"); return 0; }  

 (2) int main (void) { struct train { char nazn[40]; int numb; char time[10]; } tr[3]; int i = 0, sum = 0, n, ch=0; char str[10]; while(ch!=121) { printf("\nEnter information about train (Q - quit): \n"); printf("Enter destination of the train: "); gets(tr[i].nazn); printf("Enter number of the train: "); gets(str); tr[i].numb = atoi(str); printf("Enter time of scheduled departure: "); gets(tr[i].time); ++i; printf("Quit? (y/n): "); ch = _getche(); }; printf("\n\nEnter train number for search: "); scanf("%d",&n); for (i=0; i<3; i++) { if (tr[i].numb == n) { printf("\n\tTrain: %s", tr[i].nazn); printf("\n\tTime: %s", tr[i].time); } } if (i>=3) printf("There is no train with this number"); return 0; }  

 (3) int main (void) { struct train { char nazn[40]; int numb; char time[10]; } tr[3]; int i = 0, sum = 0, n, ch=0; char str[10]; while(ch!=121) { printf("\nEnter information about train (Q - quit): \n"); printf("Enter destination of the train: "); gets(tr[i]->nazn); printf("Enter number of the train: "); gets(str); tr[i]->numb = atoi(str); printf("Enter time of scheduled departure: "); gets(tr[i]->time); ++i; printf("Quit? (y/n): "); ch = _getche(); }; printf("\n\nEnter train number for search: "); scanf("%d",&n); for (i=0; i<3; i++) { if (tr[i]->numb == n) { printf("\n\tTrain: %s", tr[i].nazn); printf("\n\tTime: %s", tr[i].time); } } if (i>=3) printf("There is no train with this number"); return 0; }  


Номер 2
Необходимо написать программу, которая позволит ввести с клавиатуры данные о поездах. Также в программе должен быть предусмотрен поиск по пункту назначения поезда. Какая программа выполняет поставленную задачу?

Ответ:

 (1) int main (void) { struct train { char *nazn; int numb; char *time; } tr[3]; int i = 0, sum = 0, ch=0; char str[10], search[10]; while(ch!=121) { printf("\nEnter information about train (Q - quit): \n"); printf("Enter destination of the train: "); gets(tr[i].nazn); printf("Enter number of the train: "); gets(str); tr[i].numb = atoi(str); printf("Enter time of scheduled departure: "); gets(tr[i].time); ++i; printf("Quit? (y/n): "); ch = _getche(); }; printf("\n\nEnter train destination: "); gets(search); for (i=0; i<3; i++) { if (strcmp(tr[i].nazn,search)==0) { printf("\n\tTrain number: %d", tr[i].numb); printf("\n\tTime: %s", tr[i].time); } } if (i>=3) printf("There is no train to this destination"); return 0; }  

 (2) int main (void) { struct train { char nazn[40]; int numb; char time[10]; } tr[3]; int i = 0, sum = 0, ch=0; char str[10], search[10]; while(ch!=121) { printf("\nEnter information about train (Q - quit): \n"); printf("Enter destination of the train: "); gets(tr[i]->nazn); printf("Enter number of the train: "); gets(str); tr[i]->numb = atoi(str); printf("Enter time of scheduled departure: "); gets(tr[i]->time); ++i; printf("Quit? (y/n): "); ch = _getche(); }; printf("\n\nEnter train destination: "); gets(search); for (i=0; i<3; i++) { if (strcmp(tr[i]->nazn,search)==0) { printf("\n\tTrain number: %d", tr[i]->numb); printf("\n\tTime: %s", tr[i]->time); } } if (i>=3) printf("There is no train to this destination"); return 0; }  

 (3) int main (void) { struct train { char nazn[40]; int numb; char time[10]; } tr[3]; int i = 0, sum = 0, ch=0; char str[10], search[10]; while(ch!=121) { printf("\nEnter information about train (Q - quit): \n"); printf("Enter destination of the train: "); gets(tr[i].nazn); printf("Enter number of the train: "); gets(str); tr[i].numb = atoi(str); printf("Enter time of scheduled departure: "); gets(tr[i].time); ++i; printf("Quit? (y/n): "); ch = _getche(); }; printf("\n\nEnter train destination: "); gets(search); for (i=0; i<3; i++) { if (strcmp(tr[i].nazn,search)==0) { printf("\n\tTrain number: %d", tr[i].numb); printf("\n\tTime: %s", tr[i].time); } } if (i>=3) printf("There is no train to this destination"); return 0; }  


Номер 3
Необходимо написать программу, которая позволит ввести с клавиатуры данные о поездах. Также в программе должен быть предусмотрен поиск по пункту назначения поезда. Какая программа не выполняет поставленную задачу?

Ответ:

 (1) int main (void) { struct train { char *nazn; int numb; char *time; } tr[3]; int i = 0, sum = 0, ch=0; char str[10], search[10]; while(ch!=121) { printf("\nEnter information about train (Q - quit): \n"); printf("Enter destination of the train: "); gets(tr[i].nazn); printf("Enter number of the train: "); gets(str); tr[i].numb = atoi(str); printf("Enter time of scheduled departure: "); gets(tr[i].time); ++i; printf("Quit? (y/n): "); ch = _getche(); }; printf("\n\nEnter train destination: "); gets(search); for (i=0; i<3; i++) { if (strcmp(tr[i].nazn,search)==0) { printf("\n\tTrain number: %d", tr[i].numb); printf("\n\tTime: %s", tr[i].time); } } if (i>=3) printf("There is no train to this destination"); return 0; }  

 (2) int main (void) { struct train { char nazn[40]; int numb; char time[10]; } tr[3]; int i = 0, sum = 0, ch=0; char str[10], search[10]; while(ch!=121) { printf("\nEnter information about train (Q - quit): \n"); printf("Enter destination of the train: "); gets(tr[i]->nazn); printf("Enter number of the train: "); gets(str); tr[i]->numb = atoi(str); printf("Enter time of scheduled departure: "); gets(tr[i]->time); ++i; printf("Quit? (y/n): "); ch = _getche(); }; printf("\n\nEnter train destination: "); gets(search); for (i=0; i<3; i++) { if (strcmp(tr[i]->nazn,search)==0) { printf("\n\tTrain number: %d", tr[i]->numb); printf("\n\tTime: %s", tr[i]->time); } } if (i>=3) printf("There is no train to this destination"); return 0; }  

 (3) int main (void) { struct train { char nazn[40]; int numb; char time[10]; } tr[3]; int i = 0, sum = 0, ch=0; char str[10], search[10]; while(ch!=121) { printf("\nEnter information about train (Q - quit): \n"); printf("Enter destination of the train: "); gets(tr[i].nazn); printf("Enter number of the train: "); gets(str); tr[i].numb = atoi(str); printf("Enter time of scheduled departure: "); gets(tr[i].time); ++i; printf("Quit? (y/n): "); ch = _getche(); }; printf("\n\nEnter train destination: "); gets(search); for (i=0; i<3; i++) { if (strcmp(tr[i].nazn,search)==0) { printf("\n\tTrain number: %d", tr[i].numb); printf("\n\tTime: %s", tr[i].time); } } if (i>=3) printf("There is no train to this destination"); return 0; }  


Упражнение 11:
Номер 1
В каком случае структура верно определена с использованием ключевого слова typedef?

Ответ:

 (1) struct foot_klub { char name[20]; int liga; float ochki; }; typedef struct foot_klub f_club; f_club a, *p, b[5];  

 (2) struct foot_klub { char name[20]; int liga; float ochki; }; typedef struct foot_klub *f_club; f_club a, *p, b[5];  

 (3) typedef struct { char name[20]; int liga; float ochki; }f_club; f_club a, *p, b[5];  


Номер 2
В каком случае структура определена с использованием ключевого слова typedef неверно?

Ответ:

 (1) struct foot_klub { char name[20]; int liga; float ochki; }; typedef struct foot_klub f_club; f_club a, *p, b[5];  

 (2) struct foot_klub { char name[20]; int liga; float ochki; }; typedef struct foot_klub *f_club; f_club a, *p, b[5];  

 (3) typedef struct { char name[20]; int liga; float ochki; }f_club; f_club a, *p, b[5];  


Номер 3
В каком случае структура верно определена с использованием ключевого слова typedef?

Ответ:

 (1) struct monitor_info { char model[40]; int price; }; typedef struct monitor_info monitor; monitor a, *p, b[5];  

 (2) struct monitor_info { char model[40]; int price; }; typedef struct monitor_info *monitor; monitor a, *p, b[5];  

 (3) typedef struct { char model[40]; int price; } monitor; monitor a, *p, b[5];  


Упражнение 12:
Номер 1
Какая ошибка содержится в приведенной программе?

int main (void)
{
	struct train_info {    
		char nazn[40];  
		int numb; 
		char time[10];
	};
	typedef struct train_info train;

	train tr[3];
	
	int i = 0, sum = 0, ch=0;
	char str[10], search[10];

	while(ch!=121) {
		printf("\nEnter information about train (Q - quit): \n");
		printf("Enter destination of the train: ");
		gets(tr[i].nazn);
		printf("Enter number of the train: ");
		gets(str);
		tr[i].numb = atoi(str);
		printf("Enter time of scheduled departure: ");
		gets(tr[i].time);
		++i;
		printf("Quit? (y/n): ");
		ch = _getche();
	};
	
	printf("\n\nEnter train destination: ");
	gets(search);

	for (i=0; i<3; i++) {
		if (strcmp(tr[i].nazn,search)==0) {
			printf("\n\tTrain number: %d", tr[i].numb);
			printf("\n\tTime: %s", tr[i].time);
		}
	}
	if (i>=3)
		printf("There is no train to this destination");

    return 0;
 }
		
		

Ответ:

 (1) неверно используется ключевое слово typedef 

 (2) выполняется неверное обращение к полям структуры 

 (3) неверно объявлен массив tr 

 (4) программа не содержит ошибок 


Номер 2
Какая ошибка содержится в приведенной программе?

int main (void)
{
	typedef struct {    
		char nazn[40];  
		int numb; 
		char time[10];
	} train;
	train tr[3];
	
	int i = 0, sum = 0, ch=0;
	char str[10], search[10];

	while(ch!=121) {
		printf("\nEnter information about train (Q - quit): \n");
		printf("Enter destination of the train: ");
		gets(tr[i].nazn);
		printf("Enter number of the train: ");
		gets(str);
		tr[i].numb = atoi(str);
		printf("Enter time of scheduled departure: ");
		gets(tr[i].time);
		++i;
		printf("Quit? (y/n): ");
		ch = _getche();
	};
	
	printf("\n\nEnter train destination: ");
	gets(search);

	for (i=0; i<3; i++) {
		if (strcmp(tr[i].nazn,search)==0) {
			printf("\n\tTrain number: %d", tr[i].numb);
			printf("\n\tTime: %s", tr[i].time);
		}
	}
	if (i>=3)
		printf("There is no train to this destination");

    return 0;
 }
		
		

Ответ:

 (1) неверно используется ключевое слово typedef 

 (2) выполняется неверное обращение к полям структуры 

 (3) неверно объявлен массив tr 

 (4) программа не содержит ошибок 


Номер 3
Какая ошибка содержится в приведенной программе?

int main (void)
{
	typedef struct {    
		char nazn[40];  
		int numb; 
		char time[10];
	} train, train_info;

	train_info ti[3];
	train tr[3];
	
	int i = 0, sum = 0, ch=0;
	char str[10], search[10];

	while(ch!=121) {
		printf("\nEnter information about train (Q - quit): \n");
		printf("Enter destination of the train: ");
		gets(tr[i].nazn);
		printf("Enter number of the train: ");
		gets(str);
		tr[i].numb = atoi(str);
		printf("Enter time of scheduled departure: ");
		gets(tr[i].time);
		++i;
		printf("Quit? (y/n): ");
		ch = _getche();
	};
	
	printf("\n\nEnter train destination: ");
	gets(search);

	for (i=0; i<3; i++) {
		if (strcmp(tr[i].nazn,search)==0) {
			printf("\n\tTrain number: %d", tr[i].numb);
			printf("\n\tTime: %s", tr[i].time);
		}
	}
	if (i>=3)
		printf("There is no train to this destination");

    return 0;
 }
		
		

Ответ:

 (1) программа не содержит ошибок 

 (2) неверно используется ключевое слово typedef 

 (3) выполняется неверное обращение к полям структуры 

 (4) неверно объявлен массив tr 




Главная / Программирование / Программирование на языке C в Microsoft Visual Studio 2010 / Тест 13