zoukankan      html  css  js  c++  java
  • 第二次作业

    基础作业
    请在第一周作业的基础上,继续完成:找出给定的文件中数组的最大值及其对应的最小下标(下标从0开始)。并将最大值和对应的最小下标数值写入文件。
     输入:
    请建立以自己英文名字命名的txt文件,并输入数组元素数值,元素值之间用逗号分隔。
     输出
    在不删除原有文件内容的情况下,将最大值和对应的最小下标数值写入文件。

    1).实验代                                                                                                  导图https://www.edrawsoft.cn/viewer/public/s/6a1fe257016539

    ```c

    第二次实验代码

    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
     FILE *fp;
     int a[10],b,i,c;
     if((fp=fopen("C:\Users\Lenovo\Desktop\text.txt","a+"))==NULL)
     {
      printf("File open error! ");
      exit (0);
     }
     fscanf(fp,"%d,",&i);
     printf("%d ",i);
     for(b=0;b<i;b++){
      fscanf(fp,"%d",&a[b]);
      printf("%d ",a[b]);
      if(b+1==i){
       printf(" ");
      }
     }
     for(c=b=0;b<i-1;b++){
     if(a[c]<a[b+1]){
         c=b+1;
        }
     }
     printf("%d  %d",a[c],c);
     fprintf(fp," %d  %d",a[c],c);
     if(fclose(fp)){
      printf("Can not close the file! ");
      exit(0);
     }
     return 0;
    }#include<stdio.h>
    #include<stdlib.h>
    int main()
    {
     FILE *fp;
     int a[10],b,i,c;
     if((fp=fopen("C:\Users\Lenovo\Desktop\text.txt","a+"))==NULL)
     {
      printf("File open error! ");
      exit (0);
     }
     fscanf(fp,"%d,",&i);
     printf("%d ",i);
     for(b=0;b<i;b++){
      fscanf(fp,"%d",&a[b]);
      printf("%d ",a[b]);
      if(b+1==i){
       printf(" ");
      }
     }
     for(c=b=0;b<i-1;b++){
     if(a[c]<a[b+1]){
         c=b+1;
        }
     }
     printf("%d  %d",a[c],c);
     fprintf(fp," %d  %d",a[c],c);
     if(fclose(fp)){
      printf("Can not close the file! ");
      exit(0);
     }
     return 0;}

     

  • 相关阅读:
    DP大作战—状态压缩dp
    DP大作战—组合背包
    DP大作战——多重背包
    单链表的使用——计算多项式加法
    单链表逆置
    钢条切割问题
    哈夫曼树及解码
    双“11”的抉择
    矩阵链相乘助教版代码
    abs()函数的返回值问题
  • 原文地址:https://www.cnblogs.com/ydlBLOG/p/10501992.html
Copyright © 2011-2022 走看看