11.29 Lab7
introduction-to-computer-science-laboratory-7.pdf
-
Finish the first 4 exercises present in the 7th tutorial.
-
Write a C program that takes a year from standard input and determines if is leap or not. A year is leap when is a multiple of 4, except for years divisible by 100 but not by 400.
#include <stdio.h> int main() { int year; scanf("%d", &year); if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) printf("Yes, this is a leap!!!"); else printf("No, this is not a leap!!!"); return 0; }
-
Write a C program that given 2 dates calculates how many days are between them.
#include <stdio.h> //determine how many days do the month have int jiyue(int month) { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) return 1; else if (month == 4 || month == 6 || month == 9 || month == 11) return 0; else return 2; } //determine whether a year is a leap year or not int jinian(int year) { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return 1; else return 0; } int main() { int year1, month1, day1, year2, month2, day2; int interval = 0; //the days between two dates printf("Please enter two dates, the form is: \"xxxx.xx.xx-xxxx.xx.xx\"\n"); scanf("%d.%d.%d-%d.%d.%d", &year1, &month1, &day1, &year2, &month2, &day2); // Calculate the first year remaining days for (int i = 12; i >= month1; i--) { if (jiyue(i) == 1) interval += 31; else if (jiyue(i) == 0) interval += 30; else { if (jinian(year1) == 1) interval += 29; else interval += 28; } } // Calculate the last year remaining days for (int j = 1; j < month2; j++) { if (jiyue(j) == 1) interval += 31; else if (jiyue(j) == 0) interval += 30; else { if (jinian(year2) == 1) interval += 29; else interval += 28; } } // Calculate the days of interval years for (int k = year1 + 1; k <= year2 - 1; k++) { if (jinian(k) == 1) interval += 366; else interval += 365; } if (year1 != year2) printf("%d", interval - day1 + day2); else printf("%d", interval - day1 + day2 - (jinian(year1) ? 366 : 365)); return 0; }
-
Write a C program that receives as an input a sequence of characters and counts the vowels letters and the consonant letters in said sequence.
#include <stdio.h> int determine(char cha) { if(cha == 'a' || cha == 'e' || cha == 'i' || cha == 'o' || cha == 'u' ) return 1; } int main() { char ch; int sumv = 0; int sumc = 0; while(ch != EOF) { ch = getchar(); if(ch >= 'A' && ch <= 'z') { if(determine(ch) == 1) { sumv += 1; } else { sumc += 1; } } } printf("the vowels letters: %d\nthe consonant letters: %d\n", sumv, sumc); return 0; }
-
Write a C program that takes as input a sequence of characters and highlights in bold all the words that start with a vowel.
#include<stdio.h> #define red "\e[31m" #define off "\e[39m" //determine whethere it is a vowel int determine(char cha) { if(cha == 'a' || cha == 'e' || cha == 'i' || cha == 'o' || cha == 'u' ) return 1; } int main() { char ch; char ch_old = ' '; ch = getchar(); while(ch != EOF) { if(ch != ' ' && ch_old == ' ') { if(determine(ch) == 1) printf("%s%c", red, ch); else printf("%s%c", off, ch); } else putchar(ch); ch_old = ch; ch = getchar(); } printf("%s", off); return 0; }
-
Write a program that takes as input a sequence of integer numbers and prints the third greatest number.
#include<stdio.h> int main() { int max, min, mid, input, max_temp, min_temp, mid_temp; printf("Please enter a sequence of numbers and enter non-number to end\n"); scanf("%d", &input); min = -2147483648; mid = input; max = +2147483648; while(scanf("%d", &input) == 1) { min_temp = min; max_temp = max; mid_temp = mid; if(input > max) { max = input; mid = max_temp; min = mid_temp; } else if(input > min && input < max) { max = max; mid = input; min = mid_temp; } } printf("The third greatest number is %d", min); }
-
Write a C program that, given a sequence of integer numbers determines if the sequence is ordered in ascending order or not.
#include<stdio.h> int main() { int new_number, old_number; int flag = 1; scanf("%d", &new_number); old_number = new_number; while(flag == 1 && scanf("%d", &new_number) == 1 ) { if(new_number < old_number) { printf("No, this is not a ascending order!!!"); flag = 0; } old_number = new_number; } return 0; }
-
Write a C program that takes as input a number k from 0 to 100 and a sequence of characters. Then prints the sequence with all characters shifted k positions forwards in the ASCII table. e.g. for the sequence "Hello" and the key 3, the result should be "Khoor".
#include<stdio.h> int main() { int k; int ch; printf("Please enter a number k to shift k positions forwards in the ASCII table\n"); scanf("%d", &k); printf("Please enter some words\n"); while(ch != EOF) { ch = getchar(); printf("%c", ch + k); } return 0; }
-
Write a C program that takes as input a number k from 0 to 100 and a sequence of characters. Then prints the sequence with all characters shifted k positions backwards in the ASCII table. e.g. for the sequence "Khoor" and the key 3, the result should be "Hello".
#include<stdio.h> int main() { int k; int ch; printf("Please enter a number k to shift k positions backwards in the ASCII table\n"); scanf("%d", &k); printf("Please enter some words\n"); while(ch != EOF) { ch = getchar(); printf("%c", ch - k); } return 0; }