char* mystrchr(char* str, char c) {
for (; *str; str++) {
if (*str==c) {
return str;
}
}
return NULL;
}
graphfun.c,
ポインタのインクリメントの絵(2)
main() {
double *px1, *px2;
niji(1, -2, 1, px1, px2); /* 間違い! */
...
}
double* niji(double a, double b, double c) {
double ans[2];
...
ans[0] = (-b+sd)/(2*a);
ans[1] = (-b-sd)/(2*a);
return ans; /* 間違い! */
}
|
#include <stdio.h>
int main() {
FILE* fp;
fp = fopen("tmp.txt", "w");
fprintf(fp, "aiueo\n");
fclose(fp);
}
|
#include <stdio.h>
int main() {
FILE* fp;
char buf[16]; /* 読み込む予定の文字数+1(ヌル文字の分)以上の領域を確保しておく */
int x, y;
fp = fopen("tmp.txt", "r");
while(1) {
if(fscanf(fp, "%s %d %d", buf, &x, &y)<3) break;
/* fscanfは読み取りに成功した項目の数を返す */
printf("%s %d\n", buf, x+y);
}
fclose(fp);
}
|
s98x001 11 12 s99x002 13 14 s99x003 15 16 |
#include <fstream.h>
int main() {
ofstream f;
f.open("tmp.txt");
f << "abcde\n";
f.close();
}
|
#include <fstream.h>
#include <iostream.h>
int main() {
ifstream f;
int x, y;
f.open("tmp.txt");
f >> x >> y;
cout << x+y << "\n";
f.close();
}
|
問 5.1.11と問 5.1.12のデータとしてはこの架空のデータファイルを使ってみてください。
(問 5.1.12は平均だけで良いです。)
その他の構造体の例
struct person {
int ID;
char name[32];
int salary;
double height;
double weight;
};
#include <iostream.h>
struct point {
double x_cor; /* x座標 */
double y_cor; /* y座標 */
};
struct point addPoint(struct point pnt1, struct point pnt2) {
pnt1.x_cor += pnt2.x_cor;
pnt1.y_cor += pnt2.y_cor;
return pnt1;
}
int main() {
struct point p1 = {2, 3}, p2 = {1, 2}, p3;
p3 = addPoint(p1, p2);
cout << p1.x << " " << p1.y << "\n";
cout << p3.x << " " << p3.y << "\n";
}
|
#include <iostream.h>
double* addPoint(double pnt1[], double pnt2[]) {
pnt1[0] += pnt2[0];
pnt1[1] += pnt2[1];
return pnt1;
}
int main() {
double p1[] = {2, 3}, p2[] = {1, 2};
double* p3;
p3 = addPoint(p1, p2);
cout << p1[0] << " " << p1[1] << "\n";
cout << p3[0] << " " << p3[1] << "\n";
}
|
ArgGraph/Unit1.cpp(プリント p.65)の訂正
int* data=NULL;
void __fastcall TForm1::FormPaint(TObject *Sender)
{
int i;
Canvas->Brush->Color=clBlue;
if (data==NULL) {
data = (int *)malloc((_argc-1)*sizeof(int));
/* または、data = new int[_argc-1]; */
for (i=1; i<_argc; i++) {
data[i-1]=StrToInt(_argv[i]);
}
}
for(i=1; i<_argc; i++) {
Canvas->Rectangle(0, (i-1)*16, data[i-1]*16, i*16);
}
} |
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
int* data=NULL;
int i;
/* 割り当てる配列の要素数は argc-1 (argv[0]はコマンド名) */
data = (int *)malloc((argc-1)*sizeof(int));
/* または、C++の場合は data = new int[argc-1]; */
for (i=1; i<argc; i++) {
data[i-1]=atoi(argv[i]);
}
for(i=1; i<argc; i++) {
int j;
for (j=0; j<data[i-1]; j++) {
putchar('*');
}
putchar('\n');
}
}
|
int main() {
struct st_schedule dummy = {0, "", (struct st_schedule *)NULL};
char buf[256];
char cmd;
int date;
char memo[128];
struct st_schedule *cur, *prev;
while(1) {
fgets(buf, 256, stdin);
if (buf[0]=='\n') { /* 入力が空行なら終了 */
break;
}
sscanf(buf, "%c %d %s", &cmd, &date, memo);
if (cmd=='i' || cmd=='I') { /* 挿入 */
prev=&dummy;
for (cur=dummy.next; cur; cur=cur->next) {
if (cur->date > date) {
/* curの手前に挿入 */
insertSchedule(prev, date, memo);
break;
}
prev=cur;
}
if (cur==NULL) { /* 最後までいってしまったら最後に挿入 */
insertSchedule(prev, date, memo);
}
} else if (cmd=='d' || cmd=='D') { /* 削除 */
prev=&dummy;
for (cur=dummy.next; cur; cur=cur->next) {
if (cur->date == date) {
/* curを削除 */
deleteSchedule(prev);
break;
}
prev=cur;
}
} else if (cmd=='p' || cmd=='P') { /* 出力 */
printSchedule(dummy.next);
}
}
return 0;
}
|
typedef struct st_schedule SCHEDULE; /* 教科書と同じ */ typedef struct st_schedule* Schedule; /* このようにポインタ型に対して typedefすることも多い */SCHEDULEという型名を struct st_scheduleの代わりに、 Scheduleという型名を struct st_schedule*の代わりに使える。
問:
Schedule/schedule.cを typedefを用いて書き換えよ。