Jillll @ 2023-09-21 16:25:44
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
void Add(char former[], char latter[]) {
printf("%s+%s=%d\n", former, latter, (int)(atoi(former) + atoi(latter))); // 使用 atoi 转换字符串为整数
int len1 = strlen(former), len2 = strlen(latter);
int len3 = snprintf(NULL, 0, "%d", atoi(former) + atoi(latter)); // 计算数字转换后的长度
printf("%d\n", len1 + len2 + len3 + 2);
}
void Sub(char former[], char latter[]) {
printf("%s-%s=%d\n", former, latter, (int)(atoi(former) - atoi(latter)));
int len1 = strlen(former), len2 = strlen(latter);
int len3 = snprintf(NULL, 0, "%d", atoi(former) - atoi(latter));
printf("%d\n", len1 + len2 + len3 + 2);
}
void Mul(char former[], char latter[]) {
printf("%s*%s=%d\n", former, latter, (int)(atoi(former) * atoi(latter)));
int len1 = strlen(former), len2 = strlen(latter);
int len3 = snprintf(NULL, 0, "%d", atoi(former) * atoi(latter));
printf("%d\n", len1 + len2 + len3 + 2);
}
void Select(char type, char former[], char latter[]) {
switch(type) {
case 'a' :
Add(former, latter);
break;
case 'b' :
Sub(former, latter);
break;
case 'c' :
Mul(former, latter);
break;
}
}
int main() {
int i = 0, flag = 1;
scanf("%d", &i);
char type;
char former[51], latter[51];
char prevtype = 'a';
for (int tot = 0; tot < i; tot++) {
if (flag) {
scanf(" %c %s %s", &type, former, latter); // 加入空格,跳过空白字符
flag = 0;
prevtype = type;
Select(type, former, latter);
continue;
}
if (scanf(" %c", &type) && isalpha(type)) {
scanf(" %s %s", former, latter);
Select(type, former, latter);
} else {
ungetc(type, stdin); // 将不符合格式的数据退回
scanf(" %s %s", former, latter);
Select(prevtype, former, latter);
}
}
return 0;
}
by Martin_CPP @ 2023-10-05 17:21:49
其实我有个比较简单的写法
by Martin_CPP @ 2023-10-05 17:22:34
std::string
by Martin_CPP @ 2023-10-05 17:24:30
无非就两种情况: 1 字母 数字 数字
2 数字 数字
所以...
by Martin_CPP @ 2023-10-05 17:34:16
#include<iostream>
#define LOG(x) std::cout << x
int main()
{
int t;
std::cin >> t;
int i = 0;
bool flag = true;
char ch = '';
for (; flag == true; )
{
i++;
int a, b;
std::string s;
std::cin >> s;
if (s[0] >= 'a' && s[0] <= 'z')
{
//分别对应 a,b,c 操作
//储存此次操作的字符
}
else
{
//将 s 转换成数字
//使用上一次的字符
//分别判断 a,b,c 操作
}
if (!(i < t))
{
flag = false;
}
}
std::cin.get();
}
by No_Rest @ 2023-10-26 21:31:04
同求。90pts #2 WA