写在前面:本笔记为个人学习各平台C语言系列课程所作,仅供交流学习,不得作他用。
1. 字符型多次读入/getchar函数

cpp
#include <stdio.h>
int main() {
char c;
int letter = 0, blank = 0, digit = 0, other = 0;
int count = 0;
while (count < 10) {
c = getchar();
if (c == '\n') {
blank++;
count++;
continue;
}
count++;// 正常字符,计数+1
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {letter++;}
else if (c == ' ') {blank++;}
else if (c >= '0' && c <= '9') {digit++;}
else {other++;}
}
printf("letter = %d, blank = %d, digit = %d, other = %d", letter, blank, digit, other);
return 0;
}
(1)getchar函数一次读入一个字符。即使你在终端里输入很多个字符,他一次也只能读第一个然后跳出。所以需要实现多个输入,需要准备循环。
(2)scanf对空格和回车敏感,当类似的字符需要作为可被读取到的输入时,用getchar。
(3)**回车/r,ASCII码13,作用是让光标回到当前行最左边,不向下移动;换行/n,ASCII码10,作用是让光标向下移动,不回到最左边。**只是现在电脑的回车键是连用两个功能的。
(4)在windows系统下,终端处换行系统会读到/r和/n。在linux和MAC系统下,换行只会读到/n。但是用getchar去读字符时,/r/n会被合并为/n,所以只会读到换行/n。
2. 百分数输出(整数不精确)

这道题我原来写的代码是这样的:
cpp
#include <stdio.h>
int main(){
int v1=0;int v2=0;
scanf("%d %d",&v1,&v2);
if(1.0*v1<v2*1.1){printf("OK");}
else if(1.0*v1>=1.1*v2 && v1<v2*1.5){printf("Exceed %d%. Ticket 200",(int)1.0*v1/v2);}
else{printf("Exceed %d%. License Revoked",(int)1.0*v1/v2);}
return 0;
}
这样在输入110和100时会进入第一个判断分支,原因是计算机内部整数不精确,用浮点数计算时第一个分支成立了。为解决这一问题,全部*10改用整数计算:
cpp
#include <stdio.h>
int main() {
int v1 = 0, v2 = 0;
scanf("%d %d", &v1, &v2);
// 全部用整数运算,避免浮点数误差
if (v1 * 10 < v2 * 11) {
printf("OK");
} else if (v1 * 10 < v2 * 15) {
// 计算超速百分比
int percent = (v1 - v2) * 100 / v2;
printf("Exceed %d%%. Ticket 200", percent);
} else {
int percent = (v1 - v2) * 100 / v2;
printf("Exceed %d%%. License Revoked", percent);
}
return 0;
}
而且在printf里打%时,要连续输入两个%%。
3. 分硬币(想的比较复杂的一道题)

cpp
#include <stdio.h>
int main(){
int n;int f;int t;int o;int c=0;
scanf("%d",&n);
f=n/5;
while(f>=1){
t=(n-5*f)/2;o=n-5*f-2*t;
while(t>=1){
if(o>=1){printf("fen5:%d, fen2:%d, fen1:%d, total:%d\n",f,t,o,f+t+o);c++;}
if(t==1){break;}
t--;o=o+2;
}
f--;
}
printf("count = %d",c);
return 0;
}
4. 字符加密(注意" "和' '的区别)

cpp
#include <stdio.h>
int main(){
char a;
a=getchar();
while(a!='\n'){
if(a>='a' && a<'z'){printf("%c",a+'A'-'a'+'B'-'A');}
else if(a=='z'){printf("A");}
else if(a=='Z'){printf("a");}
else if(a>='A' && a<'Z'){printf("%c",a+'a'-'A'+'b'-'a');}
else{printf("%c",a);}
a=getchar();
}
return 0;
}
printf()里,输出要用双引号""。表达字符的加减或者比较运算时,得用单引号''。
5. 简单计算器(字符操作)

cpp
#include <stdio.h>
int main() {
int result = 0;int num = 0;char op = 0;char c;int error = 0;
// 1. 读取第一个操作数
while ((c = getchar()) != '+' && c != '-' && c != '*' && c != '/' && c != '=') {
if(c >= '0' && c <= '9') {num = num * 10 + (c - '0');}
else{error = 1;break;}
}
if (error) {printf("ERROR");return 0;}
if (c == '=') {printf("%d", num);return 0;} // 只有一个数的情况
result = num;op = c;num = 0;//到此,c读到一个操作符并传给了op
// 2. 循环处理后续运算符和操作数
while ((c = getchar()) != '=') {
if (c >= '0' && c <= '9') {num = num * 10 + (c - '0');}
else if (c == '+' || c == '-' || c == '*' || c == '/') {
// 先计算上一步的结果
switch (op) {
case '+': result += num; break;
case '-': result -= num; break;
case '*': result *= num; break;
case '/':
if (num == 0) {error = 1;break;}
result /= num;break;
default: error = 1; break;
}
if (error) break;
// 更新运算符,重置临时数
op = c;num = 0;
}
else {error = 1;break;}
}
// 3. 处理最后一次运算(读到=时,还没算上一次的运算符)
if (!error) {
switch (op) {
case '+': result += num; break;
case '-': result -= num; break;
case '*': result *= num; break;
case '/':
if (num == 0) {error = 1;}
else {result /= num;}
break;
default: error = 1; break;
}
}
// 4. 输出结果
if (error) {printf("ERROR");}
else {printf("%d", result);}
return 0;
}
(1)如何实现字符拼接,使用string库。
cpp
#include <stdio.h>
#include <string.h> // 必须加!strcat 在这个头文件里
int main() {
// 注意:str1 必须足够大!能装下拼接后的所有字符
char str1[100] = "hello";
char str2[] = " world";
strcat(str1, str2); // 把 str2 拼到 str1 后面
printf("%s", str1); // 输出:hello world
return 0;
}
(2)这道题主要是比较复杂,第一步操作后,c已经读到了第一个操作符并传给了op。第二步开始读第二个操作数,读到第二个操作符时停止,计算上一步运算,然后重置参数计算。同时要注意error。
6. 泰勒函数求余弦(最后一项)

注意描述!这里说的是最后一项绝对值小于e,这小于e的一项也是要加进去的!!!
cpp
double funcos(double e, double x)
{
double sum = 1.0; // 第一项 1 先放进去
double term = 1.0; // 存当前项
int n = 1; // 计数
// 核心:当前项 >= e 才继续算下一项并累加
while (fabs(term) >= e)
{
// 递推计算下一项
term = term * (-x * x) / ((2*n - 1) * (2*n));
sum = sum + term;
n++;
}
return sum;
}