strcspn函数
含义:一个字符串处理函数,用于计算字符串中不包含指定字符集中任何字符的初始段的长度。
原型:size_t strcspn(const char* str, const char* reject);
参数:
str:要处理的字符串。
reject:指定要忽略的字符集。
返回值:返回字符串中不包含reject中任何字符的初始段的长度。
常用:去除换行符:input[strcspn(input, "\n")] = '\0';
与strspn的区别:strspn计算只包含accept字符的初始段的长度,而strcspn计算不包含reject字符的初始段的长度。
strcpy函数
含义:将一个字符串复制到另一个字符串中。
原型:char* strcpy(char* dest, const char* src);
参数:
dest:目标字符串。
src:源字符串。
返回值:返回目标字符串的地址。
strcmp函数
含义:比较两个字符串是否相同。
原型:int strcmp(const char* str1, const char* str2);
参数:
str1:字符串1。
str2:字符串2。
返回值:如果str1和str2相同,返回0;如果str1小于str2,返回小于0; 如果str1大于str2,返回大于0。
遇到比long long更大的整型怎么办?
如何运用字符数组?需构造什么函数方法?思路?
题目:
c语言
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define N 1002
void reverse(char* a);
void add(char* sum, char* a, char* b);
int main() {
char income[N] = { 0 };
char expense[N] = { 0 };
char surplus[N] = { 0 };
char line[N];
while (fgets(line, N, stdin) != NULL) {
line[strcspn(line, "\n")] = '\0';
if (line[0] != '-')
add(income, income, line);
else
add(expense, expense, line);
}
add(surplus, income, expense);
printf("%s\n%s\n%s\n", surplus, expense, income);
return 0;
}
void reverse(char* a) {
int len = strlen(a);
for (int i = 0; i < len / 2; i++) {
char temp = a[i];
a[i] = a[len - 1 - i];
a[len - i - 1] = temp;
}
}
void add(char* sum, char* a, char* b) {
int judge1 = (a[0] == '-') ? -1 : 1;
int judge2 = (b[0] == '-') ? -1 : 1;
char aa[N], bb[N];
//去掉负号
strcpy(aa, (judge1 == -1) ? a + 1 : a);
strcpy(bb, (judge2 == -1) ? b + 1 : b);
//存储计算结果
char c[N + 1] = { 0 };
int len1 = strlen(aa);
int len2 = strlen(bb);
//情况1:同号相加
if (judge1 == judge2) {
reverse(aa);
reverse(bb);
int len = len1 > len2 ? len1 : len2;
int carry = 0; //进位标志
for (int i = 0; i < len; i++) {
//获取当前位的数字,不足的位补0
int A = i < len1 ? aa[i] - '0' : 0;
int B = i < len2 ? bb[i] - '0' : 0;
//当前位总和
int total = A + B + carry;
c[i] = (total % 10) + '0'; //计算当前为结果
carry = total / 10; //计算进位
}
//处理最高位进位
if (carry > 0) {
c[len] = carry + '0';
len++;
}
reverse(c);
//恢复符号
if (judge1 == -1) {
sum[0] = '-';
strcpy(sum + 1, c);
}
else
strcpy(sum, c);
}
//情况2:异号相减
else {
//比较绝对值的大小
int cmp = 0;
if (len1 != len2)
cmp = len1 - len2; //长度不同,长的绝对值大
else
cmp = strcmp(aa, bb); //长度相同,逐位比较
//绝对值相等时结果为0
if (cmp == 0) {
strcpy(sum, "0");
return;
}
//确定较大数和较小数
char* larger = (cmp > 0) ? aa : bb;
char* smaller = (cmp > 0) ? bb : aa;
//结果符号
int resultSign = (cmp > 0) ? judge1 : judge2;
//便于计算
reverse(larger);
reverse(smaller);
int len1 = strlen(larger);
int len2 = strlen(smaller);
int borrow = 0; //借位标志
for (int i = 0; i < len1; i++) {
int digit1 = larger[i] - '0' - borrow;
int digit2 = i < len2 ? smaller[i] - '0' : 0;
//情况一:需要借位
if (digit1 < digit2) {
digit1 += 10;
borrow = 1;
}
else
borrow = 0;
//计算当前位
c[i] = (digit1 - digit2) + '0';
}
//去除前导零(反转前尾是零)
/*Eg:1000-999——0001-999=1000——去零:1*/
int len = strlen(c);
while (len > 1 && c[len - 1] == '0') {
c[len - 1] = '\0';
len--;
}
reverse(c);
//恢复符号
if (resultSign == -1) {
sum[0] = '-';
strcpy(sum + 1, c);
}
else
strcpy(sum, c);
}
}
Python
//Python真的简化好多啊
def calculate_finances():
# 初始化统计变量
balance = 0
expenses = 0
income = 0
# 读取输入直到文件结束(EOF)
while True:
try:
line = input().strip() # 读取一行输入并去除首尾空格
# 如果输入为空行则跳过
if not line:
continue
# 将输入转换为整数
amount = int(line)
# 根据金额正负更新统计信息
if amount < 0:
# 负数表示消费
expenses += amount # 消费总额增加(取绝对值)
balance += amount # 余额减少
else:
# 正数表示收入
income += amount # 收入总额增加
balance += amount # 余额增加
except EOFError:
# 当没有更多输入时(Ctrl+D或文件结束),退出循环
break
except ValueError:
# 如果输入不是有效整数,跳过(根据题目描述,输入应为整数,所以这行理论上不会执行)
continue
# 输出结果
print(balance) # 第一行:余额
print(expenses) # 第二行:消费总额
print(income) # 第三行:收入总额
# 调用函数执行计算
calculate_finances()