top page 書籍一覧 C言語ワークブック

情報演習6 ステップ30
C言語ワークブック
Step01 Step02 Step03 Step04 Step05 Step06 Step07 Step08 Step09 Step10
Step11 Step12 Step13 Step14 Step15 Step16 Step17 Step18 Step19 Step20
Step21 Step22 Step23 Step24 Step25 Step26 Step27 Step28 Step29 Step30
Step01
(1)
#include<stdio.h>
main()
{
printf("HELLO\n");
printf("HELLO\n");
}
(2)
(3)

Step02
(1)
#include<stdio.h>
main()
{
float r;
float L;
float S;
float PI;

PI=3.14;

r=5;

L=2*r*PI;
S=PI*r*r;

printf("ENSHUU=%f MENSEKI=%f\n",L,S);
}
(2)
#include<stdio.h>
main()
{
float a;
float b;

int wa;
int sa;
int seki;
int shou;
float fshou;

a=255;
b=16;

wa=a+b;
sa=a-b;
seki=a*b;
shou = a/b;
fshou =a/b;

printf("wa=%d sa=%d seki=%d shou=%d\n",wa,sa,seki,shou);
printf("fshou=%f\n",fshou);
}
Step03
(1)
#include<stdio.h>
main()
{
float a,b,c,sum,ave;

printf("Input a\n");
scanf("%f",&a);

printf("Input b\n");
scanf("%f",&b);

printf("Input c\n");
scanf("%f",&c);

sum=a+b+c;
ave=sum/3;

printf("sum=%f ave=%f\n",sum,ave);

}
(2)
#include<stdio.h>
main()
{
float x;
printf("input X\n");
scanf("%d",&x);
printf("int %d float %f\n",x,x);
}

s03q2.cについては答えが,おかしな値になる.
そのため,入力を促す時はprintf("Input x (int)\n");
というように型をしていする方がよい.
Step04
(1)
#include<stdio.h>
main()
{
float x,y,ans;

printf("Input tate\n");
scanf("%f",&x);

printf("Input yoko\n");
scanf("%f",&y);

ans=x*y;

printf("%f\n",ans);

}
(2)
#include<stdio.h>
main()
{
float r,pi,l,s;

printf("Input hankei\n");
scanf("%f",&r);

pi=3.14;

l=2*pi*r;
s=pi*r*r;

printf("enshuu %f imennseki %f\n",l,s);

}
(3)
#include<stdio.h>
main()
{
float a,b,c,d,e,sum,ave;

printf("Input Number\n");
scanf("%f",&a);

printf("Input Number\n");
scanf("%f",&b);

printf("Input Number\n");
scanf("%f",&c);

printf("Input Number\n");
scanf("%f",&d);

printf("Input Number\n");
scanf("%f",&e);

sum=a+b+c+d+e;
ave=sum/5;

printf("sum %f ave %f\n",sum,ave);
}
(4)
#include<stdio.h>

