隨機(jī)生成10個(gè)關(guān)于100以內(nèi)整數(shù)的算術(shù)表達(dá)式,先寫進(jìn)文件中,然后再讀取出來(lái),進(jìn)行計(jì)算,再把表達(dá)式寫回去,代碼如下:
成都創(chuàng)新互聯(lián)公司專注于網(wǎng)站建設(shè)|成都網(wǎng)站維護(hù)公司|優(yōu)化|托管以及網(wǎng)絡(luò)推廣,積累了大量的網(wǎng)站設(shè)計(jì)與制作經(jīng)驗(yàn),為許多企業(yè)提供了網(wǎng)站定制設(shè)計(jì)服務(wù),案例作品覆蓋成都木包裝箱等行業(yè)。能根據(jù)企業(yè)所處的行業(yè)與銷售的產(chǎn)品,結(jié)合品牌形象的塑造,量身制作品質(zhì)網(wǎng)站。#define _CRT_SECURE_NO_WARNINGS
#include#include#include#include#define FILE_PATH "a.txt"
FILE* open(char* str)
{FILE* fp = fopen(FILE_PATH, str);
if (NULL == fp)
{perror("open");
return -1;
}
return fp;
}
void get_data()
{srand(time(NULL));
int a;
int b;
int k;
char operation[] = {'+', '-', '*', '/' };
char buf[32];
FILE* fp = open("w");
for (int i = 0; i< 10; i++)
{a = rand() % 100 + 1;
b = rand() % 100 + 1;
k = rand() % 4;
sprintf(buf, "%d%c%d=\n", a, operation[k], b);
fputs(buf, fp);
}
fclose(fp);
}
char * compute(char *buf)
{int a;
int b;
char k;
int r;
sscanf(buf, "%d%c%d", &a, &k, &b);
switch (k)
{case '+':r = a + b; break;
case '-':r = a - b; break;
case '/':r = a / b; break;
case '*':r = a * b; break;
}
sprintf(buf, "%d%c%d=%d\n", a, k, b, r);
return buf;
}
void write_data()
{char buf[32*10];
int len = 0;
char * p = buf;
FILE* fp = open("r");
while (1)
{p = fgets(p, 32, fp);
if (NULL == p)
break;
p = compute(p);
p += strlen(p); //字符指針后移
}
fclose(fp);
//printf("%s", buf);
fp = open("w");
for (int i = 0; i< strlen(buf); i++) //逐個(gè)字符寫入
{fputc(buf[i], fp);
}
fclose(fp);
}
int main() {get_data();
write_data();
return 0;
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