1、
#include <stdio.h> #include <ctype.h> int main(void) { char ch; int n_space = 0, n_lines = 0, n_other = 0; while((ch = getchar()) != '#') { if(ch == ' ') n_space++; else if(ch == ' ') n_lines++; else n_other++; } printf("n_space: %d; n_lines: %d; n_other: %d ", n_space, n_lines, n_other); return 0; }
#include <stdio.h> int main(void) { char ch; int blank = 0; int endline = 0; int others = 0; printf("please input chars(# to quit): "); while((ch = getchar()) != '#') { if(ch == ' ') blank++; else if(ch == ' ') endline++; else others++; } printf("blank: %d; endline: %d; other: %d. ", blank, endline, others); return 0; }
2、
#include <stdio.h> int main(void) { char ch; int n = 1; while((ch = getchar()) != '#') { printf("%c→%d ", ch, ch); if(n++ % 8 == 0) printf(" "); } return 0; }
#include <stdio.h> int main(void) { char ch; int counter = 0; printf("please input chars(# for exit): "); while((ch = getchar()) != '#') { if(counter++ % 8 == 0) printf(" "); if(ch == ' ') printf("\n-%03d ", ch); else if(ch == ' ') printf("\t-%03d ", ch); else printf("%c-%03d ", ch, ch); } return 0; }
3、
#include <stdio.h> int main(void) { int num; int n_even = 0; int sum_even = 0; int odd; int n_odd = 0; int sum_odd = 0; do { printf("please input the num(0 to quit): "); scanf("%d", &num); if(num % 2 == 0 && num != 0) { n_even++; sum_even += num; } if(num % 2 == 1) { n_odd++; sum_odd += num; } } while(num != 0); if(n_even > 0) { printf("n_even: %d. ", n_even); printf("average_even: %.2f. ", (double)sum_even / n_even); } else printf("no even num! "); printf(" "); if(n_odd > 0) { printf("n_odd: %d. ", n_odd); printf("average_odd: %.2f. ", (double)sum_odd / n_odd); } else printf("no odd num!"); return 0; }
#include <stdio.h> int main(void) { int num; int n_even = 0; int sum_even = 0; int n_odd = 0; int sum_odd = 0; printf("please input the num(0 to quit): "); while(scanf("%d", &num) == 1) { if(num == 0) break; if(num % 2 == 0) { n_even++; sum_even += num; } else { n_odd++; sum_odd += num; } printf("please input the num(0 to quit): "); } putchar(' '); if(n_even > 0) { printf("n_even: %d; average_even: %.2f. ", n_even, (double)sum_even / n_even); } else printf("no even num inputed!. "); if(n_odd > 0) { printf("n_odd: %d; average_odd: %.2f. ", n_odd, (double)sum_odd / n_odd); } else printf("no odd num inputed. "); return 0; }
4、
#include <stdio.h> int main(void) { int counter = 0; char ch; printf("please input chars(# for exit): "); while((ch = getchar()) != '#') { if(ch == '!') { printf("!!"); counter++; } else if(ch == '.') { printf("!"); counter++; } else { printf("%c", ch); } } printf(" total times: %d. ", counter); return 0; }
#include <stdio.h> int main(void) { char ch; int counter = 0; printf("please input chars(# for exit): "); while((ch = getchar()) != '#') { if(ch == '!') { printf("!!"); counter++; } else if(ch == '.') { printf("!"); counter++; } else { printf("%c", ch); } } printf(" replace times: %d. ", counter); return 0; }
5、
#include <stdio.h> int main(void) { char ch; int counter = 0; printf("please input chars(# for exit): "); while((ch = getchar()) != '#') { switch(ch) { case '!': printf("!!"); counter++; break; case '.': printf("!"); counter++; break; default: printf("%c", ch); break; } } printf(" replace times: %d. ", counter); return 0; }
6、
#include <stdio.h> int main(void) { char ch; char pre; int ei_count = 0; printf("please input the charaters(# to quit): "); while((ch = getchar()) != '#') { if(pre == 'e' && ch == 'i') { ei_count++; } pre = ch; } printf("ei_count: %d. ", ei_count); return 0; }
#include <stdio.h> #include <stdbool.h> int main(void) { char ch; int ei_count = 0; bool mark1; while((ch = getchar()) != '#') { if(ch == 'e') mark1 = true; if(ch != 'e' && ch != 'i') mark1 = false; if(mark1 && ch == 'i') { ei_count++; } } printf("ei_count: %d. ", ei_count); return 0; }
#include <stdio.h> int main(void) { int counter = 0; int halfpair = 0; char ch; printf("please input chars(# for exit): "); while((ch = getchar()) != '#') { switch(ch) { case 'e': halfpair = 1; break; case 'i': if(halfpair == 1) { counter++; halfpair = 0; }; break; default: halfpair = 0; } } printf(" n_ei: %d ", counter); return 0; }
7、
#include <stdio.h> int main(void) { double sum, tax, pure_in; float hour; printf("please input the hours this week(q to quit): "); while(scanf("%f", &hour)) { if(hour <= 40) { sum = hour * 10.00; } else { sum = 40 * 10.00 + (hour - 40) * 1.5; } if(sum <= 300) { tax = sum * 0.15; } else if(sum > 300 && sum <= 450) { tax = 300 * 0.15 + (sum - 300) * 0.2; } else { tax = 300 * 0.15 + 150 * 0.2 + (sum - 450) * 0.25; } pure_in = sum - tax; printf("sum: %.2f. ", sum); printf("tax: %.2f. ", tax); printf("pure_in: %.2f. ", pure_in); printf(" please input the hours this week(q to quit): "); } return 0; }
#include <stdio.h> #define BASE_SALARY 10 #define EXTRA_SALARY 1.5 #define BASE_TAX 0.15 #define EXTRA_TAX 0.2 #define EXCEED_TAX 0.25 int main(void) { float salary, tax, taxed_salary; float hours; printf("please enter the working hours of this week(q to quit): "); while(scanf("%f", &hours)) { if(hours < 40) { salary = hours * BASE_SALARY; } else { hours = 40 + (hours - 40) * EXTRA_SALARY; salary = hours * BASE_SALARY; } if(salary <= 300) { tax = salary * BASE_TAX; } else if(salary > 300 && salary <= 450) { tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX; } else { tax = 300 * BASE_TAX + 150 * EXTRA_TAX + (salary - 450) * EXCEED_TAX; } taxed_salary = salary - tax; printf("salary: %.2f. ", salary); printf("tax : %.2f. ", tax); printf("taxed_salary: %.2f. ", taxed_salary); printf("please enter the working hours of this week(q to quit): "); } return 0; }
8、
#include <stdio.h> #define EXTRA_SALARY 1.5 #define BASE_TAX 0.15 #define EXTRA_TAX 0.2 #define EXCEED_TAX 0.25 int main(void) { float salary, tax, taxed_salary; float hours; int tab; float BASE_SALARY; printf("Enter the number corresponding to the desired pay rate of action: "); printf("1) $8.75/hr 2) $9.33/hr 3) $10.00/hr 4) $11.20/hr 5) quit"); printf(" your choice(1-5): "); while(scanf("%d", &tab)) { if(tab == 5) break; else if(tab != 1 && tab != 2 && tab != 3 && tab != 4) { printf("input error, the range is (1-5): "); continue; } else { switch(tab) { case 1: BASE_SALARY = 8.75; break; case 2: BASE_SALARY = 9.33; break; case 3: BASE_SALARY = 10.00; break; case 4: BASE_SALARY = 11.20; break; } } printf("please input the working hours of this week: "); scanf("%f", &hours); if(hours < 40) { salary = hours * BASE_SALARY; } else { hours = hours + (hours - 40) * 1.5; salary = hours * BASE_SALARY; } if(salary <= 300) { tax = salary * 0.15; } else if(salary > 300 && salary <= 450) { tax = 300 * 0.15 + (salary - 300) * 0.2; } else { tax = 300 * 0.15 + 150 * 0.2 + (salary - 450) * 0.25; } taxed_salary = salary - tax; printf("salary: %.2f. ", salary); printf("tax: %.2f. ", tax); printf("taxed_salary: %.2f. ", taxed_salary); printf("Enter the number corresponding to the desired pay rate of action: "); printf("1) $8.75/hr 2) $9.33/hr 3) $10.00/hr 4) $11.20/hr 5) quit"); printf(" your choice(1-5): "); } return 0; }
#include <stdio.h> #define EXTRA_HOUR 1.5 #define BASE_TAX 0.15 #define EXTRA_TAX 0.2 #define EXCEED_TAX 0.25 void show_menu(void); void calc_salary(float base_salary, float hours); int main(void) { float hours = 0; char selected; do { show_menu(); scanf("%c", &selected); switch(selected) { case '1': printf("hello, you selected $8.75/hr. enter the work hours: "); scanf("%f", &hours); calc_salary(8.75, hours); break; case '2': printf("hello, you selected $9.33/hr. enter the work hours: "); scanf("%f", &hours); calc_salary(9.33, hours); break; case '3': printf("hello, you selected $10.00/hr. enter the work hours: "); scanf("%f", &hours); calc_salary(10.00, hours); break; case '4': printf("hello, you selected $11.2/hr. enter the work hours: "); scanf("%f", &hours); calc_salary(11.2, hours); break; case '5': break; default: printf("error selected! please retry! "); break; } while(getchar() != ' ') continue; } while(selected != '5'); return 0; } void show_menu(void) { printf("Enter the number corresponding to the desired pay rate or action: "); printf("1) $8.75/hr 2) $9.33/hr "); printf("3) $10.00/hr 4) $11.20/hr "); printf("5) quit "); } void calc_salary(float base_salary, float hours) { float salary, tax, taxed_salary; if(hours < 40) { salary = hours * base_salary; } else { hours = hours + (hours - 40) * EXTRA_HOUR; salary = hours * base_salary; } if(salary <= 300) { tax = salary * BASE_TAX; } else if(salary > 300 && salary <= 450) { tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX; } else { tax = 300 * BASE_TAX + 150 * EXTRA_TAX + (salary - 450) * EXCEED_TAX; } taxed_salary = salary - tax; printf("salary: %.2f. ", salary); printf("tax : %.2f. ", tax); printf("taxed_salary: %.2f. ", taxed_salary); }
#include <stdio.h> #define OVER_WORK 1.5 #define BASE_TAX 0.15 #define EXTRA_TAX 0.2 #define EXCEED_TAX 0.25 int get_num(void); void calculat_salary(float time, float price); int main(void) { int num; float work_time; float price; num = get_num(); while(num != 5) { printf("please input your work time a week: "); scanf("%f", &work_time); switch(num) { case 1: price = 8.75; calculat_salary( work_time, price); break; case 2: price = 9.33; calculat_salary( work_time, price); break; case 3: price = 10.00; calculat_salary( work_time, price); break; case 4: price = 11.20; calculat_salary( work_time, price); break; default: printf("program error. "); break; } putchar(' '); num = get_num(); } } int get_num(void) { int num; printf("1) $8.75/hr 2) $9.33/hr "); printf("3) $10.00/hr 4) $11.20/hr "); printf("5) quit/ "); printf("please input your choice: "); while(scanf("%d", &num) != 1 || num < 1 || num > 6) { printf(" you can only input 1 - 5. "); printf("1) $8.75/hr 2) $9.33/hr "); printf("3) $10.00/hr 4) $11.20/hr "); printf("5) quit/ "); printf("please input again: "); while(getchar() != ' ') continue; } return num; } void calculat_salary(float time, float price) { float salary_sum; float tax; float salary_taxed; if(time <= 40) { salary_sum = time * price; } else { time = 40 + (time - 40) * OVER_WORK; salary_sum = time * price; } if(salary_sum <= 300) { tax = salary_sum * BASE_TAX; } else if(salary_sum > 300 && salary_sum <= 450) { tax = 300 * BASE_TAX + (salary_sum - 300) * EXTRA_TAX; } else { tax = 300 * BASE_TAX + 150 * EXTRA_TAX + (salary_sum - 450) * EXCEED_TAX; } salary_taxed = salary_sum - tax; printf("salary_sum: %.2f. ", salary_sum); printf("tax: %.2f. ", tax); printf("salary_taxed: %.2f ", salary_taxed); }
9、
#include <stdio.h> int main(void) { int num; printf("please input an positive num(q to quit): "); while(scanf("%d", &num) && num > 0) { int i, j; for(i = 2; i < num; i++) { int k = 0; for(j = 2; j < num; j++) { if(i % j == 0) k++; } if(k <= 1) printf("su shu: %d. ", i); } printf(" please input an positive num(q to quit): "); } return 0; }
#include <stdio.h> int main(void) { int num; printf("please input an positive num(q to quit): "); while(scanf("%d", &num)) { if(num < 2) { printf("input error, the range is >= 2, retry: "); continue; } else { int i, j; for(i = 2; i < num; i++) { int k = 0; for(j = 1; j * j <= i; j++) // ??? { if(i % j == 0) k++; } if(k == 1 ) printf("su shu: %d. ", i); } printf(" please input an positive num(q to quit): "); } } return 0; }
#include <stdio.h> int main(void) { int datum; do // 出口型循环控制 { printf("Enter a number(0 to quit): "); scanf("%d", &datum); if(datum < 2) { if(datum == 0) break; // datum = 0, 退出循环 printf("%d is out of range, retry. ", datum); continue; // 其他小于2的情况,跳出该次迭代, 小于2的数不存在素数(质数) } printf("you input %d, so the prime from %d to 2 is: ", datum, datum); int i, j; for(i = datum; i > 1; i--) //外层循环从测定数递减到2 { int is_prime = 1; // 首先设定素数的标记, for(j = 2; j <= i / 2; j++) { if(i % j == 0) // i % j == 0意味着 i除了1和本身外还有其他约数,也就是说i不是素数。 { is_prime = 0; // 检测到i为素数时,修改标记 break; // 此处break终止内层迭代,一旦检测到i有1和本身外的第一个约数,立即终止i的判断 } } if(is_prime == 1) // 检测标记是否发生变化,也就是说同过一次迭代,素数标记is_prime没有发生变化,该轮迭代中的i是素数 { printf("%d ", i); } } printf(" "); // 将输出的所有素数换行 } while(datum != 0); return 0; }
#include <stdio.h> int main(void) { int num; printf("please input an num(q to quit): "); while(scanf("%d", &num)) { if(num < 2) { printf("input error, the range is >= 2, retry: "); continue; } int i, j; for(i = 2; i < num; i++) { int marker = 0; for(j = 2; j <= i / 2; j++) // 2和3明显是素数 ,实际的循环是从 i = 4 开始进行的 { if(i % j == 0) { marker = 1; break; } } if(marker == 0) { printf("prime: %d. ", i); } } printf("please input an num(q to quit): "); } return 0; }
10、
#include <stdio.h> #define BASE1 17850 #define BASE2 23900 #define BASE3 29750 #define BASE4 14875 #define TAXRATE1 0.15 #define TAXRATE2 0.28 int main(void) { int category; printf("please choose category according the following instruction: "); printf("1) bachelor; 2) hoster; "); printf("3) marry_share; 4) marry_dovorce "); printf("input your choice(q to quit): "); while(scanf("%d", &category)) { if(category < 1 || category > 4) { printf("input error, input 1 - 4(q to quit): "); continue; } float salary, tax; printf("please input you salary: "); scanf("%f", &salary); switch(category) { case 1: tax = salary > BASE1 ? BASE1 * TAXRATE1 + (salary - BASE1) * TAXRATE2 : salary * TAXRATE1; break; case 2: tax = salary > BASE2 ? BASE2 * TAXRATE1 + (salary - BASE2) * TAXRATE2 : salary * TAXRATE1; break; case 3: tax = salary > BASE3 ? BASE3 * TAXRATE1 + (salary - BASE3) * TAXRATE2 : salary * TAXRATE1; break; case 4: tax = salary > BASE4 ? BASE4 * TAXRATE1 + (salary - BASE4) * TAXRATE2 : salary * TAXRATE1; break; } printf("your tax: %.2f. ", tax); printf("please choose category according the following instruction: "); printf("1) bachelor; 2) hoster; "); printf("3) marry_share; 4) marry_dovorce "); printf("input your choice(q to quit): "); } return 0; }
#include <stdio.h> #define SINGLE 17850 #define HOLDER 23900 #define MARRY 29750 #define DIVORCE 14875 #define BASE_TAX 0.15 #define EXTRA_TAX 0.28 int main(void) { char type; float salary; float tax, salary_taxed; do { printf("please select tax type. ther are four types: "); printf("1)single 2)holder 3)marry 4)divorced 5)quit:"); scanf("%c", &type); switch(type) { case '1': printf("enter your salary: "); scanf("%f", &salary); if(salary <= SINGLE) { tax = salary * BASE_TAX; salary_taxed = salary - tax; } else { tax = SINGLE * BASE_TAX + (salary - SINGLE) * EXTRA_TAX; salary_taxed = salary - tax; } printf("hi, your salary is %.2f, tax is %.2f, salary_taxed is %.2f. ", salary, tax, salary_taxed); break; case '2': printf("enter you salary: "); scanf("%f", &salary); if(salary <= HOLDER) { tax = salary * BASE_TAX; salary_taxed = salary - tax; } else { tax = HOLDER * BASE_TAX + (salary - HOLDER) * EXTRA_TAX; salary_taxed = salary - tax; } printf("hi, your salary is %.2f, tax is %.2f, salary_taxed is %.2f. ", salary, tax, salary_taxed); break; case '3': printf("enter your salary: "); scanf("%f", &salary); if(salary <= MARRY) { tax = salary * BASE_TAX; salary_taxed = salary - tax; } else { tax = MARRY * BASE_TAX + (salary - MARRY) * EXTRA_TAX; salary_taxed = salary - tax; } printf("hi, your salary is %.2f, tax is %.2f, salary_taxed is %.2f. ", salary, tax, salary_taxed); break; case '4': printf("enter your salary: "); scanf("%f", &salary); if(salary <= DIVORCE) { tax = salary * BASE_TAX; salary_taxed = salary - tax; } else { tax = DIVORCE * BASE_TAX + (salary - DIVORCE) * EXTRA_TAX; salary_taxed = salary - tax; } printf("hi, your salary is %.2f, tax is %.2f, salary_taxed is %.2f. ", salary, tax, salary_taxed); break; case '5': break; default: printf("wrong type, please retry. "); } while(getchar() != ' ') continue; } while(type != '5'); return 0; }
11、
#include <stdio.h> #define PRICE_ARTI 2.05 #define PRICE_BEET 1.15 #define PRICE_CARROT 1.09 #define DISCOUNT 0.05 void show_menu(void); float get_weight(void); int main(void) { float w_arti = 0; float w_beet = 0; float w_carrot = 0; char selected; float weight, amount, rebate, freight, total; do { show_menu(); scanf("%c", &selected); switch(selected) { case 'a': w_arti += get_weight(); break; case 'b': w_beet += get_weight(); break; case 'c': w_carrot += get_weight(); break; case 'q': break; default: printf("error input, retry! "); break; } while(getchar() != ' ') continue; }while(selected != 'q'); amount = w_arti * PRICE_ARTI + w_beet * PRICE_BEET + w_carrot * PRICE_CARROT; weight = w_arti + w_beet + w_carrot; if(amount >= 100) rebate = amount * DISCOUNT; else rebate = 0; if(weight <= 5) freight = 6.5; else if(weight > 5 && weight <= 20) freight = 14; else freight = 14 + (weight - 20) * 0.5; total = amount + freight - rebate; printf(" The price of vegetable: artichoke %g$/pound, beet %g$/pound, carrot %g$/pound. ", PRICE_ARTI, PRICE_BEET, PRICE_CARROT); printf("you order %g pound artichoke, %g pound beet, %g pound carrot. ", w_arti, w_beet, w_carrot); printf("you total order %g pounds, discount %g$, amount %g$, freight %g$, total %g$. ", weight, rebate, amount, freight, total); printf("Done "); return 0; } void show_menu(void) { printf(" ************************************************************************ "); printf("Enter the char corresponding to the desired vegetable. "); printf("a) artichoke b) beet "); printf("c) carrot d) quit & checkout "); printf("please input the vegetable you want to buy(a, b, c or q for quit): "); } float get_weight(void) { float weight; printf("please input how many pounds you buy: "); scanf("%f", &weight); printf("ok, add %g pound to cart. ", weight); return weight; }
#include <stdio.h> #define ARTI_PRICE 2.05 #define BEET_PRICE 1.15 #define CARROT_PRICE 1.09 #define DISCOUNT 0.05 int main(void) { char selected; float weight; float artisum = 0; float beetsum = 0; float carrotsum = 0; float totalweight; float vegeprice; float rebate; float freight; float finalprice; do { printf("please choose (a, b or c): "); printf("a) arti b) beet c) carrot q) quit and check: "); scanf("%c", &selected); switch(selected) { case 'a': while(getchar() != ' ') continue; printf("input the weight for arti: "); scanf("%f", &weight); artisum += weight; printf("add %.2f pound arti. ", weight); break; case 'b': while(getchar() != ' ') continue; printf("input the weight for beet: "); scanf("%f", &weight); beetsum += weight; printf("add %.2f pound beet. ", weight); break; case 'c': while(getchar() != ' ') continue; printf("input the weight for carrot: "); scanf("%f", &weight); carrotsum += weight; printf("add %.2f pound carrot. ", weight); break; case 'q': break; default: printf("input error, choose a, b, c or q, retry: "); } while(getchar() != ' ') continue; } while(selected != 'q'); if(artisum == 0 && beetsum == 0 && carrotsum == 0 ) { printf(" you choose nothing! "); } else { printf(" arti: %.2f$/hr; beet: %.2f$/hr; carrot: %.2f$/hr ",ARTI_PRICE,BEET_PRICE,CARROT_PRICE); totalweight = artisum + beetsum + carrotsum; vegeprice = artisum * ARTI_PRICE + beetsum * BEET_PRICE + carrotsum * CARROT_PRICE; if(vegeprice >= 100) { rebate = vegeprice * DISCOUNT; } else { rebate = 0; } if(totalweight <= 5) { freight = 6.5; } else if(totalweight > 5 && totalweight <= 20) { freight = 14; } else { freight = 14 + (totalweight - 20) * 0.5; } finalprice = vegeprice + freight - rebate; if(artisum != 0) printf("arti weight: %10.2f. ", artisum); if(beetsum != 0) printf("beet weight: %10.2f. ", beetsum); if(carrotsum != 0) printf("carrot weitht: %10.2f. ", carrotsum); printf("total weight: %10.2f. ", totalweight); printf("vegetable price: %10.2f. ", vegeprice); printf("rebate: %10.2f. ", rebate); printf("freight: %10.2f. ", freight); printf("finalprice: %10.2f ", finalprice); } return 0; }