main()
{
int heisei,seireki;

printf("Input heisei\n");
scanf("%d",&heisei);

seireki=2005-17+heisei;
printf("seireki=%d\n",seireki);
}
(5)
Step05
(1)
#include<stdio.h>
main()
{
int x;

printf("intput number(int) \n");
scanf("%d",&x);

if(x==5) {
printf("FIVE\n");
}

if(x<5) {
printf("Small\n");
}

if(x>5) {
printf("Big\n");
}

}
(2)
#include<stdio.h>
main()
{
int x;

printf("Input number\n");

scanf("%d",&x);

if(x >= 0) {
printf("Natural Number\n");
} else {
printf("Integer Number\n");
}

}
Step06
(1)
#include<stdio.h>
main()
{
int x,y;

printf("Input x\n");
scanf("%d",&x);

printf("Input y\n");
scanf("%d",&y);

if( (x==3) || (y ==5 ) ) {
printf("OK\n");
} else {
printf("NOT\n");
}

}
(2)
#include<stdio.h>
main()
{
int x,y;

printf("Input x\n");
scanf("%d",&x);

printf("Input y\n");
scanf("%d",&y);

if( (x==3) && (y ==5 ) ) {
printf("OK\n");
} else {
printf("NOT\n");
}

}
Step07
(1)
#include<stdio.h>
main()
{
int i;
for(i=0;i<10;i++) {
printf("TAHARA\n");
}
}
(2)
#include<stdio.h>
main()
{
int i,sum;

sum=0;

for(i=1;i<=10;i++) {
sum=sum+i;
}

printf("%d\n",sum);

}
(3)
#include<stdio.h>
main()
{
int i;
float x,sum,ave;
sum=0;
for(i=0;i<10;i++) {
printf("Input X\n");
scanf("%f",&x);
sum=sum+x;
}

ave=sum/10;

printf("%f %f\n",sum,ave);
}
Step08
(1)
#include<stdio.h>
main()
{
int ch;
ch = 99;;

while(ch > 5) {
printf("In LOOP\n");
printf("Input ch\n");
scanf("%d",&ch);
}

printf("OUT LOOP\n");

}
(2)
#include<stdio.h>
main()
{
int ch;
int a,b,c;
a=b=c=1;
ch = a+b+c;

while(ch < 20) {
printf("In LOOP\n");

printf("Input a\n");
scanf("%d",&a);

printf("Input b\n");
scanf("%d",&b);

printf("Input c\n");
scanf("%d",&c);

ch=a+b+c;
printf("%d\n",ch);
}

printf("OUT LOOP\n");

}
(3)
Step09
(1)
#include<stdio.h>
main()
{
int i,j,k;

for(i=0;i<10;i++) {
for(j=0;j<10;j++) {
for(k=0;k<10;k++) {
if( (i==j) && (j == k) ) {
printf("ATARI %d %d %d\n",i,j,k);
}
}
}
}
}
(2)
#include<stdio.h>
main()
{
int x;
x=99;

while( (x%2)!=0 ) {
while( (x%17) != 0 ) {
while( (x%5) != 0) {
printf("input x\n");
scanf("%d",&x);
}
}
}

printf("In LOOP \n");
}
Step10
(1)
#include<stdio.h>
int my_sum(int a, int b, int c, int d)
{
int ans=a+b+c+d;
return ans;
}

main()
{
int z;
z=my_sum(1,2,3,4);
printf("%d\n",z);
}
(2)
#include<stdio.h>
float my_sum(float a, float b, float c, float d)
{
float ans=a+b+c+d;
ans=ans/4.0;
return ans;
}

main()
{
float z;
z=my_sum(1,2,3,4);
printf("%f\n",z);
}
(3)
#include<stdio.h>
int my_pow(int a,int b)
{
int sum=1;
int i;
for(i=0;i<b;i++) {
sum=sum*a;
}
return sum;
}

main()
{
int ans;
ans=my_pow(2,3);
printf("%d\n",ans);
}
Step11
(1)
#include<stdio.h>

void print_name(int n)
{
int i;

for(i=0;i<n;i++) {
printf("TAHARA\n");
}
}

main()
{
print_name(100);
}
(2)
(●s11q2.c)
#include<stdio.h>
#include"s11q2.h"
main()
{
print_name(100);
}

(●s11q2.h)
void print_name(int n)
{
int i;

for(i=0;i<n;i++) {
printf("TAHARA\n");
}
}
(3)
(●s11q3.c)
#include<stdio.h>
#include"s11q3.h"

main()
{
int ans;

ans=my_sum(100);

printf("%d\n",ans);
}

(●s11q3.h)
int my_sum(int num)
{
int sum = 0;
int i;
for(i=1;i<=num;i++) {
sum=sum+i;
}

return sum;

}
Step12
(1)
#include<stdio.h>
#include<math.h>

