当前位置:首页 » 编程软件 » 编译编程题

编译编程题

发布时间: 2022-07-17 11:37:38

1. 一道c语言编程题,编译通过但执行时发生错误。

首先scanf_s是错误的,有两处。
另外你的程序算法有问题,会出现死循环(用f10单步执行看看你就知道了),自己再检查一下吧

这类问题用链表来做很简单的,下面我用的是双向循环链表来实现的,可以参考一下

#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#define NULL 0

struct List
{
int data;
char name[15];
struct List *next;
struct List *prior;
}List;

struct List * InitList(int n)
{//建立一个双向循环链表,以head为头结点,数据从1开始至n,返回head指针
struct List *head,*p,*q;
int i;
head=(struct List *)malloc(sizeof(struct List));
head->data=1;
printf("请输入第1个小孩的名字:");
scanf("%s",head->name);
head->next=head;
head->prior=head;
p=head;
for(i=2;i<=n;i++)
{
q=(struct List *)malloc(sizeof(struct List));
q->data=i;
printf("请输入第%d个小孩的名字:",i);
scanf("%s",q->name);
p->next=q;
q->prior=p;
q->next=head;
head->prior=q;
p=p->next;
}
return head;
}

void GetList(struct List *head,int w,int s)
{
struct List *p,*q;
p=head;
int i;
for(i=1;i<w;i++,p=p->next);
for(;p->next!=p;i++,p=p->next)
if((i-w+1)%s==0)
{
q=p->prior;
p->next->prior=p->prior;
p->prior->next=p->next;
printf("%s\n",p->name);
free(p);
p=q;
}
printf("%s\n",p->name);
}

void main()
{
int n,w,s;
struct List *head;
do
{
system("cls");
printf("请输入总小孩人数:");
scanf("%d",&n);
if(n>0)head=InitList(n); //可重复运行直至n<=0停止
else exit(0);
printf("请输入最先报数的小孩的位置及间隔:");
scanf("%d,%d",&w,&s);
GetList(head,w,s);
system("pause");
}while(1);
}

2. 编译原理编程题

#include <stdio.h>

int identf(char *str);
int isKey(char *str);
int x_strcmp(char *si, char *di);

int main()
{
char buffer[256];
printf("Enter a string: ");
gets(buffer);
if(identf(buffer))
puts("合法的标识符");
else
puts("非法的标识符");

return 0;
}

/**************************************************************************/
int identf(char *str)
{
int flag=0;
char *p=str;
/*判断第一个字符是否符合*/
if((*str>='a' && *str<='z') || (*str>='A' && *str<='Z') || *str=='_')
{
flag=1;
str++;
}
/*从第二个字符开始遍历判断*/
while(*str && flag)
{
if((*str>='a' && *str<='z') || (*str>='A' && *str<='Z') || *str=='_'
|| (*str>='0' && *str<='9'))
{
flag=1;
str++;
}
else
flag=0;
}

/*判断是不是关键字*/
if(flag)
flag=isKey(p);

return flag;
}
/*****************************是不是关键字*******************************/
int isKey(char *str)
{
char *keyword[]={"auto", "break", "case", "char", "const", "continue", "default",
"do", "double", "else", "enum", "extern", "float", "for", "goto",
"if", "int", "long", "register", "return", "short", "signed",
"sizeof", "static", "struct", "switch", "typedef", "union", "unsigned",
"void", "volatile", "while"};
int i, flag=1;

for(i=0; i<32; i++)
{
if(x_strcmp(str, keyword[i])==0)
{
flag=0;
break;
}
}

return flag;
}
/*****************************字符串比较函数*******************************/
int x_strcmp(char *si, char *di)
{
while(*si && *di && *si==*di)
{
si++;
di++;
}

return *si-*di;
}

3. C语言编程题

long fun(int k)

{

if(i<2)

return 1L;

return k*fun(k-1);

}

或:

#include "stdio.h"

main()

{

double h,c;

//printf("Input h ");

scanf("%lf",&h);

c=5.0/9*(h-32);

printf("c=%lf",c);

}

(3)编译编程题扩展阅读:

C语言包含的各种控制语句仅有9种,关键字也只有32 个,程序的编写要求不严格且以小写字母为主,对许多不必要的部分进行了精简。实际上,语句构成与硬件有关联的较少,且C语言本身不提供与硬件相关的输入输出、文件管理等功能,如需此类功能,需要通过配合编译系统所支持的各类库进行编程,故c语言拥有非常简洁的编译系统。

