Click here to Skip to main content
15,917,793 members
Please Sign up or sign in to vote.
1.31/5 (4 votes)
See more:
C++
#include <string.h>
#include <iostream>
#include <vector> 
#include <fstream>
#include <bitset>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	vector<string> codes;
	ifstream in("C:\\Users\\SHR\\Desktop\\test.txt");
	string line;
	while (getline(in, line))
	{
		codes.push_back(line);
	}
	int size = codes.size();
	string * Mcodes = new string[size];
	string * labels=new string[size];
	string * names = new string[size];
	int * ii = new int[size];
	for(int i=0 ; i<codes.size(); i++)
="" {
="" string="" s="codes[i];
" part="" ;
="" for(int="" j="0" ;="" j<s.length()="" j++)
="" char="" ch="s[j];
" if(j="=s.length()-1)
" part+="ch;
" }
="" else="" if(ch="=" '="" ||="" if(part="="add"" "lw"="" ||part="="lui"||part=="jalr"||part=="j"" names[i]="part;
" ii[i]="j+1;
" break;
="" else
="" if(labels[i]="="")
" if(part.length()<="6" &&="" !(isdigit(part[0])))
="" labels[i]="part;
" cout<<"line="" "<<i+1<<"label="" kharab="" ast"="" int="" a="" 
="" std::cin="">> a ;
							exit(1);
						}
						part="";
					}
					else
					{
						cout<<"line "<<i+1<<"in code="" jozve="" hichkodam="" az="" haie="" shenakhte="" shode="" nemibashad"="" ;
="" int="" a="" ;="" 
="" std::cin="">> a ;
						exit(1);
					}
				}
			}
			else
				part+=ch;
		}
	}	
	for(int i=0 ; i<codes.size() ;="" i++)
