技术标签: c语言 visual studio
在vs2022的编译环境中不能调用scanf函数,只能调用scanf_s函数用于输入
For语句中的间隔用分号“;”例如for(i=1;i<=n;i++)
scanf_s后边对应的数值应该取址符“&”
#include <stdio.h>
int multiply(int a,int b ) 即用户自定义函数 multiply(int a,int b)
{ 定义函数功能为 return (a*b)
return (a * b);
}
int main() 有且仅有一个main函数
{
int x, y, product; printf("please input two integers:"); scanf_s(" % d % d", &x, &y); 将转换后的数据送到变量地址列表所对应的变量中 product = multiply(x , y); 调用用户自定义函数multiply printf("The product is %d\n", product); return 0; 无返回值
}
可修改为
#include<stdio.h>
int main()
{
int x, y, product; printf("please input two integers:"); scanf_s("%d%d", &x, &y); product = x * y; printf("The product is %d\n", product); return 0;
}
#include<stdio.h>
int main()
{
int year, month, day; printf("请按标准格式输入一个日期(YYYY-MM-DD):"); scanf_s("%d-%d-%d", &year, &month, &day); printf("中国日期格式:%d年%d月%d日\n",year,month,day); printf("美国日期格式:%d/%d/%d\n", month, day, year); printf("英国日期格式:%d/%d/%d\n", day, month, year); return 0;
}
#include <stdio.h>
int main()
{
char grade1, grade2; <变量>=getchar(); grade1 = getchar(); 函数getchar用于从键盘读入一个用户输入的字符 getchar(); getchar()表示系统从输入缓冲区提取一个的字符但不赋给任何变量 grade2 = getchar(); putchar(<参数>); printf("The first grade is:");函数putchar是将给定的参数以单个字符的形式输出到显示器 putchar(grade1); 屏幕的当前位置上,其参数可以是字符常量、变量或表达式 putchar('\n'); printf("The second grade is:"); putchar(grade2); putchar('\n'); return 0;
}
#include <stdio.h>
int main()
{
const double pi = 3.14159; const <数据类型><只读变量名>; 变量初始化 double r; 用const修饰符限定只读变量 增加了程序的可读性、 scanf_s("%lf", &r); 方便了程序的维护、增强了程序的正确性并减少误操作 printf("area=%.2f\n", pi * r * r); 计算圆的面积并输出 printf("permeter=%.2f\n", 2 * pi * r); 计算圆的周长并输出 return 0;
}
#include <stdio.h>
#include <math.h> 引用数学函数 #include<stdio.h>;
int main()
{
const double pi = 3.14159; const <数据类型> <只读变量名>; const double g = 9.80; 用于定义只读变量 double v0; int theta; double R; printf("Please input v0 (m/s) and theta (degree):\n"); scanf_s("%lf%d", &v0, &theta); R = v0 * v0 * sin(2 * theta / 180.0 * pi)/g; C语言中sin函数的参数是弧度制 printf("The range is:%.2f (m)\n", R); 需要用pi将theta从角度制转换 return 0; 成弧度制。
}
#include <stdio.h>
#include <math.h>
int main()
{
int a, b; int x, y, z; int t; printf("please input a and b:"); scanf_s("%d%d", &a, &b); t = (int)sqrt(2 * a * b); printf("a=%d,b=%d\n", a, b); printf("2ab %s a perfect square number\n", t * t == 2 * a * b ? "is" : "is NOT"); x = a + t; y = b + t; z = a + b + t; printf("x=%d,y=%d,z=%d\n", x, y, z); printf("(%d,%d,%d) %s a solution of the Pythagorean Theorem equation\n", x, y, z, x * x + y * y == z * z ? "is" : "is NOT"); return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, p, s; printf("Please input three edges:"); scanf_s("%lf%lf%lf", &a, &b, &c); p = (a + b + c) / 2; s = sqrt(p * (p - a) * (p - b) * (p - c)); 调用数学函数 该例子我进行了修改 printf("s=%lf\n", s); return 0;
}
#include <stdio.h>
int main()
{
int age1, age2; printf("Enter age of two persons:"); scanf_s("%d%d", &age1, &age2); if (age1 >= age2) { printf("The older age is %d\n:", age1); } if...else语句的使用 Else { printf("The older age is %d\n:", age2); } return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, p, s; printf("Enter three edges of a triangle:"); scanf_s("%f%f%f", &a, &b, &c); if(a > 0 && b > 0 && c > 0 && a + b > c && a + c > b && b + c > a) { p = (a + b + c) / 2; s = sqrt(p * (p - a) * (p - b) * (p - c)); printf("s=%f", s); } else { printf("error input!\n"); } return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c; printf("Enter three edges of triangle:"); scanf_s("%f%f%f", &a, &b, &c); if (a <= 0 || b <= 0 || c <= 0) { printf("error input!\n"); } else { if (a + b > c && a + c > b && b + c > a) { if (fabs(a * a + b * b - c * c) < 1E-2 || fabs(a * a + c * c - b * b) < 1E-2 || fabs(b * b + c * c - a * a) < 1E-2) { printf("%f,%f,%f is a right triangle\n",a,b,c); } else { printf("%f,%f,%f is a ordinary triangle\n",a,b,c); } } else { printf("%f,%f,%f is not a triangle\n",a,b,c); }
} return 0;
}
#include <stdio.h>
int main()
{
int year, month, daysum; printf("Enter the year and the month:"); scanf_s("%d%d", &year, &month); switch (month) { case 1: case 3: switch(表达式) case 5: { case 7: case 常数表达式1: 语句系列1 case 8: case 常数表达式2: 语句系列2 case 10: ... case 12: case 常数表达式n: 语句系列n daysum = 31; } break; 首先计算switch后面表达式的值,然后与各 case 4: case分支的常量进行匹配,与哪个常量相等 case 6: 就该从该分支的语句序列开始执行,直至遇 case 9: 到break或者switch语句块的右大括号。 case 11: daysum = 30; break; case 2: if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) { daysum = 29; } else { daysum = 28; } } printf("%d.%d has %d days\n", year, month, daysum); return 0;
}
#include <stdio.h>
int main()
{
int n, i, sum; printf("Enter a positive integer:"); while语句的利用 scanf_s("%d", &n); i = 1; while(表达式) sum = 0; { while (i <= n) 语句块 { { sum += i; 1.计算表达式的值。若为真,则转步骤二 i++; 否则退出循环,执行while的下一条语句 } 2.执行语句块,并返回步骤一 printf("累加和%d=%d", n, sum); return 0;
}
#include <stdio.h>
int main()
{
int n, i; double fac; printf("Enter a positive integer:"); do while语句的利用 scanf_s("%d", &n); i= 1; fac = 1; do Do { { 语句块 fac *= i; }while(表达式); i++; 1.执行语句块,即循环体 } while (i <= n); 2.计算表达式。若为真,则转步骤一; printf("%d!=%lf", n, fac); 否则否则退出循环,执行下一语句。 return 0;
}
#include <stdio.h>
int main()
{
int sign, i; double item, sum; sum=0; sign = 1; for (i = 1; i <= 1000; i++) { item = sign / (2.0*i - 1); * 2.0 重点 double型 must 以小数形式 sum += item; sign = -sign; } printf("sum=%lf", sum);
}
代码一 break的使用
#include <stdio.h>
int main()
{
int i, n; for (i = 1; i <= 5; i++) { printf("Enter n:"); scanf_s("%d", &n); if (n < 0) break; break是结束本层循环体的的运行,并退出本层循环 printf("n=%d\n", n); } printf("The end!\n"); return 0;
}
代码二 continue的用法
#include <stdio.h>
int main()
{
int i, j; for (i = 1; i <= 9; i++) { for (j = 1; j <= i; j++) { printf("%d+%d=%2d ", i, j, i + j); } printf("\n"); } return 0;
}
#include <stdio.h>
int main()
{
int i, j; for (i = 1; i <= 4; i++) { for (j = 1; j <= 4 - i; j++) { printf(" "); 两个内循环的嵌套和使用 } for (j = 1; j <= 2 * i + 1; j++) { printf("*"); } printf("\n"); } return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
int n, i, k; do { printf("Enter a positive integer:"); scanf_s("%d", &n); } while (n <= 0); if (n == 1) { printf("%d is not a prime\n", n); 无语子,反复嵌套容易混乱。 } else { k = (int)sqrt(n); 使用到了(int)强制类型转换 for (i = 2; i <= k; i++) 和数学函数sqrt(n) { if (n % i == 0) { break; break的使用 } } if (i > k) { printf("%d is a prime", n); } else { printf(" % d is not a prime", n); } } return 0;
}
问公鸡、母鸡、小鸡各几只?
#include <stdio.h>
int main()
{
int a, b, c; for (a = 0; a <= 20; a++) { for (b = 0; b <= 33; b++) { c = 100 - a - b; if (15 * a + 9 * b + c == 300) { printf("%d,%d,%d\n", a, b, c); } } }return 0;
}
#include <stdio.h>
double totalCost (int n, double p);
int main()
{
double price, bill; int number; printf("Enter the number of items purchased:"); scanf_s("%d", &number); printf("Enter the price per item (RMB):"); scanf_s("%lf", &price); bill = totalCost(number, price); printf("The totalCost of the items purchased is:%.lf RMB.\n", bill); return 0;
}
double totalCost(int n, double p)
{
const double DISCOUNT = 0.2; double total; if (n > 1) total = n * p * (1 - DISCOUNT); else total = n * p; return total;
}
int judgeprime(int n)
{
int i, k; int judge = 1; if (n == 1) judge = 0; k = (int)sqrt(n); for (i = 2; judge && i <= k; i++) if (n % i == 0) judge = 0; return judge;
}
void drawline()
{
const int n = 30; int i; for (i = 1; i <= n; i++) printf("-"); printf("\n"); return;
}
#include <stdio.h>
#include <math.h>
int judgeprime(int n);
int judgeprime(int n)
{
int i, k; int judge = 1; if (n == 1) judge = 0; k = (int)sqrt(n); for (i = 2; judge && i <= k; i++) if (n % i == 0) judge = 0; return judge;
}
int main()
{
int m, prime; scanf_s("%d", &m); if (m <= 0) { printf("error input!\n"); return 0; } prime = judgeprime(m); if (prime) printf("%d is a prime!\n", m); else printf("%d is not a prime!\n", m); return 0;
}
#include <stdio.h>
void drawline();
void drawline()
{
const int n = 30; int i; for (i = 1; i <= n; i++) printf("-"); printf("\n"); return;
}
int main()
{
drawline(); printf("C is a beautiful language!\n"); drawline(); return 0;
}
#include <stdio.h>
double Fact(int n);
int main()
{
int n; double t; printf("Plese input n:\n"); scanf_s("%d", &n); if (n < 0) n = -n; t = Fact(n); printf("%d!=%lf", n, t); return 0;
}
double Fact(int n)
{
if (!n) return 1.0; return n * Fact(n - 1);
}
修改后
#include <stdio.h>
double Fact(int n);
int main()
{
int n; double t; printf("Plese input n:\n"); scanf_s("%d", &n); if (n < 0) n = -n; t = Fact(n); printf("%d!=%lf", n, t); return 0;
}
double Fact(int n)
{
if (n==0) { return 1.0; } else { return n * Fact(n - 1); }
}
-
#include <stdio.h>
void multibase(int n, int B);
void multibase(int n, int B)
{
int m; if (n) { multibase(n / B, B); m = n % B; if (m < 10) printf("%d", m); else printf("%c", m + 55); }
}
int main()
{
int n, B; do { scanf_s("%d%d", &n, &B); } while (n <= 0 || B <= 1 || B >16); printf("change result:\n"); multibase(n, B); printf("\n"); return 0;
}
#include <stdio.h>
#include <math.h>
int count;
int judgeprime(int n);
int main()
{
int i; printf("The primes between 2 to 100:\n"); for(i=2;i<100;i++) if (judgeprime(i)) { printf("%d ", i); count++; } printf("The total number of primes:%d\n", count); return 0;
}
int judgeprime(int n)
{
int i; int judge = 1; if (n == 1) judge = 0; { int k = (int)sqrt(n); for (i = 2; judge && i <= k; i++) if (n % i == 0) judge = 0; } return judge;
}
#include <stdio.h>
int fun(int n);
int fun(int n)
{
static int f = 1; f = f * n; return f;
}
int main()
{
int i; for (i = 1; i <= 5; i++) { printf("%d!=%d ", i, fun(i)); }return 0;
}
#include <stdio.h>
#include <math.h>
#define pi 3.14159
double getarea(double r);
double getvolumes(double r);
double getvolumec(double r, double h);
int main()
{
double r, h, s, v1, v2; printf("请输入所需半径 :"); scanf_s("%lf", &r); printf("请输入所需高:"); scanf_s("%lf", &h); s = getarea(r); v1 = getvolumes(r); v2 = getvolumec(r, h); printf("s=%lf\n", s); printf("v1=%lf\n", v1); printf("v2=%lf\n", v2); return 0;
}
double getarea(double r)
{
return (pi * pow(r, 2));
}
double getvolumes(double r)
{
return (pi * pow(r, 3) * 3 / 4);
}
double getvolumec(double r, double h)
{
return (pi * h * pow(r, 2));
}
#include <stdio.h>
int main()
{
float score[50] = { 0 }; int num; float sum = 0, average; int i; do { printf("Input the number of students:"); scanf_s("%d", &num); } while (num <= 0 || num > 50); printf("Input the score :"); for (i = 0; i < num; i++) { scanf_s("%f", &score[i]); sum += score[i]; } average = sum / num; printf("The average is :%5.2f\n", average); return 0;
}
例5.2 Fibonacci数列。有一对小兔子(一公一母),第2个月长大成大兔子,长到第3个月开始每个月就生一对小兔子(一公一母)。等这对兔子长到第3个月又开始生小兔子。假设所有的兔子都不会死,求出n个月后兔子的数目。
|时间|小兔子|大兔子|兔子总数|
|--|--|--|--|
|1 | 1 | 0 | 1 |
|2 | 0 | 1 | 1 |
|3 | 1 | 1 | 2 |
|4 | 1 | 2 | 3 |
|5 | 2 | 3 | 5 |
|..| .. | .. | ..|
| 8| 8 | 13 | 21 |
|..| .. | .. | ..|
功能为 return (a*b)
return (a * b);
}
int main() 有且仅有一个main函数
{
int x, y, product; printf("please input two integers:"); scanf_s(" % d % d", &x, &y); 将转换后的数据送到变量地址列表所对应的变量中 product = multiply(x , y); 调用用户自定义函数multiply printf("The product is %d\n", product); return 0; 无返回值
}
可修改为
#include<stdio.h>
int main()
{
int x, y, product; printf("please input two integers:"); scanf_s("%d%d", &x, &y); product = x * y; printf("The product is %d\n", product); return 0;
}
文章浏览阅读645次。这个肯定是末尾的IDAT了,因为IDAT必须要满了才会开始一下个IDAT,这个明显就是末尾的IDAT了。,对应下面的create_head()代码。,对应下面的create_tail()代码。不要考虑爆破,我已经试了一下,太多情况了。题目来源:UNCTF。_攻防世界困难模式攻略图文
文章浏览阅读2.9k次,点赞3次,收藏10次。偶尔会用到,记录、分享。1. 数据库导出1.1 切换到dmdba用户su - dmdba1.2 进入达梦数据库安装路径的bin目录,执行导库操作 导出语句:./dexp cwy_init/[email protected]:5236 file=cwy_init.dmp log=cwy_init_exp.log 注释: cwy_init/init_123..._达梦数据库导入导出
文章浏览阅读1.9k次。1. 在官网上下载KindEditor文件,可以删掉不需要要到的jsp,asp,asp.net和php文件夹。接着把文件夹放到项目文件目录下。2. 修改html文件,在页面引入js文件:<script type="text/javascript" src="./kindeditor/kindeditor-all.js"></script><script type="text/javascript" src="./kindeditor/lang/zh-CN.js"_kindeditor.js
文章浏览阅读2.3k次,点赞6次,收藏14次。SPI的详情简介不必赘述。假设我们通过SPI发送0xAA,我们的数据线就会变为10101010,通过修改不同的内容,即可修改SPI中0和1的持续时间。比如0xF0即为前半周期为高电平,后半周期为低电平的状态。在SPI的通信模式中,CPHA配置会影响该实验,下图展示了不同采样位置的SPI时序图[1]。CPOL = 0,CPHA = 1:CLK空闲状态 = 低电平,数据在下降沿采样,并在上升沿移出CPOL = 0,CPHA = 0:CLK空闲状态 = 低电平,数据在上升沿采样,并在下降沿移出。_stm32g431cbu6
文章浏览阅读1.2k次,点赞2次,收藏8次。数据链路层习题自测问题1.数据链路(即逻辑链路)与链路(即物理链路)有何区别?“电路接通了”与”数据链路接通了”的区别何在?2.数据链路层中的链路控制包括哪些功能?试讨论数据链路层做成可靠的链路层有哪些优点和缺点。3.网络适配器的作用是什么?网络适配器工作在哪一层?4.数据链路层的三个基本问题(帧定界、透明传输和差错检测)为什么都必须加以解决?5.如果在数据链路层不进行帧定界,会发生什么问题?6.PPP协议的主要特点是什么?为什么PPP不使用帧的编号?PPP适用于什么情况?为什么PPP协议不_接收方收到链路层数据后,使用crc检验后,余数为0,说明链路层的传输时可靠传输
文章浏览阅读587次。软件测试工程师移民加拿大 无证移民,未受过软件工程师的教育(第1部分) (Undocumented Immigrant With No Education to Software Engineer(Part 1))Before I start, I want you to please bear with me on the way I write, I have very little gen...
文章浏览阅读304次。Thinkpad X250笔记本电脑,装的是FreeBSD,进入BIOS修改虚拟化配置(其后可能是误设置了安全开机),保存退出后系统无法启动,显示:secure boot failed ,把自己惊出一身冷汗,因为这台笔记本刚好还没开始做备份.....根据错误提示,到bios里面去找相关配置,在Security里面找到了Secure Boot选项,发现果然被设置为Enabled,将其修改为Disabled ,再开机,终于正常启动了。_安装完系统提示secureboot failure
文章浏览阅读10w+次,点赞93次,收藏352次。1、用strtok函数进行字符串分割原型: char *strtok(char *str, const char *delim);功能:分解字符串为一组字符串。参数说明:str为要分解的字符串,delim为分隔符字符串。返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。其它:strtok函数线程不安全,可以使用strtok_r替代。示例://借助strtok实现split#include <string.h>#include <stdio.h&_c++ 字符串分割
文章浏览阅读2.3k次。1 .高斯日记 大数学家高斯有个好习惯:无论如何都要记日记。他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210后来人们知道,那个整数就是日期,它表示那一天是高斯出生后的第几天。这或许也是个好习惯,它时时刻刻提醒着主人:日子又过去一天,还有多少时光可以用于浪费呢?高斯出生于:1777年4月30日。在高斯发现的一个重要定理的日记_2013年第四届c a组蓝桥杯省赛真题解答
文章浏览阅读851次,点赞17次,收藏22次。摘要:本文利用供需算法对核极限学习机(KELM)进行优化,并用于分类。
文章浏览阅读1.1k次。一、系统弱密码登录1、在kali上执行命令行telnet 192.168.26.1292、Login和password都输入msfadmin3、登录成功,进入系统4、测试如下:二、MySQL弱密码登录:1、在kali上执行mysql –h 192.168.26.129 –u root2、登录成功,进入MySQL系统3、测试效果:三、PostgreSQL弱密码登录1、在Kali上执行psql -h 192.168.26.129 –U post..._metasploitable2怎么进入
文章浏览阅读257次。本文将为初学者提供Python学习的详细指南,从Python的历史、基础语法和数据类型到面向对象编程、模块和库的使用。通过本文,您将能够掌握Python编程的核心概念,为今后的编程学习和实践打下坚实基础。_python人工智能开发从入门到精通pdf