4. c语言编程题

#include <stdio.h>

#include <math.h>

int main()

{

double x,y;

scanf("%lf",&x);

if(x<=-2)

y=-pow(exp(1),2*x+1)+3;

else if(x<=3)

y=2*x-1;

else

y=2*log10(3*x+5)-11;

printf("%lf ",y);

return 0;

}

5. C语言编程题目

#include<stdio.h>
#include<assert.h>
#define N 3
float fee(int x)
{
double money;
assert(x>=0);
if(x<=300)
{
money=0.6*x;
}
else if(x<=600)
{
money=0.6*300+0.5*(x-300);
}
else if(x<=1000)
{
money=0.6*300+0.5*300+0.4*(x-600);
}
else
{
money=0.6*300+0.5*300+0.4*400+0.3*(x-1000);
}
return (float)money;
}

int main()
{
int a[3],i,n=N;
printf("请输入%d个客户的用电量:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
printf("fee(%d)=%0.1f\n",a[i],fee(a[i]));
}
return 0;
}

//该程序经过VC++6.0成功编译执行,没有任何问题,最后祝楼主学习进步。

6. C语言编程题,用gcc编译器

#include<stdio.h>
voidmain(){
inti,j,m,n,num=0,s=0;
scanf("%d%d",&m,&n);
if(m<1||n>500||m>n)
printf("输入数据错误");
else{
for(i=m;i<=n;i++){
for(j=2;j<=i/2;j++)
if(i%j==0)break;
if(j>i/2&&i>1)
{num++;s+=i;}
}
printf("%d%d",num,s);
}
}

//运行示例:

7. 5道简单的java编程题(高分悬赏)

很详细的帮你写下,呵呵,所以要给分哦!
1、
(1)源程序如下:
public class One {

public static void main(String[] args) {
String name = "张三";
int age = 23;
char sex = '男';
String myclass = "某某专业2班";
System.out.println("姓名:" + name);
System.out.println("姓名:" + age);
System.out.println("姓名:" + sex);
System.out.println("姓名:" + myclass);

}

}

(2)

编写完程序的后缀名是.java,如本题,文件名就是One.java。
开始\运行\cmd,进入“命令提示符窗口”,然后用javac编译器编译.java文件,语句:javac One.java。

(3)
编译成功后,生成的文件名后缀是.class,叫做字节码文件。再用java解释器来运行改程序,语句:java One

2、编写程序,输出1到100间的所有偶数
(1)for语句
public class Two1 {

public static void main(String[] args) {
for(int i=2;i<=100;i+=2)
System.out.println(i);

}
}

(2)while语句
public class Two2 {

public static void main(String[] args) {
int i = 2;
while (i <= 100) {
System.out.println(i);
i += 2;
}
}
}

(3)do…while语句
public class Two3 {

public static void main(String[] args) {
int i = 2;
do {
System.out.println(i);
i += 2;
}while(i<=100);
}
}

3、编写程序,从10个数当中找出最大值。
(1)for循环
import java.util.*;

public class Three1 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
for (int i = 0; i < 10; i++) {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max < number)
max = number;
}
System.out.println("最大值:" + max);
}
}

(2)while语句
import java.util.*;

public class Three2 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
while (i < 10) {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max < number)
max = number;
i++;
}
System.out.println("最大值:" + max);
}
}

(3)do…while语句
import java.util.*;

public class Three3 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
do {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max < number)
max = number;
i++;
}while(i<10);
System.out.println("最大值:" + max);
}
}

4、编写程序,计算从1到100之间的奇数之和。
(1)for循环

public class Four1 {

public static void main(String[] args) {
int sum=0;
for(int i = 1;i<=100;i+=2){
sum+=i;
}
System.out.println("1~100间奇数和:" + sum);
}
}

(2)while语句
public class Four2 {

public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i <= 100) {
sum += i;
i += 2;
}
System.out.println("1~100间奇数和:" + sum);
}
}

(3)do…while语句
public class Four3 {

public static void main(String[] args) {
int sum = 0;
int i = 1;
do {
sum += i;
i += 2;
} while (i <= 100);
System.out.println("1~100间奇数和:" + sum);
}
}

