譚浩強の《Cプログラムの設計》の本の後で練習問題の第11章
最近CとC++の基礎知識を復習しなければならないので、前に学んだ譚浩強の「Cプログラム設計」と「C++プログラム設計」の練習問題をもう一度やり直す計画だ.
コンパイル環境:オペレーティングシステム32ビットWin 7、コンパイルツールVC++6.0
第十一章:構造体
11.1)構造体変数(年、月、日を含む)を定義し、その日が本年で何日目であるかを計算します.
11.2)関数を書き、11.1の計算を実現する.主関数から年、月、日を関数に渡し、計算後に日を主関数から出力します.
11.3)num、name、score[3]を含む5人の学生のデータ記録を印刷する関数を作成し、これらの記録を主関数で入力し、作成した関数で出力する
11.4)11.3に基づいて、5人の学生のデータ記録を入力するための関数を作成する.
11.5)10人の学生がいて、各学生のデータは学番、名前、3科目の成績を含んで、キーボードから10人の学生のデータを入力して、3科目の総平均成績を出力することを要求して、および最高点の学生の成績(学番、3科目の成績、平均点数を含む)
11.8)a、bの2つのチェーンテーブルがあり、各チェーンテーブルのノードには学号、成績が含まれている.2つのチェーンテーブルを結合し、学号昇順に並べることを要求する.
11.9)13人が1周し,1人目から順に1,2,3を数える.新聞数が3までの者は輪を脱退する.最後に輪に残った人の元の番号を探し出す
11.10)2つのチェーンテーブルaとbがあり,ノードに学号,氏名が含まれているとする.aチェーンテーブルからbチェーンテーブルと同じ学号を持つノードを削除する
11.11)各ノードは、学号、氏名、性別、年齢を含むチェーンテーブルを作成する.チェーンテーブルのノードに含まれる年齢がこの年齢に等しい場合に削除する年齢を入力します.
11.12)チェーンテーブルを逆順に並べ、チェーンヘッドをチェーンテールとし、チェーンテールをチェーンヘッドとする
END
コンパイル環境:オペレーティングシステム32ビットWin 7、コンパイルツールVC++6.0
第十一章:構造体
11.1)構造体変数(年、月、日を含む)を定義し、その日が本年で何日目であるかを計算します.
#include<stdio.h>
struct Date
{
int Year;
int Month;
int Day;
}
d = { 2014, 9, 4 };
void main()
{
int days = 0;
//
int isLeapYear = 0;
if(d.Year / 400 == 0)
{
isLeapYear = 1;
}
if(d.Year / 4 == 0 && d.Year / 100 != 0)
{
isLeapYear = 1;
}
//
switch(d.Month)
{
case 12: days += 30; //11 30
case 11: days += 31; //10 31
case 10: days += 30; //9 30
case 9: days += 31; //8 31
case 8: days += 31; //7 31
case 7: days += 30; //6 30
case 6: days += 31; //5 31
case 5: days += 30; //4 30
case 4: days += 31; //3 31
case 3: days += (isLeapYear ? 29 : 28);
case 2: days += 31; //1 31
default:;
}
//
days += d.Day;
printf("%d %d
", d.Year, days);
}
11.2)関数を書き、11.1の計算を実現する.主関数から年、月、日を関数に渡し、計算後に日を主関数から出力します.
#include<stdio.h>
struct Date
{
int Year;
int Month;
int Day;
};
//
int Days(int year, int month, int day)
{
int days = 0;
//
int isLeapYear = 0;
if(year / 400 == 0)
{
isLeapYear = 1;
}
if(year / 4 == 0 && year / 100 != 0)
{
isLeapYear = 1;
}
//
switch(month)
{
case 12: days += 30; //11 30
case 11: days += 31; //10 31
case 10: days += 30; //9 30
case 9: days += 31; //8 31
case 8: days += 31; //7 31
case 7: days += 30; //6 30
case 6: days += 31; //5 31
case 5: days += 30; //4 30
case 4: days += 31; //3 31
case 3: days += (isLeapYear ? 29 : 28);
case 2: days += 31; //1 31
default:;
}
//
days += day;
return days;
}
void main()
{
Date d;
d.Year = 2014;
d.Month = 9;
d.Day = 4;
printf("%d %d
", d.Year, Days(d.Year, d.Month, d.Day));
}
11.3)num、name、score[3]を含む5人の学生のデータ記録を印刷する関数を作成し、これらの記録を主関数で入力し、作成した関数で出力する
#include<stdio.h>
struct Student
{
int num;
char* name;
int score[3];
}
stdlist[5] =
{
{ 0, "Tsybius", { 85, 85, 85 } },
{ 1, "Galatea", { 99, 99, 99 } },
{ 2, "Gaius", { 80, 80, 80 } },
{ 3, "Quintus", { 83, 83, 83 } },
{ 4, "Aurelia", { 82, 82, 82 } },
};
//
void PrintInfo(Student* stdlist, int no)
{
printf(" :%d
", stdlist[no].num);
printf(" :%s
", stdlist[no].name);
printf(" 1:%d
", stdlist[no].score[0]);
printf(" 2:%d
", stdlist[no].score[1]);
printf(" 3:%d
", stdlist[no].score[2]);
}
void main()
{
Student *p = stdlist;
PrintInfo(p, 3);
}
11.4)11.3に基づいて、5人の学生のデータ記録を入力するための関数を作成する.
#include<stdio.h>
struct Student
{
int num;
char name[100]; //
int score[3];
}
stdlist[5];
//
void PrintInfo(Student* stdlist, int no)
{
printf(" :%d
", stdlist[no].num);
printf(" :%s
", stdlist[no].name);
printf(" 1:%d
", stdlist[no].score[0]);
printf(" 2:%d
", stdlist[no].score[1]);
printf(" 3:%d
", stdlist[no].score[2]);
}
//
void InputInfo(Student* stdlist)
{
/*************
0 abc 1 2 3
1 def 1 2 3
2 ghi 1 2 3
3 jkl 1 2 3
4 mno 1 2 3
*************/
int i;
for(i = 0; i < 5; i++)
{
scanf("%d", &stdlist[i].num);
scanf("%s", &stdlist[i].name);
scanf("%d", &stdlist[i].score[0]);
scanf("%d", &stdlist[i].score[1]);
scanf("%d", &stdlist[i].score[2]);
}
}
void main()
{
Student *p = stdlist;
//
InputInfo(p);
// 3
PrintInfo(p, 3);
}
11.5)10人の学生がいて、各学生のデータは学番、名前、3科目の成績を含んで、キーボードから10人の学生のデータを入力して、3科目の総平均成績を出力することを要求して、および最高点の学生の成績(学番、3科目の成績、平均点数を含む)
#include<stdio.h>
struct Student
{
int num;
char name[100]; //
int score[3];
}
stdlist[10];
void main()
{
//
int i;
for(i = 0; i < 10; i++)
{
scanf("%d", &stdlist[i].num);
scanf("%s", &stdlist[i].name);
scanf("%d", &stdlist[i].score[0]);
scanf("%d", &stdlist[i].score[1]);
scanf("%d", &stdlist[i].score[2]);
}
//
int average = 0, max = 0, temp = 0, x = 0;
for(i = 0; i < 10; i++)
{
temp = (stdlist[i].score[0] + stdlist[i].score[1] + stdlist[i].score[2]);
average += temp;
if(max < temp)
{
max = temp;
x = i;
}
}
average /= 10;
printf(" :%d
", average);
printf(" :%d
,", x);
printf(" :%d
", stdlist[x].num);
printf(" :%s
", stdlist[x].name);
printf(" 1:%d
", stdlist[x].score[0]);
printf(" 2:%d
", stdlist[x].score[1]);
printf(" 3:%d
", stdlist[x].score[2]);
int averagex = 0;
averagex = (stdlist[x].score[0] + stdlist[x].score[1] + stdlist[x].score[2]);
averagex /= 3;
printf(" :%d
", averagex);
}
11.8)a、bの2つのチェーンテーブルがあり、各チェーンテーブルのノードには学号、成績が含まれている.2つのチェーンテーブルを結合し、学号昇順に並べることを要求する.
#include<stdio.h>
struct Score
{
int id;
int score;
Score* next;
};
void main()
{
Score *p;
// a
Score *a = new Score;
p = a;
p -> id = 0;
p -> score = 95;
p -> next = new Score;
p = p -> next;
p -> id = 2;
p -> score = 85;
p -> next = new Score;
p = p -> next;
p -> id = 4;
p -> score = 75;
p -> next = new Score;
p = p -> next;
p -> id = 6;
p -> score = 65;
p -> next = NULL;
// a
p = a;
printf("LinkList a:
");
while(p)
{
printf("id:%d\tscore:%d
", p -> id, p -> score);
p = p -> next;
}
// b
Score *b = new Score;
p = b;
p -> id = 5;
p -> score = 90;
p -> next = new Score;
p = p -> next;
p -> id = 3;
p -> score = 80;
p -> next = new Score;
p = p -> next;
p -> id = 1;
p -> score = 70;
p -> next = NULL;
// b
p = b;
printf("LinkList b:
");
while(p)
{
printf("id:%d\tscore:%d
", p -> id, p -> score);
p = p -> next;
}
// c(a、b )
Score *c;
c = a;
p = c;
while(p -> next)
{
p = p -> next;
}
p -> next = b;
// c
p = c;
printf("New LinkList c:
");
while(p)
{
printf("id:%d\tscore:%d
", p -> id, p -> score);
p = p -> next;
}
// c
Score *i, *j;
int temp;
for(i = c; i; i = i -> next)
{
for(j = i -> next; j; j = j -> next)
{
if(i -> id > j -> id)
{
// id
temp = i -> id;
i -> id = j -> id;
j -> id = temp;
//
temp = i -> score;
i -> score = j -> score;
j -> score = temp;
}
}
}
// c
p = c;
printf("Sorted LinkList c:
");
while(p)
{
printf("id:%d\tscore:%d
", p -> id, p -> score);
p = p -> next;
}
}
11.9)13人が1周し,1人目から順に1,2,3を数える.新聞数が3までの者は輪を脱退する.最後に輪に残った人の元の番号を探し出す
#include<stdio.h>
struct Person
{
int num;
Person* next;
};
void main()
{
Person *p;
int i, j;
// 13 a
Person *a = new Person;
p = a;
i = 12;
while(i--)
{
p -> num = 0;
p -> next = new Person;
p = p -> next;
}
p -> num = 0;
p -> next = a;
// a
i = 12;
p = a;
while(i--)
{
//
while(p -> num)
{
p = p -> next;
}
p = p -> next;
while(p -> num)
{
p = p -> next;
}
p = p -> next;
while(p -> num)
{
p = p -> next;
}
// 3 , num 1
p -> num = 1;
p = p -> next;
//
Person *q = a;
for(j = 0; j < 13; j++)
{
printf("%d ", q -> num);
q = q -> next;
}
printf("
");
}
//
int counter = 1;
p = a;
while(p -> num)
{
p = p -> next;
counter++;
}
printf(" %d
", counter);
}
11.10)2つのチェーンテーブルaとbがあり,ノードに学号,氏名が含まれているとする.aチェーンテーブルからbチェーンテーブルと同じ学号を持つノードを削除する
#include<stdio.h>
#include<string.h>
struct Student
{
int id;
char name[100];
Student* next;
Student* last;
};
void main()
{
Student *p, *q;
// a
Student *a = new Student;
p = a;
p -> last = NULL;
p -> id = 0;
strcpy(p -> name, "Tsybius");
p -> next = new Student;
p -> next -> last = p;
p = p -> next;
p -> id = 1;
strcpy(p -> name, "Quintus");
p -> next = new Student;
p -> next -> last = p;
p = p -> next;
p -> id = 2;
strcpy(p -> name, "Galatea");
p -> next = new Student;
p -> next -> last = p;
p = p -> next;
p -> id = 3;
strcpy(p -> name, "Aurelia");
p -> next = NULL;
// a
printf(" a
");
for(p = a; p; p = p -> next)
{
printf("num:%d\t%s
", p -> id, p ->name);
}
// b
Student *b = new Student;
p = b;
p -> last = NULL;
p -> id = 0;
strcpy(p -> name, "Tsybius");
p -> next = new Student;
p -> next -> last = p;
p = p -> next;
p -> id = 2;
strcpy(p -> name, "Aurelia");
p -> next = NULL;
// b
printf(" b
");
for(p = b; p; p = p -> next)
{
printf("num:%d\t%s
", p -> id, p ->name);
}
// a b
for(p = a; p; p = p -> next)
{
for(q = b; q; q = q -> next)
{
if(p -> id == q -> id)
{
printf("Delete: %s
", p -> name);
if(p -> last == NULL)
{
a = p -> next;
if(p -> next != NULL)
{
p -> next -> last = NULL;
}
}
else
{
p -> last -> next = p -> next;
if(p -> next != NULL)
{
p -> next -> last = p -> last;
}
}
}
}
}
// a
printf(" a
");
for(p = a; p; p = p -> next)
{
printf("num:%d\t%s
", p -> id, p ->name);
}
}
11.11)各ノードは、学号、氏名、性別、年齢を含むチェーンテーブルを作成する.チェーンテーブルのノードに含まれる年齢がこの年齢に等しい場合に削除する年齢を入力します.
#include<stdio.h>
#include<string.h>
struct Student
{
int id;
char name[100];
int sex; // :1 :0
int age;
Student* next;
Student* last;
};
void main()
{
Student *p;
Student *a = new Student;
p = a;
p -> last = NULL;
p -> id = 0;
strcpy(p -> name, "Tsybius");
p -> sex = 1;
p -> age = 23;
p -> next = new Student;
p -> next -> last = p;
p = p -> next;
p -> id = 1;
strcpy(p -> name, "Galatea");
p -> sex = 0;
p -> age = 21;
p -> next = new Student;
p -> next -> last = p;
p = p -> next;
p -> id = 2;
strcpy(p -> name, "Aurelia");
p -> sex = 0;
p -> age = 22;
p -> next = NULL;
int input;
scanf("%d", &input);
for(p = a; p; p = p -> next)
{
if(p -> id == input)
{
printf("Delete: %s
", p -> name);
if(p -> last == NULL)
{
a = p -> next;
if(p -> next != NULL)
{
p -> next -> last = NULL;
}
}
else
{
p -> last -> next = p -> next;
if(p -> next != NULL)
{
p -> next -> last = p -> last;
}
}
}
}
}
11.12)チェーンテーブルを逆順に並べ、チェーンヘッドをチェーンテールとし、チェーンテールをチェーンヘッドとする
#include<stdio.h>
struct Node
{
int number;
Node* next;
};
void main()
{
Node *p, *q, *r;
// a
Node* a = new Node;
p = a;
p -> number = 0;
p -> next = new Node;
p = p -> next;
p -> number = 1;
p -> next = new Node;
p = p -> next;
p -> number = 2;
p -> next = new Node;
p = p -> next;
p -> number = 3;
p -> next = NULL;
//
for(p = a; p; p = p -> next)
{
printf("%d\t", p -> number);
}
printf("
");
// ,
p = a;
q = p -> next;
p -> next = NULL;
while(q)
{
r = q -> next;
q -> next = p;
p = q;
q = r;
}
a = p;
//
for(p = a; p; p = p -> next)
{
printf("%d\t", p -> number);
}
printf("
");
}
END