• 对于表达式比较长的 for 语句和 if 语句


    对于表达式比较长的 for 语句和 if 语句,为了紧凑起见可以适当地去 掉一些空格,如 for (i=0; i<10; i++)和 if ((a<=b) && (c<=d))。

     1 #include <iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<process.h>
     5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     6 using namespace std;
     7 int main(int argc, char** argv) {
     8      //声明变量
     9     int i=0;
    10     char p[100];               // 声明输入缓冲区 
    11     FILE *fp1;                 // 声明文件指针变量
    12 
    13     //以写入方式打开d.dat文件
    14     if ((fp1=fopen("d.dat","w"))==NULL)
    15     {
    16        cout<<"
    Could not open the file."<<endl;
    17        cout<<"Exiting program."<<endl;
    18        exit(1);   //结束程序执行
    19     }
    20 
    21     // 写文件操作 
    22     for (i=1;;i++)  {            //无条件循环
    23         cout<<i<<" string:";
    24         cin>>p;                   //从键盘上输入数据
    25         if (stricmp(p,"end")) {      //如果输入的字符串为end,则结束循环
    26             fputs(p,fp1);         //写入文件操作
    27             fputs("
    ",fp1); 
    28         }
    29         else
    30             break;                //退出循环
    31     }
    32  
    33     fclose(fp1);                   //关闭文件
    34 
    35     // 以读方式打开d.dat文件
    36     if ((fp1=fopen("d.dat","r"))==NULL) 
    37     {
    38        cout<<"
    Could not open the file."<<endl;
    39        cout<<"Exiting program."<<endl;
    40        exit(1);   //结束程序执行
    41     }
    42 
    43     // 循环从文件读取字符,并显示
    44     while (fgets(p,100,fp1)!=NULL)  
    45           cout<<p;        
    46      fclose(fp1); //关闭文件
    47     return 0;
    48 }
  • 相关阅读:
    [PDF]阅读、注释最佳软件
    [CentOS 7]挂载ntfs格式U盘
    如何更改键盘按键---KeyTweak?
    ssh 文件上传、文件目录上传和下载
    centos7安装Anaconda(Anaconda3-2020.02-Linux-x86_64)与基本命令使用
    Ubuntu 下SVN常用操作
    程序员常用docker命令
    numpy&pandas
    Deep Learning with pytorch笔记(第三章)
    pytorch中的ReflectionPad2d
  • 原文地址:https://www.cnblogs.com/borter/p/9413217.html
Copyright © 2020-2023  润新知