5、
(1)什么是类的继承?什么是父类?什么是子类?举例说明。
继承:是面向对象软件技术当中的一个概念。如果一个类A继承自另一个类B,就把这个A称为"B的子类",而把B称为"A的父类"。继承可以使得子类具有父类的各种属性和方法,而不需要再次编写相同的代码。在令子类继承父类的同时,可以重新定义某些属性,并重写某些方法,即覆盖父类的原有属性和方法,使其获得与父类不同的功能。另外,为子类追加新的属性和方法也是常见的做法。继承需要关键字extends。举例:
class A{}
class B extends A{}
//成员我就不写了,本例中,A是父类,B是子类。

(2)编写一个继承的程序。
class Person {
public String name;
public int age;
public char sex;

public Person(String n, int a, char s) {
name = n;
age = a;
sex = s;
}

public void output1() {
System.out.println("姓名:" + name + "\n年龄:" + age + "\n性别:" + sex);
}
}

class StudentPerson extends Person {
String school, department, subject, myclass;

public StudentPerson(String sc, String d, String su, String m, String n,
int a, char s) {
super(n, a, s);
school = sc;
department = d;
subject = su;
myclass = m;
}

public void output2() {
super.output1();
System.out.println("学校:" + school + "\n系别:" + department + "\n专业:"
+ subject + "\n班级:" + myclass);
}
}

public class Five2 {

public static void main(String[] args) {
StudentPerson StudentPersonDemo = new StudentPerson("某某大学", "某某系别",
" 某专业", "某某班级", " 张三", 23, '男');
StudentPersonDemo.output2();
}
}

8. 几道C语言编程题

