第三次实验的内容哒。
运行环境visual studio 2019
如果需要在别的环境中运行请将诸如scanf后面的_s去掉,还有加上#include<string.h>和#include<stdlib.h>,因为vs里面的iostream头文件里默认包含了这些文件,而别的环境没有。
可能写得不一定很好,但起码都是能用的QAQ。
如果发现错误请及时留言呐,觉得不错的话扫一下下面的二维码(点一下赏字会出现哒),资助哪怕一点点也是极好哒。
PS:不然只能吃土了。服务器维护也是要花钱的啊。
1.
//从左到右计算表达式(无视运算符优先级)
//date:29/10/2019
//code by kohaku
//All rights reserved.
#include<stdio.h>
#include <iostream>
int main()
{
double a = 0;
double out = 0;
double in = 0;
char op = '\0';
printf("Enter an epression:");
scanf_s("%lf", &out); //输入第一个数字
//将一个运算符和一个数字视为一组,循环计算
while (1) {
op = getchar(); //输入运算符或者接受回车
if (op == '\n')break; //若用户回车则结束输入
scanf_s("%lf", &in);
if (op == 43)out += in; //判断运算符类型并计算
if (op == 45)out -= in;
if (op == 42)out *= in;
if (op == 47)out /= in;
}
printf("Value of expression:");
printf("%lf", out);
}
2.
//使用牛顿法计算平方根
//date:29/10/2019
//code by kohaku
//All rights reserved.
#include<stdio.h>
#include<math.h>
#include <iostream>
int main()
{
double number = 0;
double out = 1;
double avg=0;
printf("Enter a positive number:");
scanf_s("%lf", &number);
while (1) {
avg = (out + number / out) / 2;
if (fabs(avg - out) <= 0.00001 * out)break;
out = avg;
}
printf("Square root:%lf", out);
}
3.
//逆向输出句子
//date:25/10/2019
//code by kohaku
//All rights reserved.
#include<stdio.h>
#include <iostream>
int main()
{
char str[1000];
char end;
int i = 0;
char* p;
printf("Enter a sentence:");
//读取句子,将所以空格处理为\0,遇到结束符停止
while (1) {
str[i] = getchar();
if (str[i] == 32)str[i] = '\0';
if (str[i] == 33 || str[i] == 63 || str[i] == 46)break;
i++;
}
printf("Reversal of sentence:");
//存储结束符,将结束符变为\0
end=str[i];
str[i] = '\0';
//向前搜索,遇到\0时,将指针移动到下一格,以字符串输出
for (int n=i-1; n >= 0; n--) {
if (str[n] =='\0') {
p = &str[n+1];
printf("%s ", p);
}
}
//输出第一个单词
printf("%s", str);
printf("%c", end);
}
4.
//这是一个凯撒加密算法
//date:25/10/2019
//code by kohaku
//All rights reserved.
#include<stdio.h>
#include <iostream>
int main()
{
char str[80] = { 0 };
int num = 0;
int i = 0;
int move = 0;
printf("Enter message to be encrypted:");
//逐个读取字符并计数,遇到结束符停止
while (1) {
str[i] = getchar();
if (str[i] == 33 || str[i] == 63 || str[i] == 46)break;
i++;
}
//最后添加一个终止符
str[i+1] = '\0';
printf("Enter shift amount(1-25):");
scanf_s("%d", &move);
//加密过程
for (int n = 0; n < i; n++) {
if (str[n]==32||str[n]==44)continue;
else if (str[n] >= 97)str[n] = ((str[n] - 'a') + move) % 26 + 'a';
else str[n] = ((str[n] - 'A') + move) % 26 + 'A';
}
printf("Encrypted message:%s", str);
}
5.
//数字字符串转化为整数
//date:29/10/2019
//code by kohaku
//All rights reserved.
#include<stdio.h>
#include <iostream>
#include<string>
#include<math.h>
int main()
{
char in[20] = { '\0' };
int len = 0;
long long out = 0;
gets_s(in);
len = strlen(in);
//循环计算每一位的值(使用ascii码减去'0'的值),乘上对于位数的10的幂
for (int i = 0; i < len; i++) {
out += pow(10, len - i-1) * ((int)in[i] - 48);
}
printf("%lld", out);
}