="" {
="" int="" j="ii[i];
" string="" s="codes[i];
" char="" ch;
="" if(names[i]="="add"" ||="" names[i]="="sub"" {

="" arg1;
="" arg2;
="" arg3;
="" part="" ;
="" whicharg="0;
" for(="" j<s.length()="" j++)
="" ch="s[j];
" if(j="=s.length()-1)
" part+="ch;
" try
="" arg3="stoi(part);
" }
="" catch(exception="" ex)
="" cout<<"line="" "<<i+1<<"vorudi="" dasturhaie="" noe="" r="" bayad="" hatman="" adad="" bashand"="" a="" 
="" std::cin="">> a ;
						exit(1);
					}
					whichArg++;
					part="";
				}
			t	else if(ch==',')
				{

					try
					{
						if(whichArg==0)
							arg1=stoi(part);
						else if(whichArg==1)
							arg2=stoi(part);
					}
					catch(exception ex)
					{
						cout<<"line "<<i+1<<"vorudi dasturhaie="" noe="" r="" bayad="" hatman="" adad="" bashand"="" ;
="" int="" a="" ;="" 
="" std::cin="">> a ;
						exi(1);
					}
					whichArg++;
					part="";
				}
				else if(ch==' ' || ch=='\t')
				{
					try
					{
						arg3=stoi(part);
					}
					catch(exception ex)
					{
						cout<<"line "<<i+1<<"vorudi dasturhaie="" noe="" r="" bayad="" hatman="" adad="" bashand"="" ;
="" int="" b="" ;="" 
="" std::cin="">> b ;
						exit(1);
					}
					whichArg++;
					part="";
					for( ; j<s.length() ;="" j++)
="" {
="" ch="s[j];
" if(ch="" !=" " &&="" ch!="\t" )
="" break;
="" else
="" cout<<"line="" "<<i+1<<"comment="" kharab="" ast"="" ;
="" int="" a="" 
="" std::cin="">> a ;
								exit(1);
							}
						}
					}
					break;
				}
				else
					part+=ch;
			}
			Mcodes[i]="0000";
			if(names[i]=="add")
				Mcodes[i]+="0000";
			else if(names[i]=="sub")
				Mcodes[i]+="0001";
			else if(names[i]=="slt")
				Mcodes[i]+="0010";
			else if(names[i]=="or")
				Mcodes[i]+="0011";
			else if(names[i]=="nand")
				Mcodes[i]+="0100";
			Mcodes[i]+= bitset< 4 >(arg2).to_string();
			Mcodes[i]+= bitset< 4 >(arg3).to_string();
			Mcodes[i]+= bitset< 4 >(arg1).to_string();
			Mcodes[i] += "000000000000";
		}
		else if(names[i]=="addi" || names[i]=="ori" || names[i]=="slti" || names[i] == "lw" || names[i]=="sw" || names[i]=="beq")
		{

		}
		else if(names[i]=="lui")
		{
			string part="";
			int arg1;
			int arg2=0;
			int arg3;
			int whichArg=0;
			for( ; j<s.length() ;="" j++)
="" {
="" ch="s[j];
" if(ch="=',')
" try
="" arg1="stoi(part);
" }
="" catch(exception="" ex)
="" cout<<"line="" "<<i+1<<"vorudi="" dasturhaie="" noe="" r="" bayad="" hatman="" adad="" bashand"="" ;
="" int="" a="" 
="" std::cin="">> a ;
						exit(1);
					}
					part="";
				}
				else if(j==s.length()-1)
				{
					part+=ch;
					try
					{
						arg3=stoi(part);
					}
					catch(exception ex)
					{
						cout<<"line "<<i+1<<"vorudi dasturhaie="" noe="" lui="" bayad="" hatman="" adad="" bashand"="" ;
="" int="" a="" ;="" 
="" std::cin="">> a ;
						exit(1);
					}
					part="";
					if(arg3<-32767 || arg3>32767)
					{
						cout<<"line "<<i+1<<"vorudi dasturhaie="" noe="" lui="" bayad="" hatman="" adad="" bashand"="" ;
="" int="" a="" ;="" 
="" std::cin="">> a ;
						exit(1);
					}
				}
				else if(ch==' ' || ch=='\t')
				{
					try
					{
						arg3=stoi(part);
					}
					catch(exception ex)
					{
						cout<<"line "<<i+1<<"vorudi dasturhaie="" noe="" lui="" bayad="" hatman="" adad="" bashand"="" ;
="" int="" a="" ;="" 
="" std::cin="">> a ;
						exit(1);
					}
					if(arg3<-32767 || arg3>32767)
					{
						cout<<"line "<<i+1<<"damaneie offcet="" eshtebah="" ast"="" ;
="" int="" a="" ;="" 
="" std::cin="">> a ;
						exit(1);
					}
					part="";
					for( ; j<s.length() ;="" j++)
="" {
="" ch="s[j];
" if(ch="" !=" " &&="" ch!="\t" )
="" break;
="" else
="" cout<<"line="" "<<i+1<<"comment="" kharab="" ast"="" ;
="" int="" a="" 
="" std::cin="">> a ;
								exit(1);
							}
						}
					}
					break;
				}
				else
					part+=ch;
			}
			Mcodes[i]+="0000";
			Mcodes[i]+="1000";
			Mcodes[i]+= bitset< 4 >(arg2).to_string();
			Mcodes[i]+= bitset< 4 >(arg1).to_string();
			Mcodes[i]+= bitset< 16 >(arg3).to_string();
		}
		else if(names[i]=="jalr")
		{

		}
		else if(names[i]=="j")
		{
			int level=0;
			string part="";
			string arg1;
			int j = ii[i];
			for( ; j<s.length() ;="" j++)
="" {
="" char="" c="s[j];
" if(j="=s.length()-1)
" part+="c;
" arg1="part;
" }
="" else="" if(c="='" '="" ||="" for(="" j<s.length()="" ch="s[j];
" if(ch="" !=" " &&="" ch!="\t" )
="" break;
="" else
="" cout<<"line="" "<<i+1<<"comment="" kharab="" ast"="" ;
="" int="" a="" 
="" std::cin="">> a ;
								exit(1);
							}
						}
					}
				}
				else
					part+=c;
			}
			Mcodes[i]="0000";
			Mcodes[i]+="1101";
			Mcodes[i]+="00000000";
			try
			{
				int tmp = stoi(arg1);
				if(tmp<-32767 || tmp>32767)
				{
					cout<<"line "<<i+1<<"damaneie offcet="" eshtebah="" ast"="" ;
="" int="" a="" ;="" 
="" std::cin="">> a ;
					exit(1);
				}
				Mcodes[i]+= bitset< 16 >(tmp).to_string();
			}
			catch(exception ex)
			{
				bool b = false;
				for(int k=0 ; k<codes.size() ;="" k++)
="" {
="" if(labels[k]="=arg1)
" b="true;
" mcodes[i]+="bitset<" 16="">(k).to_string();
					}
				}
				if(!b)
				{
					cout<<"line "<<i+1<<"label vojud="" nadarad"="" ;
="" int="" a="" ;="" 
="" std::cin="">> a ;
					exit(1);
				}
			}
		}
		else if(names[i]=="halt")
		{
			Mcodes[i]="0000";
			Mcodes[i]+="1110";
			Mcodes[i]+="00000000";
			Mcodes[i]+="0000000000000000";
		}
		else if(names[i]==".fill")
		{
			int level=0;
			string part="";
			string arg1;
			int whichArg=0;
			for( ; j<s.length() ;="" j++)
="" {
="" char="" ch="s[j];
" if(j="=s.length()-1)
" part+="ch;
" arg1="part;
" }
="" else="" if(ch="='" '="" ||="" for(="" j<s.length()="" !=" " &&="" ch!="\t" )
="" break;
="" else
="" cout<<"line="" "<<i+1<<"comment="" kharab="" ast"="" ;
="" int="" a="" 
="" std::cin="">> a ;
								exit(1);
							}
						}
					}
					break;
				}
				else
					part+=ch;
			}
			try
			{
				int tmp = stoi(arg1);
				Mcodes[i]= bitset< 32 >(tmp).to_string();

			}
			catch(exception ex)
			{
				bool b = false;
				for(int k=0 ; k<codes.size() ;="" k++)
="" {
="" if(labels[k]="=arg1)
" b="true;
" mcodes[i]="bitset<" 32="">(k).to_string();
					}
				}
				if(!b)
				{
					cout<<"line "<<i+1<<"label vojud="" nadarad"="" ;
="" int="" a="" ;="" 
="" std::cin="">> a ;
					exit(1);
				}
			}
		}
		else if(names[i]==".space")
		{
			string str="";
			int level=0;
			names[i]="";
			string arg1;
			int whichArg=0;
			for( ; j<s.length() ;="" j++)
="" {
="" char="" ch="s[j];
" if(j="=s.length()-1)
" names[i]+="ch;
" arg1="names[i];
" }
="" else="" if(ch="='" '="" ||="" for(="" j<s.length()="" !=" " &&="" ch!="\t" )
="" break;
="" else
="" cout<<"line="" "<<i+1<<"comment="" kharab="" ast"="" ;
="" int="" a="" 
="" std::cin="">> a ;
								exit(1);
							}
						}
					}
				}
				else
					names[i]+=ch;
			}
			try
			{
				int tmp = stoi(arg1);
				for(int k=0 ; k<tmp ;="" k++)
="" {
="" if(mcodes[i]="="")
" mcodes[i]+="0" ;
="" else
="" }

="" }
="" catch(exception="" ex)
="" bool="" b="false;
" for(int="" k="0" k<codes.size()="" if(labels[k]="=arg1)
" l="0" l<k="" l++)
="" if(!b)
="" cout<<"line="" "<<i+1<<"label="" vojud="" nadarad"="" int="" a="" 
="" std::cin="">> a ;
					exit(1);
				}
			}
		}
	}
	int a ;
	std::cin>>a;
	return 0;
}


