top page 書籍一覧 C言語ワークブック
|
|
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を参考にしてください.
|
|
|
|
|
|
|
|
|
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(" | | |