float deg2rad(float deg)
{
float pi=3.14;
float ans;

ans=deg*pi/180.0;

return ans;
}

main()
{
float x,y;

x= 90.0;
y=deg2rad(x);
printf("%f %f\n",sin(y),cos(y));
}
(2)
#include<stdio.h>
#include<math.h>

float deg2rad(float deg)
{
float pi=3.14;
float ans;

ans=deg*pi/180.0;

return ans;
}

main()
{
float x,y;
float rad;
float ans;

int i;
for(i=0;i<=360;i++){
rad=deg2rad(x);
x=sin(rad);
y=cos(rad);

ans=x*x+y*y;

printf("%d %f\n",i,ans);
}

}

STEP14を参考にしてください.
Step13
(1)
(2)
Step14
(1)
(2)
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,ans1,ans2;
float D;
float ans;
printf("cal root of ax^2 + bx + c = 0\n");
printf("input a\n");
scanf("%f",&a);
printf("input b\n");
scanf("%f",&b);
printf("input c\n");
scanf("%f",&c);

if(a==0) {
printf("a is 0. can not calc\n");
printf("system exit\n");
return -1;
exit(0);
} else {
D=b*b-4*a*c;
if(D>0) {
D=sqrt(D);
ans1=(-b+D)/(2*a);
ans2=(-b-D)/(2*a);
printf("jisuukai\n");
printf("ans1=%f\n",ans1);
printf("ans2=%f\n",ans2);
}
if(D==0) {
ans1=-b/(2*a);
printf("juukai\n");
printf("ans1=%f\n",ans1);
}
if(D<0) {
D=-1*D;
D=sqrt(D);
D=D/(2*a);
ans=-b/(2*a);
printf("kyosuukai\n");
printf("ans1=%f + i* %f\n",ans,D);
printf("ans2=%f - i* %f\n",ans,D);
}
}
return 1;
}
Step15
(1)
/* 51ページsample12.c を変更した例 */
#include <stdio.h>
#include <math.h>

int main()
{
float rad, x, y;
float pi = 3.14;
int i;
FILE *fp;
fp = fopen("kekka.dat", "w");
if (fp == NULL) {
printf("Can't open kekka.dat");
exit(1);
}
for (i = 0; i <= 360; i++) {
rad = (pi * i / 180.0);
x = sin(rad);
y = cos(rad);
fprintf(fp, "%d %f %f %f\n", i, rad, x, y);
}
fclose(fp);
return 0;
}
Step16
(1)
#include <stdio.h>
#include <math.h>

int main()
{
float t, y;
FILE *fp;
fp = fopen("kekka.dat", "w");
if (fp == NULL) {
printf("Can't open kekka.dat!");
exit(1);
}
for (t = 0.0; t < 1.0; t += 0.01) {
y = t * t - 2* t + 1;
fprintf(fp, "%f\t%f\n", t, y);
}
fclose(fp);
return 0;
}
Step17
(1)
(2)
私のバイオリズムではグラフより計算した日から28日前がもっともよい値を示しています.
Step18
(1)
#include<stdio.h>
main()
{
int data[100];
int i;
int sum = 0;

for(i=0;i<100;i++) {
data[i]=i+1;
}

for(i=0;i<100;i++) {
sum=sum+data[i];
}

printf("%d\n",sum);
}
(2)
#include<stdio.h>
#include<math.h>

main()
{
float data[360];
int i;
float sum = 0;

for(i=0;i<360;i++) {
data[i]=sin(i*3.14/180.0);;
}

for(i=0;i<360;i++) {
sum=sum+data[i];
}

printf("%f\n",sum);
}
Step19
(1)
#include<stdio.h>
main()
{
int DATA[10][10][10];
int i,j,k;
int x,y,z;
int ans;

for(i=1;i<=9;i++) {
for(j=1;j<=9;j++) {
for(k=1;k<=9;k++) {
DATA[i][j][k]=i*j*k;
}
}
}


printf("input Number\n");
scanf("%d",&x);

printf("input Number\n");
scanf("%d",&y);

printf("input Number\n");
scanf("%d",&z);

ans=DATA[x][y][z];
printf("%d * %d * %d = %d\n",x,y,z,ans);
}
(2)
#include<stdio.h>