What I have tried:

is there any site to convert this code?
Posted
Updated 12-Apr-23 10:33am
v2

To add to what Richard has said, this is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it'll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.

You clearly have no idea how different C and C++ are: C++ is a superset of C, in the same way that a space shuttle is a superset of the Kitty Hawk - they both have wings, they both fly - but the Wright brothers couldn't have built a shuttle, they technology it relies upon just didn't exist in their day! Same with C / C++ - C has no concept of pretty much anything that C++ relies upon!
 
Share this answer
 
Quote:
is there any site to convert this code?
Proobably not, and definitely not this site. Apart from the fact that there are too many class objects in that code that simply do not exist in C, so you would need to write new implementations to cover what they do. TBH if that is supposed to be for an assignment you would be better writing your own C code from scratch.
 
Share this answer
 
You need to visit some Learn C tutorial to get started with language and get ready to write the code.
As already written "translating" the code isnt an acceptable solution. You must analyze the code and understand the accomplished tasks like ready a file and using every line to do something.

tip: for splitting a string in C is the function strtok but you must understand its usage.
 
Share this answer
 
Now converting a c++ code to C is a chalange.

I see two main chalanges here:
1.) Implement bitset in C, not a big thing
2.) Implement vector in C, a bigger thing.

In the very past, a person named Niklaus Wirth developed some methodics to acomplish similar things (today one will name it a pattern?).
Trying not to go to deep, he claimed data hiding, solved in Modula 2 by opaque types.
It means, a consumer of a e.g. 'vector implementation' does not know how it is implemented.
The only thing the consumer of a thing like 'vector' has available is, how to create a vector, how to add an element, how to delete, etc.

Now let's forget about the data hiding and let as assume all the users of an implementation of 'vector' are sufficiently disciplined and do not manipulate the struct directly - which is holding the relevant data of the vector - and only use the supplied C- Methods for that.

Assuming you are familar with C structs then e.g. for vector it means something like this, the consumer ask the vector library to:
1. Create a vector
2. Add an element
3. Delete an element
4. Get Element at index xyz
5. Delete the whole vector
#. More ...

All these behaviours you need to implement. Something like:

1.) aVector* vec= CreateVector();
2.) AddElement(vec, thatElement);
3.) DeleteElement(vec, 4);
4.) void* x= GetElement(vec, 3);
5.) DeleteVector(vec);

Not that easy, good luck ;)
 
Share this answer
 
v2
this site link below should do the trick . 🙃
https://www.amazon.com/Reference-Manual-Samuel-P-Harbison/dp/013089592X[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900