/*
06-0. 混合类型数据格式化输入(5)
本题要求编写程序,顺序读入浮点数1、整数、字符、浮点数2,
再按照字符、整数、浮点数1、浮点数2的顺序输出。
输入格式:输入在一行中顺序给出浮点数1、整数、
字符、浮点数2,其间以1个空格分隔。
输出格式:在一行中按照字符、整数、浮点数1、
浮点数2的顺序输出,其中浮点数保留小数点后2位。
输入样例:2.12 88 c 4.7
输出样例:c 88 2.12 4.70
*/
#include <stdio.h>
int main()
{
float f1,f2;
char c;
int x;
printf("input >>> ");
scanf("%f %d %c %f", &f1, &x, &c, &f2);
printf("%c %d %.2f %.2f", c, x, f1, f2);
return 0;
}
/*
06-1. 简单计算器(20)
模拟简单运算器的工作。假设计算器只能进行加减乘除运算,运算数和结果都是整数,4种
运算符的优先级相同,按从左到右的顺序计算。
输入格式:输入在一行中给出一个四则运算算式,没有空格,且至少有一个操作数。
遇等号”=”说明输入结束。
输出格式:在一行中输出算式的运算结果,或者如果除法分母为0或有非法运算符,
则输出错误信息“ERROR”。
输入样例:1+2*10-10/2=
输出样例:10
*/
#include <stdio.h>
int main()
{
char sym = '0';
int num;
int total;
printf("input >>> \n");
scanf("%d", &total);
while(sym != '='){
scanf("%c", &sym);
if(sym == '=') break;
scanf("%d", &num);
if(sym == '+'){
total += num;
}else if(sym == '-'){
total -= num;
}else if(sym == '*'){
total *= num;
}else if(sym == '/'){
total /= num;
}
}
printf("result : %d", total);
return 0;
}
/*
06-2. 字符串字母大小写转换(10)
输入一个以#结束的字符串,本题要求将小写字母全部转换成大写字母,把大写字母全部
转换成小写字母,其它字符不变。
输入格式:输入在一行中给出一个长度不超过40的、以#结束的非空字符串。
输出格式:在一行中按照要求输出转换后的字符串。
输入样例:Hello World! 123#
输出样例:hELLO wORLD! 123
*/
#include <stdio.h>
// a-z:97-122, A-Z:65-90
int main()
{
char c;
int arr[40];
int num = 0;
while(num < 40){
scanf("%c", &c);
if(c == '#') break;
else arr[num] = c;
num++;
}
for(int i=0; i<num; i++){
if(arr[i]>=97 && arr[i] <=122)
printf("%c", arr[i]-=32);
else if(arr[i]>=65 && arr[i]<=90)
printf("%c", arr[i]+=32);
else
printf("%c", arr[i]);
}
return 0;
}
int main()
{
char c;
c = getchar();
while(c != '#')
{
if(c >= 'a' && c <= 'z'){
c -= 32;
}
else if(c >= 'A' && c <= 'Z'){
c += 32;
}
printf("%c", c);
c = getchar();
}
return 0;
}
/*
06-3. 单词长度(15)
你的程序要读入一行文本,其中以空格分隔为若干个单词,以‘.’结束。你要输出每个单词
的长度。这里的单词与语言无关,可以包括各种符号,比如“it’s”算一个单词,长度为4。
注意,行中可能出现连续的空格;最后的‘.’不计算在内。
输入格式:输入在一行中给出一行文本,以‘.’结束。
提示:用scanf("%c",…);来读入一个字符,直到读到‘.’为止。
输出格式:在一行中输出这行文本对应的单词的长度,每个长度之间以空格隔开,行末没有最后的空格。
输入样例:It’s great to see you here.
输出样例:4 5 2 3 3 4
*/
#include <stdio.h>
int main()
{
char c;
int n = 0, m = 0;
int arr[100] = {0};
scanf("%c", &c);
while(c != '.'){
if(c != ' ') n++;
scanf("%c", &c);
if(c == ' ' || c == '.'){
arr[++m] = n;
}
}
for(int i=1; i<=m; i++){
printf("%d", arr[i]-arr[i-1]);
if(i != m) printf(" ");
}
return 0;
}