当前位置:首页 » 编程语言 » 编程c语言一个数除二减2
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

编程c语言一个数除二减2

发布时间: 2022-06-22 19:12:04

❶ 编程c语言一个数除2减2

public class watermelon {

/**
* 有1020个西瓜,第一天卖一半多2个,以后每天卖剩下的一半多2个,
* 问几天以后能卖完
*/
public static void main(String[] args) {
int x1,x2,day;
x1=1020;
day=0;

while(x1!=0){
x2=x1/2-2;
x1=x2;
day++;}
System.out.println( "the day is "+day);

// TODO Auto-generated method stub

}

}
答案是8天

❷ c语言,编写程序,要求对两个实数实现简单的加减乘除运算的功能(用switch)语句

#include<stdio.h>

void main()

{

float num1,num2;

char ch;

printf("请输入运算符(+-*/):");

scanf("%c",&ch);

printf("请输入第一个数字:");

scanf("%f",&num1);

printf("请输入第二个数字:");

scanf("%f",&num2);

switch(ch)

{

case '+':

printf("%f+%f=%f ",num1,num2,num1+num2);

break;

case '-':

printf("%f-%f=%f ",num1,num2,num1-num2);

break;

case '*':

printf("%fx%f=%f ",num1,num2,num1*num2);

break;

case '/':

printf("%f/%f=%f ",num1,num2,num1/num2);

break;

default:

printf("错误输入! ");

break;

}

}

❸ 用C语言编写一个计算器程序,实现加,减,乘,除,求平方根(正数),倒数等功能.

#include<iostream>
#include<cmath>
#include<string>
using namespace std;
const double pi = 3.14159265; const double e = 2.718281828459; const int SIZE = 1000;
typedef struct node//为了处理符号而建立的链表(如: 1+(-2)) { char data; node *next; }node;
typedef struct stack_num//存储 数 的栈 { double *top; double *base; }stack_num;
typedef struct stack_char//存储 运算符号 的栈 { char *top; char *base; }stack_char;
stack_num S_num;//定义 stack_char S_char;//定义
char fu[18] = {'\n', ')', '+', '-', '*', '/', '%', '^', 'Q', 'L', 'C', 'S', 'T', 'c', 's', 't', '('};
int compare[1000];//表现出各运算符号的优先级 double shu[1000];//存储 "数" 的数组
double dai_result;//运算的结果,是为了处理 M 运算(简介函数里有M的定义) int biao = 0;//和dia_result 一样,为了处理 M 运算 char line[SIZE];//输入的所要计算的表达式
void init()//初始化 { compare[fu[0]] = -2;//用数字的大小表现出符号的优先级

compare[fu[1]] = -1; compare[fu[2]] = 2; compare[fu[3]] = 2; compare[fu[4]] = 4; compare[fu[5]] = 4; compare[fu[6]] = 4; compare[fu[7]] = 5; for(int i = 8; i <= 15; i++) compare[fu[i]] = 6; compare[fu[16]] = 7; S_num.base = (double*)malloc(sizeof(double)*SIZE);//为栈开辟空间 S_char.base = (char*)malloc(sizeof(char)*SIZE);//同上 S_num.top = S_num.base; S_char.top = S_char.base; }
void push_num(double n)//数字进栈 { * ++S_num.top = n; }
void push_char(char c)//运算符号进栈 { * ++S_char.top = c; }
double pop_num()//数字出栈 { double m = *S_num.top; S_num.top--; return m; }
char pop_char()//运算符号出栈 { char cc = *S_char.top; S_char.top--; return cc; }
char get_top_char()//得到运算符号的栈中最顶端的运算符号 { return *S_char.top;

}

double operate(double y, char c, double x)//
对两个数计算
(
含是双目运算符
:


*, /
等等
)
{

double r;

if(c == '-')

r = x - y;

else if(c == '+')

r = x + y;

else if(c == '/' && y != 0)

r = x / y;

else if(c == '*')

r = x * y;

else if(c == '^')

{

r = 1;

for(int i = 1; i <= y; i++)

r *= x;

}

else if(c == '%')

{

int r0 = (int)x % (int)y;

r = double(r0);

}

return r;
}

double operate_one(double one, char cc)//
对一个数运算
(
含单目运算符
:

log(L), sin(S)
等等
)
{

double r;

if(cc == 'Q')

r = sqrt(one);

else if(cc == 'C')

r = cos(one);

else if(cc == 'S')

r = sin(one);

else if(cc == 'T')

r = tan(one);

else if(cc == 'c')

i++;

}

i++;

}

if(ge >= 3)

return 0;

else

return 1;
}

void output(double result)//
打出结果

