本文共 9410 字,大约阅读时间需要 31 分钟。
#includemain(){ printf("hello, world\n");}
warning: unknown escape sequence: '\c'
#includemain(){ float fahr,celsius; float lower,upper,step; lower = 0; upper = 300; step = 20; fahr = lower; printf("fahr celsius\n"); while(fahr<=upper){ celsius = (5.0/9.0)*(fahr-32.0); printf("%3.0f %6.1f\n",fahr,celsius); fahr = fahr+step; }}
#includemain(){ float fahr,celsius; float lower,upper,step; lower = 0; upper = 300; step = 20; celsius = lower; printf("celsius fahr\n"); while(celsius<=upper){ //celsius = (5.0/9.0)*(fahr-32.0); fahr=(9.0/5.0)*celsius+32.0; printf("%3.0f %6.1f\n",celsius,fahr); celsius = celsius+step; }}
#includemain(){ float fahr,celsius; float lower,upper,step; lower = 0; upper = 300; step = 20; fahr = upper; printf("fahr celsius\n"); for(fahr;fahr>=lower;fahr-=step){ celsius = (5.0/9.0)*(fahr-32.0); //fahr=(9.0/5.0)*celsius+32.0; printf("%3.0f %6.1f\n",fahr,celsius); //celsius = celsius+step; }}
#include//the answer is 1main(){ printf("%d\n",getchar()!=EOF);}
#include//the answer is -1main(){ printf("%d\n",EOF);}
#include//press ctrl+Z in windows, ctrl+D in Linuxmain(){ int tmp,emptyCount,tableCount,lineCount; emptyCount=0; tableCount=0; lineCount=0; while(1){ tmp=getchar(); if(tmp==EOF) break; if(tmp==' ') ++emptyCount; if(tmp=='\t') ++tableCount; if(tmp=='\n') ++lineCount; } printf("Empties: %d\nTables: %d\nLines: %d\n",emptyCount,tableCount,lineCount);}
#include//ctrl+Z in windows, ctrl+D in Linux to end the inputmain(void){ int c, blank_flag; blank_flag = 0; while (1){ c = getchar(); if(c==EOF) break; if (c == ' ') { if (!blank_flag) { blank_flag = 1; putchar(c); } } else { blank_flag = 0; putchar(c); } }}
#include//ctrl+Z in windows, ctrl+D in Linux to end the input.main(void){ int c; while (1){ c = getchar(); if(c==EOF) break; if (c == '\t') printf("\\t"); else if (c == '\b') printf("\\b"); else if (c == '\\') printf("\\\\"); else printf("%c", c); }}
行首空格,空行
#include//ctrl+Z in windows, ctrl+D in Linux to end the inputint main(void){ int c, lineflag; lineflag = 0; while (1){ c = getchar(); if(c==EOF) break; if (c == ' ' || c == '\t' || c == '\n') { if (!lineflag) { putchar('\n'); lineflag = 1; } } else { putchar(c); lineflag = 0; } } return 0;}
#include#define MAX_LENGTH 15#define IN_WORD 1#define OUT_WORD 0int main(void){ int c, word_in_flag; int word_length[MAX_LENGTH + 1]; int l; int i, j; unsigned int max_count; for (i = 0; i <= MAX_LENGTH; ++i) word_length[i] = 0; word_in_flag = OUT_WORD; while (1) { c = getchar(); if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { if (word_in_flag == OUT_WORD) { l = 0; word_in_flag = IN_WORD; } ++l; } else { if (word_in_flag == IN_WORD) { if (l <= MAX_LENGTH) ++word_length[l - 1]; else ++word_length[MAX_LENGTH]; word_in_flag = OUT_WORD; } if (c == EOF) break; } }//horizon for (i = 0; i <= MAX_LENGTH; ++i) { if (i != MAX_LENGTH) printf(" %2d | ", i + 1); else printf(" >%2d | ", MAX_LENGTH); for (j = 0; j < word_length[i]; ++j) putchar('+'); putchar('\n'); }//vertical max_count = 0; for (i = 0; i <= MAX_LENGTH; ++i) if (word_length[i] > max_count) max_count = word_length[i]; for (i = 0; i < max_count; ++i) { printf(" %2u | ", max_count - i); for (j = 0; j <= MAX_LENGTH; ++j) if (word_length[j] >= max_count - i) printf(" +"); else printf(" "); printf("\n"); } printf(" ------"); for (i = 0; i <= MAX_LENGTH; ++i) printf("---"); printf("--\n"); printf(" "); for (i = 0; i < MAX_LENGTH;) printf(" %2u", ++i); printf(" >%2u", MAX_LENGTH); printf("\n"); return 0;}
#include#define NUM_CHARS 128#define IN_WORD 1#define OUT_WORD 0//ctrl+Z in windows, ctrl+D in Linux to finish inputint main(void){ int c; int char_frequencies[NUM_CHARS + 1]; int i; for (i = 0; i <= NUM_CHARS; ++i) char_frequencies[i] = 0; while ((c = getchar()) != EOF) ++char_frequencies[c]; printf("\n ASCII | Count\n"); for (i = 0; i <= NUM_CHARS; ++i) if (char_frequencies[i] > 0) printf(" %5d : %5d\n", i, char_frequencies[i]); return 0;}
略
#include#define MAXLINE 1000 /* maximum input line length */int get_line(char line[], int maxline);void copy(char to[], char from[]);/* print the longest input line */int main(void){ int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ max = 0; while ((len = get_line(line, MAXLINE)) > 0) if (len > max) { max = len; copy(longest, line); } if (max > 0) { printf("The longest line is:\n"); printf("%s\n", longest); printf("The length of it is %d.\n", max); } return 0;}/* get_line: read a line into s, return length */int get_line(char s[], int lim){ int c, i, l; for (i = 0, l = 0; (c = getchar()) != EOF && c != '\n'; ++i) { if (i < lim - 1) s[l++] = c; } if (c == '\n') { if (l < lim - 1) s[l++] = c; ++i; } s[l] = '\0'; return i;}/* copy: copy 'from' into 'to'; assume to is big enough */void copy(char to[], char from[]){ int i; i = 0; while ((to[i] = from[i]) != '\0') ++i;}
#include#define MAXLINE 1000#define MAXLENGTH 10int get_line(char s[],int lim){ int c,i,l; for(i=0,l=0;(c=getchar())!=EOF&&c!='\n';++i){ if(i 0) if(len>MAXLENGTH) printf("%s\n",line); return 0;}
#include#define MAXLINE 1000#define MAXLENGTH 10int get_line(char s[],int lim){ int c,i,l; for(i=0,l=0;(c=getchar())!=EOF&&c!='\n';++i){ if(i 0){ if(len==1&&line[0]=='\n') continue; printf("%s\n",line); } return 0;}
#include#define MAXLINE 1000#define MAXLENGTH 10int get_line(char s[],int lim){ int c,i,l; for(i=0,l=0;(c=getchar())!=EOF&&c!='\n';++i){ if(i 0){ reverse(line); printf("%s\n",line); } return 0;}
#include#define MAXLINE 1000#define TAB_WIDTH 4int getchars(char s[],int lim){ int c,i,l; for(i=0,l=0;(c=getchar())!=EOF&&c!='\n';++i) if(i
#include#define MAXLINE 1000#define TAB_WIDTH 8int getchars(char s[],int lim){ int c,i,l; for(i=0,l=0;(c=getchar())!=EOF&&c!='\n';++i) if(i
#include#define MAXLINE 1000#define TAB_WIDTH 8#define LINE_WIDTH 10int getchars(char s[],int lim){ int c,i,l; for(i=0,l=0;(c=getchar())!=EOF&&c!='\n';++i) if(i =linewidth){ s2[l++]='\n'; w=0; i=k; }else{ s2[l++]='\t'; w+=tabwidth; } }else{ s2[l++]=c; if(w+1==linewidth){ s2[l++]='\n'; w=0; }else{ k=i; ++w; } } ++i; } s2[l++]='\0';}int main(){ char s1[MAXLINE]; char s2[MAXLINE]; while(getchars(s1,MAXLINE)==0) ; fold(s1,s2,LINE_WIDTH,TAB_WIDTH); printf("%s\n",s2); return 0;}
#include#define MAXLENGTH 5000//ctrl+Z in windows, ctrl+D in Linux to end the inputint _getline(char s[], int max){ int c, i, l; for (i = 0, l = 0; (c = getchar()) != EOF; ++i) if (i < max - 1) s[l++] = c; s[l] = '\0'; return l;}int main(void){ int len, i; char s[MAXLENGTH]; while ((len = _getline(s, MAXLENGTH)) > 0) { i = 0; while (s[i] != '\0') { if (s[i] == '/' && s[i+1] == '/') { i += 2; while (s[i] != '\n' && s[i] != '\0') ++i; } else if (s[i] == '/' && s[i+1] == '*') { i += 2; while (s[i] != '\0' && s[i+1] != '\0' && (s[i] != '*' || s[i+1] != '/')) ++i; if (s[i] != '\0' && s[i+1] == '\0') ++i; if (s[i] == '*') i += 2; } else if (s[i] == '\"') { putchar('\"'); ++i; while (s[i] != '\0' && (s[i-1] == '\\' || s[i] != '\"')) putchar(s[i++]); } else { putchar(s[i++]); } } } return 0;}
太长不写,括号匹配可以用栈实现。
转载地址:http://jfwai.baihongyu.com/