运算符分类
1.算术运算符
2.关系运算符
3.逻辑运算符
4.位运算符
5.特殊运算符
- 赋值运算符(=)
- 复合赋值运算符(<operator>=) =,-=,*=,/=,%=,>>=,<<=,&=,^=,|=
- 条件运算符符(?:)即<expression1> ? <expression2> : <expression3>
- sizeof
- 逗号运算符
- return
优先级
作业:
1、编写一个程序,此程序要求输入一个整数,然后打印出从输入的值(含)到比输入的值大 10(含)的所有整数值(比如输入 5,则输出 5 到 15)。要求在各个输出值之间用空格、制表符或者换行符分开。
代码语言:javascript复制#include<stdio.h>
int main()
{
printf("输入一个整数n");
unsigned int a;
scanf("%d",&a);
printf("输出该整数后10位n");
for (int i = 0; i < 10; i )
{
printf("%dn",a i);
}
return 0;
}
2 、写出下面表达式运算后 a 的值,设原来 a=12。设 a 和 n 已定义为整型变量。
a = a
24
a -= 2
10
a *= 2 3
60
a /= a a
0
a %= (n%2),n 的值为 5
0
a = a -= a *= a
0
3.编写一个程序,该程序要求输入一个 float 型数并打印概述的立方值。使用你自己设计的函数来计算该值 的立方并且将它的立方打印出来。main 函数负责把输入的值传递给该函数。
代码语言:javascript复制#include<stdio.h>
float lifang(float a)
{
float b;
b=a*a*a;
return b;
}
int main()
{
printf("输入一个浮点数n");
float a;
scanf("%f",&a);
a=lifang(a);
printf("%fn",a);
return 0;
}
4.编写一个程序,此程序要求输入天数,然后将该值转换为星期数和天数。例如输入 18,则要求输出: 18 days are 2 weeks, 4days.
代码语言:javascript复制#include<stdio.h>
int main()
{
printf("输入一个天数n");
int a;
scanf("%d",&a);
printf("%d days are %d weeks,%d days",a,a/7,a%7);
return 0;
}
5、分析并解释以下程序的执行结果。
代码语言:javascript复制#include <stdio.h>
int main(void)
{
int a,b,c,d;
a=10;
b=a ;
c= a;
d=10*a ;
printf("b,c,d:%d,%d,%d",b,c,d);
return 0;
}
- b=a ,a后自加1,先赋值给b
- C= a,a在上一步已经自增1,现在再先自增1,故为12
- D=10*a ,现在a已经为12,先把a*10赋值给d,a再增
#include<stdio.h>
int main(){
char a=123;
char b1=234;
unsigned char b2=234;
printf("a:%dnb:%dnb:%dn",a,b1,b2);
return 0;
}
代码语言:javascript复制#include<stdio.h>
int main(){
int a1=1,a2=1,a3=1,a4=1,*a9,a12=1,a13=1;
char a5=100,*a6="xy",*a10="abcdefghijk",a15=1;
float a7=1.0,a8=1.0,a11=1.0;
short a14=1;
double a16=1.0;
long double a17=1.0;
//-------------------
printf("%dn",a1);
printf("%un",a2);
printf("%on",a3);
printf("%xn",a4);
printf("%cn",a5);
printf("%sn",a6);
printf("%fn",a7);
printf("%en",a8);
printf("%pn",a9);
printf("%.5sn",a10);
printf("%.5fn",a11);
printf("]n",a12);
printf("%-5dn",a13);
printf("%hdn",a14);
printf("%hhdn",a15);
printf("%lfn",a16);
printf("%Lfn",a17);
// char var1;
// printf("输入一个整型");
// scanf("%d",&var1);
// printf("%dn",var1);
// printf("%un",var1);
// printf("%on",var1);
// printf("%xn",var1);
// printf("%cn",var1);
// printf("%sn",var1);
// printf("%fn",var1);
// printf("%en",var1);
// printf("%pn",var1);
// printf("%.5sn",var1);
// printf("%.5fn",var1);
// printf("]n",var1);
// printf("%-5dn",var1);
// printf("%hdn",var1);
// printf("%hhdn",var1);
// printf("%lfn",var1);
// printf("%Lfn",var1);
}
代码语言:javascript复制#include<stdio.h>
int main()
{
unsigned short var1=0x1234;
unsigned char var1_high8=(var1<<8)>>8;
unsigned char var1_low8=var1>>8;
printf("高%u 低%un",var1_high8,var1_low8);
unsigned short var2=var1_low8 var1_high8;
printf("%un",var2);
return 0;
}
代码语言:javascript复制#include<stdio.h>
int main()
{
printf("输入一个整数n");
unsigned int a;
scanf("%d",&a);
printf("输出该整数后10位n");
for (int i = 0; i < 10; i )
{
printf("%dn",a i);
}
return 0;
}
代码语言:javascript复制#include<stdio.h>
float lifang(float a)
{
float b;
b=a*a*a;
return b;
}
int main()
{
printf("输入一个浮点数n");
float a;
scanf("%f",&a);
a=lifang(a);
printf("%fn",a);
return 0;
}
代码语言:javascript复制#include <stdio.h>
int main(void)
{
int a,b,c,d;
a=10;
b=a ;
c= a;
d=10*a ;
printf("b,c,d:%d,%d,%d",b,c,d);
return 0;
}
代码语言:javascript复制#include<stdio.h>
int main()
{
char a1;
unsigned a2;
signed char a3;
int a4;
unsigned int a5;
short a6;
unsigned short a7;
long a8;
unsigned long a9;
long long a10;
double a11;
float a12;
printf("char:%ldn",sizeof(a1));
printf("unsigned:%ldn",sizeof(a2));
printf("signed char:%ldn",sizeof(a3));
printf("int:%ldn",sizeof(a4));
printf("unsigned int:%ldn",sizeof(a5));
printf("short:%ldn",sizeof(a6));
printf("unsigned short:%ldn",sizeof(a7));
printf("long:%ldn",sizeof(a8));
printf("unsigned long:%ldn",sizeof(a9));
printf("long long:%ldn",sizeof(a10));
printf("double:%ldn",sizeof(a11));
printf("float:%ldn",sizeof(a12));
}
代码语言:javascript复制#include<stdio.h>
int main()
{
int a=9527;
int b=a-(a/1000)*1000,b1=b-(b/100)*100;
printf("%d %d %d %d n",a/1000,b/100,b1/10,b1);
printf("-----------n");
printf("%d %d %d %d n",a/1000,a/100,a/10,a/1);
printf("-----------n");
int c=12;
int d = c;
d ;
printf("%dn",c );
printf("%dn", d);
printf("%dn",c);
return 0;
}
代码语言:javascript复制#include<stdio.h>
int main()
{
int a=12,n=5;
// printf("%d",a =a-=a*=a);
printf("%d",a%=(n%2));
return 0;
}
代码语言:javascript复制#include<stdio.h>
int main()
{
unsigned long long int a=0x12345678;
unsigned char var_low,var_high,var_low2,var_high2;
var_low=(a>>0)&0xFFFFFFFF;
var_high=(a>>8)&0xFFFFFFFF;
var_low2=(a>>16)&0xFFFFFFFF;
var_high2=(a>>24)&0xFFFFFFFF;
// printf("var_low:%hhXn",var_low);
// printf("var_high:%hhXn",var_high);
// printf("var_low2:%hhXn",var_low2);
// printf("var_high2:%hhXn",var_high2);
printf("0x%hhX%hhX%hhX%hhXn",var_low,var_high,var_low2,var_high2);
return 0;
}
代码语言:javascript复制#include<stdio.h>
int main()
{
unsigned int a=0x56;//(7654 3210)
unsigned char a1,a2;
// a1=a;
// a1=a1|(1<<0);
// a1=a1|(1<<5);
// a1=a1|(1<<7);
a1=a|(1<<0)|(1<<5)|(1<<7);
a2=a1&(~(1<<1))&(~(1<<4))&(~(1<<6));
printf("%hhXn",a2);
return 0;
}
代码语言:javascript复制#include <stdio.h>
int main(void)
{
int ascii;
printf("请输入一个ASCII码值:");
scanf("%d",&ascii);
printf("ASCII码值为 %d 对应的字符是 %cn",ascii,ascii);
return 0;
}
代码语言:javascript复制#include<stdio.h>
int main(){
int arr[4]={};
int id1,id2;
float score1,score2;
char *name1="tom",*name2="jerry",sex1,sex2;
//-------------------
printf("依次输入id,sex,score");
scanf("%d %c %f",&id1,&sex1,&score1);
scanf("%d %c %f",&id2,&sex2,&score2);
//-------------------------------
printf("IDtSEXtNAMEtSCOREn");
printf("%dt%ct%st%.1fn",id1,sex1,name1,score1);
printf("%dt%ct%st%.1fn",id2,sex2,name2,score2);
return 0;
}
代码语言:javascript复制#include<stdio.h>
#include<string.h>
char S[100][1000]={' '};//声明表为全局变量
int s1=0;
void chart(int n,int a[],int m);
int main()
{
int n;
int M1[40]={0},M2[40]={0},M3[40][100]={0};
int i,j,k,k1,k2,max=0,col,l;
char s[30][1000];
scanf("%d",&n);
getchar();//注意,必须得有这个来接收回车符
//
for(i=0;i<n;i )
// scanf("%s", &s[i]);
// gets(s[i]);
fgets(s[i],100,stdin);
//求最大列数
for(i=0;i<n;i )
{
l=strlen(s[i]);
for(j=0;j<l;j )
{
if(s[i][j]==',')
M1[i] ;
}
if(M1[i]>max)
{
max=M1[i];
}
}
col= max;
//求每列最大宽度
//1.先求每个单元的长度,赋给M3[i][j]中
for(i=0;i<n;i )
{
l=strlen(s[i]);
k=0;
for(j=0;j<l;j )
{
if(s[i][j]!=',')
{
M3[i][k] ;
}
if(s[i][j]==',')
k ;
} //for2
}//for1
//2.找每列最大的赋予M2[]
for(j=0;j<col;j )
{
max=0;
for(i=0;i<n;i )
{
if(M3[i][j]>max)
max=M3[i][j];
}
M2[j]=max;
}
//表格
chart(n,M2,col);
//输入数据
for(i=0;i<n;i )
{
k1=0;
k2=0;
k=0;
l=strlen(s[i]);
for(j=0;j<l;j )
{
S[2*i 1][k2 1]=s[i][j];
if(s[i][j 1]==',')
{
k1=k1 M2[k] 1;
k2=k1;
k ;
j ;
}
else
k2 ;
}
}
//输出表格
for(i=0;i<=2*n;i )
{
for(j=0;j<s1;j )
printf("%c",S[i][j]);
printf("n");
}
return 0;
}
void chart(int n,int a[],int m)//行数 每列的最大长度数组
{
int i,j,k;
int s;
s1=m 1;
for(i=0;i<m;i )
s1 =a[i];
for(i=0;i<=2*n;i )
{
//输出奇数行
if(i%2==0)
{
s=0;
k=0;
for(j=0;j<s1;j )
{
if(j==s)
{
S[i][j]=' ';
s =a[k];
s ;
k ;
}
else
S[i][j]='-';
}//for
} //if
//输出偶数行
else
{
s=0;
k=0;
for(j=0;j<s1;j )
{
if(j==s)
{
S[i][j]='|';
s =a[k];
s ;
k ;
}
}
}
} //for
}
代码语言:javascript复制#include<stdio.h>
#include <string.h>
int people=0;
struct kid
{
char name[50]; //姓名
int height; //身高
char sex[50]; //性别
long long num; //学号
};
void shuru(struct kid stu[50])
{
int i=0;
char v[50]={0}; //清空缓存区
struct kid *p = stu;
for(i=0;i<50;i )
{
printf("n请输入学生信息n");
printf(" 名字 ");
scanf("%s",(p people)->name);
printf(" 学号 ");
scanf("%lld",&(p people)->num);
printf(" 性别 ");
scanf("%s",(p people)->sex);
printf(" 身高 ");
scanf("%d",&(p people)->height);
people ;
printf("输入完毕n回车继续输入");
printf(" *键退出");
while(1)
{
getchar(); //(取地址)去掉输入完多出的回车符号
// gets(v);
fgets(v,100,stdin);
//scanf("%[n]",v);
if(strcmp(v,"")==0)
{
//getchar();
break;
}
if(strcmp(v,"*")==0)
{
printf("n已退出!");
return ;
}
}
}
}
void chaxun(struct kid stu[50])
{
char name[50];
struct kid *p = stu;
int i=0;
if(strcmp(p->name,"")==0)
{
printf("对不起n当前无录入的学生n");
return ;
}
while(1) //用while循环,使可以不间断输入
{
printf("请输入查询学生姓名n退出查询请按An");
scanf("%s",name);
for(i=0;i<people;i )
{
if(strcmp((p i)->name,name)==0)
{
printf("学生信息n 序号: %dn 姓名: %sn 学号: %lldn 性别: %sn 身高: %dn",i,(p i)->name,(p i)->num,(p i)->sex,(p i)->height);
printf("======================================================================n");
break;
}
if(i==people-1)
{
printf("查无此人!n");
break;
}
}
if(strcmp(name,"A")==0) //设定输入字符A退出
{
return ;
}
}
}
void shanchu(struct kid stu[50] )
{
char name[50];
struct kid *p = stu;
int i=0;
//int j=0;
if(strcmp(p->name,"")==0)
{
printf(" 对不起!!n 无学生n");
return ;
}
printf(" 你就说你想搞谁吧! n");
scanf("%s",name);
for(i=0;i<people;i )
{
if(strcmp((p i)->name,name)==0)
{
printf("学生信息n 序号: %dn 姓名: %sn 学号: %lldn 性别: %sn 身高: %dn",i,(p i)->name,(p i)->num,(p i)->sex,(p i)->height);
printf("======================================================================n");
while(strcmp((p i)->name,"")!=0)
{
strcpy((p i)->name,(p i 1)->name);
(p i)->height=(p i 1)->height;
strcpy((p i)->sex,(p i 1)->sex);
(p i)->num=(p i 1)->num;
i ;
}
people--;
printf(" ok!现在ta没了!! n");
break;
}
if(i==people-1)
{
printf(" 哟!还跟我来这套!没这人 n");
break;
}
}
}
void paixu(struct kid stu[50])
{
struct kid temp;
int i,j;
char a ;
printf("学生信息n 姓名tt学号tt性别tt身高n");
for(i=0;i<people;i )
{
for(j=i 1;j<people;j )
if(stu[i].height<stu[j].height)
{
temp=stu[i];
stu[i]=stu[j];
stu[j]=temp;
}
printf("nstt %lldtt%stt%dttn" ,stu[i].name,stu[i].num,stu[i].sex,stu[i].height);
}
//getch(); 用man手册查询头文件无法使用,上面gets同样,版本不支持
/*printf("是否保存?yes or no");
getchar();
scanf("%c",&a);
if(a=='yes')
save();
printf("保存成功");
else
printf("退出");
getch();
*/
}
void xingxi(struct kid stu[50]/*, char*p*/ )
{
struct kid *p = stu;
char name[50];
int i=0;
if(strcmp(p->name,"")==0)
{
printf("对不起n当前无录入的学生n");
return ;
}
for(i=0;i<people;i )
{
printf("学生信息n 序号: %dn 姓名: %sn 学号: %lldn 性别: %sn 身高: %dn",i,(p i)->name,(p i)->num,(p i)->sex,(p i)->height);
printf("======================================================================n");
}
}
int main ()
{
struct kid stu[50]; //定义一个结构体数组 保存 50个学生
struct kid *p = stu;
while(1) //循环体
{
printf("1.录入学生信息 2查询学生 3删除学生信息 4排序(按身高) 5显示所有学生信息n"); //设置标题
int a = 0;
while(1)
{
int ret = scanf("%d",&a); //键盘录入信息,&取变量地址
if(ret == 1 && a>=1 && a<=5 && getchar()=='n')
{
break;
}
else printf(" 大哥!你打错了!n 重新输入吧你!n");
while(getchar()!='n');
}
switch(a)
{
case 1:
{
shuru(stu);
break;
}
case 2:
{
chaxun(p);
break;
}
case 3:
{
shanchu(p);
break;
}
case 4:
{
paixu(stu);
break;
}
case 5:
{
xingxi(p);
break;
}
/*default: //输入非整型会出现持续打印
{
printf("输入错误n请重新输入n");
break ;
}
return 0;*/
}
}
}