Cプログラミング演習
1
ガイダンス
2
例題
数値計算 ・・・ 微分,積分等
画像処理 ・・・ 配列(2次元/
3次元)
データ処理 ・・・ ソート,選択
󳚄󳚽󳚺󲡀󳛃󲎜󳚈󳚗󳞩󳜆󳜜󳛟󳜘󳜎󳜢󳛟󳚟󱕒󲄲󳛃󱨇󰘴
3
画像処理の例
左が元の画像、右が一次微分(roberts
ペレータ)(利得3)の結果
4
C 言語での2次元画像の表現
濃淡画像(grayscaleの場合
image[Y_SIZE][X_SIZE]
Y_SIZE
X_SIZE
󳞯󰷀󳚟󲑥
󳚎󳚽󳚏󳚽󳙽󱎍󱢓
󳞥󱎍󳚟󰆗󰬰󳞦
5
C 言語での2次元画像の表現
カラー画像(colorの場合
image[3][Y_SIZE][X_SIZE]
red blue green
red, blue, green 󳚟󳞰󱮮
󳞰󰷀󳚟󲑥
6
C言語
D. M. Ritchie1970年にUNIXを記述するた
めに作成した言語
低レベルの操作ができる
メモリの操作など
ANSI により規格化、汎用性が高
広く利用されている
手続き型言語のひとつ
プログラムの実行順序を明示
コンパイラ型言語のひとつ 7
コンピュータの振る舞いとメカニ
ズム
8
󳟀󳟍󳟒
󳜐󳜑󳜙
󱾇
󱧉
󱾇
󱧉
󱾖󲁑󰞩󱾇󱧉
コンピュータのハードエア構成
9
󳟀󳟍󳟒
󳜐󳜑󳜙
󱾇
󱧉
󱾇
󱧉
󱾖󲁑󰞩󱾇󱧉
󳟀󳟍󳟒
󳛢󳜢󳜃󳜔󳜫󳛮󳚟󰘻󳞩󱀨󱞍󳛃󱼽
󳙷LSI 󳛰󳛲󳜆
10
󳟀󳟍󳟒
󳜐󳜑󳜙
󱾇
󱧉
󱾇
󱧉
󱾖󲁑󰞩󱾇󱧉
󳛶󳛧󳛮󳜚󳛶󳜫󳛮󳚟󲁑󰞩󳛃󱼽󳙷󳟉󳟐󳟆
󳛰󳛲󳜆
󳛶󳛧󳛮󳜚󳛶󳜫󳛮󳛃󲀍󳙹󳚆󳚌󳚐󳚻󳞩
󳚻󳚈󳚐󳚻󳚟󰵖󱫀󳙽󳙳󳚼
󳜐󳜑󳜙
11
󳟀󳟍󳟒
󳜐󳜑󳜙
󱾇
󱧉
󱾇
󱧉
󱾖󲁑󰞩󱾇󱧉
󳟀󳟍󳟒
󳜐󳜑󳜙
CPU󳞩󳜐󳜑󳜙󲚌󳚘
󳛶󳛧󳛮󳜚󳛶󳜫󳛮󳙽
󳚵󳚻󳚻󳚆󳚽󳚼
12
󳟀󳟍󳟒
󳜐󳜑󳜙
󱾇
󱧉
󱾇
󱧉
󱾖󲁑󰞩󱾇󱧉
󴎚
󴏁
󴏤
󴎱
󴎿
󴏯
󴎷
󳛑󳛸󳜛󳛨󳚠󳞩
󲂿󳚰󰬊󳙾󳚈󳚐󳙵
󳛶󳜫󳛮󳚟󰆺󰠅
13
󳜐󳜑󳜙 󳞥RAM)
󳛑󳛸󳜛󳛨󳛿󳛨 (address bus)
󳛶󳜫󳛮󳛿󳛨 (data bus)
󳛢󳜢󳛷󳜜󳜫󳜚󳛿󳛨
(control bus)
R/W
󳚛󳚚
R: Read
W: Write
CPU
14
󳜐󳜑󳜙 󳞥RAM)
󳏀 󳛑󳛸󳜛󳛨
󳜐󳜑󳜙󳙼󳚺󳚟󳛶󳜫󳛮󲂿󳚰󳚈
󳏀 󳜐󳜑󳜙󳚜󳞩󳛑󳛸󳜛󳛨󳛃󳙹󳚼󳚙
󳏁 󳜐󳜑󳜙󳙼󳚺󳞩󳛶󳜫󳛮󳙽
󲂿󳚰󳚆󳚽󳚼
󳏁󳛶󳜫󳛮
CPU
󳛑󳛸󳜛󳛨󳙣󳞱󳙤
󳛑󳛸󳜛󳛨󳞱󳞩󳞲󳚟
󳜐󳜑󳜙󳛃󳛙󳜢
󳞯󳛿󳛓󳛷
󳚟󳛶󳜫󳛮
󳚄󳚟󰆺󳞯󳛿󳛓󳛷󳚘󲂿󳚰󳚈
󰪲󲎜󳙙󳜐󳜑󳜙󳙼󳚺󳚠󳞩󱾮󰧹󳛿󳛓󳛷
󳚟󳛶󳜫󳛮󳙽󰡴󳚈󳚗󲂿󳚰󳚆󳚽󳚼
15
󳜐󳜑󳜙 󳞥RAM)
󳏀 󳛑󳛸󳜛󳛨
󳜐󳜑󳜙󳚩󳚟󳛶󳜫󳛮󰬊󳙾󲍙󳚰
󳏀 󳜐󳜑󳜙󳚜󳞩󳛑󳛸󳜛󳛨󳚙󳛶󳜫󳛮󳛃
󳙹󳚼󳚙
󳏀󳛶󳜫󳛮
CPU
󳛑󳛸󳜛󳛨󳙣󳞳󳙤
󳞯󳛿󳛓󳛷
󳚟󳛶󳜫󳛮
󳚄󳚟󰆺󳞯󳛿󳛓󳛷󳚘󰬊󳙾󲍙󳚰
󳚟󳛶󳜫󳛮󳚠
󰼹󳙹󳚼󳞥󰬊󳙾󳞦
󳏁 󳛶󳜫󳛮󳙽󰬊󳙾󲍙󳚯󳚽󳚼
16
󳟀󳟍󳟒
󳜐󳜑󳜙
󱾇
󱧉
󱾇
󱧉
󱾖󲁑󰞩󱾇󱧉󱾖󲁑󰞩󱾇󱧉
󳛑󳛸󳜛󳛨 󳛶󳜫󳛮
󳜐󳜑󳜙󳙼󳚺󳟀󳟍󳟒󳚩󳚟󲂿󳚰󳚈
17
󳟀󳟍󳟒
󳜐󳜑󳜙
󱾇
󱧉
󱾇
󱧉
󱾖󲁑󰞩󱾇󱧉󱾖󲁑󰞩󱾇󱧉
󳛑󳛸󳜛󳛨 󳛶󳜫󳛮
󳟀󳟍󳟒󳙼󳚺󳜐󳜑󳜙󳚩󳚟󰬊󳙾󲍙󳚰
18
C言語によるプログラミング
入門
19
メッセージ表示プログラム
󱎍󲞤󳚜󱽓󱘈󳚆󳚽󳚐󳜐󳛲󳛪󳜫󳛧
20
#include "stdafx.h"
int _tmain()
{
printf("Hello, world\n");
}
Visual Studio C++ 2015,
Visual Studio C++ 2013
󳚛󳚚󳚘󳚀󰬊󳙾󰨪
#include <stdio.h>
int main(int argc, char** argv)
{
printf("Hello, world\n");
}
#include "stdafx.h"
int _tmain()
{
printf("Hello, world\n");
}
メッセージ表示プログラム
C󲀾󲂳󳚘󳚟󰪲󲎜
󳚟󰬊󳙾󰨪
(󱨤) Linux 󳚘󳜆󳜜
󳛟󳜘󳜎󳜢󳛟󳚊󳚼󳚙󳙾󳚛󳚚
21
Visual Studio C++ 2015,
Visual Studio C++ 2013
󳚛󳚚󳚘󳚀󰬊󳙾󰨪
#include "stdafx.h"
int _tmain()
{printf("Hello, world\n");
}
#include <stdio.h>
int main(int argc, char** argv)
{printf("Hello, world\n");
}
C󲀾󲂳󳚘󳚟󰪲󲎜
󳚟󰬊󳙾󰨪
(󱨤) Linux 󳚘󳜆󳜜
󳛟󳜘󳜎󳜢󳛟󳚊󳚼󳚙󳙾󳚛󳚚
#ifdef WIN32
#include "stdafx.h"
#else
#include <stdio.h>
#endif
#ifdef WIN32
int _tmain()
#else
int main(int argc, char** argv)
#endif
{printf("Hello, world\n");
}
Linux 󳚘󳚳
Windows 󳚘󳚳
󳙼󳚈󳚐󳙵󳚙󳙾󳚟
󰬊󳙾󰨪󳚟
22
Visual Studio C++ 2015,
Visual Studio C++ 2013
󳚛󳚚󳚘󳚀󰬊󳙾󰨪
#include "stdafx.h"
int _tmain()
{printf("Hello, world\n");
}
#include <stdio.h>
int main( int argc, char** argv )
{printf("Hello, world\n");
}
C󲀾󲂳󳚘󳚟󰪲󲎜
󳚟󰬊󳙾󰨪
(󱨤) Linux 󳚘󳜆󳜜
󳛟󳜘󳜎󳜢󳛟󳚊󳚼󳚙󳙾󳚛󳚚
Visual Studio C++ 2015,
Visual Studio C++ 2013
󳚛󳚚󳚘󳚀󰬊󳙾󰨪
#ifdef WIN32
#include "stdafx.h"
#else
#include <stdio.h>
#endif
#ifdef WIN32
int _tmain()
#else
int main( int argc, char** argv)
#endif
{printf("Hello, world\n");
}
Linux 󳚘󳚳
Windows 󳚘󳚳
󳙼󳚈󳚐󳙵󳚙󳙾󳚟
󰬊󳙾󰨪󳚟
󳚄󳚟󲇉󰨒󳚘󳚠󳞩󳚄󳚟󰬊󳙾󰨪
󳚘󱣌󳚈󳚯󳚊
23
#include "stdafx.h"
int _tmain()
{
printf("Hello, world\n");
}
メッセージ表示プログラム
󳞮󳚕󳚟󲚖󰧹
󳞥󲚖󰧹󳞷 _tmain󳞦
_tmain 󲚖󰧹󳚟󳚘󳞩󳚟
󲚖󰧹 printf 󳛃󳚔󳚗󳙵󳚼
24
#include "stdafx.h"
int _tmain()
{
printf("Hello, world\n");
}
メッセージ表示プログラム
_tmain 󲚖󰧹
󳚟󰬰
󳙜󳚟󰨆󳚟󱢫󳛃󱘈󳚊
󳞥󳛪󳜎󳛢󳜜󳜢󳞦
󲚖󰧹
25
#include "stdafx.h"
int _tmain()
{
printf("Hello, world\n");
}
プログラムの流れ
printf 󲚖󰧹
󳜐󳛓󳜢󲚖󰧹
printf("Hello,
world\n");
󲚖󰧹󳚤󳚈
󰠁󳚻
󳜆󳜜󳛟󳜘󳜏󳚟󰎨󱼽󲚅󰊿
󳜆󳜜󳛟󳜘󳜏󳚟󰎨󱼽󱢫 26
例題1.棒グラフ
整数から,その長さだけの棒を表示す
関数 bar を作る
例) 6 → ******
関数 bar を使って,「整数を読み込
で,読み込んだ長さの棒を表示す
イン関数を作る
27
棒グラフ
󳜐󳛲󳛪󳜫󳛧󳙣len =󳙤󳙽󱽓
󱘈󳚆󳚽󳚼󳚟󳚘󳞩󳛜󳜫󳜋󳜫󳛸
󳚘󳙣6 Enter󳙤󳚙󳚊󳚼󳚙󳞩
󲙸󳚆󳞳󳚟󰱍󳛟󳜘󳜄󳙽󱽓󱘈󳚆
󳚽󳚼
󰎨󱼽󱢷󰭾󳚟 28
プログラム実行順
普通,プログラム中の文は,上から下
へ順に実行される
関数呼び出しでは,関数の先頭に
「ジャンプ」する.
関数呼び出しの例) bar( len );
呼び出された関数の中で return に出
と,関数呼び出しの場所に戻る
󳚄󳚟󳚄󳚙󳚠󳞩C󲀾󲂳󳙽󳙣󰠎󱣧󳙾󰅛󲀾󲂳󳙤󳚙󲀾󳛀󳚽󳚼󱋏󱎅󳚟󳞮󳚕
29
#include "stdafx.h"
#include <math.h>
void bar( int len )
{
int i;
for (i=0; i<len; i++) {
printf("*");
}
printf("\n");
return;
}
int _tmain()
{
int len;
char buf[256];
int ch;
printf( "len =" );
fgets( buf, 256, stdin );
sscanf_s( buf, "%d\n", &len );
bar( len );
ch = getchar();
ch = getchar();
return 0;
}
棒グラフ
bar󲚖󰧹
_tmain󲚖󰧹
󳜆󳜜󳛟󳜘󳜏󰎨󱼽󳚠
_tmain 󲚖󰧹󳞥󳜐󳛓󳜢
󲚖󰧹󳞦󳙼󳚺󰊿󳚯󳚼
󱾮󰧹󳚟󲚖󰧹󳛃󳚱
󳜆󳜜󳛟󳜘󳜏
30
󲚖󰧹󳚤󳚈
#include "stdafx.h"
#include <math.h>
void bar( int len )
{
int i;
for (i=0; i<len; i++) {
printf("*");
}
printf("\n");
return;
}
int _tmain()
{
int len;
int ch;
printf( "len=" );
scanf_s( "%d", &len );
bar( len );
ch = getchar();
ch = getchar();
return 0;
}
プログラム実行順
󳏀
󳏁
󳏂
󳏆
󳏃
󳏄
󳏅󰠁󳚻
󳜐󳛓󳜢󲚖󰧹󳚟󲠭󱼽
󳙽󳜆󳜜󳛟󳜘󳜏󰎨󱼽󳚟󰊿󳚯󳚻
󳜐󳛓󳜢󲚖󰧹󳚟 return
󳙽󳜆󳜜󳛟󳜘󳜏󰎨󱼽󳚟󱢫󳛀󳚻
󳏇
󳏈31
#include "stdafx.h"
#include <math.h>
void bar( int len )
{int i;
for (i=0; i<len; i++) {
printf("*");
}
printf("\n");
return;
}
int _tmain()
{int len;
int ch;
printf( "len=" );
scanf_s( "%d", &len );
bar( len );
ch = getchar();
ch = getchar();
return 0;
}
プログラムの流れ
bar 󲚖󰧹
󳜐󳛓󳜢󲚖󰧹
bar( len );
󲚖󰧹󳚤󳚈
return;
󳜆󳜜󳛟󳜘󳜏󳚟󰎨󱼽󲚅󰊿
󳜆󳜜󳛟󳜘󳜏󳚟󰎨󱼽󱢫 * printf, scanf 󳚟
󳚤󳚈󳚜󳚕󳙵󳚗󳚠
󰃸󳚘󳚠󱓍󱎯 32
変数
変数
変数名
󳛶󳜫󳛮󳞥󰧹󳚵󰨆󰍱󳞦󳛃󳚽󳚼󳚳󳚟
󱰆󰧹󰍱󳙼󳛑󳜢󳛯󳜫󳛿󳜫󳞥_󳞦󳚘󳚺󳚽󳚼
󰬒󳚟󰨆󰍱󳚜󳚠󰧹󰍱󳚠󳙹󳚛󳙵
󰉒󰨆󰍱󳚙󰐁󰨆󰍱󳛃󳚊󳚼
󰈻󰧹 i
33
#include "stdafx.h"
#include <math.h>
void bar( int len )
{
int i;
for (i=0; i<len; i++) {
printf("*");
}
printf("\n");
return;
}
int _tmain()
{
int len;
char buf[256];
int ch;
printf( "len =" );
fgets( buf, 256, stdin );
sscanf_s( buf, "%d\n", &len );
bar( len );
ch = getchar();
ch = getchar();
return 0;
}
󰈻󰧹 i󳛃󳜐󳜑󳜙󳛗󳜙󳛑󳚜󱖽
len 󳛃󳜐󳜑󳜙󳛗󳜙󳛑󳚜󱖽
󰈻󰧹 len, buf, ch
󳛃󳜐󳜑󳜙󳛗󳜙󳛑󳚜󱖽
󳚄󳚄󳚘󱍾
󳚄󳚄󳚘󱍾
34
#include "stdafx.h"
#include <math.h>
void bar( int len )
{
int i;
for (i=0; i<len; i++) {
printf("*");
}
printf("\n");
return;
}
int _tmain()
{
int len;
char buf[256];
int ch;
printf( "len =" );
fgets( buf, 256, stdin );
sscanf_s( buf, "%d\n", &len );
bar( len );
ch = getchar();
ch = getchar();
return 0;
}
󰈻󰧹󳚠󳞯󱚡󲡏󳚔󳚗󳙵󳚼
󰧻󰧹󳛃󰠦󳙷 int 󰅛
󰨆󰍱󳛃󰠦󳙷 char 󰅛
󰧻󰧹󳚠, 5, -3, 0 󳚛󳚚
󰨆󰍱󳚠, 1, 0, 3, -, a 󳚛󳚚
󰧹󰍱(1, 0, 3 󳚛󳚚)󳚳󰨆󰍱󳚟󱚡
len 󳛃󳜐󳜑󳜙󳛗󳜙󳛑󳚜󱖽
󰈻󰧹 i󳛃󳜐󳜑󳜙󳛗󳜙󳛑󳚜󱖽
󰈻󰧹 len, buf, ch
󳛃󳜐󳜑󳜙󳛗󳜙󳛑󳚜󱖽
35
例題.平方根の計
浮動小数データを読み込んで,平方根の計
算と表示を行プログラムを作る.
但し,負の数の場合には,メッセージを表示す
ること.
負の数であるかどかによって条件分岐を行
ために if 文を使
例) 9のとき:
-1のとき: メッセージを表示 36
条件分岐とは
Yes
No
󳞿 󳞾
󰭓󰗈
「ある条件式」が成り立てばAを、成
り立たなければBを実行
37
平方根の計算
󰎨󱼽󱢷󰭾󳚟
x=9
sqrt(9.000000)=3.000000
x=󳞪1
󲆨󳚛󳚟󳚘󲁄󱞍󳚘󳙾󳚯󳚌󳛄
38
#include "stdafx.h"
#include <math.h>
int _tmain()
{
double x;
double y;
char buf[256];
int ch;
printf("x=");
fgets( buf, 256, stdin );
sscanf_s( buf, "%lf\n", &x );
if ( x < 0 ) {
printf("󲆨󳚛󳚟󳚘󲁄󱞍󳚘󳙾󳚯󳚌󳛄\n");
}
else {
y = sqrt(x);
printf("sqrt(%f)=%f\n", x, y);
}
ch = getchar();
ch = getchar();
return 0;
}
󰭓󳙽󰟣󳚻󱜈󳚕
󰆺󳚜󰎨󱼽󳚆󳚽
󳚼󲐫
󰭓󰗈
󰭓󳙽󰟣󳚻󱜈󳚐
󳚛󳙵󰆺󳚜󰎨󱼽
󳚆󳚽󳚼󲐫
39
#include "stdafx.h"
#include <math.h>
int _tmain()
{
double x;
double y;
char buf[256];
int ch;
printf("x=");
fgets( buf, 256, stdin );
sscanf_s( buf, "%lf\n", &x );
if ( x < 0 ) {
printf("󲆨󳚛󳚟󳚘󲁄󱞍󳚘󳙾󳚯󳚌󳛄\n");
}
else {
y = sqrt(x);
printf("sqrt(%f)=%f\n", x, y);
}
ch = getchar();
ch = getchar();
return 0;
}
󰎨󱼽󲠑
(x<0) 󳚟󰆺
󳏀
󳏁
󳏂
󳏃
󳏄
󳏅
󳏆
󳏇
󳏈
󳏉
󳏊40
#include "stdafx.h"
#include <math.h>
int _tmain()
{
double x;
double y;
char buf[256];
int ch;
printf("x=");
fgets( buf, 256, stdin );
sscanf_s( buf, "%lf\n", &x );
if ( x < 0 ) {
printf("󲆨󳚛󳚟󳚘󲁄󱞍󳚘󳙾󳚯󳚌󳛄\n");
}
else {
y = sqrt(x);
printf("sqrt(%f)=%f\n", x, y);
}
ch = getchar();
ch = getchar();
return 0;
}
󰎨󱼽󲠑
(x0) 󳚟󰆺
󳏀
󳏁
󳏂
󳏃
󳏄
󳏅
󳏆
󳏇
󳏈
󳏉
󳏊41
#include "stdafx.h"
#include <math.h>
int _tmain()
{
double x;
double y;
char buf[256];
int ch;
printf("x=");
fgets( buf, 256, stdin );
sscanf_s( buf, "%lf\n", &x );
if ( x < 0 ) {
printf("󲆨󳚛󳚟󳚘󲁄󱞍󳚘󳙾󳚯󳚌󳛄\n");
}
else {
y = sqrt(x);
printf("sqrt(%f)=%f\n", x, y);
}
ch = getchar();
ch = getchar();
return 0;
}
󰈻󰧹󳚠󳞰󱚡󲡏󳚔󳚗󳙵󳚼
󰧻󰧹󳛃󰠦󳙷 int 󰅛
󰨆󰍱󳛃󰠦󳙷 char 󰅛
󰧻󰧹󳚠, 5, -3, 0 󳚛󳚚
󰨆󰍱󳚠, 1, 0, 3, -, a 󳚛󳚚
󰧹󰍱(1, 0, 3 󳚛󳚚)󳚳󰨆󰍱󳚟󱚡
󰼬󰐁󰧹󳛃󰠦󳙷 double 󰅛
3.14, -1.414, 5, 0, -3 󳚛󳚚
󳞥5, 0, -3 󳚛󳚚󳚟󰧻󰧹󳚳󰼬󰐁󰧹󳚟󱚡󳞦
󰈻󰧹 x, y,
buf, ch
󳛃󳜐󳜑󳜙󳛗󳜙󳛑
󳚜󱖽
42
繰り返しとは
繰り返しとは,ある条件が満たされる
まで,同じことを繰り返すこと.
繰り返しを行ための文としてwhile,
for などがある.
󰭓
43
繰り返し
C言語での繰り返しは次の3通り
while
do-while
for
ループ変数 ループカンタ)
繰り返しの回数を数える変数
インクリメント 値を1増やす
i++
デクリメント 値を1減らす i--44
for 文による繰り返し
󰬨󰗈
󰭓󰗈
󲁠󰎤󰗈
Yes
No
󳞮󳞫󳚯󳚋󳞩󳙣󰬨󰗈󳙤󳛃󰎨󱼽
󳞯󳞫󰷀󳚜󳞩󳙣󰭓󰗈󳙤󳛃󰎨󱼽󳞫󰭓󰗈󳙽󰟣󱜈󳚊󳚽󳚡󳞩
󰗈󳚙󳙣󲁠󰎤󰗈󳙤󳛃󰎨󱼽󳚈󳞩󳙣󰭓󰗈󳙤󳚜󰠁󳚼
󰗈
45
例題.繰り返し計算と
ファイル出力
次のページのプログラムの機能
計算の繰り返
キーボードからのデータ読み込み
ファイルへの書き出し
46
#include "stdafx.h"
#include <math.h>
int _tmain()
{
double x;
double y;
char buf[256];
int i;
double start_x;
double step_x;
FILE* fp;
printf( "start_x =" );
fgets( buf, 256, stdin );
sscanf_s( buf, "%lf\n", &start_x );
printf( "step_x =" );
fgets( buf, 256, stdin );
sscanf_s( buf, "%lf\n", &step_x );
fp = fopen( "z:\\data.csv", "w" );
for( i = 0; i < 20; i++ ) {
x = start_x + ( i * step_x );
y = sin( x );
printf( "x= %f, y= %f\n", x, y );
fprintf( fp, "x=, %f, y=, %f\n", x, y );
}
fprintf( stderr, "file z:\\data.csv created\n" );
fclose( fp );
return 0;
}
󲁄󱞍󳛃󱼽󳚔󳚗󳙵󳚼󲐫
󳛜󳜫󳜋󳜫󳛸󳙼󳚺󳚟
󳛶󳜫󳛮󲂿󳚰󲍙󳚰󳛃
󱼽󳚔󳚗󳙵󳚼󲐫
󳜄󳛐󳛓󳜚󳚩󳚟󰬊󳙾󳚈
󳛃󱼽󳚔󳚗󳙵󳚼󲐫
y = sin ( x )
47
#include "stdafx.h"
#include <math.h>
int _tmain()
{
double x;
double y;
char buf[256];
int i;
double start_x;
double step_x;
FILE* fp;
printf( "start_x =" );
fgets( buf, 256, stdin );
sscanf_s( buf, "%lf\n", &start_x );
printf( "step_x =" );
fgets( buf, 256, stdin );
sscanf_s( buf, "%lf\n", &step_x );
fp = fopen( "z:\\data.csv", "w" );
for( i = 0; i < 20; i++ ) {
x = start_x + ( i * step_x );
y = sin( x );
printf( "x= %f, y= %f\n", x, y );
fprintf( fp, "x=, %f, y=, %f\n", x, y );
}
fprintf( stderr, "file z:\\data.csv created\n" );
fclose( fp );
return 0;
}
󰈻󰧹 x, y, buf, i, start_x,
step_x, fp 󳛃󳜐󳜑󳜙󳛗󳜙󳛑󳚜󱖽
󳚄󳚄󳚘
󰈻󰧹󳛃󱍾
48
#include "stdafx.h"
#include <math.h>
int _tmain()
{
double x;
double y;
char buf[256];
int i;
double start_x;
double step_x;
FILE* fp;
printf( "start_x =" );
fgets( buf, 256, stdin );
sscanf_s( buf, "%lf\n", &start_x );
printf( "step_x =" );
fgets( buf, 256, stdin );
sscanf_s( buf, "%lf\n", &step_x );
fp = fopen( "z:\\data.csv", "w" );
for( i = 0; i < 20; i++ ) {
x = start_x + ( i * step_x );
y = sin( x );
printf( "x= %f, y= %f\n", x, y );
fprintf( fp, "x=, %f, y=, %f\n", x, y );
}
fprintf( stderr, "file z:\\data.csv created\n" );
fclose( fp );
return 0;
}
󰈻󰧹󳚠󳞱󱚡󲡏󳚔󳚗󳙵󳚼
󰧻󰧹󳛃󰠦󳙷 int 󰅛
󰨆󰍱󳛃󰠦󳙷 char 󰅛
󰧻󰧹󳚠, 5, -3, 0 󳚛󳚚
󰨆󰍱󳚠, 1, 0, 3, -, a 󳚛󳚚
󰧹󰍱(1, 0, 3 󳚛󳚚)󳚳󰨆󰍱󳚟󱚡
󰼬󰐁󰧹󳛃󰠦󳙷 double 󰅛
3.14, -1.414, 5, 0, -3 󳚛󳚚
󰧻󰧹󳚳󰼬󰐁󰧹󳚟󱚡
󳜄󳛐󳛓󳜚󳜌󳛓󳜢󳛮
󳜄󳛐󳛓󳜚󰦐󳚜󳙷󰈻󰧹
49
#include <stdio.h>
main()
{
double x;
double y;
char buf[256];
int i;
double start_x;
double step_x;
FILE* fp;
printf( "start_x =" );
fgets( buf, 256, stdin );
sscanf_s( buf, "%lf\n", &start_x );
printf( "step_x =" );
fgets( buf, 256, stdin );
sscanf_s( buf, "%lf\n", &step_x );
fp = fopen( "z:\\data.csv", "w" );
for( i = 0; i < 20; i++ ) {
x = start_x + ( i * step_x );
y = sin( x );
printf( "x= %f, y= %f\n", x, y );
fprintf( fp, "x=, %f, y=, %f\n", x, y );
}
fprintf( stderr, "file z:\\data.csv created\n" );
fclose( fp );
return 0;
}
C󳜆󳜜󳛟󳜘󳜏󳚠󳜐󳛓󳜢󲚖󰧹󳙼󳚺
󰎨󱼽󲚅󰊿
󳜆󳜜󳛟󳜘󳜏󳚠󲠑󰷀󰎨󱼽
󳚄󳚟󲐫󳚠󱥿󳚻󲍪󳚈󱋏
󳞥for 󰨆󳚜󳚹󳚼󱥿󳚻󲍪󳚈󳞦
󳚄󳚟󲐫󳛃󱥿󳚻󲍪󳚊
50