(續三)
再翻翻書,只剩下文件訪問了。
以我的觀點,文件訪問是最“水”的充篇幅的東西,只要寫好了輸入輸出,文件訪問就是小菜一碟,一個固定的偷懶流程:
第一步:定義文件指針 FILE *fp;
第二步:使用fopen()打開文件
第三步:把輸入輸出函數體的printf()和scanf()換成fprintf()和fscanf(),函數實參加上fp
第四步:使用fclose()關閉文件
明白了吧,就是在原來的輸入輸出函數的基礎上,加了個文件的開頭和結尾,換湯不換藥。本着有始有終的精神,還是寫完。
設計5:
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#define MAXSIZE 20
/* 結構體 */
struct compute
{
double a;
double b;
char op;
double r;
}; /* a op b = r */
/* 四則運算 */
double plus(double a, double b)
{
return a+b;
}
double minus(double a, double b)
{
return a-b;
}
double multi(double a, double b)
{
return a*b;
}
double divide(double a, double b)
{
return (b==0?0:a/b);
}
/* 輸入兩個整數,計算其和 */
void inputData(struct compute *px)
{
printf("請輸入表達式:");
scanf("%lf%c%lf", &px->a, &px->op, &px->b);
}
void process(struct compute *px)
{
double (*pf)(double, double);
switch(px->op)
{
case '+': pf=plus; break;
case '-': pf=minus; break;
case '*': pf=multi; break;
case '/': pf=divide; break;
}
px->r = pf(px->a, px->b);
}
void outputData(struct compute x)
{
printf("%.2lf %c %.2lf = %.2lf\n", x.a, x.op, x.b, x.r);
}
/* 隨機生成n個整數,計算其和 */
int randomNum(int low, int high)
{
int t;
t=rand();
while(t<0) t=rand();
return low+t%(high-low+1);
}
void inputBatch(int *pn)
{
do {
printf("請輸入計算次數(1-%d):", MAXSIZE);
scanf("%d", pn);
}while(*pn<1 || *pn>MAXSIZE);
}
void processBatch(struct compute ax[], int n)
{
int i, t;
for(i=0; i<n; ++i)
{
ax[i].a=randomNum(0,100);
ax[i].b=randomNum(0,100);
t=randomNum(0,3);
switch(t)
{
case 0: ax[i].op='+'; break;
case 1: ax[i].op='-'; break;
case 2: ax[i].op='*'; break;
case 3: ax[i].op='/'; break;
}
process(&ax[i]);
}
}
void outputBatch(struct compute ax[], int n)
{
int i;
for(i=0; i<n; ++i)
outputData(ax[i]);
}
/* 從文件讀取表達式,把運算結果寫回文件 */
int inputFile(struct compute ax[], int *pn)
{
FILE *fp;
int i=0;
fp=fopen("示例設計.in","r");
if (!fp)
{
printf("無法打開輸入文件!\n");
return -1;
}
while(!feof(fp))
{
fscanf(fp, "%lf%c%lf", &ax[i].a, &ax[i].op, &ax[i].b);
i++;
}
*pn=i;
fclose(fp);
return 0;
}
int outputFile(struct compute ax[], int n)
{
FILE *fp;
int i=0;
fp=fopen("示例設計.out","w");
if (!fp)
{
printf("無法打開輸出文件!\n");
return -2;
}
for(i=0; i<n; ++i)
{
outputData(ax[i]);
fprintf(fp, "%.2lf %c %.2lf = %.2lf\n", ax[i].a, ax[i].op, ax[i].b, ax[i].r);
}
fclose(fp);
return 0;
}
void showMenu()
{
struct compute xVal, xArr[MAXSIZE];
int i, n;
int in, exitFlag=0;
srand((unsigned)time(NULL)); /* 以當前時間做隨機數種子 */
while(1)
{
system("cls");
printf("***************************************\n");
printf("*************** 歡迎使用 **************\n");
printf("***************************************\n");
printf("請選擇:\n");
printf(" 1----輸入一個表達式,計算結果\n");
printf(" 2----輸入多個表達式,批量計算結果\n");
printf(" 3----從文件讀取數據,結果寫入文件\n");
printf(" 0----結束程序\n");
printf("請輸入(1,2,3,0):");
scanf("%d", &in);
switch(in)
{
case 1:
inputData(&xVal);
process(&xVal);
outputData(xVal);
system("pause"); break;
case 2:
inputBatch(&n);
processBatch(xArr,n);
outputBatch(xArr,n);
system("pause"); break;
case 3:
if (!inputFile(xArr,&n))
{
for(i=0; i<n; ++i) process(&xArr[i]);
outputFile(xArr, n);
}
system("pause"); break;
case 0: exitFlag=1; break;
}
if (exitFlag)
{
printf("***************************************\n");
printf("********* 感謝您的使用,再見!*********\n");
printf("***************************************\n");
break;
}
}
system("pause");
}
main()
{
showMenu();
}
OK,到這裏所有的課程章節都覆蓋到了,剩下的是一些細節調整和邊邊角角的掃尾工作,這纔是一個能夠稱之爲“設計”的程序。
最後,對於“求解兩個整數相加”這樣的問題,我們一共實現了14個函數(主函數不算),再次比較一下最開始5分鐘得到的結果:
#include "stdio.h"
main()
{
int a, b;
printf("請輸入兩個整數:");
scanf("%d%d", &a, &b);
printf("%d+%d=%d\n", a,b,a+b);
}
是不是覺得它弱爆了?
(未完待續)