建檔案111.txt,資料分別是12 23 44 68 95 68 47 95 10 25
撰寫程式,求出該資料檔案中,在第3個-第9個資料之間,能被2整除的最大值。
uj5u.com熱心網友回復:
#include <stdio.h>#include <string.h>
#define MAXLINE 1024
int main(){
int num[10];
int index = 0;
char str[1024];
FILE *fp = fopen("111.txt","r");
if(fp == NULL){
printf("cannot open file\n");
return 0;
}
fgets(str,MAXLINE,fp);
char *arr = strtok(str," ");
while (arr)
{
num[index] = atoi(arr);
printf("%d ",num[index]);
printf("%s\n",arr);
arr = strtok(NULL," ");
index++;
}
int max = 0;
for(int i=2;i<9;i++){
if(num[i]%2==0){
if(num[i]>max){
max = num[i];
}
}
}
printf("在第3個~第9個資料之間,能被2整除的最大值為:%d\n",max);
return 0;
}
uj5u.com熱心網友回復:
這個要分兩步走。一步是檔案操作。一步才是資料操作。不能心急哦。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/66147.html
標籤:新手樂園
上一篇:用下面的 scanf 函式輸入資料,使 a=10,b=12,c1='B',c2='b',x=2.6,y=-3.89,z=6.7,考慮在鍵盤上如何輸入資料?
