今週の課題
サンプルプログラム
1.1〜10のランダムな値を出力するプログラム。
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
main(){
int i;
int value;
time_t t;
srand((unsigned)time(&t);
value=1+(int)(10.0*rand()
/(RAND_MAX+1.0));
printf(”%d¥n”,value);
}
2.ウィンドウをクリア。(エスケープシーケンス)
#include <stdio.h>
#define CLEAR ”¥033[H¥033[2J”
main()
{
printf(”これを消します¥n”);
printf(CLEAR);
printf(”消しました。¥n”);
}
3.文字の色を変える。(エスケープシーケンス)
#include <stdio.h>
#define BLACK ”¥033[0;30m”
#define RED ”¥033[0;31m”
#define BLUE ”¥033[0;34m”
main()
{
printf(RED);
printf(”赤になります。¥n”);
printf(BLUE);
printf(”青です。¥n”);
}
エスケープシーケンス:
キーボードから入力できる文字以外にも、スクリーン上に表示できる文字
4.1文字の入力。(getchar)
/************************************************/
/* 1文字入力のサンプルプログラム */
/************************************************/
#include
char moji();
int main(void)
{
char ch;
ch=moji(); /* 使用例 */
printf("%cです。\n",ch);
return 0;
}
/************************************************
実行すると1文字入力を待つ。入力した文字の中に、
N/Yどちらかがあれば、それを返す。どちらかが
入力されなければ終了しない。
返り値:y、nのどちらか。
************************************************/
char moji(){
int a,b;
while(1){
printf("どちらかを入力(y/n)−>");
while((a=getchar())!='y' &&a!='Y'
&&a!='n' &&a!='N');
while((b=getchar())<0);
if(a=='y'||a=='Y') return 'y';
else if(a=='n'||a=='N') return 'n';
}
}
戻る
情報通信工学実験 I・II C言語入門 II
Copyright(C)©ShinyaItoh 1999-2000
ito@tb.in.teu.ac.jp