main()
{
float a[2][2];
float b[2][2];
float ans[2][2];
int i,j;

a[0][0]=1;
a[0][1]=0;
a[1][0]=0;
a[1][1]=1;

b[0][0]=1;
b[0][1]=2;
b[1][0]=3;
b[1][1]=4;

for(i=0;i<2;i++) {
for(j=0;i<2;j++) {
ans[i][j]=a[i][j]+b[i][j];
}
}

printf("%f %f\n%f %f\n",ans[0][0],ans[0][1],ans[1][0],ans[1][1]);
}
Step20
(1)
#include <stdio.h>

int main()
{
int year, month;
int yc, mc;
int week;
int i, j;
int day;
int last[12] = {31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31};
int lastday;
int cal[6][7];
printf("ヌッ、??ホマ、キ、ニイシ、オ、、: ");
scanf("%d", &year);
for (month = 1; month <= 12; month++) {
yc = year;
mc = month;
if (month == 1 || month == 2) {
yc--;
mc += 12;
}
week = ((yc+(yc/4)-(yc/100)
+(yc/400)+(13*mc+8)/5)+1) % 7;
day = 1 - week;
for (i = 0; i < 6; i++) {
for (j = 0; j < 7; j++) {
cal[i][j] = day++;
}
}
lastday = last[month - 1];
if (month == 2) {
if (year % 400 == 0 ||
(year % 4 == 0 && year % 100 != 0)) {
lastday = 29;
}
}
printf("\t%dヌッ%dキ?n", year, month);
printf("。。ニ?。キ?。イミ。。ソ蝪。フレ。。カ筍。ナレ\n");
for (i = 0; i < 6; i++) {
for (j = 0; j < 7; j++) {
if (cal[i][j] > 0 &&
cal[i][j] <= lastday) {
printf("%4d", cal[i][j]);
} else {
printf(" ");
}
}
printf("\n");
}
}
return 0;
}
(2)
#include <stdio.h>

int main()
{
int year, month;
int yc, mc;
int week;
int i, j;
int day;
int last[12] = {31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31};
int lastday;
int cal[6][7];
FILE *fp;
fp = fopen("calendar.txt", "w");
if (fp == NULL) {
printf("Can't open calendar.txt!\n");
exit(1);
}
printf("ヌッ、??ホマ、キ、ニイシ、オ、、: ");
scanf("%d", &year);
printf("キ???ホマ、キ、ニイシ、オ、、: ");
scanf("%d", &month);
yc = year;
mc = month;
if (month == 1 || month == 2) {
yc--;
mc += 12;
}
week = ((yc+(yc/4)-(yc/100)
+(yc/400)+(13*mc+8)/5)+1) % 7;
day = 1 - week;
for (i = 0; i < 6; i++) {
for (j = 0; j < 7; j++) {
cal[i][j] = day++;
}
}
lastday = last[month - 1];
if (month == 2) {
if (year % 400 == 0 ||
(year % 4 == 0 && year % 100 != 0)) {
lastday = 29;
}
}
fprintf(fp, "。。ニ?。キ?。イミ。。ソ蝪。フレ。。カ筍。ナレ\n");
for (i = 0; i < 6; i++) {
for (j = 0; j < 7; j++) {
if (cal[i][j] > 0 &&
cal[i][j] <= lastday) {
fprintf(fp, "%4d", cal[i][j]);
} else {
fprintf(fp, " ");
}
}
fprintf(fp, "\n");
}
return 0;
}
Step21
(1)
int find_max( int DATA[] , int n)
{
int i,max;

max=DATA[0];

for(i=0;i<n;i++) {
if(max <= DATA[i]) {
max=DATA[i];
}
}
return max;
}