多给些分吧,累死我了:(
给,都已经编译运行确认了:
1.
下标法:
#include<conio.h>
#include<stdio.h>

int main()
{
int a[11]={0,1,2,3,4,5,6,7,8,9,10};
int N;
int i,j,temp;

printf("Please input N: \n");
scanf("%d",&N);

for(i=0;i<N;i++)
{
temp=a[10];
for(j=10;j>=1;j--)
{
a[j]=a[j-1];
}
a[0]=temp;
}

printf("After Move: \n");
for(i=0;i<11;i++) printf("%d ",a[i]);

getch();
return 1;
}

指针法:
#include<conio.h>
#include<stdio.h>

int main()
{
int a[11]={0,1,2,3,4,5,6,7,8,9,10};
int N;
int i,j,temp;

printf("Please input N: \n");
scanf("%d",&N);

for(i=0;i<N;i++)
{
temp=*(a+10);
for(j=10;j>=1;j--)
{
*(a+j)=*(a+j-1);
}
*a=temp;
}

printf("After Move: \n");
for(i=0;i<11;i++) printf("%d ",*(a+i));

getch();
return 1;
}

2.
数组法:
#include<conio.h>
#include<stdio.h>

int main()
{
int a[20]={0};
int i,j,sum=0;

printf("请输入20个数: \n");
for(i=0;i<20;i++)
{
printf("%d: ",i+1);
fflush(stdin);
scanf("%d",&a[i]);
}

printf("输入的数为: \n");
for(i=0;i<20;i++)
{
printf("%d ",a[i]);
if((i+1)%5==0) printf("\n");
}

for(i=0;i<20;i++)
if(a[i]%2==0) sum+=a[i];

printf("\n偶数的和为: %d",sum);

getch();
return 1;
}

指针法:
#include<conio.h>
#include<stdio.h>

int main()
{
int a[20]={0};
int i,j,sum=0;

printf("请输入20个数: \n");
for(i=0;i<20;i++)
{
printf("%d: ",i+1);
fflush(stdin);
scanf("%d",a+i);
}

printf("输入的数为: \n");
for(i=0;i<20;i++)
{
printf("%d ",*(a+i));
if((i+1)%5==0) printf("\n");
}

for(i=0;i<20;i++)
if(*(a+i)%2==0) sum+=*(a+i);

printf("\n偶数的和为: %d",sum);

getch();
return 1;
}

3.
指针法:#include<conio.h>
#include<stdio.h>

int main()
{
int a[15]={0};
int i,num;

printf("请输入15个数: \n");
for(i=0;i<15;i++)
{
printf("%d: ",i+1);
fflush(stdin);
scanf("%d",a+i);
}

printf("要找的数为: \n");
scanf("%d",&num);

for(i=0;i<15;i++)
if(*(a+i)==num)
{
printf("该数是数组中的元素的第%d个元素的值.\n",i+1);
break;
}

if(i==15)
printf("无此数.");

getch();
return 1;
}

数组法:
#include<conio.h>
#include<stdio.h>

int main()
{
int a[15]={0};
int i,num;

printf("请输入15个数: \n");
for(i=0;i<15;i++)
{
printf("%d: ",i+1);
fflush(stdin);
scanf("%d",&a[i]);
}

printf("要找的数为: \n");
scanf("%d",&num);

for(i=0;i<15;i++)
if(a[i]==num)
{
printf("该数是数组中的元素的第%d个元素的值.\n",i+1);
break;
}

if(i==15)
printf("无此数.");

getch();
return 1;
}

4.
数组法:
#include<conio.h>
#include<stdio.h>

int main()
{
int a[11]={0};
int i,num;
int p=0;

printf("请输入10个数: \n");
for(i=0;i<10;i++)
{
printf("%d: ",i+1);
fflush(stdin);
scanf("%d",&a[i]);
}

printf("要插入的数为: \n");
scanf("%d",&num);

while(num>a[p]&&p<10)
p=p+1;

for(i=9;i>=p-1;i--)
a[i+1]=a[i];

a[p]=num;

printf("Result: \n");
for(i=0;i<11;i++)
printf("%d ",a[i]);

getch();
return 1;
}

指针法:
#include<conio.h>
#include<stdio.h>

int main()
{
int a[11]={0};
int i,num;
int p=0;

printf("请输入10个数: \n");
for(i=0;i<10;i++)
{
printf("%d: ",i+1);
fflush(stdin);
scanf("%d",a+i);
}

printf("要插入的数为: \n");
scanf("%d",&num);

while(num>*(a+p)&&p<10)
p=p+1;

for(i=9;i>=p-1;i--)
*(a+i+1)=*(a+i);

*(a+p)=num;

printf("Result: \n");
for(i=0;i<11;i++)
printf("%d ",*(a+i));

getch();
return 1;
}

9. C语言编程题,DEVC编译器

#include<stdio.h>
#include<string.h>

intmain()
{
intn,i,j;
charout='a';
scanf("%d",&n);
for(i=0;i<n-1;++i){
for(j=0;j<i;++j){
printf("");
}
printf("%c",out);
out++;
if(out>'h')out='a';
for(j=0;j<(n-1-i)*2+1;++j){
printf("");
}
printf("%c",out);
out++;
if(out>'h')out='a';
printf(" ");
}
for(j=0;j<=i;++j){
printf("");
}
printf("%c",out);
return0;
}


/*

ab↙

cd↙

ef↙

gh↙

a↙
*/

10. 编译预处理编程题 :用条件编译实现从键盘上输入一行电文,可以任意选择两种输出

如果定义了 宏名 ENCODE 则编译后的程序输出编了码的。
如果没有定义这个宏名 则编译后的程序 按原句输出。
#include<stdio.h>
// if you have the line, it will output encoded
#define ENCODE

int main()
{
char s[100];
int i;
printf("input one line string:\n");
gets(s);
#ifdef ENCODE
for (i=0;i<strlen(s);i++){
if (s[i]>='a' && s[i]<='z') s[i]=(s[i]-'a'+3) % 26 + 'a';
else if (s[i]>='A' && s[i]<='Z') s[i]=(s[i]-'A'+3) % 26 + 'A';
}
#endif
printf("%s\n",s);
return 0;
}
例如:
input one line string:
ABCD abcd 123 XYZ xyz -- 输入
DEFG defg 123 ABC abc -- 输出

热点内容
什么漫画软件可以缓存 发布:2025-02-07 17:56:21 浏览:267
安卓如何取消手机搜索 发布:2025-02-07 17:46:04 浏览:217
ontoucheventandroid 发布:2025-02-07 17:45:50 浏览:869
爱思助手如何看配置 发布:2025-02-07 17:32:27 浏览:175
自己的电脑怎么搭建手游服务器端 发布:2025-02-07 17:21:44 浏览:47
怎样修改苹果密码怎么办 发布:2025-02-07 17:15:44 浏览:716
电脑一般怎么连接服务器 发布:2025-02-07 17:12:55 浏览:491
ftp用ie打开文件 发布:2025-02-07 17:07:42 浏览:271
android列表显示 发布:2025-02-07 17:01:19 浏览:66
芒果tv缓存的视频在哪个文件里 发布:2025-02-07 16:45:05 浏览:817