Input and output formats summary

C language

  • scanfIn addition to %coutside input to other formats is a symbol of white space (spaces, TAB, Enter) marks the end
  • Therefore, scanfunless %cthe character is read by a space, otherwise the space will be skipped

  • %c You can receive a space, tab, carriage return

  • Use string %sread in, is a space and line feed as read into the end marker

  • Use getchar, putcharinput and output of a single character

    • getcharCan recognize line breaks, so when the knockout round if the latter have getcharwill be put into your account
  • 2#3This input format, do not forget the scanfinside to keep this format

  • getsAn input line of the string, putsto the output string, and a line feed plus

    • If getsthere's still a scanfOr cin, remember to use getcharabsorption
  • Utility output format

    //%md  超过或等于m位保持原样,不足m位,高位用空格补齐
    printf("%5d");
    //%0md  超过或等于m位保持原样,不足m位,高位用0补齐!!!!!!非常好用
    printf("%05d");
    //%.mf  保留m位小数输出
    printf("%.5f");

    C++

  • Control output bits

#include<iomanip> //控制时要加入这个头文件
cout << setiosflags(ios::fixed)<<setprecision(2)<<123.4567<<endl;//控制输出位数
  • stringOnly with cin,coutinput and output
  • 2#3 This input format may be a char additional variable, cin >> ch absorb it
  • getline Whole line for inputting a character string
    • If getlinethere's still a scanfOr cin, remember to use getcharabsorption
//string类型
string str;
getline(cin, str);
//char *类型
char s[20];
cin.getline(s, 20);
  • Used map, the character string to integer mapping muststring

Case

//输入格式:字符串1 字符串2 字符串3 ...数量未知
//输入格式:数字1 数字2 数字3 ...
//这种限于后面没有其他输入了
//这也是输入多组数据,组数未知的方式
while(scanf("%s")!=EOF){
    
}
/*
输入格式:
123 [数字]
aa bb cc [是一个整体的字符串]
字符串1 字符串2 字符串3 ... [组数未知的多组字符串]
1998 [数字]
*/
//c++
cin>>id;
getchar();  //注意!!
getline(cin,str1);
while(cin>>str2){
    ...
    char ch = getchar();
    if(ch == '\n') break; //注意!!
}
cin>>num;
//c语言
scanf("%d",&n);
getchar(); //注意!!
gets(str);
while(scanf("%s",str2)!=EOF){
    ...
    char ch = getchar();
    if(ch == '\n') break;
}
scanf("%d",&num);

Guess you like

Origin www.cnblogs.com/doragd/p/11315191.html