第7回資料 - 湘南工科大学 情報工学科 ホームページ

Download Report

Transcript 第7回資料 - 湘南工科大学 情報工学科 ホームページ

2011年11月22日
湘南工科大学
情報理論2
第7回
小林 学
〒251-8511 神奈川県藤沢市辻堂西海岸1-1-25
Tel.
0466-30-0232(直通)
Fax.
0466-34-5932
[email protected]
[前回の課題1] キュー構造について,以下の問に答えよ
int DataNo=0;
int Data[5];
Page 2
:データの数を表す int 型変数
:データを格納する int 型配列
(1) 上の定義に対して,以下の命令を実行したときの配列Data
の内容と DataNo の変化,画面出力を示しなさい
Input(2) → Output → Input(4) → Input(7) →
Output
(2) (1)と同様に,以下の結果を示しなさい
Input(4) → Input(12) → Input(22) → Output →
Output → Output
[前回の課題1解答](1)
[1]
Data [0]
Page 3
[2]
[3]
[4]
DataNo
画面出力
0
Input(2)
1
2
Output
0
2
Input(4)
1
4
Input(7)
4
2
7
Output
7
1
4
[前回の課題1解答](2)
[1]
Data [0]
Page 4
[2]
[3]
[4]
DataNo
画面出力
0
Input(4)
1
4
Input(12)
4
2
12
Input(22)
4
12
3
22
Output
12
22
2
4
1
12
0
22
Output
22
Output
Page 5
[前回の課題2の解答]
配列 Data の内容をシフトさせるプログラムを完成させなさい
#include<stdio.h>
void main(void){
int i, Data[5]={5,7,2,9,3};
printf("Data=");
for(i=0;i<5;i++) printf(" %d",Data[i]);
for(i=0;i<4;i++) Data[i] = Data[i+1];
}
printf("\nData=");
for(i=0;i<5;i++) printf(" %d",Data[i]);
[実行結果]
Page 6
[前回の課題3]
次ページのスタックのプログラムを修正して,以下のキューを実現
するプログラムを作成しなさい
[実行例]
[前回の課題3の解答]
Page 7
#include<stdio.h>
void main(void){
int select, i, DataNo=0, Data[5], input;
while(1){
printf("\nInputならば0,Outputならば1を入力:");
scanf("%d",&select);
if(select==0){
printf("入力データ:");
scanf("%d",&input);
Data[DataNo] = input;
DataNo++;
}else{
printf("出力:%d\n",Data[0]);
DataNo--;
for(i=0;i<4;i++) Data[i] = Data[i+1];
}
printf("Data=");
for(i=0;i<DataNo;i++) printf(" %d",Data[i]);
}
}
アスキーコード
Page 8
Page 9
文字表示プログラム
#include<stdio.h>
#include<stdio.h>
void main(void){
char ch;
void main(void){
char ch;
}
ch = 97;
printf("%c\n",ch);
#include<stdio.h>
void main(void){
char ch;
}
ch = 0x61;
printf("%c\n",ch);
}
ch = 'a';
printf("%c\n",ch);
文字入力プログラム
Page 10
#include<stdio.h>
void main(void){
char ch;
scanf("%c",&ch);
printf("文字:%c コード:%d\n", ch, ch);
if(0x61 <= ch && ch <= 0x7a) printf("英小文字です¥n");
}
文字列の扱い (1)
Page 11
#include<stdio.h>
void main(void){
char str[5]=“Test”;
}
printf(“%s”, str);
文字列の扱い (2)
文字列の表示
正しい!!
#include<stdio.h>
void main(void){
char str[5]=“Test#”;
}
文字配列の定義
文字配列の初期化
5文字入れると?
printf(“%s”, str);
おかしくなる!!
[実行結果] (1)
Page 12
[実行結果] (2)
[0] [1] [2] [3] [4]
配列 str
‘T’ ‘e’ ‘s’ ‘t’
0
文字列の最後には数字の 0 が入る (0 がなければならない)
0 が文字列の終わりを表す
文字列の扱い (3)
Page 13
#include<stdio.h>
void main(void){
char str[5]=“Test##”;
}
6文字以上(最悪)
printf(“%s”, str);
文字列の扱い (4)
#include<stdio.h>
void main(void){
char str[5];
str = “Test”;
}
printf(“%s”, str);
初期化以外で配列に文字列を
=では入れられない
文字列の扱い (5)
Page 14
#include<stdio.h>
void main(void){
char str[5];
str[0]='T';
str[1]='e';
str[2]='s';
str[3]='t';
str[4]= 0;
}
絶対必要!!
printf("%s", str);
正しい!!
文字’a’を数える(カウント)
i
str[i]
0
1
2
3
4
‘a’
‘b’
‘a’
‘a’
0
初期値:
繰り返し:
(for文)
count
0
0→1
1
1→2
2→3
3
Page 15
if(str[i]=='a')
count++;
[課題1]上の文字’a’を数えるプログラムを作成しなさい
実行結果
一致文字のカウント
Page 16
char str1[5]="abba", str2[5]="aabb";
i
str1[i] str2[i]
0
初期値:
繰り返し:
(for文)
count
0
1
2
3
‘a’
‘b’
‘b’
‘a’
‘a’
‘a’
‘b’
‘b’
0→1
1
1→2
2
[課題2]上の一致文字を数えるプログラムを作成しなさい
実行結果
ヒント: if(str1[i]==str2[i]) count++;
先頭からの一致文字数
Page 17
char str1[5]="abba", str2[5]="abaa";
i
str1[i] str2[i]
0
初期値:
繰り返し:
(for文)
count
0
1
2
3
‘a’
‘b’
‘b’
‘a’
1
‘b’
2
‘a’ break(繰り返しを抜ける)
if(str1[i]!=str2[i]) break;
[課題3]先頭からの一致文字を数えるプログラムを作成
しなさい
実行結果
Page 18
途中からの一致文字数
char str1[10]="ababbaaba";
char str2[10]="bbababbab";
str1の1番からとstr2の3番からの一致文字数を求める
i
str1[1+i] str2[3+i]
初期値:
繰り返し:
(for文)
0
1
2
3
4
5
‘b’
‘a’
‘b’
‘b’
‘a’
‘a’
‘b’
‘a’
‘b’
‘b’
‘a’
‘b’
count
0
1
2
3
4
5
break
途中からの一致文字数
Page 19
[課題4]前ページの途中からの一致文字を数えるプログ
ラムを作成しなさい
実行結果
Page 20
発展課題
char str1[10]="ababaaaba";
char str2[4]="aba";
str1の中にstr2と完全一致する文字列は何回入ってい
るか?またその一致する先頭の位置は?
位置:0 1 2 3 4 5 6 7 8
ababaaaba
一致した先頭位置:0,2,6
一致回数:3回
[課題5]上のプログラムを作成しなさい