int find_min( int DATA[] , int n)
{
int i,min;

min=DATA[0];

for(i=0;i<n;i++) {
if(min >= DATA[i]) {
min=DATA[i];
}
}
return min;
}


main()
{
int mydata[10] = { 99, 48, 40, -100, 60, 100, 20, -19 ,46, 100 };

int ans1,ans2;

ans1= find_max(mydata,10);
ans2= find_min(mydata,10);
printf("%d %d\n",ans1,ans2);
}
(2)
#include<stdio.h>
#include<math.h>
int niji_houteishiki(float a,float b,float c,float kai[2][2])
{
float ans1,ans2;
float D;
float ans;

if(a==0) {
printf("a is 0. can not calc\n");
printf("system exit\n");
return -1;
exit(0);
} else {
D=b*b-4*a*c;

if(D>0) {
D=sqrt(D);
ans1=(-b+D)/(2*a);
ans2=(-b-D)/(2*a);
kai[0][0]=ans1;
kai[1][0]=ans2;
return 1;
}

if(D==0) {
ans1=-b/(2*a);
kai[0][0]=ans1;
return 2;
}

if(D<0) {
D=-1*D;
D=sqrt(D);
D=D/(2*a);
ans=-b/(2*a);
kai[0][0]=ans;
kai[1][0]=ans;

kai[0][1]=D;
kai[1][1]=-D;
return 3;
}
}

}


main()
{
float ans[2][2];
int mode;

mode=niji_houteishiki(1,2,1,ans);
printf("%d\n",mode);
printf(" %f %f\n %f %f\n",ans[0][0],ans[0][1],ans[1][0],ans[1][1]);
}
Step22
(1)
#include<stdio.h>
main()
{
int *x;
int *y;

int a;
int b;
int ans;

a=5;
b=6;

x=&a;
y=&1;

ans= *x + *y;

printf("%d\n",ans);
}
(2)
#include<stdio.h>
main()
{
int *x;
int *y;

int a;
int b;
int ans;

a=5;
b=1;

x=&a;
y=&b;

ans=(*x)*(*y);

printf("%d\n",ans);
}
Step23
(1)
#include<stdio.h>
main()
{
float *pa;
float x;

x=100.0;

pa=&x;

printf("%p %f\n",pa,*pa);

}
(2)
#include<stdio.h>
main()
{
float *pa;
float data[5] = { 0.0, 0.1 , 0.2 , 0.3, 0.4 };
int i;

pa=data;

for(i=0;i<5;i++) {
printf("%d %p %f\n",i,pa+i,*(pa+i));
}
}
Step24
(1)
void find_max_min( int DATA[] , int n , int *Max, int *Min)
{
int i,max,min;

max=DATA[0];

for(i=0;i<n;i++) {
if(max <= DATA[i]) {
max=DATA[i];
}
}

min=DATA[0];

for(i=0;i<n;i++) {
if(min >= DATA[i]) {
min=DATA[i];
}
}
*Max = max;
*Min = min;
}


main()
{
int mydata[10] = { 99, 48, 40, -100, 60, 100, 20, -19 ,46, 100 };

int max,min;

find_max_min(mydata,10,&max,&min);
printf("%d %d\n",max,min);
}
(2)
#include<stdio.h>
#include<math.h>
void find_ave_hensa( float DATA[] , int n , float *ave ,float *hensa)
{
int i;
float sum=0;
float diff;


for(i=0;i<n;i++) {
sum=DATA[i]+sum;
}

*ave=sum/n;

sum=0;
for(i=0;i<n;i++) {
diff=DATA[i]-(*ave);
sum=sum + diff*diff;
}
*hensa=sqrt(sum/(n-1));
}