{

printf("
所得结果是
: ");

cout<<result<<endl;
}

void check()//
检查表达式是否合法

{

void introce();

char cc;//
决定计算器按哪种功能进行计算

double result;//
结果

void input();//
定义

if( check_kuohao() && check_char() )//
看是否合法
,
合法则计算

{

result = compute();

output(result);

cout<<"
输入一个字符
'M'

'D'

'F',
决定是否继续
: "<<endl;

while(cin>>cc)

{

if(cc == 'M')

{

system("cls");

introce();

printf("
您上次所得结果为
: ");

cout<<result<<endl;

cout<<"
在上次计算结果的基础上
,
请继续输入想计算的表达式
"<<endl;

dai_result = result;

biao = 1;

input();//
输入表达式

break;

}

else if(cc == 'D')

{

system("cls");

introce();

cout<<"
计算器已清零
,
请输入您所要计算的表达式
"<<endl;

input();//
输入表达式

break;

}

else if(cc == 'F')

{

system("cls");

cout<<"
计算器关闭
,
谢谢使用
!"<<endl;

break;

}

else

{

cout<<"
所输入字符无效
,
请输入一个字符
'M'

'D'

'F'!"<<endl;

continue;

}

}

}

else//
不合法,分两种不合法

{

if(check_kuohao() == 0 && check_char() == 1)

{

cout<<"
您所输入的表达式括号不匹配
,
请重新输入
:"<<endl;

input();//
输入表达式

}

else

{

cout<<"
您所输入的表达式不合法
,
请重新输入
:"<<endl;

input();//
输入表达式

}

}
}

void tackle_fuhao()//
处理负号

{

node *root, *head, *p, *q, *p1;

root = head = new node;

head->next = NULL;

int i;

for(i = 0; line[i] != '\0'; i++)//
建立链表

{

p = new node;

p->data = line[i];

p->next = head->next;

head->next = p;

head = p;

}

// delete p;

q = (node*)malloc(sizeof(node));

head = root;

if(root->next->data == '+' || root->next->data == '-')//
处理第一个字符

{

p = new node;

p->data = '0';

p->next = head->next;

head->next = p;

}

if(root->next != NULL)

{

for(q = root->next; q; q = q->next)

{

if(q->data == '(' && (q->next->data == '-' || q->next->data == '+'))

{

p = new node;

p->data = '0';

p->next = q->next;

q->next = p;

}

}

}

// delete q;

p1 = new node;

int qi = -1;

for(p1 = root->next; p1; p1 = p1->next)

{

line[++qi] = p1->data;

}

line[++qi] = '\0';
}

void input()//
输入

{

cin>>line;

if(biao == 0)

tackle_fuhao();//
处理负号

check();//
检查表达式是否合法

}

void introce()//
对计算器的符号功能的简要介绍

{

cout<<"
计算器简要介绍
"<<endl;

cout<<"C(cos)

S(sin)

T(tan)

a(arccos)

c(arcsin) "<<endl;

cout<<"7

8

9

/

on

t(arctan) "<<endl;

cout<<"4

5

6

*

%

L(log)"<<endl;

cout<<"1

2

3

-

M(M+)

Q(sqrt)

"<<endl;

cout<<"0

.

+

^(
乘方
) F(off)

Enter(=) "<<endl;

cout<<"
对于对数输入

L2_5
表示以
2
为底
5
的对数
"<<endl;

cout<<"M(
在前面结果的基础上继续计算,
如:
上次结果为
10

现输入
+10.5*2)"<<endl;

cout<<"D(
清零并继续输入
)"<<endl;

cout<<"F(
计算机关闭
)"<<endl;

cout<<"
输入

P
就代表输入圆周率
,
输入

E
代表输入自然对数
"<<endl<<endl;
}

void print()
{

system("color 2");

cout<<"

欢迎使用本计算器
"<<endl;

cout<<"
输入一个字符串

on,
计算器开始启动
"<<endl;
}

void if_start()//
是否启动计算器

{

string start;

print();

while(cin>>start)

{

if(start != "on")

{

cout<<"
您所输入的字符无效
,
请按照介绍的继续输入
:"<<endl;

continue;

}

else

break;

}

if(start == "on")

{

system("color 5");//
颜色的处理

system("cls");//
刷屏

}

introce();//
对计算器的简要介绍

cout<<"
现在
,
请输入您所要计算的表达式
"<<endl;

input();//
输入所要计算的表达式

}

int main()
{

if_start();//
调用是否启动计算器函数

return 0;
}

r = acos(one);

else if(cc == 's')

r = asin(one);

else if(cc == 't')

r = atan(one);

❹ 一道C语言的编程题

判断素数的原理是:
素数是只能被1和本身整除的数。例如 3只能被1和3整除,17只能被1和17整除等等,想9就不是素数(能被1,3,9整除)。

编程的算法是:
穷举法,就是将需要判断的数除2、除3....一直除到这个数减1.
例如:判断7是不是素数的算法是进行如下运算 7/2,7/3,7/4,7/5,7/6,在上述除的过程中如果都不能被除尽则可以断定7是素数,否则就说明7不是素数。
注意:编程时使用求余运算,判断是否能被除尽。
参考程序如下:
#include<stdio.h>
void main()
{ int i,j,x;
for(i=2;i<=1000;i++) //1不是素数,所以从2算起
{
for(j=2;j<=999;j++)
if(i%j==0) break; //能被除尽则退出循环
if(j==i) printf("%d\n",i); //正常除尽时被除数和除数相等,说明是素数
}
}
当然程序还可以被优化,例如不一定非除到n-1,完全可以除到根号下n,
举例一:判断25是不是素数的话,只需要除到5即可
举例二:对于开根后是小数的可以取整,如判断15是不是素数只需要除到3即可。
简化后的程序运行速度会更快的。

