C語言程式設計第一章部分習題
阿新 • • 發佈:2019-01-11
作為一個程式設計師一直不會C程式設計語言,覺得缺點什麼,所以最近在學習C ,會定期分享學習心得,最近剛看完第一章,部分習題供大家參考,歡迎指正。
提取碼: nbnb
#include "Temperature.h" Temperature::Temperature(void) { } Temperature::~Temperature(void) { } void statisticsEmptyLineTablesNum(){ int emptyChar = 0; int changeLineChar = 0; int tableChar = 0; int c; bool slash = false; while ((c = getchar())!=EOF){ if (c == 10){ printf("emptyChar %d changeLineChar %d tableChar %d", emptyChar, changeLineChar, tableChar); return; } else if(c == ' '){ emptyChar ++; }else if(slash == 1) { switch(c){ case 'n': changeLineChar ++; break; case 't': tableChar ++; break; } } slash = c == '\\'; } } /* *只顯示一個空格 */ void practice1_9_only_one_empty(){ printf("輸入的多個空格字元只會保留一個\n"); bool lastEmpty = false; int c; while((c = getchar()) != EOF){ bool charIsEmpty = c == ' '; if(!lastEmpty || !charIsEmpty){ putchar(c); } lastEmpty = charIsEmpty; } } void practice1_10(){ printf("輸入的多個製表符用\\t替換\n"); bool lastEmpty = false; char s; int c; while((c = getchar()) != EOF){ char cha; if(c == '\t'){ putchar('\\'); c = 't'; } putchar(c); } } /* * 一行列印一個單詞 */ void practice1_12(){ printf("輸入的多個單詞將逐行列印且一行只會有一個單詞\n"); bool isLastEnd = false; // 最後一個字元是否是結束字元 int lastc; while ((lastc = getchar()) != EOF) { if (lastc == ' ' || lastc == '\n' || lastc =='\t'){ if (!isLastEnd) printf("\n"); isLastEnd = true; } else { isLastEnd = false; putchar(lastc); } } printf("\n End print"); } void printVertical(bool isPrintFromat, int countPosition, int countArray[]){ if(isPrintFromat) printf("\n列印格式\n"); else { printf("\n不列印格式\n"); } int maxCount = 0; for (int i = 0; i < countPosition; i ++) { if (maxCount < countArray[i]) maxCount = countArray[i]; } for (int i = 0; i < countPosition; i ++) { printf("%2d", countPosition - i); int showNum = countArray[i]; // 輸出白色背景個數 int showStart = (maxCount - showNum) / 2; // 左邊輸出的空格 for (int j = 0; j < maxCount; j ++) { if (j > showStart -1 && showNum > 0){ printf("C"); showNum --; } else if(showNum > 0 && isPrintFromat) { printf(" "); } else if( showNum == 0) { break; } } printf("\n"); } printf(" "); for (int i = 0; i < maxCount; i ++) { printf("%d", i + 1); } } void practice1_13() { printf("start printf\n"); bool isLastEnd = false; int count, countPosition; int lastc; int countArray[10]; count = countPosition = 0; for (int i =0; i < 10; i ++) countArray[i] = 0; while ((lastc = getchar()) != EOF && lastc != 'E') { if (lastc == ' ' || lastc == '\n' || lastc =='\t') { if (!isLastEnd) { countArray[countPosition] = count; printf("\n%d", count); count = 0; countPosition ++; } isLastEnd = true; } else { isLastEnd = false; // putchar(lastc); count ++; } } printf("\n"); printVertical(false, countPosition, countArray); printVertical(true, countPosition, countArray); printf("end printf"); } void practice1_17(); void practice1_18(); void practice1_19(); void reverse(char string[], int len); int getline(char string[]); void main() #define UPPER 300 #define LOWER 0 #define STEP 20 #define MAXLINE 500 { printf("start print\n"); practice1_19(); printf("\nend print"); } void practice1_17(){ char contents[MAXLINE]; int len = 0; while((len = getline(contents)) > 80){ printf("\n支付串長度%4d", len); printf("\n輸入的字串%s", contents); printf("\n%s\t%4d", contents, len); } } void practice1_18(){ char contents[MAXLINE]; int len = 0; len = getline(contents); len --; printf("\n輸入的字元%s\t數量%4d", contents, len); char lastCha; int emptyChar, tableChar; emptyChar = tableChar = 0; for (; len > 0; len --) { lastCha = contents[len-1]; if(lastCha == ' '){ emptyChar ++; contents[len] = '\0'; }else if (lastCha == '\t') { tableChar ++; contents[len] = '\0'; }else { break; } } printf("\n末尾空格數量%3d\t末尾製表符數量", emptyChar, tableChar); printf("\n過濾後的字元%s\t數量%4d", contents, len); } void practice1_19(){ int c; char string[MAXLINE]; int index = 0; while((c = getchar()) != EOF && c != '\n'){ string[index] = c; index ++; } index --; reverse(string, index); } void reverse(char string[], int len) { for (; len >= 0; len --) printf("%c", string[len]); } int getline(char string[]){ int c, i; for (i = 0; (c = getchar()) != EOF && c != '\n'; i ++) string[i] = c; if (c == '\n'){ string[i] = c; i++; } string[i] = '\0'; return i; }