main()
{
float mydata[10] = { 99, 48, 40, -100, 60, 100, 20, -19 ,46, 100 };

float ave,hensa;

find_ave_hensa(mydata,10,&ave,&hensa);
printf("%f %fd\n",ave,hensa);
}
Step25
(1)
#include <stdio.h>
typedef struct {
float shinchou;
float taijuu;
float bmi;
} bmi;

int main()
{
bmi my;
printf("ソネトケ(cm)、??ホマ、キ、ニ、ッ、タ、オ、、: ");
scanf("%f", &my.shinchou);
my.shinchou /= 100;
printf("ツホスナ(kg)、??ホマ、キ、ニ、ッ、タ、オ、、: ");
scanf("%f", &my.taijuu);
my.bmi = my.taijuu / (my.shinchou * my.shinchou);
printf("ソネトケ = %f(m), ツホスナ = %f(kg), BMI = %f\n",
my.shinchou, my.taijuu, my.bmi);
return 0;
}
Step26
(1)
#include <stdio.h>

int main()
{
char c = ' ';
int i, j;
printf("--0123456789ABCDEF\n");
for (i = 2; i <= 7; i++) {
printf("%1d:", i);
for (j = 0; j < 16; j++) {
printf("%c", c++);
}
printf("\n");
}
return 0;
}
Step27
(1)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
char name[50];
char zip[9];
char address[200];
char tel[15];
char birthday[15];
} address;

char *wordcpy(char *dst, char *src);

int main()
{
address data[100];
char line[500];
char *next;
FILE *fp;
int i;
int num = 0;
fp = fopen("address.dat", "r");
if (fp == NULL) {
printf("Can't open address.dat\n");
exit(1);
}
while (fgets(line, 500, fp) != NULL) {
line[ strlen(line) - 1 ] = '\0';
if (strcmp(line, "end") == 0)
break;
next = wordcpy(data[num].name, line);
next = wordcpy(data[num].zip, next);
next = wordcpy(data[num].address, next);
next = wordcpy(data[num].tel, next);
next = wordcpy(data[num].birthday, next);
num++;
}
for (i = 0; i < num; i++) {
printf("〒%s\n%s\n%s 様\nTel: %s, 誕生日: %s\n",
data[i].zip, data[i].address,
data[i].name, data[i].tel, data[i].birthday);
}
}
char *wordcpy(char *dst, char *src)
{
while (*src != '\t' && *src != '\0') {
*dst++ = *src++;
}
*dst++ = '\0';
if (*src == '\t') {
src ++;
}
return src;
}
Step28
(1)
#include <stdio.h>
#include <math.h>
#define N 1000
int main()
{
int flag[N+1];
int i, j;
int x;
int first = 1;
for (i = 1; i <=N; i++) {
flag[i] = 1;
}
flag[1] = 0;
for (i = 2; i <= sqrt(N); i++) {
if (flag[i] == 1) {
for (j = i*2; j <= N; j += i) {
flag[j] = 0;
}
}
}
do {
printf("%d までの整数を入れてください\n", N);
scanf("%d", &x);
} while (x > N);
printf("整数%dは", x);
i = 2;
while (x != 1) {
if (flag[i] == 1 && (x % i) == 0) {
if (first != 1) {
printf("×");
}
first = 0;
printf("%d", i);
x /= i;
} else {
i++;
}
}
printf("\n");
return 0;
}
Step29
(1)
(2)
(3)
(4)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 512

int main()
{
int y, x, d;
FILE *fp;
fp = fopen("circle.pgm", "w");
if (fp == NULL) {
printf("Can't open circle.pgm!");
exit(1);
}
fprintf(fp, "P2\n%d %d\n255\n", N, N);
for (y = -N/2; y < N/2; y++) {
for (x = -N/2; x < N/2; x++) {
if (x*x + y*y <= 40000) {
d = 255;
} else {
d = 0;
}
fprintf(fp, "%d ", d);
}
fprintf(fp, "\n");
}
fclose(fp);
return 0;
}
Step30
(1)
ページ先頭に戻る

top page 書籍一覧 C言語ワークブック