12週目 解答

Download Report

Transcript 12週目 解答

課題 1
/* 加古富志雄 学籍番号 99999999 */
Complex cmult(Complex a, Complex b)
{
Complex c;
c.real = a.real*b.real-a.imag*b.imag;
c.imag = a.real*b.imag + a.imag * b.real;
return c;
}
1
課題 2
/* 加古富志雄 学籍番号 99999999 */
struct student {
char name[20];
int no, age;
double h, w;};
void read_data(struct student *s)
{
printf(“名前は:”); scanf(“%s”, s->name);
printf(“学籍番号は:”); scanf(“%d”, &s->no);
printf(“年齢:”); scanf(“%d”, &s->age);
printf(“身長:”); scanf(“%lf”, &s->h);
printf(“体重:”); scanf(“%lf”, &s->w);
}
void print_data(struct student s)
{
printf(“%s\n%d\n%d歳\n%.1f cm\n%.1f kg\n”, s.name, s.no, s.age, s.h, s.w);
}
2
課題 3
/* 加古富志雄 学籍番号 99999999 */
#include <stdio.h>
strucdt time {
int h, m, s;};
struct time timeadd(struct time a, struct time b);
struct time timesub(struct time a, struct time b);
struct time timeadd(struct time a, struct time b)
{
struct time c;
c.s = a.s+b.s; c.m = a.m + b.m; c.h = a.h + b.h;
if(c.s >= 60) {c.m++; c.s -= 60;}
if(c.m >= 60) {c.h++; c.m -= 60;}
if(c.h >= 24) c.h -= 24;
return c;
}
3
課題 3
struct time timesub(struct time a struct time b)
{
struct time c;
c.s = a.s-b.s; c.m = a.m - b.m; c.h = a.h - b.h;
if(c.s < 0) {c.m--; c.s += 60;}
if(c.m <60) {c.h--; c.m += 60;}
if(c.h < 0) c.h += 24;
return c;
}
main()
{
struct time a = {12, 23, 10}, b = {5,40,15};
struct time c, d;
c = timeadd(a, b);
d = timesub(a,b);
print(a); printf(“ + “); print(b); printf(“ = “); print(c); printf(“\n”);
print(a); printf(“ - “); print(b); printf(“ = “); print(d); printf(“\n”);
}
4
課題 3
void print(struct time a)
{
printf(“%d時%d分%d秒”, a.h, a.m, a.s);
}
5
課題 4
/* 加古富志雄 学籍番号 99999999 */
#include <stdio.h>
#include <math.h>
struct vector {
double x, y;
};
double distance(struct vector a, struct vector b);
double distance(struct vector a, struct vector b)
{
double d;
d = (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
d = sqrt(d);
return d;
}
6
課題 4
/* 加古富志雄 学籍番号 99999999 */
main()
{
struct vector a = {1.0, 2.0}, b = {2.0, 1.0},
c = {3.0, 3.0};
double ab, bc, ca, s, S;
ab = distance(a, b); bc = distance(b, c); ca = distance(c, a);
s = (ab+bc+ca)/2;
S = s*(s-ab)*(s-bc)*(s-ca);
S = sqrt(S);
printf(“面積は%lfです\n”, S);
}
7
課題 5
/* 加古富志雄 学籍番号 99999999 */
#include <stdio.h>
main()
{
int c;
char name[100];
FILE *fp;
printf(“ファイル名:”);
scanf(“%s”, name);
fp = fopen(name, “w”);
if(fp == NULL) {
printf(“ファイル%sは開けませんでした。\n”, name);
exit(1);
}
while((c = getchar()) != EOF) {
putc(c, fp);
}
fclose(fp);
}
8