❺ c语言的一道编程题

66877
第一次输入2,c-'2',因为是跟字符2相减,系统把c当做字符看,'2'-'2'转换为asc玛,结果为0,执行case 0
直到遇到break;第一次结果为case 0:不需要输出case 1: putchar(c+4);计算得6 case 2: putchar(c+4);break计算得6
第二次输入7 c-'2',得5,没有case 5这个选中,跳过,无输出
第三次输入4,计算方法同第一次
第四次输入3,计算方法同第一次

❻ 用C语言编程222222-22222-...-2,for语句,怎样实现减

这是加法的
#include<stdio.h>

intmain()
{
intn=6,a=0,s=0,i;
for(i=0;i<n;i++)
{
a=a*10+2;
s+=a;
}
printf("%d ",s);
return0;
}

以下上减法的
#include<stdio.h>

intmain()
{
intn=6,a=0,s=0,i;
for(i=0;i<n-1;i++)
{
a=a*10+2;
s-=a;
}
a=a*10+2;
s+=a;
printf("%d ",s);
return0;
}

❼ 怎样用C语言编写一个简单的可以进行加减乘除运算混合运算的计算器

用C语言编写一个简单的可以进行加减乘除运算混合运算的计算器的方法:

1、打开visual C++ 6.0-文件-新建-文件-C++ Source File;

❽ c语言编程、一个二位数加减法测验程序

Score= 3, Right= 2,Wrong= 0,Total= 2
66 + 60 =?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main()
{
int i,j,tmp,num1,num2,op,answer,flag,cnt,right=0,wrong=0,score=0,quest[10][3]={0};
srand((unsigned)time(NULL));
for (i=0;score<100;i++)
{
num1 = rand()%89+11;
num2 = rand()%89+11;
op = rand()%2;
num1<num2?tmp=num1,num1=num2,num2=tmp:NULL;
op>0?op='+':op='-';
for (j=0;j<i;j++)
{
if (quest[i][0]==num1 && quest[i][1]==op && quest[i][2]==num2)
{
i--;
}
else
{
quest[i][0]=num1;
quest[i][1]=op;
quest[i][2]=num2;
system("cls");
printf("Score=%2d, Right=%2d,Wrong=%2d,Total=%2d\n",score,right,wrong,i);
printf("%2d %c %2d =?",num1,op,num2);
cnt=0;
do
{
flag=0;
scanf("%d",&answer);
if ((op=='+' && answer==num1+num2) || (op=='-' && answer==num1-num2))
{
if (cnt==0)
{
score+=2;
}else
{
score++;
}
right++;
printf("Right!\n");
flag=1;
}else
{
printf("Wrong!\n");
if(cnt==1)
{
wrong++;
break;
}
}
cnt++;
} while (!flag);
}
}
}
}
望采纳

❾ 怒求一个能有加减乘除的C语言编程

#include <stdio.h>
int main()
{
int a,b,s,c;
printf("这是你用 c语言写出来的一个加,减,乘,除。的程序");
printf("这里是一些这个程序的使用方法介绍,和使用的示例。");
printf("请您从键盘上输入两个整数比如1003,两个整数之间要用空格键割开\n比如1000 10注意一千和十之间要用一个空格键隔开。");
printf("如果 你想要做加法请在第二个整数后面再打一个空隔键,然后输入数字1 像这样:50 20 1");
printf("加法计算:50 20 1,减法计算:50 20 2,乘法计算:50 20 3,除法计算:50 20 4。");
for ( c=1; c<10; ++c ) {
scanf("%d%d%d",&a,&b,&s);

if( s == 1 )
printf("%d+%d=%d",a,b,a+b);
if( s == 2 )
printf("%d-%d=%d",a,b,a-b);
if( s == 3 )
printf("%d*%d=%d",a,b,a*b);
if ( s == 4 )
printf("%d/%d=%d",a,b,a/b);
}
return 0;
}
网络为你解答

❿ C语言问题 1、编写程序,输入2个数以及加、减、乘、除中的某运算符号,并调用自己编写的函数计算相应的结

已经测试,运行正常,先输入两个数,然后输入+-*/中的一个:接着就有结果
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
float a, b;
char c;
float res;

scanf("%f %f\n", &a, &b);

c = getchar();

switch (c)
{
case '+':
res = a + b;
break;
case '-':
res = a - b;
break;
case '*':
res = a * b;
break;
case '/':
res = a / b;
break;
default:
printf("input error\n");
}

printf("result is: %f\n", res);
return 0;
}